blob: df2a105a7aaa1e67396ce231cb480872ff551514 [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 );
381 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
382 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200383 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200384
Ronald Cron5425a212020-08-04 14:58:35 +0200385 PSA_ASSERT( psa_destroy_key( key ) );
386 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200387
388exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100389 /*
390 * Key attributes may have been returned by psa_get_key_attributes()
391 * thus reset them as required.
392 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200393 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100394
395 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200396 PSA_DONE( );
397}
398/* END_CASE */
399
400/* BEGIN_CASE */
401void import_with_data( data_t *data, int type_arg,
402 int attr_bits_arg,
403 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200404{
405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
406 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200408 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200409 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200410 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100411 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412
Gilles Peskine8817f612018-12-18 00:18:46 +0100413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100414
Gilles Peskine4747d192019-04-17 15:05:45 +0200415 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200416 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200417
Ronald Cron5425a212020-08-04 14:58:35 +0200418 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100419 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200420 if( status != PSA_SUCCESS )
421 goto exit;
422
Ronald Cron5425a212020-08-04 14:58:35 +0200423 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200424 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200425 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200426 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200427 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200428
Ronald Cron5425a212020-08-04 14:58:35 +0200429 PSA_ASSERT( psa_destroy_key( key ) );
430 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100431
432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100433 /*
434 * Key attributes may have been returned by psa_get_key_attributes()
435 * thus reset them as required.
436 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200437 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100438
439 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200440 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100441}
442/* END_CASE */
443
444/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200445void import_large_key( int type_arg, int byte_size_arg,
446 int expected_status_arg )
447{
448 psa_key_type_t type = type_arg;
449 size_t byte_size = byte_size_arg;
450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
451 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200452 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200453 psa_status_t status;
454 uint8_t *buffer = NULL;
455 size_t buffer_size = byte_size + 1;
456 size_t n;
457
Steven Cooreman69967ce2021-01-18 18:01:08 +0100458 /* Skip the test case if the target running the test cannot
459 * accomodate large keys due to heap size constraints */
460 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200461 memset( buffer, 'K', byte_size );
462
463 PSA_ASSERT( psa_crypto_init( ) );
464
465 /* Try importing the key */
466 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
467 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200468 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100469 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200470 TEST_EQUAL( status, expected_status );
471
472 if( status == PSA_SUCCESS )
473 {
Ronald Cron5425a212020-08-04 14:58:35 +0200474 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200475 TEST_EQUAL( psa_get_key_type( &attributes ), type );
476 TEST_EQUAL( psa_get_key_bits( &attributes ),
477 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200478 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200479 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200480 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200481 for( n = 0; n < byte_size; n++ )
482 TEST_EQUAL( buffer[n], 'K' );
483 for( n = byte_size; n < buffer_size; n++ )
484 TEST_EQUAL( buffer[n], 0 );
485 }
486
487exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100488 /*
489 * Key attributes may have been returned by psa_get_key_attributes()
490 * thus reset them as required.
491 */
492 psa_reset_key_attributes( &attributes );
493
Ronald Cron5425a212020-08-04 14:58:35 +0200494 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200495 PSA_DONE( );
496 mbedtls_free( buffer );
497}
498/* END_CASE */
499
500/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200501void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
502{
Ronald Cron5425a212020-08-04 14:58:35 +0200503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200504 size_t bits = bits_arg;
505 psa_status_t expected_status = expected_status_arg;
506 psa_status_t status;
507 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200508 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200509 size_t buffer_size = /* Slight overapproximations */
510 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200511 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200512 unsigned char *p;
513 int ret;
514 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200516
Gilles Peskine8817f612018-12-18 00:18:46 +0100517 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200518 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200519
520 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
521 bits, keypair ) ) >= 0 );
522 length = ret;
523
524 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200525 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200526 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100527 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200528
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200529 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200530 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200531
532exit:
533 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200534 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200535}
536/* END_CASE */
537
538/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300539void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300540 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200541 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 int expected_bits,
543 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200544 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 int canonical_input )
546{
Ronald Cron5425a212020-08-04 14:58:35 +0200547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200549 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200550 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 unsigned char *exported = NULL;
553 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100555 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200558 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559
Moran Pekercb088e72018-07-17 17:36:59 +0300560 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200561 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200563 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100564 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565
Gilles Peskine4747d192019-04-17 15:05:45 +0200566 psa_set_key_usage_flags( &attributes, usage_arg );
567 psa_set_key_algorithm( &attributes, alg );
568 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700569
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572
573 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200574 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200575 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
576 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200577 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578
579 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200580 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100581 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100582
583 /* The exported length must be set by psa_export_key() to a value between 0
584 * and export_size. On errors, the exported length must be 0. */
585 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
586 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
587 TEST_ASSERT( exported_length <= export_size );
588
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200589 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200590 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200592 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100593 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200595 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596
Gilles Peskineea38a922021-02-13 00:05:16 +0100597 /* Run sanity checks on the exported key. For non-canonical inputs,
598 * this validates the canonical representations. For canonical inputs,
599 * this doesn't directly validate the implementation, but it still helps
600 * by cross-validating the test data with the sanity check code. */
601 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200602 goto exit;
603
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200605 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606 else
607 {
Ronald Cron5425a212020-08-04 14:58:35 +0200608 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200609 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200610 &key2 ) );
611 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100612 reexported,
613 export_size,
614 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200615 ASSERT_COMPARE( exported, exported_length,
616 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200617 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100619 TEST_ASSERT( exported_length <=
620 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
621 psa_get_key_bits( &got_attributes ) ) );
622 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623
624destroy:
625 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200626 PSA_ASSERT( psa_destroy_key( key ) );
627 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100628
629exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100630 /*
631 * Key attributes may have been returned by psa_get_key_attributes()
632 * thus reset them as required.
633 */
634 psa_reset_key_attributes( &got_attributes );
635
itayzafrir3e02b3b2018-06-12 17:06:52 +0300636 mbedtls_free( exported );
637 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200638 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100639}
640/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100641
Moran Pekerf709f4a2018-06-06 17:26:04 +0300642/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200644 int type_arg,
645 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100646 int export_size_delta,
647 int expected_export_status_arg,
648 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300649{
Ronald Cron5425a212020-08-04 14:58:35 +0200650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300651 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200652 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200653 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300654 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100656 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100657 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300659
Gilles Peskine8817f612018-12-18 00:18:46 +0100660 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300661
Gilles Peskine4747d192019-04-17 15:05:45 +0200662 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
663 psa_set_key_algorithm( &attributes, alg );
664 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300665
666 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200667 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300668
Gilles Peskine49c25912018-10-29 15:15:31 +0100669 /* Export the public key */
670 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200671 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200672 exported, export_size,
673 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100674 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100675 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100676 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200677 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100678 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200679 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200680 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100681 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100682 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100683 TEST_ASSERT( expected_public_key->len <=
684 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
685 TEST_ASSERT( expected_public_key->len <=
686 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100687 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
688 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100689 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300690
691exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100692 /*
693 * Key attributes may have been returned by psa_get_key_attributes()
694 * thus reset them as required.
695 */
696 psa_reset_key_attributes( &attributes );
697
itayzafrir3e02b3b2018-06-12 17:06:52 +0300698 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200699 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200700 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300701}
702/* END_CASE */
703
Gilles Peskine20035e32018-02-03 22:44:14 +0100704/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200705void import_and_exercise_key( data_t *data,
706 int type_arg,
707 int bits_arg,
708 int alg_arg )
709{
Ronald Cron5425a212020-08-04 14:58:35 +0200710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200711 psa_key_type_t type = type_arg;
712 size_t bits = bits_arg;
713 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100714 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200715 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200716 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200717
Gilles Peskine8817f612018-12-18 00:18:46 +0100718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200719
Gilles Peskine4747d192019-04-17 15:05:45 +0200720 psa_set_key_usage_flags( &attributes, usage );
721 psa_set_key_algorithm( &attributes, alg );
722 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200723
724 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200725 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200726
727 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200728 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200729 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
730 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200731
732 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100733 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200734 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200735
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_destroy_key( key ) );
737 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200738
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200739exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100740 /*
741 * Key attributes may have been returned by psa_get_key_attributes()
742 * thus reset them as required.
743 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200744 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100745
746 psa_reset_key_attributes( &attributes );
747 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200748 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200749}
750/* END_CASE */
751
752/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100753void effective_key_attributes( int type_arg, int expected_type_arg,
754 int bits_arg, int expected_bits_arg,
755 int usage_arg, int expected_usage_arg,
756 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200757{
Ronald Cron5425a212020-08-04 14:58:35 +0200758 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100759 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100760 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100761 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100762 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200763 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100764 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200765 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100766 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200768
Gilles Peskine8817f612018-12-18 00:18:46 +0100769 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200770
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200771 psa_set_key_usage_flags( &attributes, usage );
772 psa_set_key_algorithm( &attributes, alg );
773 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100774 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200775
Ronald Cron5425a212020-08-04 14:58:35 +0200776 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100777 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200778
Ronald Cron5425a212020-08-04 14:58:35 +0200779 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100780 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
781 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
782 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
783 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200784
785exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100786 /*
787 * Key attributes may have been returned by psa_get_key_attributes()
788 * thus reset them as required.
789 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200790 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100791
792 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200793 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200794}
795/* END_CASE */
796
797/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100798void check_key_policy( int type_arg, int bits_arg,
799 int usage_arg, int alg_arg )
800{
801 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
802 usage_arg, usage_arg, alg_arg, alg_arg );
803 goto exit;
804}
805/* END_CASE */
806
807/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200808void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000809{
810 /* Test each valid way of initializing the object, except for `= {0}`, as
811 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
812 * though it's OK by the C standard. We could test for this, but we'd need
813 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200814 psa_key_attributes_t func = psa_key_attributes_init( );
815 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
816 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000817
818 memset( &zero, 0, sizeof( zero ) );
819
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200820 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
821 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
822 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000823
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200824 TEST_EQUAL( psa_get_key_type( &func ), 0 );
825 TEST_EQUAL( psa_get_key_type( &init ), 0 );
826 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
827
828 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
829 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
830 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
831
832 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
833 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
834 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
835
836 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
837 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
838 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000839}
840/* END_CASE */
841
842/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200843void mac_key_policy( int policy_usage,
844 int policy_alg,
845 int key_type,
846 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100847 int exercise_alg,
848 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200849{
Ronald Cron5425a212020-08-04 14:58:35 +0200850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200851 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000852 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200853 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100854 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200855 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200856
Gilles Peskine8817f612018-12-18 00:18:46 +0100857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200858
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200859 psa_set_key_usage_flags( &attributes, policy_usage );
860 psa_set_key_algorithm( &attributes, policy_alg );
861 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200862
Gilles Peskine049c7532019-05-15 20:22:09 +0200863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200864 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200865
Ronald Cron5425a212020-08-04 14:58:35 +0200866 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100867 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100868 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100869 else
870 TEST_EQUAL( status, expected_status );
871
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200872 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200873
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200874 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200875 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100876 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100877 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100878 else
879 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200880
881exit:
882 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200883 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200884 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200885}
886/* END_CASE */
887
888/* BEGIN_CASE */
889void cipher_key_policy( int policy_usage,
890 int policy_alg,
891 int key_type,
892 data_t *key_data,
893 int exercise_alg )
894{
Ronald Cron5425a212020-08-04 14:58:35 +0200895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000897 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200898 psa_status_t status;
899
Gilles Peskine8817f612018-12-18 00:18:46 +0100900 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200901
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200902 psa_set_key_usage_flags( &attributes, policy_usage );
903 psa_set_key_algorithm( &attributes, policy_alg );
904 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905
Gilles Peskine049c7532019-05-15 20:22:09 +0200906 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200907 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200908
Ronald Cron5425a212020-08-04 14:58:35 +0200909 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 if( policy_alg == exercise_alg &&
911 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100912 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200913 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100914 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915 psa_cipher_abort( &operation );
916
Ronald Cron5425a212020-08-04 14:58:35 +0200917 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918 if( policy_alg == exercise_alg &&
919 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100920 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200921 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100922 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200923
924exit:
925 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200926 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200927 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928}
929/* END_CASE */
930
931/* BEGIN_CASE */
932void aead_key_policy( int policy_usage,
933 int policy_alg,
934 int key_type,
935 data_t *key_data,
936 int nonce_length_arg,
937 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100938 int exercise_alg,
939 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940{
Ronald Cron5425a212020-08-04 14:58:35 +0200941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100944 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945 unsigned char nonce[16] = {0};
946 size_t nonce_length = nonce_length_arg;
947 unsigned char tag[16];
948 size_t tag_length = tag_length_arg;
949 size_t output_length;
950
951 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
952 TEST_ASSERT( tag_length <= sizeof( tag ) );
953
Gilles Peskine8817f612018-12-18 00:18:46 +0100954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200956 psa_set_key_usage_flags( &attributes, policy_usage );
957 psa_set_key_algorithm( &attributes, policy_alg );
958 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959
Gilles Peskine049c7532019-05-15 20:22:09 +0200960 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200961 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200962
Ronald Cron5425a212020-08-04 14:58:35 +0200963 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964 nonce, nonce_length,
965 NULL, 0,
966 NULL, 0,
967 tag, tag_length,
968 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100969 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
970 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200971 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100972 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973
974 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200975 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200976 nonce, nonce_length,
977 NULL, 0,
978 tag, tag_length,
979 NULL, 0,
980 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100981 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
982 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
983 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100984 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200985 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100986 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200987
988exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200989 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200990 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200991}
992/* END_CASE */
993
994/* BEGIN_CASE */
995void asymmetric_encryption_key_policy( int policy_usage,
996 int policy_alg,
997 int key_type,
998 data_t *key_data,
999 int exercise_alg )
1000{
Ronald Cron5425a212020-08-04 14:58:35 +02001001 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003 psa_status_t status;
1004 size_t key_bits;
1005 size_t buffer_length;
1006 unsigned char *buffer = NULL;
1007 size_t output_length;
1008
Gilles Peskine8817f612018-12-18 00:18:46 +01001009 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001011 psa_set_key_usage_flags( &attributes, policy_usage );
1012 psa_set_key_algorithm( &attributes, policy_alg );
1013 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014
Gilles Peskine049c7532019-05-15 20:22:09 +02001015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001016 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001017
Ronald Cron5425a212020-08-04 14:58:35 +02001018 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001019 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001020 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1021 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001022 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001023
Ronald Cron5425a212020-08-04 14:58:35 +02001024 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025 NULL, 0,
1026 NULL, 0,
1027 buffer, buffer_length,
1028 &output_length );
1029 if( policy_alg == exercise_alg &&
1030 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001031 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001034
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001035 if( buffer_length != 0 )
1036 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001037 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001038 buffer, buffer_length,
1039 NULL, 0,
1040 buffer, buffer_length,
1041 &output_length );
1042 if( policy_alg == exercise_alg &&
1043 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001044 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001045 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001046 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001047
1048exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001049 /*
1050 * Key attributes may have been returned by psa_get_key_attributes()
1051 * thus reset them as required.
1052 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001053 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001054
1055 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001056 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001057 mbedtls_free( buffer );
1058}
1059/* END_CASE */
1060
1061/* BEGIN_CASE */
1062void asymmetric_signature_key_policy( int policy_usage,
1063 int policy_alg,
1064 int key_type,
1065 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001066 int exercise_alg,
1067 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068{
Ronald Cron5425a212020-08-04 14:58:35 +02001069 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001071 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001072 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1073 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1074 * compatible with the policy and `payload_length_arg` is supposed to be
1075 * a valid input length to sign. If `payload_length_arg <= 0`,
1076 * `exercise_alg` is supposed to be forbidden by the policy. */
1077 int compatible_alg = payload_length_arg > 0;
1078 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001079 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001080 size_t signature_length;
1081
Gilles Peskine8817f612018-12-18 00:18:46 +01001082 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001083
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001084 psa_set_key_usage_flags( &attributes, policy_usage );
1085 psa_set_key_algorithm( &attributes, policy_alg );
1086 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087
Gilles Peskine049c7532019-05-15 20:22:09 +02001088 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001089 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001090
Ronald Cron5425a212020-08-04 14:58:35 +02001091 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001092 payload, payload_length,
1093 signature, sizeof( signature ),
1094 &signature_length );
1095 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001096 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001097 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001098 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001099
1100 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001101 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001102 payload, payload_length,
1103 signature, sizeof( signature ) );
1104 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001105 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001106 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001107 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001108
1109exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001110 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001111 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001112}
1113/* END_CASE */
1114
Janos Follathba3fab92019-06-11 14:50:16 +01001115/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001116void derive_key_policy( int policy_usage,
1117 int policy_alg,
1118 int key_type,
1119 data_t *key_data,
1120 int exercise_alg )
1121{
Ronald Cron5425a212020-08-04 14:58:35 +02001122 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001124 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001125 psa_status_t status;
1126
Gilles Peskine8817f612018-12-18 00:18:46 +01001127 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001128
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001129 psa_set_key_usage_flags( &attributes, policy_usage );
1130 psa_set_key_algorithm( &attributes, policy_alg );
1131 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001132
Gilles Peskine049c7532019-05-15 20:22:09 +02001133 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001134 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001135
Janos Follathba3fab92019-06-11 14:50:16 +01001136 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1137
1138 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1139 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001140 {
Janos Follathba3fab92019-06-11 14:50:16 +01001141 PSA_ASSERT( psa_key_derivation_input_bytes(
1142 &operation,
1143 PSA_KEY_DERIVATION_INPUT_SEED,
1144 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001145 }
Janos Follathba3fab92019-06-11 14:50:16 +01001146
1147 status = psa_key_derivation_input_key( &operation,
1148 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001149 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001150
Gilles Peskineea0fb492018-07-12 17:17:20 +02001151 if( policy_alg == exercise_alg &&
1152 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001153 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001154 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001155 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001156
1157exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001158 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001159 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001160 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001161}
1162/* END_CASE */
1163
1164/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001165void agreement_key_policy( int policy_usage,
1166 int policy_alg,
1167 int key_type_arg,
1168 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001169 int exercise_alg,
1170 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001171{
Ronald Cron5425a212020-08-04 14:58:35 +02001172 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001174 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001175 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001176 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001177 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001178
Gilles Peskine8817f612018-12-18 00:18:46 +01001179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001180
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001181 psa_set_key_usage_flags( &attributes, policy_usage );
1182 psa_set_key_algorithm( &attributes, policy_alg );
1183 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001184
Gilles Peskine049c7532019-05-15 20:22:09 +02001185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001186 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001187
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001188 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001189 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001190
Steven Cooremance48e852020-10-05 16:02:45 +02001191 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001192
1193exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001194 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001195 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001196 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001197}
1198/* END_CASE */
1199
1200/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001201void key_policy_alg2( int key_type_arg, data_t *key_data,
1202 int usage_arg, int alg_arg, int alg2_arg )
1203{
Ronald Cron5425a212020-08-04 14:58:35 +02001204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001205 psa_key_type_t key_type = key_type_arg;
1206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1207 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1208 psa_key_usage_t usage = usage_arg;
1209 psa_algorithm_t alg = alg_arg;
1210 psa_algorithm_t alg2 = alg2_arg;
1211
1212 PSA_ASSERT( psa_crypto_init( ) );
1213
1214 psa_set_key_usage_flags( &attributes, usage );
1215 psa_set_key_algorithm( &attributes, alg );
1216 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1217 psa_set_key_type( &attributes, key_type );
1218 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001219 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001220
Ronald Cron5425a212020-08-04 14:58:35 +02001221 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001222 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1223 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1224 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1225
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001226 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001227 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001228 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001229 goto exit;
1230
1231exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001232 /*
1233 * Key attributes may have been returned by psa_get_key_attributes()
1234 * thus reset them as required.
1235 */
1236 psa_reset_key_attributes( &got_attributes );
1237
Ronald Cron5425a212020-08-04 14:58:35 +02001238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001239 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001244void raw_agreement_key_policy( int policy_usage,
1245 int policy_alg,
1246 int key_type_arg,
1247 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001248 int exercise_alg,
1249 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001250{
Ronald Cron5425a212020-08-04 14:58:35 +02001251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001254 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001255 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001256 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001257
1258 PSA_ASSERT( psa_crypto_init( ) );
1259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001260 psa_set_key_usage_flags( &attributes, policy_usage );
1261 psa_set_key_algorithm( &attributes, policy_alg );
1262 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001263
Gilles Peskine049c7532019-05-15 20:22:09 +02001264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001265 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001266
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001267 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001268
Steven Cooremance48e852020-10-05 16:02:45 +02001269 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001270
1271exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001272 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001273 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001274 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001275}
1276/* END_CASE */
1277
1278/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001279void copy_success( int source_usage_arg,
1280 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001281 int type_arg, data_t *material,
1282 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001283 int target_usage_arg,
1284 int target_alg_arg, int target_alg2_arg,
1285 int expected_usage_arg,
1286 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001287{
Gilles Peskineca25db92019-04-19 11:43:08 +02001288 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1289 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001290 psa_key_usage_t expected_usage = expected_usage_arg;
1291 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001292 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001293 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1294 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001295 uint8_t *export_buffer = NULL;
1296
Gilles Peskine57ab7212019-01-28 13:03:09 +01001297 PSA_ASSERT( psa_crypto_init( ) );
1298
Gilles Peskineca25db92019-04-19 11:43:08 +02001299 /* Prepare the source key. */
1300 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1301 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001302 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001303 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001304 PSA_ASSERT( psa_import_key( &source_attributes,
1305 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001306 &source_key ) );
1307 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001308
Gilles Peskineca25db92019-04-19 11:43:08 +02001309 /* Prepare the target attributes. */
1310 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001311 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001312 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001313 /* Set volatile lifetime to reset the key identifier to 0. */
1314 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1315 }
1316
Gilles Peskineca25db92019-04-19 11:43:08 +02001317 if( target_usage_arg != -1 )
1318 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1319 if( target_alg_arg != -1 )
1320 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001321 if( target_alg2_arg != -1 )
1322 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001323
1324 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001325 PSA_ASSERT( psa_copy_key( source_key,
1326 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001327
1328 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001329 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001330
1331 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001333 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1334 psa_get_key_type( &target_attributes ) );
1335 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1336 psa_get_key_bits( &target_attributes ) );
1337 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1338 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001339 TEST_EQUAL( expected_alg2,
1340 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001341 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1342 {
1343 size_t length;
1344 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001345 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001346 material->len, &length ) );
1347 ASSERT_COMPARE( material->x, material->len,
1348 export_buffer, length );
1349 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001350
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001351 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001352 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001353 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001354 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355
Ronald Cron5425a212020-08-04 14:58:35 +02001356 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001357
1358exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001359 /*
1360 * Source and target key attributes may have been returned by
1361 * psa_get_key_attributes() thus reset them as required.
1362 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001363 psa_reset_key_attributes( &source_attributes );
1364 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001365
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001366 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001367 mbedtls_free( export_buffer );
1368}
1369/* END_CASE */
1370
1371/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001372void copy_fail( int source_usage_arg,
1373 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001374 int type_arg, data_t *material,
1375 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001376 int target_usage_arg,
1377 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001378 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001379 int expected_status_arg )
1380{
1381 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1382 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001383 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1384 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001385 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001386
1387 PSA_ASSERT( psa_crypto_init( ) );
1388
1389 /* Prepare the source key. */
1390 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1391 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001392 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001393 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001394 PSA_ASSERT( psa_import_key( &source_attributes,
1395 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001396 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001397
1398 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001399 psa_set_key_id( &target_attributes, key_id );
1400 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001401 psa_set_key_type( &target_attributes, target_type_arg );
1402 psa_set_key_bits( &target_attributes, target_bits_arg );
1403 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1404 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001405 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001406
1407 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001408 TEST_EQUAL( psa_copy_key( source_key,
1409 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001410 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001411
Ronald Cron5425a212020-08-04 14:58:35 +02001412 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001413
Gilles Peskine4a644642019-05-03 17:14:08 +02001414exit:
1415 psa_reset_key_attributes( &source_attributes );
1416 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001417 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001418}
1419/* END_CASE */
1420
1421/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001422void hash_operation_init( )
1423{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001424 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001425 /* Test each valid way of initializing the object, except for `= {0}`, as
1426 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1427 * though it's OK by the C standard. We could test for this, but we'd need
1428 * to supress the Clang warning for the test. */
1429 psa_hash_operation_t func = psa_hash_operation_init( );
1430 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1431 psa_hash_operation_t zero;
1432
1433 memset( &zero, 0, sizeof( zero ) );
1434
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001435 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001436 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1437 PSA_ERROR_BAD_STATE );
1438 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1439 PSA_ERROR_BAD_STATE );
1440 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1441 PSA_ERROR_BAD_STATE );
1442
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001443 /* A default hash operation should be abortable without error. */
1444 PSA_ASSERT( psa_hash_abort( &func ) );
1445 PSA_ASSERT( psa_hash_abort( &init ) );
1446 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001447}
1448/* END_CASE */
1449
1450/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001451void hash_setup( int alg_arg,
1452 int expected_status_arg )
1453{
1454 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001455 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001456 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001457 psa_status_t status;
1458
Gilles Peskine8817f612018-12-18 00:18:46 +01001459 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001461 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001462 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001463
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001464 /* Whether setup succeeded or failed, abort must succeed. */
1465 PSA_ASSERT( psa_hash_abort( &operation ) );
1466
1467 /* If setup failed, reproduce the failure, so as to
1468 * test the resulting state of the operation object. */
1469 if( status != PSA_SUCCESS )
1470 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1471
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001472 /* Now the operation object should be reusable. */
1473#if defined(KNOWN_SUPPORTED_HASH_ALG)
1474 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1475 PSA_ASSERT( psa_hash_abort( &operation ) );
1476#endif
1477
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001478exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001479 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001480}
1481/* END_CASE */
1482
1483/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001484void hash_compute_fail( int alg_arg, data_t *input,
1485 int output_size_arg, int expected_status_arg )
1486{
1487 psa_algorithm_t alg = alg_arg;
1488 uint8_t *output = NULL;
1489 size_t output_size = output_size_arg;
1490 size_t output_length = INVALID_EXPORT_LENGTH;
1491 psa_status_t expected_status = expected_status_arg;
1492 psa_status_t status;
1493
1494 ASSERT_ALLOC( output, output_size );
1495
1496 PSA_ASSERT( psa_crypto_init( ) );
1497
1498 status = psa_hash_compute( alg, input->x, input->len,
1499 output, output_size, &output_length );
1500 TEST_EQUAL( status, expected_status );
1501 TEST_ASSERT( output_length <= output_size );
1502
1503exit:
1504 mbedtls_free( output );
1505 PSA_DONE( );
1506}
1507/* END_CASE */
1508
1509/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001510void hash_compare_fail( int alg_arg, data_t *input,
1511 data_t *reference_hash,
1512 int expected_status_arg )
1513{
1514 psa_algorithm_t alg = alg_arg;
1515 psa_status_t expected_status = expected_status_arg;
1516 psa_status_t status;
1517
1518 PSA_ASSERT( psa_crypto_init( ) );
1519
1520 status = psa_hash_compare( alg, input->x, input->len,
1521 reference_hash->x, reference_hash->len );
1522 TEST_EQUAL( status, expected_status );
1523
1524exit:
1525 PSA_DONE( );
1526}
1527/* END_CASE */
1528
1529/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001530void hash_compute_compare( int alg_arg, data_t *input,
1531 data_t *expected_output )
1532{
1533 psa_algorithm_t alg = alg_arg;
1534 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1535 size_t output_length = INVALID_EXPORT_LENGTH;
1536 size_t i;
1537
1538 PSA_ASSERT( psa_crypto_init( ) );
1539
1540 /* Compute with tight buffer */
1541 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001542 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001543 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001544 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001545 ASSERT_COMPARE( output, output_length,
1546 expected_output->x, expected_output->len );
1547
1548 /* Compute with larger buffer */
1549 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1550 output, sizeof( output ),
1551 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001552 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001553 ASSERT_COMPARE( output, output_length,
1554 expected_output->x, expected_output->len );
1555
1556 /* Compare with correct hash */
1557 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1558 output, output_length ) );
1559
1560 /* Compare with trailing garbage */
1561 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1562 output, output_length + 1 ),
1563 PSA_ERROR_INVALID_SIGNATURE );
1564
1565 /* Compare with truncated hash */
1566 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1567 output, output_length - 1 ),
1568 PSA_ERROR_INVALID_SIGNATURE );
1569
1570 /* Compare with corrupted value */
1571 for( i = 0; i < output_length; i++ )
1572 {
Chris Jones9634bb12021-01-20 15:56:42 +00001573 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001574 output[i] ^= 1;
1575 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1576 output, output_length ),
1577 PSA_ERROR_INVALID_SIGNATURE );
1578 output[i] ^= 1;
1579 }
1580
1581exit:
1582 PSA_DONE( );
1583}
1584/* END_CASE */
1585
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001586/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001587void hash_bad_order( )
1588{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001589 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001590 unsigned char input[] = "";
1591 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001592 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001593 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1594 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1595 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001596 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001597 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001598 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001599
Gilles Peskine8817f612018-12-18 00:18:46 +01001600 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001601
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001602 /* Call setup twice in a row. */
1603 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1604 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1605 PSA_ERROR_BAD_STATE );
1606 PSA_ASSERT( psa_hash_abort( &operation ) );
1607
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001608 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001609 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001610 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001611 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001612
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001613 /* Call update after finish. */
1614 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1615 PSA_ASSERT( psa_hash_finish( &operation,
1616 hash, sizeof( hash ), &hash_len ) );
1617 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001618 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001619 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001620
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001621 /* Call verify without calling setup beforehand. */
1622 TEST_EQUAL( psa_hash_verify( &operation,
1623 valid_hash, sizeof( valid_hash ) ),
1624 PSA_ERROR_BAD_STATE );
1625 PSA_ASSERT( psa_hash_abort( &operation ) );
1626
1627 /* Call verify after finish. */
1628 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1629 PSA_ASSERT( psa_hash_finish( &operation,
1630 hash, sizeof( hash ), &hash_len ) );
1631 TEST_EQUAL( psa_hash_verify( &operation,
1632 valid_hash, sizeof( valid_hash ) ),
1633 PSA_ERROR_BAD_STATE );
1634 PSA_ASSERT( psa_hash_abort( &operation ) );
1635
1636 /* Call verify twice in a row. */
1637 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1638 PSA_ASSERT( psa_hash_verify( &operation,
1639 valid_hash, sizeof( valid_hash ) ) );
1640 TEST_EQUAL( psa_hash_verify( &operation,
1641 valid_hash, sizeof( valid_hash ) ),
1642 PSA_ERROR_BAD_STATE );
1643 PSA_ASSERT( psa_hash_abort( &operation ) );
1644
1645 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001646 TEST_EQUAL( psa_hash_finish( &operation,
1647 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001648 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001649 PSA_ASSERT( psa_hash_abort( &operation ) );
1650
1651 /* Call finish twice in a row. */
1652 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1653 PSA_ASSERT( psa_hash_finish( &operation,
1654 hash, sizeof( hash ), &hash_len ) );
1655 TEST_EQUAL( psa_hash_finish( &operation,
1656 hash, sizeof( hash ), &hash_len ),
1657 PSA_ERROR_BAD_STATE );
1658 PSA_ASSERT( psa_hash_abort( &operation ) );
1659
1660 /* Call finish after calling verify. */
1661 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1662 PSA_ASSERT( psa_hash_verify( &operation,
1663 valid_hash, sizeof( valid_hash ) ) );
1664 TEST_EQUAL( psa_hash_finish( &operation,
1665 hash, sizeof( hash ), &hash_len ),
1666 PSA_ERROR_BAD_STATE );
1667 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001668
1669exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001670 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001671}
1672/* END_CASE */
1673
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001674/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001675void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001676{
1677 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001678 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1679 * appended to it */
1680 unsigned char hash[] = {
1681 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1682 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1683 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001684 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001685 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001686
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001688
itayzafrir27e69452018-11-01 14:26:34 +02001689 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001690 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001691 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001692 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001693
itayzafrir27e69452018-11-01 14:26:34 +02001694 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001695 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001696 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001697 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001698
itayzafrir27e69452018-11-01 14:26:34 +02001699 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001700 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001701 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001703
itayzafrirec93d302018-10-18 18:01:10 +03001704exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001705 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001706}
1707/* END_CASE */
1708
Ronald Cronee414c72021-03-18 18:50:08 +01001709/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001710void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001711{
1712 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001713 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001714 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001715 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001716 size_t hash_len;
1717
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001719
itayzafrir58028322018-10-25 10:22:01 +03001720 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001721 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001722 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001723 hash, expected_size - 1, &hash_len ),
1724 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001725
1726exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001727 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001728}
1729/* END_CASE */
1730
Ronald Cronee414c72021-03-18 18:50:08 +01001731/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001732void hash_clone_source_state( )
1733{
1734 psa_algorithm_t alg = PSA_ALG_SHA_256;
1735 unsigned char hash[PSA_HASH_MAX_SIZE];
1736 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1737 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1738 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1739 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1740 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1741 size_t hash_len;
1742
1743 PSA_ASSERT( psa_crypto_init( ) );
1744 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1745
1746 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1747 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1748 PSA_ASSERT( psa_hash_finish( &op_finished,
1749 hash, sizeof( hash ), &hash_len ) );
1750 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1751 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1752
1753 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1754 PSA_ERROR_BAD_STATE );
1755
1756 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1757 PSA_ASSERT( psa_hash_finish( &op_init,
1758 hash, sizeof( hash ), &hash_len ) );
1759 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1760 PSA_ASSERT( psa_hash_finish( &op_finished,
1761 hash, sizeof( hash ), &hash_len ) );
1762 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1763 PSA_ASSERT( psa_hash_finish( &op_aborted,
1764 hash, sizeof( hash ), &hash_len ) );
1765
1766exit:
1767 psa_hash_abort( &op_source );
1768 psa_hash_abort( &op_init );
1769 psa_hash_abort( &op_setup );
1770 psa_hash_abort( &op_finished );
1771 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001772 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001773}
1774/* END_CASE */
1775
Ronald Cronee414c72021-03-18 18:50:08 +01001776/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001777void hash_clone_target_state( )
1778{
1779 psa_algorithm_t alg = PSA_ALG_SHA_256;
1780 unsigned char hash[PSA_HASH_MAX_SIZE];
1781 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1782 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1783 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1784 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1785 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1786 size_t hash_len;
1787
1788 PSA_ASSERT( psa_crypto_init( ) );
1789
1790 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1791 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1792 PSA_ASSERT( psa_hash_finish( &op_finished,
1793 hash, sizeof( hash ), &hash_len ) );
1794 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1795 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1796
1797 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1798 PSA_ASSERT( psa_hash_finish( &op_target,
1799 hash, sizeof( hash ), &hash_len ) );
1800
1801 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1802 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1803 PSA_ERROR_BAD_STATE );
1804 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1805 PSA_ERROR_BAD_STATE );
1806
1807exit:
1808 psa_hash_abort( &op_target );
1809 psa_hash_abort( &op_init );
1810 psa_hash_abort( &op_setup );
1811 psa_hash_abort( &op_finished );
1812 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001813 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001814}
1815/* END_CASE */
1816
itayzafrir58028322018-10-25 10:22:01 +03001817/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001818void mac_operation_init( )
1819{
Jaeden Amero252ef282019-02-15 14:05:35 +00001820 const uint8_t input[1] = { 0 };
1821
Jaeden Amero769ce272019-01-04 11:48:03 +00001822 /* Test each valid way of initializing the object, except for `= {0}`, as
1823 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1824 * though it's OK by the C standard. We could test for this, but we'd need
1825 * to supress the Clang warning for the test. */
1826 psa_mac_operation_t func = psa_mac_operation_init( );
1827 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1828 psa_mac_operation_t zero;
1829
1830 memset( &zero, 0, sizeof( zero ) );
1831
Jaeden Amero252ef282019-02-15 14:05:35 +00001832 /* A freshly-initialized MAC operation should not be usable. */
1833 TEST_EQUAL( psa_mac_update( &func,
1834 input, sizeof( input ) ),
1835 PSA_ERROR_BAD_STATE );
1836 TEST_EQUAL( psa_mac_update( &init,
1837 input, sizeof( input ) ),
1838 PSA_ERROR_BAD_STATE );
1839 TEST_EQUAL( psa_mac_update( &zero,
1840 input, sizeof( input ) ),
1841 PSA_ERROR_BAD_STATE );
1842
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001843 /* A default MAC operation should be abortable without error. */
1844 PSA_ASSERT( psa_mac_abort( &func ) );
1845 PSA_ASSERT( psa_mac_abort( &init ) );
1846 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001847}
1848/* END_CASE */
1849
1850/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001851void mac_setup( int key_type_arg,
1852 data_t *key,
1853 int alg_arg,
1854 int expected_status_arg )
1855{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001856 psa_key_type_t key_type = key_type_arg;
1857 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001858 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001859 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001860 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1861#if defined(KNOWN_SUPPORTED_MAC_ALG)
1862 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1863#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001864
Gilles Peskine8817f612018-12-18 00:18:46 +01001865 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001866
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001867 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1868 &operation, &status ) )
1869 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001870 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001871
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001872 /* The operation object should be reusable. */
1873#if defined(KNOWN_SUPPORTED_MAC_ALG)
1874 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1875 smoke_test_key_data,
1876 sizeof( smoke_test_key_data ),
1877 KNOWN_SUPPORTED_MAC_ALG,
1878 &operation, &status ) )
1879 goto exit;
1880 TEST_EQUAL( status, PSA_SUCCESS );
1881#endif
1882
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001883exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001884 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001885}
1886/* END_CASE */
1887
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001888/* 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 +00001889void mac_bad_order( )
1890{
Ronald Cron5425a212020-08-04 14:58:35 +02001891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001892 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1893 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001894 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001895 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1896 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1897 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001899 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1900 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1901 size_t sign_mac_length = 0;
1902 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1903 const uint8_t verify_mac[] = {
1904 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1905 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1906 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1907
1908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001909 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001910 psa_set_key_algorithm( &attributes, alg );
1911 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001912
Ronald Cron5425a212020-08-04 14:58:35 +02001913 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1914 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001915
Jaeden Amero252ef282019-02-15 14:05:35 +00001916 /* Call update without calling setup beforehand. */
1917 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1918 PSA_ERROR_BAD_STATE );
1919 PSA_ASSERT( psa_mac_abort( &operation ) );
1920
1921 /* Call sign finish without calling setup beforehand. */
1922 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1923 &sign_mac_length),
1924 PSA_ERROR_BAD_STATE );
1925 PSA_ASSERT( psa_mac_abort( &operation ) );
1926
1927 /* Call verify finish without calling setup beforehand. */
1928 TEST_EQUAL( psa_mac_verify_finish( &operation,
1929 verify_mac, sizeof( verify_mac ) ),
1930 PSA_ERROR_BAD_STATE );
1931 PSA_ASSERT( psa_mac_abort( &operation ) );
1932
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001933 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001934 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1935 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001936 PSA_ERROR_BAD_STATE );
1937 PSA_ASSERT( psa_mac_abort( &operation ) );
1938
Jaeden Amero252ef282019-02-15 14:05:35 +00001939 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001940 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001941 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1942 PSA_ASSERT( psa_mac_sign_finish( &operation,
1943 sign_mac, sizeof( sign_mac ),
1944 &sign_mac_length ) );
1945 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1946 PSA_ERROR_BAD_STATE );
1947 PSA_ASSERT( psa_mac_abort( &operation ) );
1948
1949 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001950 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001951 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1952 PSA_ASSERT( psa_mac_verify_finish( &operation,
1953 verify_mac, sizeof( verify_mac ) ) );
1954 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1955 PSA_ERROR_BAD_STATE );
1956 PSA_ASSERT( psa_mac_abort( &operation ) );
1957
1958 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001959 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001960 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1961 PSA_ASSERT( psa_mac_sign_finish( &operation,
1962 sign_mac, sizeof( sign_mac ),
1963 &sign_mac_length ) );
1964 TEST_EQUAL( psa_mac_sign_finish( &operation,
1965 sign_mac, sizeof( sign_mac ),
1966 &sign_mac_length ),
1967 PSA_ERROR_BAD_STATE );
1968 PSA_ASSERT( psa_mac_abort( &operation ) );
1969
1970 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001971 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001972 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1973 PSA_ASSERT( psa_mac_verify_finish( &operation,
1974 verify_mac, sizeof( verify_mac ) ) );
1975 TEST_EQUAL( psa_mac_verify_finish( &operation,
1976 verify_mac, sizeof( verify_mac ) ),
1977 PSA_ERROR_BAD_STATE );
1978 PSA_ASSERT( psa_mac_abort( &operation ) );
1979
1980 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001981 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001982 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1983 TEST_EQUAL( psa_mac_verify_finish( &operation,
1984 verify_mac, sizeof( verify_mac ) ),
1985 PSA_ERROR_BAD_STATE );
1986 PSA_ASSERT( psa_mac_abort( &operation ) );
1987
1988 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001989 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001990 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1991 TEST_EQUAL( psa_mac_sign_finish( &operation,
1992 sign_mac, sizeof( sign_mac ),
1993 &sign_mac_length ),
1994 PSA_ERROR_BAD_STATE );
1995 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001996
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001998
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001999exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002000 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002001}
2002/* END_CASE */
2003
2004/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002006 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002007 int alg_arg,
2008 data_t *input,
2009 data_t *expected_mac )
2010{
Ronald Cron5425a212020-08-04 14:58:35 +02002011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002012 psa_key_type_t key_type = key_type_arg;
2013 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002014 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002016 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002017 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002018 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002019 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002020 const size_t output_sizes_to_test[] = {
2021 0,
2022 1,
2023 expected_mac->len - 1,
2024 expected_mac->len,
2025 expected_mac->len + 1,
2026 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002027
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002028 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002029 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002030 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002031
Gilles Peskine8817f612018-12-18 00:18:46 +01002032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002033
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002034 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002035 psa_set_key_algorithm( &attributes, alg );
2036 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002037
Ronald Cron5425a212020-08-04 14:58:35 +02002038 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2039 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002040
Gilles Peskine8b356b52020-08-25 23:44:59 +02002041 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2042 {
2043 const size_t output_size = output_sizes_to_test[i];
2044 psa_status_t expected_status =
2045 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2046 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002047
Chris Jones9634bb12021-01-20 15:56:42 +00002048 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002049 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002050
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002051 /* Calculate the MAC, one-shot case. */
2052 TEST_EQUAL( psa_mac_compute( key, alg,
2053 input->x, input->len,
2054 actual_mac, output_size, &mac_length ),
2055 expected_status );
2056 if( expected_status == PSA_SUCCESS )
2057 {
2058 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2059 actual_mac, mac_length );
2060 }
2061
2062 if( output_size > 0 )
2063 memset( actual_mac, 0, output_size );
2064
2065 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002066 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002067 PSA_ASSERT( psa_mac_update( &operation,
2068 input->x, input->len ) );
2069 TEST_EQUAL( psa_mac_sign_finish( &operation,
2070 actual_mac, output_size,
2071 &mac_length ),
2072 expected_status );
2073 PSA_ASSERT( psa_mac_abort( &operation ) );
2074
2075 if( expected_status == PSA_SUCCESS )
2076 {
2077 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2078 actual_mac, mac_length );
2079 }
2080 mbedtls_free( actual_mac );
2081 actual_mac = NULL;
2082 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002083
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002084exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002085 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002086 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002087 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002088 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002089}
2090/* END_CASE */
2091
2092/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002093void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002094 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002095 int alg_arg,
2096 data_t *input,
2097 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002098{
Ronald Cron5425a212020-08-04 14:58:35 +02002099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002100 psa_key_type_t key_type = key_type_arg;
2101 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002102 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002103 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002104 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002105
Gilles Peskine69c12672018-06-28 00:07:19 +02002106 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2107
Gilles Peskine8817f612018-12-18 00:18:46 +01002108 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002109
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002110 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002111 psa_set_key_algorithm( &attributes, alg );
2112 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002113
Ronald Cron5425a212020-08-04 14:58:35 +02002114 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2115 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002116
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002117 /* Verify correct MAC, one-shot case. */
2118 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2119 expected_mac->x, expected_mac->len ) );
2120
2121 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002122 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002123 PSA_ASSERT( psa_mac_update( &operation,
2124 input->x, input->len ) );
2125 PSA_ASSERT( psa_mac_verify_finish( &operation,
2126 expected_mac->x,
2127 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002129 /* Test a MAC that's too short, one-shot case. */
2130 TEST_EQUAL( psa_mac_verify( key, alg,
2131 input->x, input->len,
2132 expected_mac->x,
2133 expected_mac->len - 1 ),
2134 PSA_ERROR_INVALID_SIGNATURE );
2135
2136 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002137 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002138 PSA_ASSERT( psa_mac_update( &operation,
2139 input->x, input->len ) );
2140 TEST_EQUAL( psa_mac_verify_finish( &operation,
2141 expected_mac->x,
2142 expected_mac->len - 1 ),
2143 PSA_ERROR_INVALID_SIGNATURE );
2144
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002145 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002146 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2147 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002148 TEST_EQUAL( psa_mac_verify( key, alg,
2149 input->x, input->len,
2150 perturbed_mac, expected_mac->len + 1 ),
2151 PSA_ERROR_INVALID_SIGNATURE );
2152
2153 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002154 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002155 PSA_ASSERT( psa_mac_update( &operation,
2156 input->x, input->len ) );
2157 TEST_EQUAL( psa_mac_verify_finish( &operation,
2158 perturbed_mac,
2159 expected_mac->len + 1 ),
2160 PSA_ERROR_INVALID_SIGNATURE );
2161
2162 /* Test changing one byte. */
2163 for( size_t i = 0; i < expected_mac->len; i++ )
2164 {
Chris Jones9634bb12021-01-20 15:56:42 +00002165 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002166 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002167
2168 TEST_EQUAL( psa_mac_verify( key, alg,
2169 input->x, input->len,
2170 perturbed_mac, expected_mac->len ),
2171 PSA_ERROR_INVALID_SIGNATURE );
2172
Ronald Cron5425a212020-08-04 14:58:35 +02002173 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002174 PSA_ASSERT( psa_mac_update( &operation,
2175 input->x, input->len ) );
2176 TEST_EQUAL( psa_mac_verify_finish( &operation,
2177 perturbed_mac,
2178 expected_mac->len ),
2179 PSA_ERROR_INVALID_SIGNATURE );
2180 perturbed_mac[i] ^= 1;
2181 }
2182
Gilles Peskine8c9def32018-02-08 10:02:12 +01002183exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002184 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002186 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002187 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002188}
2189/* END_CASE */
2190
2191/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002192void cipher_operation_init( )
2193{
Jaeden Ameroab439972019-02-15 14:12:05 +00002194 const uint8_t input[1] = { 0 };
2195 unsigned char output[1] = { 0 };
2196 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002197 /* Test each valid way of initializing the object, except for `= {0}`, as
2198 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2199 * though it's OK by the C standard. We could test for this, but we'd need
2200 * to supress the Clang warning for the test. */
2201 psa_cipher_operation_t func = psa_cipher_operation_init( );
2202 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2203 psa_cipher_operation_t zero;
2204
2205 memset( &zero, 0, sizeof( zero ) );
2206
Jaeden Ameroab439972019-02-15 14:12:05 +00002207 /* A freshly-initialized cipher operation should not be usable. */
2208 TEST_EQUAL( psa_cipher_update( &func,
2209 input, sizeof( input ),
2210 output, sizeof( output ),
2211 &output_length ),
2212 PSA_ERROR_BAD_STATE );
2213 TEST_EQUAL( psa_cipher_update( &init,
2214 input, sizeof( input ),
2215 output, sizeof( output ),
2216 &output_length ),
2217 PSA_ERROR_BAD_STATE );
2218 TEST_EQUAL( psa_cipher_update( &zero,
2219 input, sizeof( input ),
2220 output, sizeof( output ),
2221 &output_length ),
2222 PSA_ERROR_BAD_STATE );
2223
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002224 /* A default cipher operation should be abortable without error. */
2225 PSA_ASSERT( psa_cipher_abort( &func ) );
2226 PSA_ASSERT( psa_cipher_abort( &init ) );
2227 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002228}
2229/* END_CASE */
2230
2231/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002232void cipher_setup( int key_type_arg,
2233 data_t *key,
2234 int alg_arg,
2235 int expected_status_arg )
2236{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002237 psa_key_type_t key_type = key_type_arg;
2238 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002239 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002240 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002241 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002242#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002243 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2244#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002245
Gilles Peskine8817f612018-12-18 00:18:46 +01002246 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002247
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002248 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2249 &operation, &status ) )
2250 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002251 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002252
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002253 /* The operation object should be reusable. */
2254#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2255 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2256 smoke_test_key_data,
2257 sizeof( smoke_test_key_data ),
2258 KNOWN_SUPPORTED_CIPHER_ALG,
2259 &operation, &status ) )
2260 goto exit;
2261 TEST_EQUAL( status, PSA_SUCCESS );
2262#endif
2263
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002265 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002266 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002267}
2268/* END_CASE */
2269
Ronald Cronee414c72021-03-18 18:50:08 +01002270/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002271void cipher_bad_order( )
2272{
Ronald Cron5425a212020-08-04 14:58:35 +02002273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002274 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2275 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002277 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002278 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002279 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002280 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2281 0xaa, 0xaa, 0xaa, 0xaa };
2282 const uint8_t text[] = {
2283 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2284 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002285 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002286 size_t length = 0;
2287
2288 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2290 psa_set_key_algorithm( &attributes, alg );
2291 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002292 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2293 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002294
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002295 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002296 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2297 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002298 PSA_ERROR_BAD_STATE );
2299 PSA_ASSERT( psa_cipher_abort( &operation ) );
2300
2301 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002302 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2303 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002304 PSA_ERROR_BAD_STATE );
2305 PSA_ASSERT( psa_cipher_abort( &operation ) );
2306
Jaeden Ameroab439972019-02-15 14:12:05 +00002307 /* Generate an IV without calling setup beforehand. */
2308 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2309 buffer, sizeof( buffer ),
2310 &length ),
2311 PSA_ERROR_BAD_STATE );
2312 PSA_ASSERT( psa_cipher_abort( &operation ) );
2313
2314 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002315 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002316 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2317 buffer, sizeof( buffer ),
2318 &length ) );
2319 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2320 buffer, sizeof( buffer ),
2321 &length ),
2322 PSA_ERROR_BAD_STATE );
2323 PSA_ASSERT( psa_cipher_abort( &operation ) );
2324
2325 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002326 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002327 PSA_ASSERT( psa_cipher_set_iv( &operation,
2328 iv, sizeof( iv ) ) );
2329 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2330 buffer, sizeof( buffer ),
2331 &length ),
2332 PSA_ERROR_BAD_STATE );
2333 PSA_ASSERT( psa_cipher_abort( &operation ) );
2334
2335 /* Set an IV without calling setup beforehand. */
2336 TEST_EQUAL( psa_cipher_set_iv( &operation,
2337 iv, sizeof( iv ) ),
2338 PSA_ERROR_BAD_STATE );
2339 PSA_ASSERT( psa_cipher_abort( &operation ) );
2340
2341 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002342 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002343 PSA_ASSERT( psa_cipher_set_iv( &operation,
2344 iv, sizeof( iv ) ) );
2345 TEST_EQUAL( psa_cipher_set_iv( &operation,
2346 iv, sizeof( iv ) ),
2347 PSA_ERROR_BAD_STATE );
2348 PSA_ASSERT( psa_cipher_abort( &operation ) );
2349
2350 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002351 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002352 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2353 buffer, sizeof( buffer ),
2354 &length ) );
2355 TEST_EQUAL( psa_cipher_set_iv( &operation,
2356 iv, sizeof( iv ) ),
2357 PSA_ERROR_BAD_STATE );
2358 PSA_ASSERT( psa_cipher_abort( &operation ) );
2359
2360 /* Call update without calling setup beforehand. */
2361 TEST_EQUAL( psa_cipher_update( &operation,
2362 text, sizeof( text ),
2363 buffer, sizeof( buffer ),
2364 &length ),
2365 PSA_ERROR_BAD_STATE );
2366 PSA_ASSERT( psa_cipher_abort( &operation ) );
2367
2368 /* Call update without an IV where an IV is required. */
2369 TEST_EQUAL( psa_cipher_update( &operation,
2370 text, sizeof( text ),
2371 buffer, sizeof( buffer ),
2372 &length ),
2373 PSA_ERROR_BAD_STATE );
2374 PSA_ASSERT( psa_cipher_abort( &operation ) );
2375
2376 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002377 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002378 PSA_ASSERT( psa_cipher_set_iv( &operation,
2379 iv, sizeof( iv ) ) );
2380 PSA_ASSERT( psa_cipher_finish( &operation,
2381 buffer, sizeof( buffer ), &length ) );
2382 TEST_EQUAL( psa_cipher_update( &operation,
2383 text, sizeof( text ),
2384 buffer, sizeof( buffer ),
2385 &length ),
2386 PSA_ERROR_BAD_STATE );
2387 PSA_ASSERT( psa_cipher_abort( &operation ) );
2388
2389 /* Call finish without calling setup beforehand. */
2390 TEST_EQUAL( psa_cipher_finish( &operation,
2391 buffer, sizeof( buffer ), &length ),
2392 PSA_ERROR_BAD_STATE );
2393 PSA_ASSERT( psa_cipher_abort( &operation ) );
2394
2395 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002396 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002397 /* Not calling update means we are encrypting an empty buffer, which is OK
2398 * for cipher modes with padding. */
2399 TEST_EQUAL( psa_cipher_finish( &operation,
2400 buffer, sizeof( buffer ), &length ),
2401 PSA_ERROR_BAD_STATE );
2402 PSA_ASSERT( psa_cipher_abort( &operation ) );
2403
2404 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002405 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002406 PSA_ASSERT( psa_cipher_set_iv( &operation,
2407 iv, sizeof( iv ) ) );
2408 PSA_ASSERT( psa_cipher_finish( &operation,
2409 buffer, sizeof( buffer ), &length ) );
2410 TEST_EQUAL( psa_cipher_finish( &operation,
2411 buffer, sizeof( buffer ), &length ),
2412 PSA_ERROR_BAD_STATE );
2413 PSA_ASSERT( psa_cipher_abort( &operation ) );
2414
Ronald Cron5425a212020-08-04 14:58:35 +02002415 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002416
Jaeden Ameroab439972019-02-15 14:12:05 +00002417exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002418 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002419 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002420}
2421/* END_CASE */
2422
2423/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002424void cipher_encrypt_error( int alg_arg,
2425 int key_type_arg,
2426 data_t *key_data,
2427 data_t *input,
2428 int expected_status_arg )
2429{
2430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2431 psa_status_t status;
2432 psa_key_type_t key_type = key_type_arg;
2433 psa_algorithm_t alg = alg_arg;
2434 psa_status_t expected_status = expected_status_arg;
2435 unsigned char *output = NULL;
2436 size_t output_buffer_size = 0;
2437 size_t output_length = 0;
2438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2439
2440 if ( PSA_ERROR_BAD_STATE != expected_status )
2441 {
2442 PSA_ASSERT( psa_crypto_init( ) );
2443
2444 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2445 psa_set_key_algorithm( &attributes, alg );
2446 psa_set_key_type( &attributes, key_type );
2447
2448 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2449 input->len );
2450 ASSERT_ALLOC( output, output_buffer_size );
2451
2452 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2453 &key ) );
2454 }
2455
2456 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2457 output_buffer_size, &output_length );
2458
2459 TEST_EQUAL( status, expected_status );
2460
2461exit:
2462 mbedtls_free( output );
2463 psa_destroy_key( key );
2464 PSA_DONE( );
2465}
2466/* END_CASE */
2467
2468/* BEGIN_CASE */
2469void cipher_encrypt_noiv( int alg_arg,
2470 int key_type_arg,
2471 data_t *key_data,
2472 data_t *input,
2473 data_t *expected_output )
2474{
2475 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2476 psa_key_type_t key_type = key_type_arg;
2477 psa_algorithm_t alg = alg_arg;
2478 unsigned char *output = NULL;
2479 size_t output_buffer_size = 0;
2480 size_t output_length = 0;
2481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2482
2483 PSA_ASSERT( psa_crypto_init( ) );
2484
2485 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2486 psa_set_key_algorithm( &attributes, alg );
2487 psa_set_key_type( &attributes, key_type );
2488
2489 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2490 ASSERT_ALLOC( output, output_buffer_size );
2491
2492 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2493 &key ) );
2494
2495 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2496 output_buffer_size, &output_length ) );
2497 TEST_ASSERT( output_length <=
2498 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2499 TEST_ASSERT( output_length <=
2500 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2501
2502 ASSERT_COMPARE( expected_output->x, expected_output->len,
2503 output, output_length );
2504exit:
2505 mbedtls_free( output );
2506 psa_destroy_key( key );
2507 PSA_DONE( );
2508}
2509/* END_CASE */
2510
2511/* BEGIN_CASE */
2512void cipher_encrypt_sanity( int alg_arg,
2513 int key_type_arg,
2514 data_t *key_data,
2515 data_t *input )
2516{
2517 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2518 psa_key_type_t key_type = key_type_arg;
2519 psa_algorithm_t alg = alg_arg;
2520 unsigned char *output = NULL;
2521 size_t output_buffer_size = 0;
2522 size_t output_length = 0;
2523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2524
2525 PSA_ASSERT( psa_crypto_init( ) );
2526
2527 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2528 psa_set_key_algorithm( &attributes, alg );
2529 psa_set_key_type( &attributes, key_type );
2530
2531 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2532 ASSERT_ALLOC( output, output_buffer_size );
2533
2534 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2535 &key ) );
2536
2537 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2538 output_buffer_size, &output_length ) );
2539 TEST_ASSERT( output_length <=
2540 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2541 TEST_ASSERT( output_length <=
2542 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2543
2544 /* The one-shot cipher encryption uses generated iv so validating
2545 the output is not possible. Only sanity checking is done here. */
2546exit:
2547 mbedtls_free( output );
2548 psa_destroy_key( key );
2549 PSA_DONE( );
2550}
2551/* END_CASE */
2552
2553/* BEGIN_CASE */
2554void cipher_encrypt_validation( int alg_arg,
2555 int key_type_arg,
2556 data_t *key_data,
2557 data_t *input )
2558{
2559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2560 psa_key_type_t key_type = key_type_arg;
2561 psa_algorithm_t alg = alg_arg;
2562 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2563 unsigned char *output1 = NULL;
2564 size_t output1_buffer_size = 0;
2565 size_t output1_length = 0;
2566 unsigned char *output2 = NULL;
2567 size_t output2_buffer_size = 0;
2568 size_t output2_length = 0;
2569 size_t function_output_length = 0;
2570 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2572
2573 PSA_ASSERT( psa_crypto_init( ) );
2574
2575 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2576 psa_set_key_algorithm( &attributes, alg );
2577 psa_set_key_type( &attributes, key_type );
2578
2579 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2580 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2581 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2582 ASSERT_ALLOC( output1, output1_buffer_size );
2583 ASSERT_ALLOC( output2, output2_buffer_size );
2584
2585 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2586 &key ) );
2587
2588 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2589 output1_buffer_size, &output1_length ) );
2590 TEST_ASSERT( output1_length <=
2591 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2592 TEST_ASSERT( output1_length <=
2593 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2594
2595 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2596 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
2597
2598 PSA_ASSERT( psa_cipher_update( &operation,
2599 input->x, input->len,
2600 output2, output2_buffer_size,
2601 &function_output_length ) );
2602 TEST_ASSERT( function_output_length <=
2603 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2604 TEST_ASSERT( function_output_length <=
2605 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2606 output2_length += function_output_length;
2607
2608 PSA_ASSERT( psa_cipher_finish( &operation,
2609 output2 + output2_length,
2610 output2_buffer_size - output2_length,
2611 &function_output_length ) );
2612 TEST_ASSERT( function_output_length <=
2613 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2614 TEST_ASSERT( function_output_length <=
2615 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
2616 output2_length += function_output_length;
2617
2618 PSA_ASSERT( psa_cipher_abort( &operation ) );
2619 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2620 output2, output2_length );
2621
2622exit:
2623 psa_cipher_abort( &operation );
2624 mbedtls_free( output1 );
2625 mbedtls_free( output2 );
2626 psa_destroy_key( key );
2627 PSA_DONE( );
2628}
2629/* END_CASE */
2630
2631/* BEGIN_CASE */
2632void cipher_encrypt_simple_multipart( int alg_arg,
2633 int key_type_arg,
2634 data_t *key_data,
2635 data_t *iv,
2636 data_t *input,
2637 data_t *expected_output,
2638 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002639{
Ronald Cron5425a212020-08-04 14:58:35 +02002640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002641 psa_status_t status;
2642 psa_key_type_t key_type = key_type_arg;
2643 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002644 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002645 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002646 size_t output_buffer_size = 0;
2647 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002648 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002649 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002650 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002651
Gilles Peskine8817f612018-12-18 00:18:46 +01002652 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002653
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002654 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2655 psa_set_key_algorithm( &attributes, alg );
2656 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002657
Ronald Cron5425a212020-08-04 14:58:35 +02002658 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2659 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002660
Ronald Cron5425a212020-08-04 14:58:35 +02002661 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002662
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002663 if( iv->len > 0 )
2664 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002665 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002666 }
2667
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002668 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2669 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002670 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002671
Gilles Peskine8817f612018-12-18 00:18:46 +01002672 PSA_ASSERT( psa_cipher_update( &operation,
2673 input->x, input->len,
2674 output, output_buffer_size,
2675 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002676 TEST_ASSERT( function_output_length <=
2677 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2678 TEST_ASSERT( function_output_length <=
2679 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002680 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002681
Gilles Peskine50e586b2018-06-08 14:28:46 +02002682 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002683 ( output_buffer_size == 0 ? NULL :
2684 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002685 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002686 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002687 TEST_ASSERT( function_output_length <=
2688 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2689 TEST_ASSERT( function_output_length <=
2690 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002691 total_output_length += function_output_length;
2692
Gilles Peskinefe11b722018-12-18 00:24:04 +01002693 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002694 if( expected_status == PSA_SUCCESS )
2695 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002696 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002697 ASSERT_COMPARE( expected_output->x, expected_output->len,
2698 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002699 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002700
Gilles Peskine50e586b2018-06-08 14:28:46 +02002701exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002702 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002703 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002704 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002705 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706}
2707/* END_CASE */
2708
2709/* BEGIN_CASE */
2710void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002711 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002712 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002713 int first_part_size_arg,
2714 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002715 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002716{
Ronald Cron5425a212020-08-04 14:58:35 +02002717 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002718 psa_key_type_t key_type = key_type_arg;
2719 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002720 size_t first_part_size = first_part_size_arg;
2721 size_t output1_length = output1_length_arg;
2722 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002723 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002724 size_t output_buffer_size = 0;
2725 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002726 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002727 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002728 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002729
Gilles Peskine8817f612018-12-18 00:18:46 +01002730 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002731
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002732 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2733 psa_set_key_algorithm( &attributes, alg );
2734 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002735
Ronald Cron5425a212020-08-04 14:58:35 +02002736 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2737 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002738
Ronald Cron5425a212020-08-04 14:58:35 +02002739 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002741 if( iv->len > 0 )
2742 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002743 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002744 }
2745
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002746 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2747 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002748 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002749
Gilles Peskinee0866522019-02-19 19:44:00 +01002750 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002751 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2752 output, output_buffer_size,
2753 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002754 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002755 TEST_ASSERT( function_output_length <=
2756 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2757 TEST_ASSERT( function_output_length <=
2758 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002759 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002760
Gilles Peskine8817f612018-12-18 00:18:46 +01002761 PSA_ASSERT( psa_cipher_update( &operation,
2762 input->x + first_part_size,
2763 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002764 ( output_buffer_size == 0 ? NULL :
2765 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002766 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002767 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002768 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002769 TEST_ASSERT( function_output_length <=
2770 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2771 alg,
2772 input->len - first_part_size ) );
2773 TEST_ASSERT( function_output_length <=
2774 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002775 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002776
Gilles Peskine8817f612018-12-18 00:18:46 +01002777 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002778 ( output_buffer_size == 0 ? NULL :
2779 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002780 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002781 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002782 TEST_ASSERT( function_output_length <=
2783 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2784 TEST_ASSERT( function_output_length <=
2785 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002786 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002787 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002788
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002789 ASSERT_COMPARE( expected_output->x, expected_output->len,
2790 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002791
2792exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002793 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002794 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002795 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002796 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002797}
2798/* END_CASE */
2799
2800/* BEGIN_CASE */
2801void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002802 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002803 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002804 int first_part_size_arg,
2805 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002806 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002807{
Ronald Cron5425a212020-08-04 14:58:35 +02002808 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002809 psa_key_type_t key_type = key_type_arg;
2810 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002811 size_t first_part_size = first_part_size_arg;
2812 size_t output1_length = output1_length_arg;
2813 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002815 size_t output_buffer_size = 0;
2816 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002817 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002818 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002819 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002820
Gilles Peskine8817f612018-12-18 00:18:46 +01002821 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002822
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002823 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2824 psa_set_key_algorithm( &attributes, alg );
2825 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002826
Ronald Cron5425a212020-08-04 14:58:35 +02002827 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2828 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002829
Ronald Cron5425a212020-08-04 14:58:35 +02002830 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002831
Steven Cooreman177deba2020-09-07 17:14:14 +02002832 if( iv->len > 0 )
2833 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002834 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002835 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002836
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002837 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2838 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002839 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002840
Gilles Peskinee0866522019-02-19 19:44:00 +01002841 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 PSA_ASSERT( psa_cipher_update( &operation,
2843 input->x, first_part_size,
2844 output, output_buffer_size,
2845 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002846 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002847 TEST_ASSERT( function_output_length <=
2848 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2849 TEST_ASSERT( function_output_length <=
2850 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002851 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002852
Gilles Peskine8817f612018-12-18 00:18:46 +01002853 PSA_ASSERT( psa_cipher_update( &operation,
2854 input->x + first_part_size,
2855 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002856 ( output_buffer_size == 0 ? NULL :
2857 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002858 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002859 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002860 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002861 TEST_ASSERT( function_output_length <=
2862 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2863 alg,
2864 input->len - first_part_size ) );
2865 TEST_ASSERT( function_output_length <=
2866 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002867 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002868
Gilles Peskine8817f612018-12-18 00:18:46 +01002869 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002870 ( output_buffer_size == 0 ? NULL :
2871 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002872 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002874 TEST_ASSERT( function_output_length <=
2875 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2876 TEST_ASSERT( function_output_length <=
2877 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002878 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002879 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002880
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002881 ASSERT_COMPARE( expected_output->x, expected_output->len,
2882 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002883
2884exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002885 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002886 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002887 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002888 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889}
2890/* END_CASE */
2891
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002893void cipher_decrypt_simple_multipart( int alg_arg,
2894 int key_type_arg,
2895 data_t *key_data,
2896 data_t *iv,
2897 data_t *input,
2898 data_t *expected_output,
2899 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002900{
Ronald Cron5425a212020-08-04 14:58:35 +02002901 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002902 psa_status_t status;
2903 psa_key_type_t key_type = key_type_arg;
2904 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002905 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002906 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002907 size_t output_buffer_size = 0;
2908 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002909 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002910 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002912
Gilles Peskine8817f612018-12-18 00:18:46 +01002913 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002914
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002915 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2916 psa_set_key_algorithm( &attributes, alg );
2917 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002918
Ronald Cron5425a212020-08-04 14:58:35 +02002919 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2920 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921
Ronald Cron5425a212020-08-04 14:58:35 +02002922 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002923
Steven Cooreman177deba2020-09-07 17:14:14 +02002924 if( iv->len > 0 )
2925 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002926 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002927 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002928
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002929 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2930 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002931 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002932
Gilles Peskine8817f612018-12-18 00:18:46 +01002933 PSA_ASSERT( psa_cipher_update( &operation,
2934 input->x, input->len,
2935 output, output_buffer_size,
2936 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002937 TEST_ASSERT( function_output_length <=
2938 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2939 TEST_ASSERT( function_output_length <=
2940 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002941 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002942
Gilles Peskine50e586b2018-06-08 14:28:46 +02002943 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002944 ( output_buffer_size == 0 ? NULL :
2945 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002946 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002947 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002948 TEST_ASSERT( function_output_length <=
2949 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2950 TEST_ASSERT( function_output_length <=
2951 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002952 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002953 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002954
2955 if( expected_status == PSA_SUCCESS )
2956 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002957 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002958 ASSERT_COMPARE( expected_output->x, expected_output->len,
2959 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002960 }
2961
Gilles Peskine50e586b2018-06-08 14:28:46 +02002962exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002963 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002964 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002965 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002966 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967}
2968/* END_CASE */
2969
Gilles Peskine50e586b2018-06-08 14:28:46 +02002970/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002971void cipher_decrypt_error( int alg_arg,
2972 int key_type_arg,
2973 data_t *key_data,
2974 data_t *iv,
2975 data_t *input_arg,
2976 int expected_status_arg )
2977{
2978 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2979 psa_status_t status;
2980 psa_key_type_t key_type = key_type_arg;
2981 psa_algorithm_t alg = alg_arg;
2982 psa_status_t expected_status = expected_status_arg;
2983 unsigned char *input = NULL;
2984 size_t input_buffer_size = 0;
2985 unsigned char *output = NULL;
2986 size_t output_buffer_size = 0;
2987 size_t output_length = 0;
2988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2989
2990 if ( PSA_ERROR_BAD_STATE != expected_status )
2991 {
2992 PSA_ASSERT( psa_crypto_init( ) );
2993
2994 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2995 psa_set_key_algorithm( &attributes, alg );
2996 psa_set_key_type( &attributes, key_type );
2997
2998 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2999 &key ) );
3000 }
3001
3002 /* Allocate input buffer and copy the iv and the plaintext */
3003 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3004 if ( input_buffer_size > 0 )
3005 {
3006 ASSERT_ALLOC( input, input_buffer_size );
3007 memcpy( input, iv->x, iv->len );
3008 memcpy( input + iv->len, input_arg->x, input_arg->len );
3009 }
3010
3011 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3012 ASSERT_ALLOC( output, output_buffer_size );
3013
3014 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3015 output_buffer_size, &output_length );
3016 TEST_EQUAL( status, expected_status );
3017
3018exit:
3019 mbedtls_free( input );
3020 mbedtls_free( output );
3021 psa_destroy_key( key );
3022 PSA_DONE( );
3023}
3024/* END_CASE */
3025
3026/* BEGIN_CASE */
3027void cipher_decrypt( int alg_arg,
3028 int key_type_arg,
3029 data_t *key_data,
3030 data_t *iv,
3031 data_t *input_arg,
3032 data_t *expected_output )
3033{
3034 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3035 psa_key_type_t key_type = key_type_arg;
3036 psa_algorithm_t alg = alg_arg;
3037 unsigned char *input = NULL;
3038 size_t input_buffer_size = 0;
3039 unsigned char *output = NULL;
3040 size_t output_buffer_size = 0;
3041 size_t output_length = 0;
3042 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3043
3044 PSA_ASSERT( psa_crypto_init( ) );
3045
3046 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3047 psa_set_key_algorithm( &attributes, alg );
3048 psa_set_key_type( &attributes, key_type );
3049
3050 /* Allocate input buffer and copy the iv and the plaintext */
3051 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3052 if ( input_buffer_size > 0 )
3053 {
3054 ASSERT_ALLOC( input, input_buffer_size );
3055 memcpy( input, iv->x, iv->len );
3056 memcpy( input + iv->len, input_arg->x, input_arg->len );
3057 }
3058
3059 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3060 ASSERT_ALLOC( output, output_buffer_size );
3061
3062 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3063 &key ) );
3064
3065 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3066 output_buffer_size, &output_length ) );
3067 TEST_ASSERT( output_length <=
3068 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3069 TEST_ASSERT( output_length <=
3070 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3071
3072 ASSERT_COMPARE( expected_output->x, expected_output->len,
3073 output, output_length );
3074exit:
3075 mbedtls_free( input );
3076 mbedtls_free( output );
3077 psa_destroy_key( key );
3078 PSA_DONE( );
3079}
3080/* END_CASE */
3081
3082/* BEGIN_CASE */
3083void cipher_verify_output( int alg_arg,
3084 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003085 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003086 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003087{
Ronald Cron5425a212020-08-04 14:58:35 +02003088 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003089 psa_key_type_t key_type = key_type_arg;
3090 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003091 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003092 size_t output1_size = 0;
3093 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003094 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003095 size_t output2_size = 0;
3096 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003098
Gilles Peskine8817f612018-12-18 00:18:46 +01003099 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003100
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003101 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3102 psa_set_key_algorithm( &attributes, alg );
3103 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003104
Ronald Cron5425a212020-08-04 14:58:35 +02003105 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3106 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003107 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003108 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003109
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003110 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3111 output1, output1_size,
3112 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003113 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003114 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003115 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003116 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003117
3118 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003119 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003120
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003121 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3122 output2, output2_size,
3123 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003124 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003125 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003126 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003127 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003128
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003129 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003130
3131exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003132 mbedtls_free( output1 );
3133 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003134 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003135 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003136}
3137/* END_CASE */
3138
3139/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003140void cipher_verify_output_multipart( int alg_arg,
3141 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003142 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003143 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003144 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003145{
Ronald Cron5425a212020-08-04 14:58:35 +02003146 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003147 psa_key_type_t key_type = key_type_arg;
3148 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003149 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003150 unsigned char iv[16] = {0};
3151 size_t iv_size = 16;
3152 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003153 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003154 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003155 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003156 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003157 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003158 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003159 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003160 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3161 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003163
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003165
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003166 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3167 psa_set_key_algorithm( &attributes, alg );
3168 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003169
Ronald Cron5425a212020-08-04 14:58:35 +02003170 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3171 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003172
Ronald Cron5425a212020-08-04 14:58:35 +02003173 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3174 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003175
Steven Cooreman177deba2020-09-07 17:14:14 +02003176 if( alg != PSA_ALG_ECB_NO_PADDING )
3177 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003178 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3179 iv, iv_size,
3180 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003181 }
3182
gabor-mezei-armceface22021-01-21 12:26:17 +01003183 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3184 TEST_ASSERT( output1_buffer_size <=
3185 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003186 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003187
Gilles Peskinee0866522019-02-19 19:44:00 +01003188 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003189
Gilles Peskine8817f612018-12-18 00:18:46 +01003190 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3191 output1, output1_buffer_size,
3192 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003193 TEST_ASSERT( function_output_length <=
3194 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3195 TEST_ASSERT( function_output_length <=
3196 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003197 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003198
Gilles Peskine8817f612018-12-18 00:18:46 +01003199 PSA_ASSERT( psa_cipher_update( &operation1,
3200 input->x + first_part_size,
3201 input->len - first_part_size,
3202 output1, output1_buffer_size,
3203 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003204 TEST_ASSERT( function_output_length <=
3205 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3206 alg,
3207 input->len - first_part_size ) );
3208 TEST_ASSERT( function_output_length <=
3209 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003210 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003211
Gilles Peskine8817f612018-12-18 00:18:46 +01003212 PSA_ASSERT( psa_cipher_finish( &operation1,
3213 output1 + output1_length,
3214 output1_buffer_size - output1_length,
3215 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003216 TEST_ASSERT( function_output_length <=
3217 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3218 TEST_ASSERT( function_output_length <=
3219 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003220 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003221
Gilles Peskine8817f612018-12-18 00:18:46 +01003222 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003223
Gilles Peskine048b7f02018-06-08 14:20:49 +02003224 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003225 TEST_ASSERT( output2_buffer_size <=
3226 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3227 TEST_ASSERT( output2_buffer_size <=
3228 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003229 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003230
Steven Cooreman177deba2020-09-07 17:14:14 +02003231 if( iv_length > 0 )
3232 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003233 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3234 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003235 }
Moran Pekerded84402018-06-06 16:36:50 +03003236
Gilles Peskine8817f612018-12-18 00:18:46 +01003237 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3238 output2, output2_buffer_size,
3239 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003240 TEST_ASSERT( function_output_length <=
3241 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3242 TEST_ASSERT( function_output_length <=
3243 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003244 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003245
Gilles Peskine8817f612018-12-18 00:18:46 +01003246 PSA_ASSERT( psa_cipher_update( &operation2,
3247 output1 + first_part_size,
3248 output1_length - first_part_size,
3249 output2, output2_buffer_size,
3250 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003251 TEST_ASSERT( function_output_length <=
3252 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3253 alg,
3254 output1_length - first_part_size ) );
3255 TEST_ASSERT( function_output_length <=
3256 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003257 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003258
Gilles Peskine8817f612018-12-18 00:18:46 +01003259 PSA_ASSERT( psa_cipher_finish( &operation2,
3260 output2 + output2_length,
3261 output2_buffer_size - output2_length,
3262 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003263 TEST_ASSERT( function_output_length <=
3264 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3265 TEST_ASSERT( function_output_length <=
3266 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003267 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003268
Gilles Peskine8817f612018-12-18 00:18:46 +01003269 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003270
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003271 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003272
3273exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003274 psa_cipher_abort( &operation1 );
3275 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003276 mbedtls_free( output1 );
3277 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003278 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003279 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003280}
3281/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003282
Gilles Peskine20035e32018-02-03 22:44:14 +01003283/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003284void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003285 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003286 data_t *nonce,
3287 data_t *additional_data,
3288 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003289 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003290{
Ronald Cron5425a212020-08-04 14:58:35 +02003291 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003292 psa_key_type_t key_type = key_type_arg;
3293 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003294 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003295 unsigned char *output_data = NULL;
3296 size_t output_size = 0;
3297 size_t output_length = 0;
3298 unsigned char *output_data2 = NULL;
3299 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003300 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003301 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003302 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003303
Gilles Peskine8817f612018-12-18 00:18:46 +01003304 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003305
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003306 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3307 psa_set_key_algorithm( &attributes, alg );
3308 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003309
Gilles Peskine049c7532019-05-15 20:22:09 +02003310 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003311 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003312 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3313 key_bits = psa_get_key_bits( &attributes );
3314
3315 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3316 alg );
3317 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3318 * should be exact. */
3319 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3320 expected_result != PSA_ERROR_NOT_SUPPORTED )
3321 {
3322 TEST_EQUAL( output_size,
3323 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3324 TEST_ASSERT( output_size <=
3325 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3326 }
3327 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003328
Steven Cooremanf49478b2021-02-15 15:19:25 +01003329 status = psa_aead_encrypt( key, alg,
3330 nonce->x, nonce->len,
3331 additional_data->x,
3332 additional_data->len,
3333 input_data->x, input_data->len,
3334 output_data, output_size,
3335 &output_length );
3336
3337 /* If the operation is not supported, just skip and not fail in case the
3338 * encryption involves a common limitation of cryptography hardwares and
3339 * an alternative implementation. */
3340 if( status == PSA_ERROR_NOT_SUPPORTED )
3341 {
3342 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3343 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3344 }
3345
3346 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003347
3348 if( PSA_SUCCESS == expected_result )
3349 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003350 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351
Gilles Peskine003a4a92019-05-14 16:09:40 +02003352 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3353 * should be exact. */
3354 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003355 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003356
gabor-mezei-armceface22021-01-21 12:26:17 +01003357 TEST_ASSERT( input_data->len <=
3358 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3359
Ronald Cron5425a212020-08-04 14:58:35 +02003360 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003361 nonce->x, nonce->len,
3362 additional_data->x,
3363 additional_data->len,
3364 output_data, output_length,
3365 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003366 &output_length2 ),
3367 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003368
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003369 ASSERT_COMPARE( input_data->x, input_data->len,
3370 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003371 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003372
Gilles Peskinea1cac842018-06-11 19:33:02 +02003373exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003374 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003375 mbedtls_free( output_data );
3376 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003377 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003378}
3379/* END_CASE */
3380
3381/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003382void aead_encrypt( int key_type_arg, data_t *key_data,
3383 int alg_arg,
3384 data_t *nonce,
3385 data_t *additional_data,
3386 data_t *input_data,
3387 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003388{
Ronald Cron5425a212020-08-04 14:58:35 +02003389 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390 psa_key_type_t key_type = key_type_arg;
3391 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003392 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003393 unsigned char *output_data = NULL;
3394 size_t output_size = 0;
3395 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003397 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398
Gilles Peskine8817f612018-12-18 00:18:46 +01003399 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003401 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3402 psa_set_key_algorithm( &attributes, alg );
3403 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404
Gilles Peskine049c7532019-05-15 20:22:09 +02003405 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003406 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003407 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3408 key_bits = psa_get_key_bits( &attributes );
3409
3410 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3411 alg );
3412 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3413 * should be exact. */
3414 TEST_EQUAL( output_size,
3415 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3416 TEST_ASSERT( output_size <=
3417 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3418 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003419
Steven Cooremand588ea12021-01-11 19:36:04 +01003420 status = psa_aead_encrypt( key, alg,
3421 nonce->x, nonce->len,
3422 additional_data->x, additional_data->len,
3423 input_data->x, input_data->len,
3424 output_data, output_size,
3425 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003426
Ronald Cron28a45ed2021-02-09 20:35:42 +01003427 /* If the operation is not supported, just skip and not fail in case the
3428 * encryption involves a common limitation of cryptography hardwares and
3429 * an alternative implementation. */
3430 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003431 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003432 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3433 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003434 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003435
3436 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003437 ASSERT_COMPARE( expected_result->x, expected_result->len,
3438 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003439
Gilles Peskinea1cac842018-06-11 19:33:02 +02003440exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003441 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003443 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444}
3445/* END_CASE */
3446
3447/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003448void aead_decrypt( int key_type_arg, data_t *key_data,
3449 int alg_arg,
3450 data_t *nonce,
3451 data_t *additional_data,
3452 data_t *input_data,
3453 data_t *expected_data,
3454 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003455{
Ronald Cron5425a212020-08-04 14:58:35 +02003456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003457 psa_key_type_t key_type = key_type_arg;
3458 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003459 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460 unsigned char *output_data = NULL;
3461 size_t output_size = 0;
3462 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003463 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003464 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003465 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003466
Gilles Peskine8817f612018-12-18 00:18:46 +01003467 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003468
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003469 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3470 psa_set_key_algorithm( &attributes, alg );
3471 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003472
Gilles Peskine049c7532019-05-15 20:22:09 +02003473 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003474 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003475 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3476 key_bits = psa_get_key_bits( &attributes );
3477
3478 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3479 alg );
3480 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3481 expected_result != PSA_ERROR_NOT_SUPPORTED )
3482 {
3483 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3484 * should be exact. */
3485 TEST_EQUAL( output_size,
3486 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3487 TEST_ASSERT( output_size <=
3488 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3489 }
3490 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003491
Steven Cooremand588ea12021-01-11 19:36:04 +01003492 status = psa_aead_decrypt( key, alg,
3493 nonce->x, nonce->len,
3494 additional_data->x,
3495 additional_data->len,
3496 input_data->x, input_data->len,
3497 output_data, output_size,
3498 &output_length );
3499
Ronald Cron28a45ed2021-02-09 20:35:42 +01003500 /* If the operation is not supported, just skip and not fail in case the
3501 * decryption involves a common limitation of cryptography hardwares and
3502 * an alternative implementation. */
3503 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003504 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003505 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3506 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003507 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003508
3509 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003510
Gilles Peskine2d277862018-06-18 15:41:12 +02003511 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003512 ASSERT_COMPARE( expected_data->x, expected_data->len,
3513 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003514
Gilles Peskinea1cac842018-06-11 19:33:02 +02003515exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003516 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003517 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003518 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003519}
3520/* END_CASE */
3521
3522/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003523void signature_size( int type_arg,
3524 int bits,
3525 int alg_arg,
3526 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003527{
3528 psa_key_type_t type = type_arg;
3529 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003530 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003531
Gilles Peskinefe11b722018-12-18 00:24:04 +01003532 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003533#if defined(MBEDTLS_TEST_DEPRECATED)
3534 TEST_EQUAL( actual_size,
3535 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3536#endif /* MBEDTLS_TEST_DEPRECATED */
3537
Gilles Peskinee59236f2018-01-27 23:32:46 +01003538exit:
3539 ;
3540}
3541/* END_CASE */
3542
3543/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003544void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3545 int alg_arg, data_t *input_data,
3546 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003547{
Ronald Cron5425a212020-08-04 14:58:35 +02003548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003549 psa_key_type_t key_type = key_type_arg;
3550 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003551 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003552 unsigned char *signature = NULL;
3553 size_t signature_size;
3554 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003556
Gilles Peskine8817f612018-12-18 00:18:46 +01003557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003558
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003559 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003560 psa_set_key_algorithm( &attributes, alg );
3561 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003562
Gilles Peskine049c7532019-05-15 20:22:09 +02003563 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003564 &key ) );
3565 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003566 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003567
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003568 /* Allocate a buffer which has the size advertized by the
3569 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003570 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003571 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003572 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003573 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003574 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003575
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003576 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003577 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003578 input_data->x, input_data->len,
3579 signature, signature_size,
3580 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003581 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003582 ASSERT_COMPARE( output_data->x, output_data->len,
3583 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003584
Gilles Peskine0627f982019-11-26 19:12:16 +01003585#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003586 memset( signature, 0, signature_size );
3587 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003588 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003589 input_data->x, input_data->len,
3590 signature, signature_size,
3591 &signature_length ) );
3592 ASSERT_COMPARE( output_data->x, output_data->len,
3593 signature, signature_length );
3594#endif /* MBEDTLS_TEST_DEPRECATED */
3595
Gilles Peskine20035e32018-02-03 22:44:14 +01003596exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003597 /*
3598 * Key attributes may have been returned by psa_get_key_attributes()
3599 * thus reset them as required.
3600 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003601 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003602
Ronald Cron5425a212020-08-04 14:58:35 +02003603 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003604 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003605 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003606}
3607/* END_CASE */
3608
3609/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003610void sign_hash_fail( int key_type_arg, data_t *key_data,
3611 int alg_arg, data_t *input_data,
3612 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003613{
Ronald Cron5425a212020-08-04 14:58:35 +02003614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003615 psa_key_type_t key_type = key_type_arg;
3616 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003617 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003618 psa_status_t actual_status;
3619 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003620 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003621 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003622 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003623
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003624 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003625
Gilles Peskine8817f612018-12-18 00:18:46 +01003626 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003627
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003628 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003629 psa_set_key_algorithm( &attributes, alg );
3630 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003631
Gilles Peskine049c7532019-05-15 20:22:09 +02003632 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003633 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003634
Ronald Cron5425a212020-08-04 14:58:35 +02003635 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003636 input_data->x, input_data->len,
3637 signature, signature_size,
3638 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003639 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003640 /* The value of *signature_length is unspecified on error, but
3641 * whatever it is, it should be less than signature_size, so that
3642 * if the caller tries to read *signature_length bytes without
3643 * checking the error code then they don't overflow a buffer. */
3644 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003645
Gilles Peskine895242b2019-11-29 12:15:40 +01003646#if defined(MBEDTLS_TEST_DEPRECATED)
3647 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003648 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003649 input_data->x, input_data->len,
3650 signature, signature_size,
3651 &signature_length ),
3652 expected_status );
3653 TEST_ASSERT( signature_length <= signature_size );
3654#endif /* MBEDTLS_TEST_DEPRECATED */
3655
Gilles Peskine20035e32018-02-03 22:44:14 +01003656exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003657 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003658 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003659 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003660 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003661}
3662/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003663
3664/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003665void sign_verify_hash( int key_type_arg, data_t *key_data,
3666 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003667{
Ronald Cron5425a212020-08-04 14:58:35 +02003668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003669 psa_key_type_t key_type = key_type_arg;
3670 psa_algorithm_t alg = alg_arg;
3671 size_t key_bits;
3672 unsigned char *signature = NULL;
3673 size_t signature_size;
3674 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003676
Gilles Peskine8817f612018-12-18 00:18:46 +01003677 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003678
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003679 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003680 psa_set_key_algorithm( &attributes, alg );
3681 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003682
Gilles Peskine049c7532019-05-15 20:22:09 +02003683 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003684 &key ) );
3685 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003686 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003687
3688 /* Allocate a buffer which has the size advertized by the
3689 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003690 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003691 key_bits, alg );
3692 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003693 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003694 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003695
3696 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003697 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003698 input_data->x, input_data->len,
3699 signature, signature_size,
3700 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003701 /* Check that the signature length looks sensible. */
3702 TEST_ASSERT( signature_length <= signature_size );
3703 TEST_ASSERT( signature_length > 0 );
3704
3705 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003706 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003707 input_data->x, input_data->len,
3708 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003709
3710 if( input_data->len != 0 )
3711 {
3712 /* Flip a bit in the input and verify that the signature is now
3713 * detected as invalid. Flip a bit at the beginning, not at the end,
3714 * because ECDSA may ignore the last few bits of the input. */
3715 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003716 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003717 input_data->x, input_data->len,
3718 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003719 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003720 }
3721
3722exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003723 /*
3724 * Key attributes may have been returned by psa_get_key_attributes()
3725 * thus reset them as required.
3726 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003727 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003728
Ronald Cron5425a212020-08-04 14:58:35 +02003729 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003730 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003731 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003732}
3733/* END_CASE */
3734
3735/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003736void verify_hash( int key_type_arg, data_t *key_data,
3737 int alg_arg, data_t *hash_data,
3738 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003739{
Ronald Cron5425a212020-08-04 14:58:35 +02003740 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003741 psa_key_type_t key_type = key_type_arg;
3742 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003743 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003744
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003745 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003746
Gilles Peskine8817f612018-12-18 00:18:46 +01003747 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003748
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003749 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003750 psa_set_key_algorithm( &attributes, alg );
3751 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003752
Gilles Peskine049c7532019-05-15 20:22:09 +02003753 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003754 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003755
Ronald Cron5425a212020-08-04 14:58:35 +02003756 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003757 hash_data->x, hash_data->len,
3758 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003759
3760#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003761 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003762 hash_data->x, hash_data->len,
3763 signature_data->x,
3764 signature_data->len ) );
3765
3766#endif /* MBEDTLS_TEST_DEPRECATED */
3767
itayzafrir5c753392018-05-08 11:18:38 +03003768exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003769 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003770 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003771 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003772}
3773/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003774
3775/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003776void verify_hash_fail( int key_type_arg, data_t *key_data,
3777 int alg_arg, data_t *hash_data,
3778 data_t *signature_data,
3779 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003780{
Ronald Cron5425a212020-08-04 14:58:35 +02003781 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003782 psa_key_type_t key_type = key_type_arg;
3783 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003784 psa_status_t actual_status;
3785 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003786 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003787
Gilles Peskine8817f612018-12-18 00:18:46 +01003788 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003789
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003790 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003791 psa_set_key_algorithm( &attributes, alg );
3792 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003793
Gilles Peskine049c7532019-05-15 20:22:09 +02003794 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003795 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003796
Ronald Cron5425a212020-08-04 14:58:35 +02003797 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003798 hash_data->x, hash_data->len,
3799 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003800 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003801
Gilles Peskine895242b2019-11-29 12:15:40 +01003802#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003803 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003804 hash_data->x, hash_data->len,
3805 signature_data->x, signature_data->len ),
3806 expected_status );
3807#endif /* MBEDTLS_TEST_DEPRECATED */
3808
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003809exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003810 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003811 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003812 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003813}
3814/* END_CASE */
3815
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003816/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003817void sign_message_deterministic( int key_type_arg,
3818 data_t *key_data,
3819 int alg_arg,
3820 data_t *input_data,
3821 data_t *output_data )
3822{
3823 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3824 psa_key_type_t key_type = key_type_arg;
3825 psa_algorithm_t alg = alg_arg;
3826 size_t key_bits;
3827 unsigned char *signature = NULL;
3828 size_t signature_size;
3829 size_t signature_length = 0xdeadbeef;
3830 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3831
3832 PSA_ASSERT( psa_crypto_init( ) );
3833
3834 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3835 psa_set_key_algorithm( &attributes, alg );
3836 psa_set_key_type( &attributes, key_type );
3837
3838 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3839 &key ) );
3840 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3841 key_bits = psa_get_key_bits( &attributes );
3842
3843 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3844 TEST_ASSERT( signature_size != 0 );
3845 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3846 ASSERT_ALLOC( signature, signature_size );
3847
3848 PSA_ASSERT( psa_sign_message( key, alg,
3849 input_data->x, input_data->len,
3850 signature, signature_size,
3851 &signature_length ) );
3852
3853 ASSERT_COMPARE( output_data->x, output_data->len,
3854 signature, signature_length );
3855
3856exit:
3857 psa_reset_key_attributes( &attributes );
3858
3859 psa_destroy_key( key );
3860 mbedtls_free( signature );
3861 PSA_DONE( );
3862
3863}
3864/* END_CASE */
3865
3866/* BEGIN_CASE */
3867void sign_message_fail( int key_type_arg,
3868 data_t *key_data,
3869 int alg_arg,
3870 data_t *input_data,
3871 int signature_size_arg,
3872 int expected_status_arg )
3873{
3874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3875 psa_key_type_t key_type = key_type_arg;
3876 psa_algorithm_t alg = alg_arg;
3877 size_t signature_size = signature_size_arg;
3878 psa_status_t actual_status;
3879 psa_status_t expected_status = expected_status_arg;
3880 unsigned char *signature = NULL;
3881 size_t signature_length = 0xdeadbeef;
3882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3883
3884 ASSERT_ALLOC( signature, signature_size );
3885
3886 PSA_ASSERT( psa_crypto_init( ) );
3887
3888 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3889 psa_set_key_algorithm( &attributes, alg );
3890 psa_set_key_type( &attributes, key_type );
3891
3892 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3893 &key ) );
3894
3895 actual_status = psa_sign_message( key, alg,
3896 input_data->x, input_data->len,
3897 signature, signature_size,
3898 &signature_length );
3899 TEST_EQUAL( actual_status, expected_status );
3900 /* The value of *signature_length is unspecified on error, but
3901 * whatever it is, it should be less than signature_size, so that
3902 * if the caller tries to read *signature_length bytes without
3903 * checking the error code then they don't overflow a buffer. */
3904 TEST_ASSERT( signature_length <= signature_size );
3905
3906exit:
3907 psa_reset_key_attributes( &attributes );
3908 psa_destroy_key( key );
3909 mbedtls_free( signature );
3910 PSA_DONE( );
3911}
3912/* END_CASE */
3913
3914/* BEGIN_CASE */
3915void sign_verify_message( int key_type_arg,
3916 data_t *key_data,
3917 int alg_arg,
3918 data_t *input_data )
3919{
3920 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3921 psa_key_type_t key_type = key_type_arg;
3922 psa_algorithm_t alg = alg_arg;
3923 size_t key_bits;
3924 unsigned char *signature = NULL;
3925 size_t signature_size;
3926 size_t signature_length = 0xdeadbeef;
3927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3928
3929 PSA_ASSERT( psa_crypto_init( ) );
3930
3931 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3932 PSA_KEY_USAGE_VERIFY_MESSAGE );
3933 psa_set_key_algorithm( &attributes, alg );
3934 psa_set_key_type( &attributes, key_type );
3935
3936 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3937 &key ) );
3938 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3939 key_bits = psa_get_key_bits( &attributes );
3940
3941 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3942 TEST_ASSERT( signature_size != 0 );
3943 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3944 ASSERT_ALLOC( signature, signature_size );
3945
3946 PSA_ASSERT( psa_sign_message( key, alg,
3947 input_data->x, input_data->len,
3948 signature, signature_size,
3949 &signature_length ) );
3950 TEST_ASSERT( signature_length <= signature_size );
3951 TEST_ASSERT( signature_length > 0 );
3952
3953 PSA_ASSERT( psa_verify_message( key, alg,
3954 input_data->x, input_data->len,
3955 signature, signature_length ) );
3956
3957 if( input_data->len != 0 )
3958 {
3959 /* Flip a bit in the input and verify that the signature is now
3960 * detected as invalid. Flip a bit at the beginning, not at the end,
3961 * because ECDSA may ignore the last few bits of the input. */
3962 input_data->x[0] ^= 1;
3963 TEST_EQUAL( psa_verify_message( key, alg,
3964 input_data->x, input_data->len,
3965 signature, signature_length ),
3966 PSA_ERROR_INVALID_SIGNATURE );
3967 }
3968
3969exit:
3970 psa_reset_key_attributes( &attributes );
3971
3972 psa_destroy_key( key );
3973 mbedtls_free( signature );
3974 PSA_DONE( );
3975}
3976/* END_CASE */
3977
3978/* BEGIN_CASE */
3979void verify_message( int key_type_arg,
3980 data_t *key_data,
3981 int alg_arg,
3982 data_t *input_data,
3983 data_t *signature_data )
3984{
3985 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3986 psa_key_type_t key_type = key_type_arg;
3987 psa_algorithm_t alg = alg_arg;
3988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3989
3990 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3991
3992 PSA_ASSERT( psa_crypto_init( ) );
3993
3994 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3995 psa_set_key_algorithm( &attributes, alg );
3996 psa_set_key_type( &attributes, key_type );
3997
3998 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3999 &key ) );
4000
4001 PSA_ASSERT( psa_verify_message( key, alg,
4002 input_data->x, input_data->len,
4003 signature_data->x, signature_data->len ) );
4004
4005exit:
4006 psa_reset_key_attributes( &attributes );
4007 psa_destroy_key( key );
4008 PSA_DONE( );
4009}
4010/* END_CASE */
4011
4012/* BEGIN_CASE */
4013void verify_message_fail( int key_type_arg,
4014 data_t *key_data,
4015 int alg_arg,
4016 data_t *hash_data,
4017 data_t *signature_data,
4018 int expected_status_arg )
4019{
4020 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4021 psa_key_type_t key_type = key_type_arg;
4022 psa_algorithm_t alg = alg_arg;
4023 psa_status_t actual_status;
4024 psa_status_t expected_status = expected_status_arg;
4025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4026
4027 PSA_ASSERT( psa_crypto_init( ) );
4028
4029 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4030 psa_set_key_algorithm( &attributes, alg );
4031 psa_set_key_type( &attributes, key_type );
4032
4033 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4034 &key ) );
4035
4036 actual_status = psa_verify_message( key, alg,
4037 hash_data->x, hash_data->len,
4038 signature_data->x,
4039 signature_data->len );
4040 TEST_EQUAL( actual_status, expected_status );
4041
4042exit:
4043 psa_reset_key_attributes( &attributes );
4044 psa_destroy_key( key );
4045 PSA_DONE( );
4046}
4047/* END_CASE */
4048
4049/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004050void asymmetric_encrypt( int key_type_arg,
4051 data_t *key_data,
4052 int alg_arg,
4053 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004054 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004055 int expected_output_length_arg,
4056 int expected_status_arg )
4057{
Ronald Cron5425a212020-08-04 14:58:35 +02004058 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004059 psa_key_type_t key_type = key_type_arg;
4060 psa_algorithm_t alg = alg_arg;
4061 size_t expected_output_length = expected_output_length_arg;
4062 size_t key_bits;
4063 unsigned char *output = NULL;
4064 size_t output_size;
4065 size_t output_length = ~0;
4066 psa_status_t actual_status;
4067 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004068 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004069
Gilles Peskine8817f612018-12-18 00:18:46 +01004070 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004071
Gilles Peskine656896e2018-06-29 19:12:28 +02004072 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004073 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4074 psa_set_key_algorithm( &attributes, alg );
4075 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004076 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004077 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004078
4079 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004080 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004081 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004082
Gilles Peskine656896e2018-06-29 19:12:28 +02004083 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004084 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004085 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004086
4087 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004088 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004089 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004090 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004091 output, output_size,
4092 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004093 TEST_EQUAL( actual_status, expected_status );
4094 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004095
Gilles Peskine68428122018-06-30 18:42:41 +02004096 /* If the label is empty, the test framework puts a non-null pointer
4097 * in label->x. Test that a null pointer works as well. */
4098 if( label->len == 0 )
4099 {
4100 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004101 if( output_size != 0 )
4102 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004103 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004104 input_data->x, input_data->len,
4105 NULL, label->len,
4106 output, output_size,
4107 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004108 TEST_EQUAL( actual_status, expected_status );
4109 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004110 }
4111
Gilles Peskine656896e2018-06-29 19:12:28 +02004112exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004113 /*
4114 * Key attributes may have been returned by psa_get_key_attributes()
4115 * thus reset them as required.
4116 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004117 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004118
Ronald Cron5425a212020-08-04 14:58:35 +02004119 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004120 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004121 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004122}
4123/* END_CASE */
4124
4125/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004126void asymmetric_encrypt_decrypt( int key_type_arg,
4127 data_t *key_data,
4128 int alg_arg,
4129 data_t *input_data,
4130 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004131{
Ronald Cron5425a212020-08-04 14:58:35 +02004132 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004133 psa_key_type_t key_type = key_type_arg;
4134 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004135 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004136 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004137 size_t output_size;
4138 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004139 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004140 size_t output2_size;
4141 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004143
Gilles Peskine8817f612018-12-18 00:18:46 +01004144 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004145
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4147 psa_set_key_algorithm( &attributes, alg );
4148 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004149
Gilles Peskine049c7532019-05-15 20:22:09 +02004150 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004151 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004152
4153 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004154 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004155 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004156
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004157 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004158 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004159 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004160
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004161 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004162 TEST_ASSERT( output2_size <=
4163 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4164 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004165 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004166
Gilles Peskineeebd7382018-06-08 18:11:54 +02004167 /* We test encryption by checking that encrypt-then-decrypt gives back
4168 * the original plaintext because of the non-optional random
4169 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004170 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004171 input_data->x, input_data->len,
4172 label->x, label->len,
4173 output, output_size,
4174 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004175 /* We don't know what ciphertext length to expect, but check that
4176 * it looks sensible. */
4177 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004178
Ronald Cron5425a212020-08-04 14:58:35 +02004179 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004180 output, output_length,
4181 label->x, label->len,
4182 output2, output2_size,
4183 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004184 ASSERT_COMPARE( input_data->x, input_data->len,
4185 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004186
4187exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004188 /*
4189 * Key attributes may have been returned by psa_get_key_attributes()
4190 * thus reset them as required.
4191 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004192 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004193
Ronald Cron5425a212020-08-04 14:58:35 +02004194 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004195 mbedtls_free( output );
4196 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004197 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004198}
4199/* END_CASE */
4200
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004201/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004202void asymmetric_decrypt( int key_type_arg,
4203 data_t *key_data,
4204 int alg_arg,
4205 data_t *input_data,
4206 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004207 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004208{
Ronald Cron5425a212020-08-04 14:58:35 +02004209 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004210 psa_key_type_t key_type = key_type_arg;
4211 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004212 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004213 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004214 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004215 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004216 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004217
Gilles Peskine8817f612018-12-18 00:18:46 +01004218 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004219
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4221 psa_set_key_algorithm( &attributes, alg );
4222 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004223
Gilles Peskine049c7532019-05-15 20:22:09 +02004224 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004225 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004226
gabor-mezei-armceface22021-01-21 12:26:17 +01004227 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4228 key_bits = psa_get_key_bits( &attributes );
4229
4230 /* Determine the maximum ciphertext length */
4231 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4232 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4233 ASSERT_ALLOC( output, output_size );
4234
Ronald Cron5425a212020-08-04 14:58:35 +02004235 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004236 input_data->x, input_data->len,
4237 label->x, label->len,
4238 output,
4239 output_size,
4240 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004241 ASSERT_COMPARE( expected_data->x, expected_data->len,
4242 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004243
Gilles Peskine68428122018-06-30 18:42:41 +02004244 /* If the label is empty, the test framework puts a non-null pointer
4245 * in label->x. Test that a null pointer works as well. */
4246 if( label->len == 0 )
4247 {
4248 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004249 if( output_size != 0 )
4250 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004251 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004252 input_data->x, input_data->len,
4253 NULL, label->len,
4254 output,
4255 output_size,
4256 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004257 ASSERT_COMPARE( expected_data->x, expected_data->len,
4258 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004259 }
4260
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004261exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004262 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004263 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004264 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004265 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004266}
4267/* END_CASE */
4268
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004269/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004270void asymmetric_decrypt_fail( int key_type_arg,
4271 data_t *key_data,
4272 int alg_arg,
4273 data_t *input_data,
4274 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004275 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004276 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004277{
Ronald Cron5425a212020-08-04 14:58:35 +02004278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004279 psa_key_type_t key_type = key_type_arg;
4280 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004281 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004282 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004283 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004284 psa_status_t actual_status;
4285 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004287
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004288 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004289
Gilles Peskine8817f612018-12-18 00:18:46 +01004290 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004291
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004292 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4293 psa_set_key_algorithm( &attributes, alg );
4294 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004295
Gilles Peskine049c7532019-05-15 20:22:09 +02004296 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004297 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004298
Ronald Cron5425a212020-08-04 14:58:35 +02004299 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004300 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004301 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004302 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004303 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004304 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004305 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004306
Gilles Peskine68428122018-06-30 18:42:41 +02004307 /* If the label is empty, the test framework puts a non-null pointer
4308 * in label->x. Test that a null pointer works as well. */
4309 if( label->len == 0 )
4310 {
4311 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004312 if( output_size != 0 )
4313 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004314 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004315 input_data->x, input_data->len,
4316 NULL, label->len,
4317 output, output_size,
4318 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004319 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004320 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004321 }
4322
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004323exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004324 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004325 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004326 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004327 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004328}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004329/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004330
4331/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004332void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004333{
4334 /* Test each valid way of initializing the object, except for `= {0}`, as
4335 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4336 * though it's OK by the C standard. We could test for this, but we'd need
4337 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004338 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004339 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4340 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4341 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004342
4343 memset( &zero, 0, sizeof( zero ) );
4344
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004345 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004346 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004347 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004348 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004349 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004350 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004351 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004352
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004353 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004354 PSA_ASSERT( psa_key_derivation_abort(&func) );
4355 PSA_ASSERT( psa_key_derivation_abort(&init) );
4356 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004357}
4358/* END_CASE */
4359
Janos Follath16de4a42019-06-13 16:32:24 +01004360/* BEGIN_CASE */
4361void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004362{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004363 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004364 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004365 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004366
Gilles Peskine8817f612018-12-18 00:18:46 +01004367 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004368
Janos Follath16de4a42019-06-13 16:32:24 +01004369 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004370 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004371
4372exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004374 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004375}
4376/* END_CASE */
4377
Janos Follathaf3c2a02019-06-12 12:34:34 +01004378/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004379void derive_set_capacity( int alg_arg, int capacity_arg,
4380 int expected_status_arg )
4381{
4382 psa_algorithm_t alg = alg_arg;
4383 size_t capacity = capacity_arg;
4384 psa_status_t expected_status = expected_status_arg;
4385 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4386
4387 PSA_ASSERT( psa_crypto_init( ) );
4388
4389 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4390
4391 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4392 expected_status );
4393
4394exit:
4395 psa_key_derivation_abort( &operation );
4396 PSA_DONE( );
4397}
4398/* END_CASE */
4399
4400/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004401void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004402 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004403 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004404 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004405 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004406 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004407 int expected_status_arg3,
4408 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004409{
4410 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004411 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4412 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004413 psa_status_t expected_statuses[] = {expected_status_arg1,
4414 expected_status_arg2,
4415 expected_status_arg3};
4416 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004417 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4418 MBEDTLS_SVC_KEY_ID_INIT,
4419 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004420 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4422 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004423 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004424 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004425 psa_status_t expected_output_status = expected_output_status_arg;
4426 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004427
4428 PSA_ASSERT( psa_crypto_init( ) );
4429
4430 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4431 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004432
4433 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4434
4435 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4436 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004437 mbedtls_test_set_step( i );
4438 if( steps[i] == 0 )
4439 {
4440 /* Skip this step */
4441 }
4442 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004443 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004444 psa_set_key_type( &attributes, key_types[i] );
4445 PSA_ASSERT( psa_import_key( &attributes,
4446 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004447 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004448 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4449 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4450 {
4451 // When taking a private key as secret input, use key agreement
4452 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004453 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4454 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004455 expected_statuses[i] );
4456 }
4457 else
4458 {
4459 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004460 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004461 expected_statuses[i] );
4462 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004463 }
4464 else
4465 {
4466 TEST_EQUAL( psa_key_derivation_input_bytes(
4467 &operation, steps[i],
4468 inputs[i]->x, inputs[i]->len ),
4469 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004470 }
4471 }
4472
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004473 if( output_key_type != PSA_KEY_TYPE_NONE )
4474 {
4475 psa_reset_key_attributes( &attributes );
4476 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4477 psa_set_key_bits( &attributes, 8 );
4478 actual_output_status =
4479 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004480 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004481 }
4482 else
4483 {
4484 uint8_t buffer[1];
4485 actual_output_status =
4486 psa_key_derivation_output_bytes( &operation,
4487 buffer, sizeof( buffer ) );
4488 }
4489 TEST_EQUAL( actual_output_status, expected_output_status );
4490
Janos Follathaf3c2a02019-06-12 12:34:34 +01004491exit:
4492 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004493 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4494 psa_destroy_key( keys[i] );
4495 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004496 PSA_DONE( );
4497}
4498/* END_CASE */
4499
Janos Follathd958bb72019-07-03 15:02:16 +01004500/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004501void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004502{
Janos Follathd958bb72019-07-03 15:02:16 +01004503 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004505 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004506 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004507 unsigned char input1[] = "Input 1";
4508 size_t input1_length = sizeof( input1 );
4509 unsigned char input2[] = "Input 2";
4510 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004511 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004512 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004513 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4514 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4515 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004517
Gilles Peskine8817f612018-12-18 00:18:46 +01004518 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004519
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004520 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4521 psa_set_key_algorithm( &attributes, alg );
4522 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004523
Gilles Peskine73676cb2019-05-15 20:15:10 +02004524 PSA_ASSERT( psa_import_key( &attributes,
4525 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004526 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004527
4528 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004529 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4530 input1, input1_length,
4531 input2, input2_length,
4532 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004533 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004534
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004535 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004536 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004537 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004538
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004539 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004540
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004541 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004542 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004543
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004544exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004546 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004547 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004548}
4549/* END_CASE */
4550
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004551/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004552void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004553{
4554 uint8_t output_buffer[16];
4555 size_t buffer_size = 16;
4556 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004557 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004558
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004559 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4560 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004561 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004562
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004563 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004564 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004565
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004566 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004567
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004568 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4569 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004570 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004571
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004572 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004573 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004574
4575exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004576 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004577}
4578/* END_CASE */
4579
4580/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004581void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004582 int step1_arg, data_t *input1,
4583 int step2_arg, data_t *input2,
4584 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004585 int requested_capacity_arg,
4586 data_t *expected_output1,
4587 data_t *expected_output2 )
4588{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004589 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004590 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4591 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004592 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4593 MBEDTLS_SVC_KEY_ID_INIT,
4594 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004595 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004596 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004597 uint8_t *expected_outputs[2] =
4598 {expected_output1->x, expected_output2->x};
4599 size_t output_sizes[2] =
4600 {expected_output1->len, expected_output2->len};
4601 size_t output_buffer_size = 0;
4602 uint8_t *output_buffer = NULL;
4603 size_t expected_capacity;
4604 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004606 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004607 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004608
4609 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4610 {
4611 if( output_sizes[i] > output_buffer_size )
4612 output_buffer_size = output_sizes[i];
4613 if( output_sizes[i] == 0 )
4614 expected_outputs[i] = NULL;
4615 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004616 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004617 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004618
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004619 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4620 psa_set_key_algorithm( &attributes, alg );
4621 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004622
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004623 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004624 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4625 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4626 requested_capacity ) );
4627 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004628 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004629 switch( steps[i] )
4630 {
4631 case 0:
4632 break;
4633 case PSA_KEY_DERIVATION_INPUT_SECRET:
4634 PSA_ASSERT( psa_import_key( &attributes,
4635 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004636 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004637
4638 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4639 {
4640 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4641 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4642 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4643 }
4644
Gilles Peskine1468da72019-05-29 17:35:49 +02004645 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004646 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004647 break;
4648 default:
4649 PSA_ASSERT( psa_key_derivation_input_bytes(
4650 &operation, steps[i],
4651 inputs[i]->x, inputs[i]->len ) );
4652 break;
4653 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004654 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004655
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004656 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004657 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004658 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004659 expected_capacity = requested_capacity;
4660
4661 /* Expansion phase. */
4662 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4663 {
4664 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004665 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004666 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004667 if( expected_capacity == 0 && output_sizes[i] == 0 )
4668 {
4669 /* Reading 0 bytes when 0 bytes are available can go either way. */
4670 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004671 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004672 continue;
4673 }
4674 else if( expected_capacity == 0 ||
4675 output_sizes[i] > expected_capacity )
4676 {
4677 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004678 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004679 expected_capacity = 0;
4680 continue;
4681 }
4682 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004683 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004684 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004685 ASSERT_COMPARE( output_buffer, output_sizes[i],
4686 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004687 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004688 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004689 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004690 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004691 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004692 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004693 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004694
4695exit:
4696 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004697 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004698 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4699 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004700 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004701}
4702/* END_CASE */
4703
4704/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004705void derive_full( int alg_arg,
4706 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004707 data_t *input1,
4708 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004709 int requested_capacity_arg )
4710{
Ronald Cron5425a212020-08-04 14:58:35 +02004711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004712 psa_algorithm_t alg = alg_arg;
4713 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004715 unsigned char output_buffer[16];
4716 size_t expected_capacity = requested_capacity;
4717 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004719
Gilles Peskine8817f612018-12-18 00:18:46 +01004720 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004721
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004722 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4723 psa_set_key_algorithm( &attributes, alg );
4724 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004725
Gilles Peskine049c7532019-05-15 20:22:09 +02004726 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004727 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004728
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004729 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4730 input1->x, input1->len,
4731 input2->x, input2->len,
4732 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004733 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004734
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004735 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004736 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004737 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004738
4739 /* Expansion phase. */
4740 while( current_capacity > 0 )
4741 {
4742 size_t read_size = sizeof( output_buffer );
4743 if( read_size > current_capacity )
4744 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004746 output_buffer,
4747 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004748 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004750 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004751 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004752 }
4753
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004754 /* Check that the operation refuses to go over capacity. */
4755 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004756 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004757
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004758 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004759
4760exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004761 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004762 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004763 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004764}
4765/* END_CASE */
4766
Janos Follathe60c9052019-07-03 13:51:30 +01004767/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004768void derive_key_exercise( int alg_arg,
4769 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004770 data_t *input1,
4771 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004772 int derived_type_arg,
4773 int derived_bits_arg,
4774 int derived_usage_arg,
4775 int derived_alg_arg )
4776{
Ronald Cron5425a212020-08-04 14:58:35 +02004777 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4778 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004779 psa_algorithm_t alg = alg_arg;
4780 psa_key_type_t derived_type = derived_type_arg;
4781 size_t derived_bits = derived_bits_arg;
4782 psa_key_usage_t derived_usage = derived_usage_arg;
4783 psa_algorithm_t derived_alg = derived_alg_arg;
4784 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004785 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004786 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004787 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004788
Gilles Peskine8817f612018-12-18 00:18:46 +01004789 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004790
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004791 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4792 psa_set_key_algorithm( &attributes, alg );
4793 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004794 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004795 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004796
4797 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004798 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4799 input1->x, input1->len,
4800 input2->x, input2->len,
4801 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004802 goto exit;
4803
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004804 psa_set_key_usage_flags( &attributes, derived_usage );
4805 psa_set_key_algorithm( &attributes, derived_alg );
4806 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004807 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004809 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004810
4811 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004812 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004813 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4814 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004815
4816 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004817 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004818 goto exit;
4819
4820exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004821 /*
4822 * Key attributes may have been returned by psa_get_key_attributes()
4823 * thus reset them as required.
4824 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004825 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004826
4827 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004828 psa_destroy_key( base_key );
4829 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004830 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004831}
4832/* END_CASE */
4833
Janos Follath42fd8882019-07-03 14:17:09 +01004834/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004835void derive_key_export( int alg_arg,
4836 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004837 data_t *input1,
4838 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004839 int bytes1_arg,
4840 int bytes2_arg )
4841{
Ronald Cron5425a212020-08-04 14:58:35 +02004842 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4843 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004844 psa_algorithm_t alg = alg_arg;
4845 size_t bytes1 = bytes1_arg;
4846 size_t bytes2 = bytes2_arg;
4847 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004849 uint8_t *output_buffer = NULL;
4850 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004851 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4852 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004853 size_t length;
4854
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004855 ASSERT_ALLOC( output_buffer, capacity );
4856 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004858
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004859 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4860 psa_set_key_algorithm( &base_attributes, alg );
4861 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004862 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004863 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004864
4865 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004866 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4867 input1->x, input1->len,
4868 input2->x, input2->len,
4869 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004870 goto exit;
4871
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004872 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004873 output_buffer,
4874 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004876
4877 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004878 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4879 input1->x, input1->len,
4880 input2->x, input2->len,
4881 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004882 goto exit;
4883
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004884 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4885 psa_set_key_algorithm( &derived_attributes, 0 );
4886 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004887 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004889 &derived_key ) );
4890 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004891 export_buffer, bytes1,
4892 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004893 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004894 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004895 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004897 &derived_key ) );
4898 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004899 export_buffer + bytes1, bytes2,
4900 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004901 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004902
4903 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004904 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4905 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004906
4907exit:
4908 mbedtls_free( output_buffer );
4909 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004911 psa_destroy_key( base_key );
4912 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004913 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004914}
4915/* END_CASE */
4916
4917/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004918void derive_key( int alg_arg,
4919 data_t *key_data, data_t *input1, data_t *input2,
4920 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004921 int expected_status_arg,
4922 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004923{
Ronald Cron5425a212020-08-04 14:58:35 +02004924 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4925 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004926 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004927 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004928 size_t bits = bits_arg;
4929 psa_status_t expected_status = expected_status_arg;
4930 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4931 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4932 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4933
4934 PSA_ASSERT( psa_crypto_init( ) );
4935
4936 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4937 psa_set_key_algorithm( &base_attributes, alg );
4938 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4939 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004940 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004941
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004942 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4943 input1->x, input1->len,
4944 input2->x, input2->len,
4945 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004946 goto exit;
4947
4948 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4949 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004950 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004951 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004952
4953 psa_status_t status =
4954 psa_key_derivation_output_key( &derived_attributes,
4955 &operation,
4956 &derived_key );
4957 if( is_large_output > 0 )
4958 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4959 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004960
4961exit:
4962 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004963 psa_destroy_key( base_key );
4964 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004965 PSA_DONE( );
4966}
4967/* END_CASE */
4968
4969/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004970void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004971 int our_key_type_arg, int our_key_alg_arg,
4972 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004973 int expected_status_arg )
4974{
Ronald Cron5425a212020-08-04 14:58:35 +02004975 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004976 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004977 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004978 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004981 psa_status_t expected_status = expected_status_arg;
4982 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004983
Gilles Peskine8817f612018-12-18 00:18:46 +01004984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004985
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004986 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004987 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004988 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004989 PSA_ASSERT( psa_import_key( &attributes,
4990 our_key_data->x, our_key_data->len,
4991 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004992
Gilles Peskine77f40d82019-04-11 21:27:06 +02004993 /* The tests currently include inputs that should fail at either step.
4994 * Test cases that fail at the setup step should be changed to call
4995 * key_derivation_setup instead, and this function should be renamed
4996 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004997 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004998 if( status == PSA_SUCCESS )
4999 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005000 TEST_EQUAL( psa_key_derivation_key_agreement(
5001 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5002 our_key,
5003 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005004 expected_status );
5005 }
5006 else
5007 {
5008 TEST_ASSERT( status == expected_status );
5009 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005010
5011exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005012 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005013 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005014 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005015}
5016/* END_CASE */
5017
5018/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005019void raw_key_agreement( int alg_arg,
5020 int our_key_type_arg, data_t *our_key_data,
5021 data_t *peer_key_data,
5022 data_t *expected_output )
5023{
Ronald Cron5425a212020-08-04 14:58:35 +02005024 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005025 psa_algorithm_t alg = alg_arg;
5026 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005028 unsigned char *output = NULL;
5029 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005030 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005031
5032 ASSERT_ALLOC( output, expected_output->len );
5033 PSA_ASSERT( psa_crypto_init( ) );
5034
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005035 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5036 psa_set_key_algorithm( &attributes, alg );
5037 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005038 PSA_ASSERT( psa_import_key( &attributes,
5039 our_key_data->x, our_key_data->len,
5040 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005041
gabor-mezei-armceface22021-01-21 12:26:17 +01005042 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5043 key_bits = psa_get_key_bits( &attributes );
5044
Gilles Peskinebe697d82019-05-16 18:00:41 +02005045 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5046 peer_key_data->x, peer_key_data->len,
5047 output, expected_output->len,
5048 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005049 ASSERT_COMPARE( output, output_length,
5050 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005051 TEST_ASSERT( output_length <=
5052 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5053 TEST_ASSERT( output_length <=
5054 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005055
5056exit:
5057 mbedtls_free( output );
5058 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005059 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005060}
5061/* END_CASE */
5062
5063/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005064void key_agreement_capacity( int alg_arg,
5065 int our_key_type_arg, data_t *our_key_data,
5066 data_t *peer_key_data,
5067 int expected_capacity_arg )
5068{
Ronald Cron5425a212020-08-04 14:58:35 +02005069 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005070 psa_algorithm_t alg = alg_arg;
5071 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005073 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005074 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005075 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005076
Gilles Peskine8817f612018-12-18 00:18:46 +01005077 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005078
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5080 psa_set_key_algorithm( &attributes, alg );
5081 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005082 PSA_ASSERT( psa_import_key( &attributes,
5083 our_key_data->x, our_key_data->len,
5084 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005085
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005087 PSA_ASSERT( psa_key_derivation_key_agreement(
5088 &operation,
5089 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5090 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005091 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5092 {
5093 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005095 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005096 NULL, 0 ) );
5097 }
Gilles Peskine59685592018-09-18 12:11:34 +02005098
Gilles Peskinebf491972018-10-25 22:36:12 +02005099 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005100 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005101 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005102 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005103
Gilles Peskinebf491972018-10-25 22:36:12 +02005104 /* Test the actual capacity by reading the output. */
5105 while( actual_capacity > sizeof( output ) )
5106 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005107 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005108 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005109 actual_capacity -= sizeof( output );
5110 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005111 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005112 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005113 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005114 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005115
Gilles Peskine59685592018-09-18 12:11:34 +02005116exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005117 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005118 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005119 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005120}
5121/* END_CASE */
5122
5123/* BEGIN_CASE */
5124void key_agreement_output( int alg_arg,
5125 int our_key_type_arg, data_t *our_key_data,
5126 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005127 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005128{
Ronald Cron5425a212020-08-04 14:58:35 +02005129 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005130 psa_algorithm_t alg = alg_arg;
5131 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005134 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005135
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005136 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5137 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005138
Gilles Peskine8817f612018-12-18 00:18:46 +01005139 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005140
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5142 psa_set_key_algorithm( &attributes, alg );
5143 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005144 PSA_ASSERT( psa_import_key( &attributes,
5145 our_key_data->x, our_key_data->len,
5146 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005147
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005149 PSA_ASSERT( psa_key_derivation_key_agreement(
5150 &operation,
5151 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5152 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005153 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5154 {
5155 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005156 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005157 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005158 NULL, 0 ) );
5159 }
Gilles Peskine59685592018-09-18 12:11:34 +02005160
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005161 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005162 actual_output,
5163 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005164 ASSERT_COMPARE( actual_output, expected_output1->len,
5165 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005166 if( expected_output2->len != 0 )
5167 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005169 actual_output,
5170 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005171 ASSERT_COMPARE( actual_output, expected_output2->len,
5172 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005173 }
Gilles Peskine59685592018-09-18 12:11:34 +02005174
5175exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005176 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005177 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005178 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005179 mbedtls_free( actual_output );
5180}
5181/* END_CASE */
5182
5183/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005184void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005185{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005186 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005187 unsigned char *output = NULL;
5188 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005189 size_t i;
5190 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005191
Simon Butcher49f8e312020-03-03 15:51:50 +00005192 TEST_ASSERT( bytes_arg >= 0 );
5193
Gilles Peskine91892022021-02-08 19:50:26 +01005194 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005195 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005196
Gilles Peskine8817f612018-12-18 00:18:46 +01005197 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005198
Gilles Peskinea50d7392018-06-21 10:22:13 +02005199 /* Run several times, to ensure that every output byte will be
5200 * nonzero at least once with overwhelming probability
5201 * (2^(-8*number_of_runs)). */
5202 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005203 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005204 if( bytes != 0 )
5205 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005206 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005207
Gilles Peskinea50d7392018-06-21 10:22:13 +02005208 for( i = 0; i < bytes; i++ )
5209 {
5210 if( output[i] != 0 )
5211 ++changed[i];
5212 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005213 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005214
5215 /* Check that every byte was changed to nonzero at least once. This
5216 * validates that psa_generate_random is overwriting every byte of
5217 * the output buffer. */
5218 for( i = 0; i < bytes; i++ )
5219 {
5220 TEST_ASSERT( changed[i] != 0 );
5221 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005222
5223exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005224 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005225 mbedtls_free( output );
5226 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005227}
5228/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005229
5230/* BEGIN_CASE */
5231void generate_key( int type_arg,
5232 int bits_arg,
5233 int usage_arg,
5234 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005235 int expected_status_arg,
5236 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005237{
Ronald Cron5425a212020-08-04 14:58:35 +02005238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005239 psa_key_type_t type = type_arg;
5240 psa_key_usage_t usage = usage_arg;
5241 size_t bits = bits_arg;
5242 psa_algorithm_t alg = alg_arg;
5243 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005244 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005245 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005246
Gilles Peskine8817f612018-12-18 00:18:46 +01005247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005248
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005249 psa_set_key_usage_flags( &attributes, usage );
5250 psa_set_key_algorithm( &attributes, alg );
5251 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005252 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005253
5254 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005255 psa_status_t status = psa_generate_key( &attributes, &key );
5256
5257 if( is_large_key > 0 )
5258 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5259 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005260 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005261 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005262
5263 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005264 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005265 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5266 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005267
Gilles Peskine818ca122018-06-20 18:16:48 +02005268 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005269 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005270 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005271
5272exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005273 /*
5274 * Key attributes may have been returned by psa_get_key_attributes()
5275 * thus reset them as required.
5276 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005277 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005278
Ronald Cron5425a212020-08-04 14:58:35 +02005279 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005280 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005281}
5282/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005283
Ronald Cronee414c72021-03-18 18:50:08 +01005284/* 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 +02005285void generate_key_rsa( int bits_arg,
5286 data_t *e_arg,
5287 int expected_status_arg )
5288{
Ronald Cron5425a212020-08-04 14:58:35 +02005289 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005290 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005291 size_t bits = bits_arg;
5292 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5293 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5294 psa_status_t expected_status = expected_status_arg;
5295 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5296 uint8_t *exported = NULL;
5297 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005298 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005299 size_t exported_length = SIZE_MAX;
5300 uint8_t *e_read_buffer = NULL;
5301 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005302 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005303 size_t e_read_length = SIZE_MAX;
5304
5305 if( e_arg->len == 0 ||
5306 ( e_arg->len == 3 &&
5307 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5308 {
5309 is_default_public_exponent = 1;
5310 e_read_size = 0;
5311 }
5312 ASSERT_ALLOC( e_read_buffer, e_read_size );
5313 ASSERT_ALLOC( exported, exported_size );
5314
5315 PSA_ASSERT( psa_crypto_init( ) );
5316
5317 psa_set_key_usage_flags( &attributes, usage );
5318 psa_set_key_algorithm( &attributes, alg );
5319 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5320 e_arg->x, e_arg->len ) );
5321 psa_set_key_bits( &attributes, bits );
5322
5323 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005324 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005325 if( expected_status != PSA_SUCCESS )
5326 goto exit;
5327
5328 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005329 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005330 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5331 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5332 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5333 e_read_buffer, e_read_size,
5334 &e_read_length ) );
5335 if( is_default_public_exponent )
5336 TEST_EQUAL( e_read_length, 0 );
5337 else
5338 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5339
5340 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005341 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005342 goto exit;
5343
5344 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005345 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005346 exported, exported_size,
5347 &exported_length ) );
5348 {
5349 uint8_t *p = exported;
5350 uint8_t *end = exported + exported_length;
5351 size_t len;
5352 /* RSAPublicKey ::= SEQUENCE {
5353 * modulus INTEGER, -- n
5354 * publicExponent INTEGER } -- e
5355 */
5356 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005357 MBEDTLS_ASN1_SEQUENCE |
5358 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005359 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005360 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5361 MBEDTLS_ASN1_INTEGER ) );
5362 if( len >= 1 && p[0] == 0 )
5363 {
5364 ++p;
5365 --len;
5366 }
5367 if( e_arg->len == 0 )
5368 {
5369 TEST_EQUAL( len, 3 );
5370 TEST_EQUAL( p[0], 1 );
5371 TEST_EQUAL( p[1], 0 );
5372 TEST_EQUAL( p[2], 1 );
5373 }
5374 else
5375 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5376 }
5377
5378exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005379 /*
5380 * Key attributes may have been returned by psa_get_key_attributes() or
5381 * set by psa_set_key_domain_parameters() thus reset them as required.
5382 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005383 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005384
Ronald Cron5425a212020-08-04 14:58:35 +02005385 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005386 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005387 mbedtls_free( e_read_buffer );
5388 mbedtls_free( exported );
5389}
5390/* END_CASE */
5391
Darryl Greend49a4992018-06-18 17:27:26 +01005392/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005393void persistent_key_load_key_from_storage( data_t *data,
5394 int type_arg, int bits_arg,
5395 int usage_flags_arg, int alg_arg,
5396 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005397{
Ronald Cron71016a92020-08-28 19:01:50 +02005398 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005399 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5401 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005402 psa_key_type_t type = type_arg;
5403 size_t bits = bits_arg;
5404 psa_key_usage_t usage_flags = usage_flags_arg;
5405 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005406 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005407 unsigned char *first_export = NULL;
5408 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005409 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005410 size_t first_exported_length;
5411 size_t second_exported_length;
5412
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005413 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5414 {
5415 ASSERT_ALLOC( first_export, export_size );
5416 ASSERT_ALLOC( second_export, export_size );
5417 }
Darryl Greend49a4992018-06-18 17:27:26 +01005418
Gilles Peskine8817f612018-12-18 00:18:46 +01005419 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005420
Gilles Peskinec87af662019-05-15 16:12:22 +02005421 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005422 psa_set_key_usage_flags( &attributes, usage_flags );
5423 psa_set_key_algorithm( &attributes, alg );
5424 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005425 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005426
Darryl Green0c6575a2018-11-07 16:05:30 +00005427 switch( generation_method )
5428 {
5429 case IMPORT_KEY:
5430 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005431 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005432 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005433 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005434
Darryl Green0c6575a2018-11-07 16:05:30 +00005435 case GENERATE_KEY:
5436 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005437 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005438 break;
5439
5440 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005441#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005442 {
5443 /* Create base key */
5444 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5445 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5446 psa_set_key_usage_flags( &base_attributes,
5447 PSA_KEY_USAGE_DERIVE );
5448 psa_set_key_algorithm( &base_attributes, derive_alg );
5449 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005450 PSA_ASSERT( psa_import_key( &base_attributes,
5451 data->x, data->len,
5452 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005453 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005454 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005455 PSA_ASSERT( psa_key_derivation_input_key(
5456 &operation,
5457 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005458 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005459 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005460 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005461 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5462 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005463 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005464 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005465 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005466 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005467 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005468#else
5469 TEST_ASSUME( ! "KDF not supported in this configuration" );
5470#endif
5471 break;
5472
5473 default:
5474 TEST_ASSERT( ! "generation_method not implemented in test" );
5475 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005476 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005477 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005478
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005479 /* Export the key if permitted by the key policy. */
5480 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5481 {
Ronald Cron5425a212020-08-04 14:58:35 +02005482 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005483 first_export, export_size,
5484 &first_exported_length ) );
5485 if( generation_method == IMPORT_KEY )
5486 ASSERT_COMPARE( data->x, data->len,
5487 first_export, first_exported_length );
5488 }
Darryl Greend49a4992018-06-18 17:27:26 +01005489
5490 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005491 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005492 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005493 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005494
Darryl Greend49a4992018-06-18 17:27:26 +01005495 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005496 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005497 TEST_ASSERT( mbedtls_svc_key_id_equal(
5498 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005499 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5500 PSA_KEY_LIFETIME_PERSISTENT );
5501 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5502 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5503 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5504 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005505
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005506 /* Export the key again if permitted by the key policy. */
5507 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005508 {
Ronald Cron5425a212020-08-04 14:58:35 +02005509 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005510 second_export, export_size,
5511 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005512 ASSERT_COMPARE( first_export, first_exported_length,
5513 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005514 }
5515
5516 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005517 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005518 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005519
5520exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005521 /*
5522 * Key attributes may have been returned by psa_get_key_attributes()
5523 * thus reset them as required.
5524 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005525 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005526
Darryl Greend49a4992018-06-18 17:27:26 +01005527 mbedtls_free( first_export );
5528 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005529 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005530 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005531 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005532 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005533}
5534/* END_CASE */