blob: c6563b3646b10ecfc579b3ac8baebaa77103cc27 [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
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +0200208 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +0200220 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +0200223 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Gilles Peskinee59236f2018-01-27 23:32:46 +0100267/* END_HEADER */
268
269/* BEGIN_DEPENDENCIES
270 * depends_on:MBEDTLS_PSA_CRYPTO_C
271 * END_DEPENDENCIES
272 */
273
274/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200275void static_checks( )
276{
277 size_t max_truncated_mac_size =
278 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
279
280 /* Check that the length for a truncated MAC always fits in the algorithm
281 * encoding. The shifted mask is the maximum truncated value. The
282 * untruncated algorithm may be one byte larger. */
283 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100284
285#if defined(MBEDTLS_TEST_DEPRECATED)
286 /* Check deprecated constants. */
287 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
288 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
289 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
290 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
291 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
292 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
293 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
294 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100295
Paul Elliott8ff510a2020-06-02 17:19:28 +0100296 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
297 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
298 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
299 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
300 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
301 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
302 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
303 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
304 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
321 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
323 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
324 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
325 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
326
327 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
328 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
329 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
330 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
331 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
333 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
334 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100335
Paul Elliott75e27032020-06-03 15:17:39 +0100336 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
337 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
338 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
339 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
340 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
341
342 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
343 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100344#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200345}
346/* END_CASE */
347
348/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200349void import_with_policy( int type_arg,
350 int usage_arg, int alg_arg,
351 int expected_status_arg )
352{
353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
354 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200356 psa_key_type_t type = type_arg;
357 psa_key_usage_t usage = usage_arg;
358 psa_algorithm_t alg = alg_arg;
359 psa_status_t expected_status = expected_status_arg;
360 const uint8_t key_material[16] = {0};
361 psa_status_t status;
362
363 PSA_ASSERT( psa_crypto_init( ) );
364
365 psa_set_key_type( &attributes, type );
366 psa_set_key_usage_flags( &attributes, usage );
367 psa_set_key_algorithm( &attributes, alg );
368
369 status = psa_import_key( &attributes,
370 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200371 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200372 TEST_EQUAL( status, expected_status );
373 if( status != PSA_SUCCESS )
374 goto exit;
375
Ronald Cron5425a212020-08-04 14:58:35 +0200376 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200377 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
378 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
379 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200380 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200381
Ronald Cron5425a212020-08-04 14:58:35 +0200382 PSA_ASSERT( psa_destroy_key( key ) );
383 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200384
385exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100386 /*
387 * Key attributes may have been returned by psa_get_key_attributes()
388 * thus reset them as required.
389 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200390 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100391
392 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200393 PSA_DONE( );
394}
395/* END_CASE */
396
397/* BEGIN_CASE */
398void import_with_data( data_t *data, int type_arg,
399 int attr_bits_arg,
400 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200401{
402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
403 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200404 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200405 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200406 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200407 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100408 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409
Gilles Peskine8817f612018-12-18 00:18:46 +0100410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100411
Gilles Peskine4747d192019-04-17 15:05:45 +0200412 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200413 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200414
Ronald Cron5425a212020-08-04 14:58:35 +0200415 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100416 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200417 if( status != PSA_SUCCESS )
418 goto exit;
419
Ronald Cron5425a212020-08-04 14:58:35 +0200420 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200421 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200422 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200423 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200424 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200425
Ronald Cron5425a212020-08-04 14:58:35 +0200426 PSA_ASSERT( psa_destroy_key( key ) );
427 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100428
429exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100430 /*
431 * Key attributes may have been returned by psa_get_key_attributes()
432 * thus reset them as required.
433 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200434 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100435
436 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200437 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100438}
439/* END_CASE */
440
441/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200442void import_large_key( int type_arg, int byte_size_arg,
443 int expected_status_arg )
444{
445 psa_key_type_t type = type_arg;
446 size_t byte_size = byte_size_arg;
447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
448 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200449 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200450 psa_status_t status;
451 uint8_t *buffer = NULL;
452 size_t buffer_size = byte_size + 1;
453 size_t n;
454
Steven Cooreman69967ce2021-01-18 18:01:08 +0100455 /* Skip the test case if the target running the test cannot
456 * accomodate large keys due to heap size constraints */
457 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200458 memset( buffer, 'K', byte_size );
459
460 PSA_ASSERT( psa_crypto_init( ) );
461
462 /* Try importing the key */
463 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
464 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200465 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100466 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200467 TEST_EQUAL( status, expected_status );
468
469 if( status == PSA_SUCCESS )
470 {
Ronald Cron5425a212020-08-04 14:58:35 +0200471 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200472 TEST_EQUAL( psa_get_key_type( &attributes ), type );
473 TEST_EQUAL( psa_get_key_bits( &attributes ),
474 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200475 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200476 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200477 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200478 for( n = 0; n < byte_size; n++ )
479 TEST_EQUAL( buffer[n], 'K' );
480 for( n = byte_size; n < buffer_size; n++ )
481 TEST_EQUAL( buffer[n], 0 );
482 }
483
484exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100485 /*
486 * Key attributes may have been returned by psa_get_key_attributes()
487 * thus reset them as required.
488 */
489 psa_reset_key_attributes( &attributes );
490
Ronald Cron5425a212020-08-04 14:58:35 +0200491 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200492 PSA_DONE( );
493 mbedtls_free( buffer );
494}
495/* END_CASE */
496
497/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200498void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
499{
Ronald Cron5425a212020-08-04 14:58:35 +0200500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200501 size_t bits = bits_arg;
502 psa_status_t expected_status = expected_status_arg;
503 psa_status_t status;
504 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200505 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200506 size_t buffer_size = /* Slight overapproximations */
507 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200508 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200509 unsigned char *p;
510 int ret;
511 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200513
Gilles Peskine8817f612018-12-18 00:18:46 +0100514 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200515 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200516
517 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
518 bits, keypair ) ) >= 0 );
519 length = ret;
520
521 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200522 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200523 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100524 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200525
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200526 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200527 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200528
529exit:
530 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200531 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200532}
533/* END_CASE */
534
535/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300536void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300537 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200538 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539 int expected_bits,
540 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200541 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 int canonical_input )
543{
Ronald Cron5425a212020-08-04 14:58:35 +0200544 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200546 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200547 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 unsigned char *exported = NULL;
550 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100552 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200555 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556
Moran Pekercb088e72018-07-17 17:36:59 +0300557 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200558 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200560 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100561 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562
Gilles Peskine4747d192019-04-17 15:05:45 +0200563 psa_set_key_usage_flags( &attributes, usage_arg );
564 psa_set_key_algorithm( &attributes, alg );
565 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700566
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200568 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569
570 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200572 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
573 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200574 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575
576 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200577 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100578 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100579
580 /* The exported length must be set by psa_export_key() to a value between 0
581 * and export_size. On errors, the exported length must be 0. */
582 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
583 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
584 TEST_ASSERT( exported_length <= export_size );
585
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200586 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200587 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100588 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200589 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100590 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200592 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100593
Gilles Peskineea38a922021-02-13 00:05:16 +0100594 /* Run sanity checks on the exported key. For non-canonical inputs,
595 * this validates the canonical representations. For canonical inputs,
596 * this doesn't directly validate the implementation, but it still helps
597 * by cross-validating the test data with the sanity check code. */
598 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200599 goto exit;
600
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200602 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603 else
604 {
Ronald Cron5425a212020-08-04 14:58:35 +0200605 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200606 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200607 &key2 ) );
608 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100609 reexported,
610 export_size,
611 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200612 ASSERT_COMPARE( exported, exported_length,
613 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200614 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100615 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100616 TEST_ASSERT( exported_length <=
617 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
618 psa_get_key_bits( &got_attributes ) ) );
619 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100620
621destroy:
622 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200623 PSA_ASSERT( psa_destroy_key( key ) );
624 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625
626exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100627 /*
628 * Key attributes may have been returned by psa_get_key_attributes()
629 * thus reset them as required.
630 */
631 psa_reset_key_attributes( &got_attributes );
632
itayzafrir3e02b3b2018-06-12 17:06:52 +0300633 mbedtls_free( exported );
634 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200635 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636}
637/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100638
Moran Pekerf709f4a2018-06-06 17:26:04 +0300639/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300640void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200641 int type_arg,
642 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100643 int export_size_delta,
644 int expected_export_status_arg,
645 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300646{
Ronald Cron5425a212020-08-04 14:58:35 +0200647 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300648 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200649 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200650 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300651 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300652 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100653 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100654 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200655 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300656
Gilles Peskine8817f612018-12-18 00:18:46 +0100657 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300658
Gilles Peskine4747d192019-04-17 15:05:45 +0200659 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
660 psa_set_key_algorithm( &attributes, alg );
661 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662
663 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200664 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300665
Gilles Peskine49c25912018-10-29 15:15:31 +0100666 /* Export the public key */
667 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200668 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200669 exported, export_size,
670 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100671 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100672 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100673 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200674 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100675 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200676 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200677 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100678 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100679 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100680 TEST_ASSERT( expected_public_key->len <=
681 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
682 TEST_ASSERT( expected_public_key->len <=
683 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100684 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
685 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100686 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300687
688exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100689 /*
690 * Key attributes may have been returned by psa_get_key_attributes()
691 * thus reset them as required.
692 */
693 psa_reset_key_attributes( &attributes );
694
itayzafrir3e02b3b2018-06-12 17:06:52 +0300695 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200696 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200697 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300698}
699/* END_CASE */
700
Gilles Peskine20035e32018-02-03 22:44:14 +0100701/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200702void import_and_exercise_key( data_t *data,
703 int type_arg,
704 int bits_arg,
705 int alg_arg )
706{
Ronald Cron5425a212020-08-04 14:58:35 +0200707 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200708 psa_key_type_t type = type_arg;
709 size_t bits = bits_arg;
710 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100711 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200713 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200714
Gilles Peskine8817f612018-12-18 00:18:46 +0100715 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200716
Gilles Peskine4747d192019-04-17 15:05:45 +0200717 psa_set_key_usage_flags( &attributes, usage );
718 psa_set_key_algorithm( &attributes, alg );
719 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200720
721 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200722 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200723
724 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200725 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200726 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
727 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200728
729 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100730 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200731 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200732
Ronald Cron5425a212020-08-04 14:58:35 +0200733 PSA_ASSERT( psa_destroy_key( key ) );
734 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200735
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200736exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100737 /*
738 * Key attributes may have been returned by psa_get_key_attributes()
739 * thus reset them as required.
740 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200741 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100742
743 psa_reset_key_attributes( &attributes );
744 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200745 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200746}
747/* END_CASE */
748
749/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100750void effective_key_attributes( int type_arg, int expected_type_arg,
751 int bits_arg, int expected_bits_arg,
752 int usage_arg, int expected_usage_arg,
753 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200754{
Ronald Cron5425a212020-08-04 14:58:35 +0200755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100756 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100757 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100758 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100759 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200760 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100761 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200762 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100763 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200765
Gilles Peskine8817f612018-12-18 00:18:46 +0100766 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200767
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200768 psa_set_key_usage_flags( &attributes, usage );
769 psa_set_key_algorithm( &attributes, alg );
770 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100771 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200772
Ronald Cron5425a212020-08-04 14:58:35 +0200773 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100774 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200775
Ronald Cron5425a212020-08-04 14:58:35 +0200776 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100777 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
778 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
779 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
780 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200781
782exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100783 /*
784 * Key attributes may have been returned by psa_get_key_attributes()
785 * thus reset them as required.
786 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200787 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100788
789 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200790 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200791}
792/* END_CASE */
793
794/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100795void check_key_policy( int type_arg, int bits_arg,
796 int usage_arg, int alg_arg )
797{
798 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
799 usage_arg, usage_arg, alg_arg, alg_arg );
800 goto exit;
801}
802/* END_CASE */
803
804/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200805void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000806{
807 /* Test each valid way of initializing the object, except for `= {0}`, as
808 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
809 * though it's OK by the C standard. We could test for this, but we'd need
810 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200811 psa_key_attributes_t func = psa_key_attributes_init( );
812 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
813 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000814
815 memset( &zero, 0, sizeof( zero ) );
816
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200817 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
818 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
819 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000820
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200821 TEST_EQUAL( psa_get_key_type( &func ), 0 );
822 TEST_EQUAL( psa_get_key_type( &init ), 0 );
823 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
824
825 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
826 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
827 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
828
829 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
830 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
831 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
832
833 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
834 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
835 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000836}
837/* END_CASE */
838
839/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200840void mac_key_policy( int policy_usage,
841 int policy_alg,
842 int key_type,
843 data_t *key_data,
844 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200845{
Ronald Cron5425a212020-08-04 14:58:35 +0200846 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000848 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200849 psa_status_t status;
850 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200851
Gilles Peskine8817f612018-12-18 00:18:46 +0100852 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200853
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200854 psa_set_key_usage_flags( &attributes, policy_usage );
855 psa_set_key_algorithm( &attributes, policy_alg );
856 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200857
Gilles Peskine049c7532019-05-15 20:22:09 +0200858 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200859 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200860
Ronald Cron5425a212020-08-04 14:58:35 +0200861 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200862 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100863 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100864 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200865 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100866 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200867 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200868
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200870 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200871 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100872 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100873 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200874 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100875 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200876
877exit:
878 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200879 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200880 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200881}
882/* END_CASE */
883
884/* BEGIN_CASE */
885void cipher_key_policy( int policy_usage,
886 int policy_alg,
887 int key_type,
888 data_t *key_data,
889 int exercise_alg )
890{
Ronald Cron5425a212020-08-04 14:58:35 +0200891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000893 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200894 psa_status_t status;
895
Gilles Peskine8817f612018-12-18 00:18:46 +0100896 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200897
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200898 psa_set_key_usage_flags( &attributes, policy_usage );
899 psa_set_key_algorithm( &attributes, policy_alg );
900 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200901
Gilles Peskine049c7532019-05-15 20:22:09 +0200902 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200903 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200904
Ronald Cron5425a212020-08-04 14:58:35 +0200905 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200906 if( policy_alg == exercise_alg &&
907 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100908 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200909 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100910 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200911 psa_cipher_abort( &operation );
912
Ronald Cron5425a212020-08-04 14:58:35 +0200913 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200914 if( policy_alg == exercise_alg &&
915 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100916 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200917 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100918 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200919
920exit:
921 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200922 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200923 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200924}
925/* END_CASE */
926
927/* BEGIN_CASE */
928void aead_key_policy( int policy_usage,
929 int policy_alg,
930 int key_type,
931 data_t *key_data,
932 int nonce_length_arg,
933 int tag_length_arg,
934 int exercise_alg )
935{
Ronald Cron5425a212020-08-04 14:58:35 +0200936 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200938 psa_status_t status;
939 unsigned char nonce[16] = {0};
940 size_t nonce_length = nonce_length_arg;
941 unsigned char tag[16];
942 size_t tag_length = tag_length_arg;
943 size_t output_length;
944
945 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
946 TEST_ASSERT( tag_length <= sizeof( tag ) );
947
Gilles Peskine8817f612018-12-18 00:18:46 +0100948 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200949
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200950 psa_set_key_usage_flags( &attributes, policy_usage );
951 psa_set_key_algorithm( &attributes, policy_alg );
952 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200953
Gilles Peskine049c7532019-05-15 20:22:09 +0200954 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200955 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200956
Ronald Cron5425a212020-08-04 14:58:35 +0200957 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200958 nonce, nonce_length,
959 NULL, 0,
960 NULL, 0,
961 tag, tag_length,
962 &output_length );
963 if( policy_alg == exercise_alg &&
964 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100965 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200966 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100967 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968
969 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200970 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200971 nonce, nonce_length,
972 NULL, 0,
973 tag, tag_length,
974 NULL, 0,
975 &output_length );
976 if( policy_alg == exercise_alg &&
977 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100978 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200979 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100980 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200981
982exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200983 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200984 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200985}
986/* END_CASE */
987
988/* BEGIN_CASE */
989void asymmetric_encryption_key_policy( int policy_usage,
990 int policy_alg,
991 int key_type,
992 data_t *key_data,
993 int exercise_alg )
994{
Ronald Cron5425a212020-08-04 14:58:35 +0200995 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200996 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200997 psa_status_t status;
998 size_t key_bits;
999 size_t buffer_length;
1000 unsigned char *buffer = NULL;
1001 size_t output_length;
1002
Gilles Peskine8817f612018-12-18 00:18:46 +01001003 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001004
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001005 psa_set_key_usage_flags( &attributes, policy_usage );
1006 psa_set_key_algorithm( &attributes, policy_alg );
1007 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001008
Gilles Peskine049c7532019-05-15 20:22:09 +02001009 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001010 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001011
Ronald Cron5425a212020-08-04 14:58:35 +02001012 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001013 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1015 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001016 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001017
Ronald Cron5425a212020-08-04 14:58:35 +02001018 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001019 NULL, 0,
1020 NULL, 0,
1021 buffer, buffer_length,
1022 &output_length );
1023 if( policy_alg == exercise_alg &&
1024 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001025 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001026 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001027 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001028
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001029 if( buffer_length != 0 )
1030 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001031 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032 buffer, buffer_length,
1033 NULL, 0,
1034 buffer, buffer_length,
1035 &output_length );
1036 if( policy_alg == exercise_alg &&
1037 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001038 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001039 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001040 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001041
1042exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001043 /*
1044 * Key attributes may have been returned by psa_get_key_attributes()
1045 * thus reset them as required.
1046 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001047 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001048
1049 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001050 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001051 mbedtls_free( buffer );
1052}
1053/* END_CASE */
1054
1055/* BEGIN_CASE */
1056void asymmetric_signature_key_policy( int policy_usage,
1057 int policy_alg,
1058 int key_type,
1059 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001060 int exercise_alg,
1061 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001062{
Ronald Cron5425a212020-08-04 14:58:35 +02001063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001065 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001066 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1067 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1068 * compatible with the policy and `payload_length_arg` is supposed to be
1069 * a valid input length to sign. If `payload_length_arg <= 0`,
1070 * `exercise_alg` is supposed to be forbidden by the policy. */
1071 int compatible_alg = payload_length_arg > 0;
1072 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001073 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001074 size_t signature_length;
1075
Gilles Peskine8817f612018-12-18 00:18:46 +01001076 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001078 psa_set_key_usage_flags( &attributes, policy_usage );
1079 psa_set_key_algorithm( &attributes, policy_alg );
1080 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001081
Gilles Peskine049c7532019-05-15 20:22:09 +02001082 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001083 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001084
Ronald Cron5425a212020-08-04 14:58:35 +02001085 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001086 payload, payload_length,
1087 signature, sizeof( signature ),
1088 &signature_length );
1089 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001090 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001091 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001092 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001093
1094 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001095 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001096 payload, payload_length,
1097 signature, sizeof( signature ) );
1098 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001099 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001100 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001101 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001102
1103exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001104 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001105 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001106}
1107/* END_CASE */
1108
Janos Follathba3fab92019-06-11 14:50:16 +01001109/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001110void derive_key_policy( int policy_usage,
1111 int policy_alg,
1112 int key_type,
1113 data_t *key_data,
1114 int exercise_alg )
1115{
Ronald Cron5425a212020-08-04 14:58:35 +02001116 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001118 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001119 psa_status_t status;
1120
Gilles Peskine8817f612018-12-18 00:18:46 +01001121 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001122
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001123 psa_set_key_usage_flags( &attributes, policy_usage );
1124 psa_set_key_algorithm( &attributes, policy_alg );
1125 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001126
Gilles Peskine049c7532019-05-15 20:22:09 +02001127 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001128 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001129
Janos Follathba3fab92019-06-11 14:50:16 +01001130 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1131
1132 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1133 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001134 {
Janos Follathba3fab92019-06-11 14:50:16 +01001135 PSA_ASSERT( psa_key_derivation_input_bytes(
1136 &operation,
1137 PSA_KEY_DERIVATION_INPUT_SEED,
1138 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001139 }
Janos Follathba3fab92019-06-11 14:50:16 +01001140
1141 status = psa_key_derivation_input_key( &operation,
1142 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001143 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001144
Gilles Peskineea0fb492018-07-12 17:17:20 +02001145 if( policy_alg == exercise_alg &&
1146 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001147 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001148 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001149 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001150
1151exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001152 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001153 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001154 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001155}
1156/* END_CASE */
1157
1158/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001159void agreement_key_policy( int policy_usage,
1160 int policy_alg,
1161 int key_type_arg,
1162 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001163 int exercise_alg,
1164 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001165{
Ronald Cron5425a212020-08-04 14:58:35 +02001166 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001168 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001169 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001170 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001171 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001172
Gilles Peskine8817f612018-12-18 00:18:46 +01001173 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001174
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001175 psa_set_key_usage_flags( &attributes, policy_usage );
1176 psa_set_key_algorithm( &attributes, policy_alg );
1177 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001178
Gilles Peskine049c7532019-05-15 20:22:09 +02001179 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001180 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001182 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001183 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001184
Steven Cooremance48e852020-10-05 16:02:45 +02001185 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001186
1187exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001188 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001189 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001190 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001191}
1192/* END_CASE */
1193
1194/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001195void key_policy_alg2( int key_type_arg, data_t *key_data,
1196 int usage_arg, int alg_arg, int alg2_arg )
1197{
Ronald Cron5425a212020-08-04 14:58:35 +02001198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001199 psa_key_type_t key_type = key_type_arg;
1200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1201 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1202 psa_key_usage_t usage = usage_arg;
1203 psa_algorithm_t alg = alg_arg;
1204 psa_algorithm_t alg2 = alg2_arg;
1205
1206 PSA_ASSERT( psa_crypto_init( ) );
1207
1208 psa_set_key_usage_flags( &attributes, usage );
1209 psa_set_key_algorithm( &attributes, alg );
1210 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1211 psa_set_key_type( &attributes, key_type );
1212 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001213 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001214
Ronald Cron5425a212020-08-04 14:58:35 +02001215 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001216 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1217 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1218 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1219
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001220 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001221 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001222 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001223 goto exit;
1224
1225exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001226 /*
1227 * Key attributes may have been returned by psa_get_key_attributes()
1228 * thus reset them as required.
1229 */
1230 psa_reset_key_attributes( &got_attributes );
1231
Ronald Cron5425a212020-08-04 14:58:35 +02001232 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001233 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001234}
1235/* END_CASE */
1236
1237/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001238void raw_agreement_key_policy( int policy_usage,
1239 int policy_alg,
1240 int key_type_arg,
1241 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001242 int exercise_alg,
1243 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001244{
Ronald Cron5425a212020-08-04 14:58:35 +02001245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001247 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001248 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001249 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001250 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001251
1252 PSA_ASSERT( psa_crypto_init( ) );
1253
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001254 psa_set_key_usage_flags( &attributes, policy_usage );
1255 psa_set_key_algorithm( &attributes, policy_alg );
1256 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001257
Gilles Peskine049c7532019-05-15 20:22:09 +02001258 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001259 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001261 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001262
Steven Cooremance48e852020-10-05 16:02:45 +02001263 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001264
1265exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001266 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001267 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001268 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001269}
1270/* END_CASE */
1271
1272/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001273void copy_success( int source_usage_arg,
1274 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001275 int type_arg, data_t *material,
1276 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001277 int target_usage_arg,
1278 int target_alg_arg, int target_alg2_arg,
1279 int expected_usage_arg,
1280 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001281{
Gilles Peskineca25db92019-04-19 11:43:08 +02001282 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1283 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001284 psa_key_usage_t expected_usage = expected_usage_arg;
1285 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001286 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001287 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1288 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001289 uint8_t *export_buffer = NULL;
1290
Gilles Peskine57ab7212019-01-28 13:03:09 +01001291 PSA_ASSERT( psa_crypto_init( ) );
1292
Gilles Peskineca25db92019-04-19 11:43:08 +02001293 /* Prepare the source key. */
1294 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1295 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001296 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001297 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001298 PSA_ASSERT( psa_import_key( &source_attributes,
1299 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001300 &source_key ) );
1301 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001302
Gilles Peskineca25db92019-04-19 11:43:08 +02001303 /* Prepare the target attributes. */
1304 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001305 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001306 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001307 /* Set volatile lifetime to reset the key identifier to 0. */
1308 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1309 }
1310
Gilles Peskineca25db92019-04-19 11:43:08 +02001311 if( target_usage_arg != -1 )
1312 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1313 if( target_alg_arg != -1 )
1314 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001315 if( target_alg2_arg != -1 )
1316 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001317
1318 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001319 PSA_ASSERT( psa_copy_key( source_key,
1320 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001321
1322 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001323 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001324
1325 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001326 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001327 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1328 psa_get_key_type( &target_attributes ) );
1329 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1330 psa_get_key_bits( &target_attributes ) );
1331 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1332 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001333 TEST_EQUAL( expected_alg2,
1334 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001335 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1336 {
1337 size_t length;
1338 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001339 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001340 material->len, &length ) );
1341 ASSERT_COMPARE( material->x, material->len,
1342 export_buffer, length );
1343 }
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001344 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001345 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001346 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001347 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001348
Ronald Cron5425a212020-08-04 14:58:35 +02001349 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001350
1351exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001352 /*
1353 * Source and target key attributes may have been returned by
1354 * psa_get_key_attributes() thus reset them as required.
1355 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001356 psa_reset_key_attributes( &source_attributes );
1357 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001358
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001359 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001360 mbedtls_free( export_buffer );
1361}
1362/* END_CASE */
1363
1364/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001365void copy_fail( int source_usage_arg,
1366 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001367 int type_arg, data_t *material,
1368 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001369 int target_usage_arg,
1370 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001371 int expected_status_arg )
1372{
1373 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1374 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001375 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1376 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02001377
1378 PSA_ASSERT( psa_crypto_init( ) );
1379
1380 /* Prepare the source key. */
1381 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1382 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001383 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001384 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001385 PSA_ASSERT( psa_import_key( &source_attributes,
1386 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001387 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001388
1389 /* Prepare the target attributes. */
1390 psa_set_key_type( &target_attributes, target_type_arg );
1391 psa_set_key_bits( &target_attributes, target_bits_arg );
1392 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1393 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001394 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001395
1396 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001397 TEST_EQUAL( psa_copy_key( source_key,
1398 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001399 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001400
Ronald Cron5425a212020-08-04 14:58:35 +02001401 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001402
Gilles Peskine4a644642019-05-03 17:14:08 +02001403exit:
1404 psa_reset_key_attributes( &source_attributes );
1405 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001406 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001411void hash_operation_init( )
1412{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001413 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001414 /* Test each valid way of initializing the object, except for `= {0}`, as
1415 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1416 * though it's OK by the C standard. We could test for this, but we'd need
1417 * to supress the Clang warning for the test. */
1418 psa_hash_operation_t func = psa_hash_operation_init( );
1419 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1420 psa_hash_operation_t zero;
1421
1422 memset( &zero, 0, sizeof( zero ) );
1423
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001424 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001425 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1426 PSA_ERROR_BAD_STATE );
1427 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1428 PSA_ERROR_BAD_STATE );
1429 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1430 PSA_ERROR_BAD_STATE );
1431
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001432 /* A default hash operation should be abortable without error. */
1433 PSA_ASSERT( psa_hash_abort( &func ) );
1434 PSA_ASSERT( psa_hash_abort( &init ) );
1435 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001436}
1437/* END_CASE */
1438
1439/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001440void hash_setup( int alg_arg,
1441 int expected_status_arg )
1442{
1443 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001444 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001445 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001446 psa_status_t status;
1447
Gilles Peskine8817f612018-12-18 00:18:46 +01001448 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001449
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001450 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001451 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001452
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001453 /* Whether setup succeeded or failed, abort must succeed. */
1454 PSA_ASSERT( psa_hash_abort( &operation ) );
1455
1456 /* If setup failed, reproduce the failure, so as to
1457 * test the resulting state of the operation object. */
1458 if( status != PSA_SUCCESS )
1459 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1460
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001461 /* Now the operation object should be reusable. */
1462#if defined(KNOWN_SUPPORTED_HASH_ALG)
1463 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1464 PSA_ASSERT( psa_hash_abort( &operation ) );
1465#endif
1466
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001467exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001468 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001469}
1470/* END_CASE */
1471
1472/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001473void hash_compute_fail( int alg_arg, data_t *input,
1474 int output_size_arg, int expected_status_arg )
1475{
1476 psa_algorithm_t alg = alg_arg;
1477 uint8_t *output = NULL;
1478 size_t output_size = output_size_arg;
1479 size_t output_length = INVALID_EXPORT_LENGTH;
1480 psa_status_t expected_status = expected_status_arg;
1481 psa_status_t status;
1482
1483 ASSERT_ALLOC( output, output_size );
1484
1485 PSA_ASSERT( psa_crypto_init( ) );
1486
1487 status = psa_hash_compute( alg, input->x, input->len,
1488 output, output_size, &output_length );
1489 TEST_EQUAL( status, expected_status );
1490 TEST_ASSERT( output_length <= output_size );
1491
1492exit:
1493 mbedtls_free( output );
1494 PSA_DONE( );
1495}
1496/* END_CASE */
1497
1498/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001499void hash_compare_fail( int alg_arg, data_t *input,
1500 data_t *reference_hash,
1501 int expected_status_arg )
1502{
1503 psa_algorithm_t alg = alg_arg;
1504 psa_status_t expected_status = expected_status_arg;
1505 psa_status_t status;
1506
1507 PSA_ASSERT( psa_crypto_init( ) );
1508
1509 status = psa_hash_compare( alg, input->x, input->len,
1510 reference_hash->x, reference_hash->len );
1511 TEST_EQUAL( status, expected_status );
1512
1513exit:
1514 PSA_DONE( );
1515}
1516/* END_CASE */
1517
1518/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001519void hash_compute_compare( int alg_arg, data_t *input,
1520 data_t *expected_output )
1521{
1522 psa_algorithm_t alg = alg_arg;
1523 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1524 size_t output_length = INVALID_EXPORT_LENGTH;
1525 size_t i;
1526
1527 PSA_ASSERT( psa_crypto_init( ) );
1528
1529 /* Compute with tight buffer */
1530 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001531 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001532 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001533 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001534 ASSERT_COMPARE( output, output_length,
1535 expected_output->x, expected_output->len );
1536
1537 /* Compute with larger buffer */
1538 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1539 output, sizeof( output ),
1540 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001541 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001542 ASSERT_COMPARE( output, output_length,
1543 expected_output->x, expected_output->len );
1544
1545 /* Compare with correct hash */
1546 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1547 output, output_length ) );
1548
1549 /* Compare with trailing garbage */
1550 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1551 output, output_length + 1 ),
1552 PSA_ERROR_INVALID_SIGNATURE );
1553
1554 /* Compare with truncated hash */
1555 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1556 output, output_length - 1 ),
1557 PSA_ERROR_INVALID_SIGNATURE );
1558
1559 /* Compare with corrupted value */
1560 for( i = 0; i < output_length; i++ )
1561 {
Chris Jones9634bb12021-01-20 15:56:42 +00001562 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001563 output[i] ^= 1;
1564 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1565 output, output_length ),
1566 PSA_ERROR_INVALID_SIGNATURE );
1567 output[i] ^= 1;
1568 }
1569
1570exit:
1571 PSA_DONE( );
1572}
1573/* END_CASE */
1574
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001575/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001576void hash_bad_order( )
1577{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001578 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001579 unsigned char input[] = "";
1580 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001581 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001582 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1583 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1584 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001585 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001586 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001587 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001588
Gilles Peskine8817f612018-12-18 00:18:46 +01001589 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001590
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001591 /* Call setup twice in a row. */
1592 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1593 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1594 PSA_ERROR_BAD_STATE );
1595 PSA_ASSERT( psa_hash_abort( &operation ) );
1596
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001597 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001598 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001599 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001600 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001601
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001602 /* Call update after finish. */
1603 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1604 PSA_ASSERT( psa_hash_finish( &operation,
1605 hash, sizeof( hash ), &hash_len ) );
1606 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001607 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001608 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001609
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001610 /* Call verify without calling setup beforehand. */
1611 TEST_EQUAL( psa_hash_verify( &operation,
1612 valid_hash, sizeof( valid_hash ) ),
1613 PSA_ERROR_BAD_STATE );
1614 PSA_ASSERT( psa_hash_abort( &operation ) );
1615
1616 /* Call verify after finish. */
1617 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1618 PSA_ASSERT( psa_hash_finish( &operation,
1619 hash, sizeof( hash ), &hash_len ) );
1620 TEST_EQUAL( psa_hash_verify( &operation,
1621 valid_hash, sizeof( valid_hash ) ),
1622 PSA_ERROR_BAD_STATE );
1623 PSA_ASSERT( psa_hash_abort( &operation ) );
1624
1625 /* Call verify twice in a row. */
1626 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1627 PSA_ASSERT( psa_hash_verify( &operation,
1628 valid_hash, sizeof( valid_hash ) ) );
1629 TEST_EQUAL( psa_hash_verify( &operation,
1630 valid_hash, sizeof( valid_hash ) ),
1631 PSA_ERROR_BAD_STATE );
1632 PSA_ASSERT( psa_hash_abort( &operation ) );
1633
1634 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001635 TEST_EQUAL( psa_hash_finish( &operation,
1636 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001637 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001638 PSA_ASSERT( psa_hash_abort( &operation ) );
1639
1640 /* Call finish twice in a row. */
1641 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1642 PSA_ASSERT( psa_hash_finish( &operation,
1643 hash, sizeof( hash ), &hash_len ) );
1644 TEST_EQUAL( psa_hash_finish( &operation,
1645 hash, sizeof( hash ), &hash_len ),
1646 PSA_ERROR_BAD_STATE );
1647 PSA_ASSERT( psa_hash_abort( &operation ) );
1648
1649 /* Call finish after calling verify. */
1650 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1651 PSA_ASSERT( psa_hash_verify( &operation,
1652 valid_hash, sizeof( valid_hash ) ) );
1653 TEST_EQUAL( psa_hash_finish( &operation,
1654 hash, sizeof( hash ), &hash_len ),
1655 PSA_ERROR_BAD_STATE );
1656 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001657
1658exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001659 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001660}
1661/* END_CASE */
1662
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001663/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001664void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001665{
1666 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001667 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1668 * appended to it */
1669 unsigned char hash[] = {
1670 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1671 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1672 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001673 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001674 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001675
Gilles Peskine8817f612018-12-18 00:18:46 +01001676 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001677
itayzafrir27e69452018-11-01 14:26:34 +02001678 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001680 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001681 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001682
itayzafrir27e69452018-11-01 14:26:34 +02001683 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001684 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001685 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001686 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001687
itayzafrir27e69452018-11-01 14:26:34 +02001688 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001689 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001690 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001691 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001692
itayzafrirec93d302018-10-18 18:01:10 +03001693exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001694 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001695}
1696/* END_CASE */
1697
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001698/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1699void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001700{
1701 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001702 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001703 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001704 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001705 size_t hash_len;
1706
Gilles Peskine8817f612018-12-18 00:18:46 +01001707 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001708
itayzafrir58028322018-10-25 10:22:01 +03001709 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001710 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001711 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001712 hash, expected_size - 1, &hash_len ),
1713 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001714
1715exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001716 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001717}
1718/* END_CASE */
1719
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001720/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1721void hash_clone_source_state( )
1722{
1723 psa_algorithm_t alg = PSA_ALG_SHA_256;
1724 unsigned char hash[PSA_HASH_MAX_SIZE];
1725 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1726 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1727 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1728 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1729 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1730 size_t hash_len;
1731
1732 PSA_ASSERT( psa_crypto_init( ) );
1733 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1734
1735 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1736 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1737 PSA_ASSERT( psa_hash_finish( &op_finished,
1738 hash, sizeof( hash ), &hash_len ) );
1739 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1740 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1741
1742 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1743 PSA_ERROR_BAD_STATE );
1744
1745 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1746 PSA_ASSERT( psa_hash_finish( &op_init,
1747 hash, sizeof( hash ), &hash_len ) );
1748 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1749 PSA_ASSERT( psa_hash_finish( &op_finished,
1750 hash, sizeof( hash ), &hash_len ) );
1751 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1752 PSA_ASSERT( psa_hash_finish( &op_aborted,
1753 hash, sizeof( hash ), &hash_len ) );
1754
1755exit:
1756 psa_hash_abort( &op_source );
1757 psa_hash_abort( &op_init );
1758 psa_hash_abort( &op_setup );
1759 psa_hash_abort( &op_finished );
1760 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001761 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001762}
1763/* END_CASE */
1764
1765/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1766void hash_clone_target_state( )
1767{
1768 psa_algorithm_t alg = PSA_ALG_SHA_256;
1769 unsigned char hash[PSA_HASH_MAX_SIZE];
1770 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1771 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1772 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1773 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1774 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1775 size_t hash_len;
1776
1777 PSA_ASSERT( psa_crypto_init( ) );
1778
1779 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1780 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1781 PSA_ASSERT( psa_hash_finish( &op_finished,
1782 hash, sizeof( hash ), &hash_len ) );
1783 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1784 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1785
1786 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1787 PSA_ASSERT( psa_hash_finish( &op_target,
1788 hash, sizeof( hash ), &hash_len ) );
1789
1790 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1791 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1792 PSA_ERROR_BAD_STATE );
1793 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1794 PSA_ERROR_BAD_STATE );
1795
1796exit:
1797 psa_hash_abort( &op_target );
1798 psa_hash_abort( &op_init );
1799 psa_hash_abort( &op_setup );
1800 psa_hash_abort( &op_finished );
1801 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001802 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001803}
1804/* END_CASE */
1805
itayzafrir58028322018-10-25 10:22:01 +03001806/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001807void mac_operation_init( )
1808{
Jaeden Amero252ef282019-02-15 14:05:35 +00001809 const uint8_t input[1] = { 0 };
1810
Jaeden Amero769ce272019-01-04 11:48:03 +00001811 /* Test each valid way of initializing the object, except for `= {0}`, as
1812 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1813 * though it's OK by the C standard. We could test for this, but we'd need
1814 * to supress the Clang warning for the test. */
1815 psa_mac_operation_t func = psa_mac_operation_init( );
1816 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1817 psa_mac_operation_t zero;
1818
1819 memset( &zero, 0, sizeof( zero ) );
1820
Jaeden Amero252ef282019-02-15 14:05:35 +00001821 /* A freshly-initialized MAC operation should not be usable. */
1822 TEST_EQUAL( psa_mac_update( &func,
1823 input, sizeof( input ) ),
1824 PSA_ERROR_BAD_STATE );
1825 TEST_EQUAL( psa_mac_update( &init,
1826 input, sizeof( input ) ),
1827 PSA_ERROR_BAD_STATE );
1828 TEST_EQUAL( psa_mac_update( &zero,
1829 input, sizeof( input ) ),
1830 PSA_ERROR_BAD_STATE );
1831
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001832 /* A default MAC operation should be abortable without error. */
1833 PSA_ASSERT( psa_mac_abort( &func ) );
1834 PSA_ASSERT( psa_mac_abort( &init ) );
1835 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001836}
1837/* END_CASE */
1838
1839/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001840void mac_setup( int key_type_arg,
1841 data_t *key,
1842 int alg_arg,
1843 int expected_status_arg )
1844{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001845 psa_key_type_t key_type = key_type_arg;
1846 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001847 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001848 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001849 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1850#if defined(KNOWN_SUPPORTED_MAC_ALG)
1851 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1852#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001853
Gilles Peskine8817f612018-12-18 00:18:46 +01001854 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001855
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001856 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1857 &operation, &status ) )
1858 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001859 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001860
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001861 /* The operation object should be reusable. */
1862#if defined(KNOWN_SUPPORTED_MAC_ALG)
1863 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1864 smoke_test_key_data,
1865 sizeof( smoke_test_key_data ),
1866 KNOWN_SUPPORTED_MAC_ALG,
1867 &operation, &status ) )
1868 goto exit;
1869 TEST_EQUAL( status, PSA_SUCCESS );
1870#endif
1871
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001872exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001873 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001874}
1875/* END_CASE */
1876
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001877/* 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 +00001878void mac_bad_order( )
1879{
Ronald Cron5425a212020-08-04 14:58:35 +02001880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001881 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1882 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001883 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001884 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1885 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1886 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001888 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1889 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1890 size_t sign_mac_length = 0;
1891 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1892 const uint8_t verify_mac[] = {
1893 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1894 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1895 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1896
1897 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001898 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001899 psa_set_key_algorithm( &attributes, alg );
1900 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001901
Ronald Cron5425a212020-08-04 14:58:35 +02001902 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1903 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001904
Jaeden Amero252ef282019-02-15 14:05:35 +00001905 /* Call update without calling setup beforehand. */
1906 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1907 PSA_ERROR_BAD_STATE );
1908 PSA_ASSERT( psa_mac_abort( &operation ) );
1909
1910 /* Call sign finish without calling setup beforehand. */
1911 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1912 &sign_mac_length),
1913 PSA_ERROR_BAD_STATE );
1914 PSA_ASSERT( psa_mac_abort( &operation ) );
1915
1916 /* Call verify finish without calling setup beforehand. */
1917 TEST_EQUAL( psa_mac_verify_finish( &operation,
1918 verify_mac, sizeof( verify_mac ) ),
1919 PSA_ERROR_BAD_STATE );
1920 PSA_ASSERT( psa_mac_abort( &operation ) );
1921
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001922 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001923 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1924 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001925 PSA_ERROR_BAD_STATE );
1926 PSA_ASSERT( psa_mac_abort( &operation ) );
1927
Jaeden Amero252ef282019-02-15 14:05:35 +00001928 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001929 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001930 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1931 PSA_ASSERT( psa_mac_sign_finish( &operation,
1932 sign_mac, sizeof( sign_mac ),
1933 &sign_mac_length ) );
1934 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1935 PSA_ERROR_BAD_STATE );
1936 PSA_ASSERT( psa_mac_abort( &operation ) );
1937
1938 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001939 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001940 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1941 PSA_ASSERT( psa_mac_verify_finish( &operation,
1942 verify_mac, sizeof( verify_mac ) ) );
1943 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1944 PSA_ERROR_BAD_STATE );
1945 PSA_ASSERT( psa_mac_abort( &operation ) );
1946
1947 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001948 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001949 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1950 PSA_ASSERT( psa_mac_sign_finish( &operation,
1951 sign_mac, sizeof( sign_mac ),
1952 &sign_mac_length ) );
1953 TEST_EQUAL( psa_mac_sign_finish( &operation,
1954 sign_mac, sizeof( sign_mac ),
1955 &sign_mac_length ),
1956 PSA_ERROR_BAD_STATE );
1957 PSA_ASSERT( psa_mac_abort( &operation ) );
1958
1959 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001960 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001961 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1962 PSA_ASSERT( psa_mac_verify_finish( &operation,
1963 verify_mac, sizeof( verify_mac ) ) );
1964 TEST_EQUAL( psa_mac_verify_finish( &operation,
1965 verify_mac, sizeof( verify_mac ) ),
1966 PSA_ERROR_BAD_STATE );
1967 PSA_ASSERT( psa_mac_abort( &operation ) );
1968
1969 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001970 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001971 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1972 TEST_EQUAL( psa_mac_verify_finish( &operation,
1973 verify_mac, sizeof( verify_mac ) ),
1974 PSA_ERROR_BAD_STATE );
1975 PSA_ASSERT( psa_mac_abort( &operation ) );
1976
1977 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001978 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001979 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1980 TEST_EQUAL( psa_mac_sign_finish( &operation,
1981 sign_mac, sizeof( sign_mac ),
1982 &sign_mac_length ),
1983 PSA_ERROR_BAD_STATE );
1984 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001985
Ronald Cron5425a212020-08-04 14:58:35 +02001986 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001987
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001988exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001989 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001990}
1991/* END_CASE */
1992
1993/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001994void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02001995 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001996 int alg_arg,
1997 data_t *input,
1998 data_t *expected_mac )
1999{
Ronald Cron5425a212020-08-04 14:58:35 +02002000 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002001 psa_key_type_t key_type = key_type_arg;
2002 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002003 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002004 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002005 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002006 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002007 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002008 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002009 const size_t output_sizes_to_test[] = {
2010 0,
2011 1,
2012 expected_mac->len - 1,
2013 expected_mac->len,
2014 expected_mac->len + 1,
2015 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002016
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002017 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002018 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002019 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002020
Gilles Peskine8817f612018-12-18 00:18:46 +01002021 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002022
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002023 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002024 psa_set_key_algorithm( &attributes, alg );
2025 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002026
Ronald Cron5425a212020-08-04 14:58:35 +02002027 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2028 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002029
Gilles Peskine8b356b52020-08-25 23:44:59 +02002030 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2031 {
2032 const size_t output_size = output_sizes_to_test[i];
2033 psa_status_t expected_status =
2034 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2035 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002036
Chris Jones9634bb12021-01-20 15:56:42 +00002037 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002038 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002039
Gilles Peskine8b356b52020-08-25 23:44:59 +02002040 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002041 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002042 PSA_ASSERT( psa_mac_update( &operation,
2043 input->x, input->len ) );
2044 TEST_EQUAL( psa_mac_sign_finish( &operation,
2045 actual_mac, output_size,
2046 &mac_length ),
2047 expected_status );
2048 PSA_ASSERT( psa_mac_abort( &operation ) );
2049
2050 if( expected_status == PSA_SUCCESS )
2051 {
2052 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2053 actual_mac, mac_length );
2054 }
2055 mbedtls_free( actual_mac );
2056 actual_mac = NULL;
2057 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002058
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002059exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002060 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002061 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002062 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002063 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002064}
2065/* END_CASE */
2066
2067/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002068void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002069 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002070 int alg_arg,
2071 data_t *input,
2072 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002073{
Ronald Cron5425a212020-08-04 14:58:35 +02002074 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002075 psa_key_type_t key_type = key_type_arg;
2076 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002077 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002078 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002079 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002080
Gilles Peskine69c12672018-06-28 00:07:19 +02002081 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2082
Gilles Peskine8817f612018-12-18 00:18:46 +01002083 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002084
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002085 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002086 psa_set_key_algorithm( &attributes, alg );
2087 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002088
Ronald Cron5425a212020-08-04 14:58:35 +02002089 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2090 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002091
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002092 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002093 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002094 PSA_ASSERT( psa_mac_update( &operation,
2095 input->x, input->len ) );
2096 PSA_ASSERT( psa_mac_verify_finish( &operation,
2097 expected_mac->x,
2098 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002099
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002100 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002101 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002102 PSA_ASSERT( psa_mac_update( &operation,
2103 input->x, input->len ) );
2104 TEST_EQUAL( psa_mac_verify_finish( &operation,
2105 expected_mac->x,
2106 expected_mac->len - 1 ),
2107 PSA_ERROR_INVALID_SIGNATURE );
2108
2109 /* Test a MAC that's too long. */
2110 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2111 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002112 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002113 PSA_ASSERT( psa_mac_update( &operation,
2114 input->x, input->len ) );
2115 TEST_EQUAL( psa_mac_verify_finish( &operation,
2116 perturbed_mac,
2117 expected_mac->len + 1 ),
2118 PSA_ERROR_INVALID_SIGNATURE );
2119
2120 /* Test changing one byte. */
2121 for( size_t i = 0; i < expected_mac->len; i++ )
2122 {
Chris Jones9634bb12021-01-20 15:56:42 +00002123 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002124 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002125 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002126 PSA_ASSERT( psa_mac_update( &operation,
2127 input->x, input->len ) );
2128 TEST_EQUAL( psa_mac_verify_finish( &operation,
2129 perturbed_mac,
2130 expected_mac->len ),
2131 PSA_ERROR_INVALID_SIGNATURE );
2132 perturbed_mac[i] ^= 1;
2133 }
2134
Gilles Peskine8c9def32018-02-08 10:02:12 +01002135exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002136 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002137 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002138 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002139 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002140}
2141/* END_CASE */
2142
2143/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002144void cipher_operation_init( )
2145{
Jaeden Ameroab439972019-02-15 14:12:05 +00002146 const uint8_t input[1] = { 0 };
2147 unsigned char output[1] = { 0 };
2148 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002149 /* Test each valid way of initializing the object, except for `= {0}`, as
2150 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2151 * though it's OK by the C standard. We could test for this, but we'd need
2152 * to supress the Clang warning for the test. */
2153 psa_cipher_operation_t func = psa_cipher_operation_init( );
2154 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2155 psa_cipher_operation_t zero;
2156
2157 memset( &zero, 0, sizeof( zero ) );
2158
Jaeden Ameroab439972019-02-15 14:12:05 +00002159 /* A freshly-initialized cipher operation should not be usable. */
2160 TEST_EQUAL( psa_cipher_update( &func,
2161 input, sizeof( input ),
2162 output, sizeof( output ),
2163 &output_length ),
2164 PSA_ERROR_BAD_STATE );
2165 TEST_EQUAL( psa_cipher_update( &init,
2166 input, sizeof( input ),
2167 output, sizeof( output ),
2168 &output_length ),
2169 PSA_ERROR_BAD_STATE );
2170 TEST_EQUAL( psa_cipher_update( &zero,
2171 input, sizeof( input ),
2172 output, sizeof( output ),
2173 &output_length ),
2174 PSA_ERROR_BAD_STATE );
2175
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002176 /* A default cipher operation should be abortable without error. */
2177 PSA_ASSERT( psa_cipher_abort( &func ) );
2178 PSA_ASSERT( psa_cipher_abort( &init ) );
2179 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002180}
2181/* END_CASE */
2182
2183/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002184void cipher_setup( int key_type_arg,
2185 data_t *key,
2186 int alg_arg,
2187 int expected_status_arg )
2188{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002189 psa_key_type_t key_type = key_type_arg;
2190 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002191 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002192 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002193 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002194#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002195 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2196#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002197
Gilles Peskine8817f612018-12-18 00:18:46 +01002198 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002199
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002200 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2201 &operation, &status ) )
2202 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002203 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002204
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002205 /* The operation object should be reusable. */
2206#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2207 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2208 smoke_test_key_data,
2209 sizeof( smoke_test_key_data ),
2210 KNOWN_SUPPORTED_CIPHER_ALG,
2211 &operation, &status ) )
2212 goto exit;
2213 TEST_EQUAL( status, PSA_SUCCESS );
2214#endif
2215
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002216exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002217 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002218 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002219}
2220/* END_CASE */
2221
Steven Cooreman29eecbf2021-01-28 19:41:25 +01002222/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002223void cipher_bad_order( )
2224{
Ronald Cron5425a212020-08-04 14:58:35 +02002225 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002226 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2227 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002229 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002230 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002231 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002232 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2233 0xaa, 0xaa, 0xaa, 0xaa };
2234 const uint8_t text[] = {
2235 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2236 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002237 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002238 size_t length = 0;
2239
2240 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002241 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2242 psa_set_key_algorithm( &attributes, alg );
2243 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002244 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2245 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002246
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002247 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002248 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2249 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002250 PSA_ERROR_BAD_STATE );
2251 PSA_ASSERT( psa_cipher_abort( &operation ) );
2252
2253 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002254 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2255 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002256 PSA_ERROR_BAD_STATE );
2257 PSA_ASSERT( psa_cipher_abort( &operation ) );
2258
Jaeden Ameroab439972019-02-15 14:12:05 +00002259 /* Generate an IV without calling setup beforehand. */
2260 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2261 buffer, sizeof( buffer ),
2262 &length ),
2263 PSA_ERROR_BAD_STATE );
2264 PSA_ASSERT( psa_cipher_abort( &operation ) );
2265
2266 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002267 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002268 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2269 buffer, sizeof( buffer ),
2270 &length ) );
2271 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2272 buffer, sizeof( buffer ),
2273 &length ),
2274 PSA_ERROR_BAD_STATE );
2275 PSA_ASSERT( psa_cipher_abort( &operation ) );
2276
2277 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002278 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002279 PSA_ASSERT( psa_cipher_set_iv( &operation,
2280 iv, sizeof( iv ) ) );
2281 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2282 buffer, sizeof( buffer ),
2283 &length ),
2284 PSA_ERROR_BAD_STATE );
2285 PSA_ASSERT( psa_cipher_abort( &operation ) );
2286
2287 /* Set an IV without calling setup beforehand. */
2288 TEST_EQUAL( psa_cipher_set_iv( &operation,
2289 iv, sizeof( iv ) ),
2290 PSA_ERROR_BAD_STATE );
2291 PSA_ASSERT( psa_cipher_abort( &operation ) );
2292
2293 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002294 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002295 PSA_ASSERT( psa_cipher_set_iv( &operation,
2296 iv, sizeof( iv ) ) );
2297 TEST_EQUAL( psa_cipher_set_iv( &operation,
2298 iv, sizeof( iv ) ),
2299 PSA_ERROR_BAD_STATE );
2300 PSA_ASSERT( psa_cipher_abort( &operation ) );
2301
2302 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002303 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002304 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2305 buffer, sizeof( buffer ),
2306 &length ) );
2307 TEST_EQUAL( psa_cipher_set_iv( &operation,
2308 iv, sizeof( iv ) ),
2309 PSA_ERROR_BAD_STATE );
2310 PSA_ASSERT( psa_cipher_abort( &operation ) );
2311
2312 /* Call update without calling setup beforehand. */
2313 TEST_EQUAL( psa_cipher_update( &operation,
2314 text, sizeof( text ),
2315 buffer, sizeof( buffer ),
2316 &length ),
2317 PSA_ERROR_BAD_STATE );
2318 PSA_ASSERT( psa_cipher_abort( &operation ) );
2319
2320 /* Call update without an IV where an IV is required. */
2321 TEST_EQUAL( psa_cipher_update( &operation,
2322 text, sizeof( text ),
2323 buffer, sizeof( buffer ),
2324 &length ),
2325 PSA_ERROR_BAD_STATE );
2326 PSA_ASSERT( psa_cipher_abort( &operation ) );
2327
2328 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002329 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002330 PSA_ASSERT( psa_cipher_set_iv( &operation,
2331 iv, sizeof( iv ) ) );
2332 PSA_ASSERT( psa_cipher_finish( &operation,
2333 buffer, sizeof( buffer ), &length ) );
2334 TEST_EQUAL( psa_cipher_update( &operation,
2335 text, sizeof( text ),
2336 buffer, sizeof( buffer ),
2337 &length ),
2338 PSA_ERROR_BAD_STATE );
2339 PSA_ASSERT( psa_cipher_abort( &operation ) );
2340
2341 /* Call finish without calling setup beforehand. */
2342 TEST_EQUAL( psa_cipher_finish( &operation,
2343 buffer, sizeof( buffer ), &length ),
2344 PSA_ERROR_BAD_STATE );
2345 PSA_ASSERT( psa_cipher_abort( &operation ) );
2346
2347 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002348 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002349 /* Not calling update means we are encrypting an empty buffer, which is OK
2350 * for cipher modes with padding. */
2351 TEST_EQUAL( psa_cipher_finish( &operation,
2352 buffer, sizeof( buffer ), &length ),
2353 PSA_ERROR_BAD_STATE );
2354 PSA_ASSERT( psa_cipher_abort( &operation ) );
2355
2356 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002357 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002358 PSA_ASSERT( psa_cipher_set_iv( &operation,
2359 iv, sizeof( iv ) ) );
2360 PSA_ASSERT( psa_cipher_finish( &operation,
2361 buffer, sizeof( buffer ), &length ) );
2362 TEST_EQUAL( psa_cipher_finish( &operation,
2363 buffer, sizeof( buffer ), &length ),
2364 PSA_ERROR_BAD_STATE );
2365 PSA_ASSERT( psa_cipher_abort( &operation ) );
2366
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002368
Jaeden Ameroab439972019-02-15 14:12:05 +00002369exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002370 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002371 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002372}
2373/* END_CASE */
2374
2375/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002376void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002377 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002378 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002379 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002380{
Ronald Cron5425a212020-08-04 14:58:35 +02002381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002382 psa_status_t status;
2383 psa_key_type_t key_type = key_type_arg;
2384 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002385 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002386 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002387 size_t output_buffer_size = 0;
2388 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002389 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002390 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002391 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002392
Gilles Peskine8817f612018-12-18 00:18:46 +01002393 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002394
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002395 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2396 psa_set_key_algorithm( &attributes, alg );
2397 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002398
Ronald Cron5425a212020-08-04 14:58:35 +02002399 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2400 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002401
Ronald Cron5425a212020-08-04 14:58:35 +02002402 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002403
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002404 if( iv->len > 0 )
2405 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002406 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002407 }
2408
gabor-mezei-armceface22021-01-21 12:26:17 +01002409 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2410 TEST_ASSERT( output_buffer_size <=
2411 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002412 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002413
Gilles Peskine8817f612018-12-18 00:18:46 +01002414 PSA_ASSERT( psa_cipher_update( &operation,
2415 input->x, input->len,
2416 output, output_buffer_size,
2417 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002418 TEST_ASSERT( function_output_length <=
2419 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2420 TEST_ASSERT( function_output_length <=
2421 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002422 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002423
Gilles Peskine50e586b2018-06-08 14:28:46 +02002424 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002425 output + total_output_length,
2426 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002427 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002428 TEST_ASSERT( function_output_length <=
2429 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2430 TEST_ASSERT( function_output_length <=
2431 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002432 total_output_length += function_output_length;
2433
Gilles Peskinefe11b722018-12-18 00:24:04 +01002434 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002435 if( expected_status == PSA_SUCCESS )
2436 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002437 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002438 ASSERT_COMPARE( expected_output->x, expected_output->len,
2439 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002440 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002441
Gilles Peskine50e586b2018-06-08 14:28:46 +02002442exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002443 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002444 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002445 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002446 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002447}
2448/* END_CASE */
2449
2450/* BEGIN_CASE */
2451void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002452 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002453 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002454 int first_part_size_arg,
2455 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002456 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002457{
Ronald Cron5425a212020-08-04 14:58:35 +02002458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002459 psa_key_type_t key_type = key_type_arg;
2460 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002461 size_t first_part_size = first_part_size_arg;
2462 size_t output1_length = output1_length_arg;
2463 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002464 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002465 size_t output_buffer_size = 0;
2466 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002467 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002468 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002469 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002470
Gilles Peskine8817f612018-12-18 00:18:46 +01002471 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002472
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002473 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2474 psa_set_key_algorithm( &attributes, alg );
2475 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002476
Ronald Cron5425a212020-08-04 14:58:35 +02002477 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2478 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002479
Ronald Cron5425a212020-08-04 14:58:35 +02002480 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002481
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002482 if( iv->len > 0 )
2483 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002484 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002485 }
2486
gabor-mezei-armceface22021-01-21 12:26:17 +01002487 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2488 TEST_ASSERT( output_buffer_size <=
2489 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002490 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002491
Gilles Peskinee0866522019-02-19 19:44:00 +01002492 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002493 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2494 output, output_buffer_size,
2495 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002496 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002497 TEST_ASSERT( function_output_length <=
2498 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2499 TEST_ASSERT( function_output_length <=
2500 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002501 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002502
Gilles Peskine8817f612018-12-18 00:18:46 +01002503 PSA_ASSERT( psa_cipher_update( &operation,
2504 input->x + first_part_size,
2505 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002506 output + total_output_length,
2507 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002508 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002509 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002510 TEST_ASSERT( function_output_length <=
2511 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2512 alg,
2513 input->len - first_part_size ) );
2514 TEST_ASSERT( function_output_length <=
2515 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002516 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002517
Gilles Peskine8817f612018-12-18 00:18:46 +01002518 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002519 output + total_output_length,
2520 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002521 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002522 TEST_ASSERT( function_output_length <=
2523 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2524 TEST_ASSERT( function_output_length <=
2525 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002526 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002527 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002528
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002529 ASSERT_COMPARE( expected_output->x, expected_output->len,
2530 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002531
2532exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002533 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002534 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002535 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002536 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002537}
2538/* END_CASE */
2539
2540/* BEGIN_CASE */
2541void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002542 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002543 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002544 int first_part_size_arg,
2545 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002546 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002547{
Ronald Cron5425a212020-08-04 14:58:35 +02002548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002549 psa_key_type_t key_type = key_type_arg;
2550 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002551 size_t first_part_size = first_part_size_arg;
2552 size_t output1_length = output1_length_arg;
2553 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002554 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002555 size_t output_buffer_size = 0;
2556 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002557 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002558 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002559 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002560
Gilles Peskine8817f612018-12-18 00:18:46 +01002561 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002562
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002563 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2564 psa_set_key_algorithm( &attributes, alg );
2565 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002566
Ronald Cron5425a212020-08-04 14:58:35 +02002567 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2568 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002569
Ronald Cron5425a212020-08-04 14:58:35 +02002570 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002571
Steven Cooreman177deba2020-09-07 17:14:14 +02002572 if( iv->len > 0 )
2573 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002574 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002575 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002576
gabor-mezei-armceface22021-01-21 12:26:17 +01002577 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2578 TEST_ASSERT( output_buffer_size <=
2579 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002580 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002581
Gilles Peskinee0866522019-02-19 19:44:00 +01002582 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002583 PSA_ASSERT( psa_cipher_update( &operation,
2584 input->x, first_part_size,
2585 output, output_buffer_size,
2586 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002587 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002588 TEST_ASSERT( function_output_length <=
2589 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2590 TEST_ASSERT( function_output_length <=
2591 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002592 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002593
Gilles Peskine8817f612018-12-18 00:18:46 +01002594 PSA_ASSERT( psa_cipher_update( &operation,
2595 input->x + first_part_size,
2596 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002597 output + total_output_length,
2598 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002599 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002600 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002601 TEST_ASSERT( function_output_length <=
2602 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2603 alg,
2604 input->len - first_part_size ) );
2605 TEST_ASSERT( function_output_length <=
2606 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002607 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002608
Gilles Peskine8817f612018-12-18 00:18:46 +01002609 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002610 output + total_output_length,
2611 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002612 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002613 TEST_ASSERT( function_output_length <=
2614 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2615 TEST_ASSERT( function_output_length <=
2616 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002617 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002618 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002619
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002620 ASSERT_COMPARE( expected_output->x, expected_output->len,
2621 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002622
2623exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002624 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002625 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002626 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002627 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002628}
2629/* END_CASE */
2630
Gilles Peskine50e586b2018-06-08 14:28:46 +02002631/* BEGIN_CASE */
2632void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002633 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002634 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002635 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002636{
Ronald Cron5425a212020-08-04 14:58:35 +02002637 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002638 psa_status_t status;
2639 psa_key_type_t key_type = key_type_arg;
2640 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002641 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002642 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002643 size_t output_buffer_size = 0;
2644 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002645 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002646 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002647 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002648
Gilles Peskine8817f612018-12-18 00:18:46 +01002649 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002650
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002651 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2652 psa_set_key_algorithm( &attributes, alg );
2653 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002654
Ronald Cron5425a212020-08-04 14:58:35 +02002655 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2656 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657
Ronald Cron5425a212020-08-04 14:58:35 +02002658 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659
Steven Cooreman177deba2020-09-07 17:14:14 +02002660 if( iv->len > 0 )
2661 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002662 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002663 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002664
gabor-mezei-armceface22021-01-21 12:26:17 +01002665 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2666 TEST_ASSERT( output_buffer_size <=
2667 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002668 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002669
Gilles Peskine8817f612018-12-18 00:18:46 +01002670 PSA_ASSERT( psa_cipher_update( &operation,
2671 input->x, input->len,
2672 output, output_buffer_size,
2673 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002674 TEST_ASSERT( function_output_length <=
2675 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2676 TEST_ASSERT( function_output_length <=
2677 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002678 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002679
Gilles Peskine50e586b2018-06-08 14:28:46 +02002680 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002681 output + total_output_length,
2682 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002683 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002684 TEST_ASSERT( function_output_length <=
2685 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2686 TEST_ASSERT( function_output_length <=
2687 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002688 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002689 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002690
2691 if( expected_status == PSA_SUCCESS )
2692 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002693 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002694 ASSERT_COMPARE( expected_output->x, expected_output->len,
2695 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696 }
2697
Gilles Peskine50e586b2018-06-08 14:28:46 +02002698exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002699 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002700 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002702 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703}
2704/* END_CASE */
2705
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706/* BEGIN_CASE */
2707void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002708 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002709 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002710{
Ronald Cron5425a212020-08-04 14:58:35 +02002711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002712 psa_key_type_t key_type = key_type_arg;
2713 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002714 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002715 size_t iv_size = 16;
2716 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002717 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002718 size_t output1_size = 0;
2719 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002720 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002721 size_t output2_size = 0;
2722 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002723 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002724 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2725 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002727
Gilles Peskine8817f612018-12-18 00:18:46 +01002728 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002729
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002730 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2731 psa_set_key_algorithm( &attributes, alg );
2732 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002733
Ronald Cron5425a212020-08-04 14:58:35 +02002734 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2735 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002736
Ronald Cron5425a212020-08-04 14:58:35 +02002737 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2738 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002739
Steven Cooreman177deba2020-09-07 17:14:14 +02002740 if( alg != PSA_ALG_ECB_NO_PADDING )
2741 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002742 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2743 iv, iv_size,
2744 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002745 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002746 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2747 TEST_ASSERT( output1_size <=
2748 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002749 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002750
Gilles Peskine8817f612018-12-18 00:18:46 +01002751 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2752 output1, output1_size,
2753 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002754 TEST_ASSERT( output1_length <=
2755 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2756 TEST_ASSERT( output1_length <=
2757 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2758
Gilles Peskine8817f612018-12-18 00:18:46 +01002759 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002760 output1 + output1_length,
2761 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002762 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002763 TEST_ASSERT( function_output_length <=
2764 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2765 TEST_ASSERT( function_output_length <=
2766 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002767
Gilles Peskine048b7f02018-06-08 14:20:49 +02002768 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002769
Gilles Peskine8817f612018-12-18 00:18:46 +01002770 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002771
2772 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002773 TEST_ASSERT( output2_size <=
2774 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2775 TEST_ASSERT( output2_size <=
2776 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002777 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002778
Steven Cooreman177deba2020-09-07 17:14:14 +02002779 if( iv_length > 0 )
2780 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002781 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2782 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002783 }
2784
Gilles Peskine8817f612018-12-18 00:18:46 +01002785 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2786 output2, output2_size,
2787 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002788 TEST_ASSERT( output2_length <=
2789 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2790 TEST_ASSERT( output2_length <=
2791 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2792
Gilles Peskine048b7f02018-06-08 14:20:49 +02002793 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002794 PSA_ASSERT( psa_cipher_finish( &operation2,
2795 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002796 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002797 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002798 TEST_ASSERT( function_output_length <=
2799 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2800 TEST_ASSERT( function_output_length <=
2801 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002802
Gilles Peskine048b7f02018-06-08 14:20:49 +02002803 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002804
Gilles Peskine8817f612018-12-18 00:18:46 +01002805 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002806
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002807 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002808
2809exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002810 psa_cipher_abort( &operation1 );
2811 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002812 mbedtls_free( output1 );
2813 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002814 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002815 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002816}
2817/* END_CASE */
2818
2819/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002820void cipher_verify_output_multipart( int alg_arg,
2821 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002822 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002823 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002824 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002825{
Ronald Cron5425a212020-08-04 14:58:35 +02002826 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002827 psa_key_type_t key_type = key_type_arg;
2828 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002829 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002830 unsigned char iv[16] = {0};
2831 size_t iv_size = 16;
2832 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002833 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002834 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002835 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002836 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002837 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002838 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002839 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002840 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2841 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002842 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002843
Gilles Peskine8817f612018-12-18 00:18:46 +01002844 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002845
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002846 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2847 psa_set_key_algorithm( &attributes, alg );
2848 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002849
Ronald Cron5425a212020-08-04 14:58:35 +02002850 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2851 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002852
Ronald Cron5425a212020-08-04 14:58:35 +02002853 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2854 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002855
Steven Cooreman177deba2020-09-07 17:14:14 +02002856 if( alg != PSA_ALG_ECB_NO_PADDING )
2857 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002858 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2859 iv, iv_size,
2860 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002861 }
2862
gabor-mezei-armceface22021-01-21 12:26:17 +01002863 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2864 TEST_ASSERT( output1_buffer_size <=
2865 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002866 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002867
Gilles Peskinee0866522019-02-19 19:44:00 +01002868 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002869
Gilles Peskine8817f612018-12-18 00:18:46 +01002870 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2871 output1, output1_buffer_size,
2872 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002873 TEST_ASSERT( function_output_length <=
2874 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2875 TEST_ASSERT( function_output_length <=
2876 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002877 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002878
Gilles Peskine8817f612018-12-18 00:18:46 +01002879 PSA_ASSERT( psa_cipher_update( &operation1,
2880 input->x + first_part_size,
2881 input->len - first_part_size,
2882 output1, output1_buffer_size,
2883 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002884 TEST_ASSERT( function_output_length <=
2885 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2886 alg,
2887 input->len - first_part_size ) );
2888 TEST_ASSERT( function_output_length <=
2889 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002890 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002891
Gilles Peskine8817f612018-12-18 00:18:46 +01002892 PSA_ASSERT( psa_cipher_finish( &operation1,
2893 output1 + output1_length,
2894 output1_buffer_size - output1_length,
2895 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002896 TEST_ASSERT( function_output_length <=
2897 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2898 TEST_ASSERT( function_output_length <=
2899 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002900 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002901
Gilles Peskine8817f612018-12-18 00:18:46 +01002902 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002903
Gilles Peskine048b7f02018-06-08 14:20:49 +02002904 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002905 TEST_ASSERT( output2_buffer_size <=
2906 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2907 TEST_ASSERT( output2_buffer_size <=
2908 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002909 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002910
Steven Cooreman177deba2020-09-07 17:14:14 +02002911 if( iv_length > 0 )
2912 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002913 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2914 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002915 }
Moran Pekerded84402018-06-06 16:36:50 +03002916
Gilles Peskine8817f612018-12-18 00:18:46 +01002917 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2918 output2, output2_buffer_size,
2919 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002920 TEST_ASSERT( function_output_length <=
2921 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2922 TEST_ASSERT( function_output_length <=
2923 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002924 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002925
Gilles Peskine8817f612018-12-18 00:18:46 +01002926 PSA_ASSERT( psa_cipher_update( &operation2,
2927 output1 + first_part_size,
2928 output1_length - first_part_size,
2929 output2, output2_buffer_size,
2930 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002931 TEST_ASSERT( function_output_length <=
2932 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2933 alg,
2934 output1_length - first_part_size ) );
2935 TEST_ASSERT( function_output_length <=
2936 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002937 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002938
Gilles Peskine8817f612018-12-18 00:18:46 +01002939 PSA_ASSERT( psa_cipher_finish( &operation2,
2940 output2 + output2_length,
2941 output2_buffer_size - output2_length,
2942 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002943 TEST_ASSERT( function_output_length <=
2944 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2945 TEST_ASSERT( function_output_length <=
2946 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002947 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002948
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002950
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002951 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002952
2953exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002954 psa_cipher_abort( &operation1 );
2955 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002956 mbedtls_free( output1 );
2957 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002958 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002959 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002960}
2961/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002962
Gilles Peskine20035e32018-02-03 22:44:14 +01002963/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002964void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002965 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002966 data_t *nonce,
2967 data_t *additional_data,
2968 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002969 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002970{
Ronald Cron5425a212020-08-04 14:58:35 +02002971 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002972 psa_key_type_t key_type = key_type_arg;
2973 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002974 unsigned char *output_data = NULL;
2975 size_t output_size = 0;
2976 size_t output_length = 0;
2977 unsigned char *output_data2 = NULL;
2978 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002979 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002980 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002981 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002982
Gilles Peskine4abf7412018-06-18 16:35:34 +02002983 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002984 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2985 * should be exact. */
2986 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
2987 TEST_EQUAL( output_size,
2988 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002989 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002990
Gilles Peskine8817f612018-12-18 00:18:46 +01002991 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002992
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002993 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2994 psa_set_key_algorithm( &attributes, alg );
2995 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002996
Gilles Peskine049c7532019-05-15 20:22:09 +02002997 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002998 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002999
Ronald Cron5425a212020-08-04 14:58:35 +02003000 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003001 nonce->x, nonce->len,
3002 additional_data->x,
3003 additional_data->len,
3004 input_data->x, input_data->len,
3005 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003006 &output_length ),
3007 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003008
3009 if( PSA_SUCCESS == expected_result )
3010 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003011 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003012
Gilles Peskine003a4a92019-05-14 16:09:40 +02003013 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3014 * should be exact. */
3015 TEST_EQUAL( input_data->len,
3016 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3017
gabor-mezei-armceface22021-01-21 12:26:17 +01003018 TEST_ASSERT( input_data->len <=
3019 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3020
Ronald Cron5425a212020-08-04 14:58:35 +02003021 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003022 nonce->x, nonce->len,
3023 additional_data->x,
3024 additional_data->len,
3025 output_data, output_length,
3026 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003027 &output_length2 ),
3028 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003029
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003030 ASSERT_COMPARE( input_data->x, input_data->len,
3031 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003032 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003033
Gilles Peskinea1cac842018-06-11 19:33:02 +02003034exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003035 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003036 mbedtls_free( output_data );
3037 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003038 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003039}
3040/* END_CASE */
3041
3042/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003043void aead_encrypt( int key_type_arg, data_t *key_data,
3044 int alg_arg,
3045 data_t *nonce,
3046 data_t *additional_data,
3047 data_t *input_data,
3048 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003049{
Ronald Cron5425a212020-08-04 14:58:35 +02003050 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003051 psa_key_type_t key_type = key_type_arg;
3052 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003053 unsigned char *output_data = NULL;
3054 size_t output_size = 0;
3055 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003056 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003057 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003058 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003059
Gilles Peskine4abf7412018-06-18 16:35:34 +02003060 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003061 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3062 * should be exact. */
3063 TEST_EQUAL( output_size,
3064 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003065 TEST_ASSERT( output_size <=
3066 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003067 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003068
Gilles Peskine8817f612018-12-18 00:18:46 +01003069 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003070
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003071 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3072 psa_set_key_algorithm( &attributes, alg );
3073 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003074
Gilles Peskine049c7532019-05-15 20:22:09 +02003075 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003076 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003077
Steven Cooremand588ea12021-01-11 19:36:04 +01003078 status = psa_aead_encrypt( key, alg,
3079 nonce->x, nonce->len,
3080 additional_data->x, additional_data->len,
3081 input_data->x, input_data->len,
3082 output_data, output_size,
3083 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003084
Ronald Cron28a45ed2021-02-09 20:35:42 +01003085 /* If the operation is not supported, just skip and not fail in case the
3086 * encryption involves a common limitation of cryptography hardwares and
3087 * an alternative implementation. */
3088 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003089 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003090 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3091 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003092 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003093
3094 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003095 ASSERT_COMPARE( expected_result->x, expected_result->len,
3096 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003097
Gilles Peskinea1cac842018-06-11 19:33:02 +02003098exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003099 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003100 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003101 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003102}
3103/* END_CASE */
3104
3105/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003106void aead_decrypt( int key_type_arg, data_t *key_data,
3107 int alg_arg,
3108 data_t *nonce,
3109 data_t *additional_data,
3110 data_t *input_data,
3111 data_t *expected_data,
3112 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003113{
Ronald Cron5425a212020-08-04 14:58:35 +02003114 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003115 psa_key_type_t key_type = key_type_arg;
3116 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003117 unsigned char *output_data = NULL;
3118 size_t output_size = 0;
3119 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003120 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003121 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003122 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003123 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003124
Gilles Peskine003a4a92019-05-14 16:09:40 +02003125 output_size = input_data->len - tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003126 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
gabor-mezei-armceface22021-01-21 12:26:17 +01003127 {
3128 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3129 * should be exact. */
Gilles Peskine003a4a92019-05-14 16:09:40 +02003130 TEST_EQUAL( output_size,
3131 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003132 TEST_ASSERT( output_size <=
3133 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3134 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003135 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003136
Gilles Peskine8817f612018-12-18 00:18:46 +01003137 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003138
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003139 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3140 psa_set_key_algorithm( &attributes, alg );
3141 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003142
Gilles Peskine049c7532019-05-15 20:22:09 +02003143 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003144 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003145
Steven Cooremand588ea12021-01-11 19:36:04 +01003146 status = psa_aead_decrypt( key, alg,
3147 nonce->x, nonce->len,
3148 additional_data->x,
3149 additional_data->len,
3150 input_data->x, input_data->len,
3151 output_data, output_size,
3152 &output_length );
3153
Ronald Cron28a45ed2021-02-09 20:35:42 +01003154 /* If the operation is not supported, just skip and not fail in case the
3155 * decryption involves a common limitation of cryptography hardwares and
3156 * an alternative implementation. */
3157 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003158 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003159 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3160 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003161 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003162
3163 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003164
Gilles Peskine2d277862018-06-18 15:41:12 +02003165 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003166 ASSERT_COMPARE( expected_data->x, expected_data->len,
3167 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003168
Gilles Peskinea1cac842018-06-11 19:33:02 +02003169exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003170 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003171 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003172 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003173}
3174/* END_CASE */
3175
3176/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003177void signature_size( int type_arg,
3178 int bits,
3179 int alg_arg,
3180 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003181{
3182 psa_key_type_t type = type_arg;
3183 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003184 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003185
Gilles Peskinefe11b722018-12-18 00:24:04 +01003186 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003187#if defined(MBEDTLS_TEST_DEPRECATED)
3188 TEST_EQUAL( actual_size,
3189 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3190#endif /* MBEDTLS_TEST_DEPRECATED */
3191
Gilles Peskinee59236f2018-01-27 23:32:46 +01003192exit:
3193 ;
3194}
3195/* END_CASE */
3196
3197/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003198void sign_deterministic( int key_type_arg, data_t *key_data,
3199 int alg_arg, data_t *input_data,
3200 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003201{
Ronald Cron5425a212020-08-04 14:58:35 +02003202 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003203 psa_key_type_t key_type = key_type_arg;
3204 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003205 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003206 unsigned char *signature = NULL;
3207 size_t signature_size;
3208 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003210
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003212
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003213 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003214 psa_set_key_algorithm( &attributes, alg );
3215 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003216
Gilles Peskine049c7532019-05-15 20:22:09 +02003217 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003218 &key ) );
3219 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003220 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003221
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003222 /* Allocate a buffer which has the size advertized by the
3223 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003224 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003225 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003226 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003227 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003228 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003229
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003230 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003231 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003232 input_data->x, input_data->len,
3233 signature, signature_size,
3234 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003235 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003236 ASSERT_COMPARE( output_data->x, output_data->len,
3237 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003238
Gilles Peskine0627f982019-11-26 19:12:16 +01003239#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003240 memset( signature, 0, signature_size );
3241 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003242 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003243 input_data->x, input_data->len,
3244 signature, signature_size,
3245 &signature_length ) );
3246 ASSERT_COMPARE( output_data->x, output_data->len,
3247 signature, signature_length );
3248#endif /* MBEDTLS_TEST_DEPRECATED */
3249
Gilles Peskine20035e32018-02-03 22:44:14 +01003250exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003251 /*
3252 * Key attributes may have been returned by psa_get_key_attributes()
3253 * thus reset them as required.
3254 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003255 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003256
Ronald Cron5425a212020-08-04 14:58:35 +02003257 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003258 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003259 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003260}
3261/* END_CASE */
3262
3263/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003264void sign_fail( int key_type_arg, data_t *key_data,
3265 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003266 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003267{
Ronald Cron5425a212020-08-04 14:58:35 +02003268 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003269 psa_key_type_t key_type = key_type_arg;
3270 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003271 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003272 psa_status_t actual_status;
3273 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003274 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003275 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003277
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003278 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003279
Gilles Peskine8817f612018-12-18 00:18:46 +01003280 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003281
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003282 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003283 psa_set_key_algorithm( &attributes, alg );
3284 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003285
Gilles Peskine049c7532019-05-15 20:22:09 +02003286 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003287 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003288
Ronald Cron5425a212020-08-04 14:58:35 +02003289 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003290 input_data->x, input_data->len,
3291 signature, signature_size,
3292 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003293 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003294 /* The value of *signature_length is unspecified on error, but
3295 * whatever it is, it should be less than signature_size, so that
3296 * if the caller tries to read *signature_length bytes without
3297 * checking the error code then they don't overflow a buffer. */
3298 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003299
Gilles Peskine895242b2019-11-29 12:15:40 +01003300#if defined(MBEDTLS_TEST_DEPRECATED)
3301 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003302 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003303 input_data->x, input_data->len,
3304 signature, signature_size,
3305 &signature_length ),
3306 expected_status );
3307 TEST_ASSERT( signature_length <= signature_size );
3308#endif /* MBEDTLS_TEST_DEPRECATED */
3309
Gilles Peskine20035e32018-02-03 22:44:14 +01003310exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003311 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003312 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003313 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003314 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003315}
3316/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003317
3318/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003319void sign_verify( int key_type_arg, data_t *key_data,
3320 int alg_arg, data_t *input_data )
3321{
Ronald Cron5425a212020-08-04 14:58:35 +02003322 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003323 psa_key_type_t key_type = key_type_arg;
3324 psa_algorithm_t alg = alg_arg;
3325 size_t key_bits;
3326 unsigned char *signature = NULL;
3327 size_t signature_size;
3328 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003330
Gilles Peskine8817f612018-12-18 00:18:46 +01003331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003332
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003333 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003334 psa_set_key_algorithm( &attributes, alg );
3335 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003336
Gilles Peskine049c7532019-05-15 20:22:09 +02003337 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003338 &key ) );
3339 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003340 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003341
3342 /* Allocate a buffer which has the size advertized by the
3343 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003344 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003345 key_bits, alg );
3346 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003347 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003348 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003349
3350 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003351 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003352 input_data->x, input_data->len,
3353 signature, signature_size,
3354 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003355 /* Check that the signature length looks sensible. */
3356 TEST_ASSERT( signature_length <= signature_size );
3357 TEST_ASSERT( signature_length > 0 );
3358
3359 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003360 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003361 input_data->x, input_data->len,
3362 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003363
3364 if( input_data->len != 0 )
3365 {
3366 /* Flip a bit in the input and verify that the signature is now
3367 * detected as invalid. Flip a bit at the beginning, not at the end,
3368 * because ECDSA may ignore the last few bits of the input. */
3369 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003370 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003371 input_data->x, input_data->len,
3372 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003373 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003374 }
3375
3376exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003377 /*
3378 * Key attributes may have been returned by psa_get_key_attributes()
3379 * thus reset them as required.
3380 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003381 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003382
Ronald Cron5425a212020-08-04 14:58:35 +02003383 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003384 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003385 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003386}
3387/* END_CASE */
3388
3389/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003390void asymmetric_verify( int key_type_arg, data_t *key_data,
3391 int alg_arg, data_t *hash_data,
3392 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003393{
Ronald Cron5425a212020-08-04 14:58:35 +02003394 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003395 psa_key_type_t key_type = key_type_arg;
3396 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003398
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003399 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003400
Gilles Peskine8817f612018-12-18 00:18:46 +01003401 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003402
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003403 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003404 psa_set_key_algorithm( &attributes, alg );
3405 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003406
Gilles Peskine049c7532019-05-15 20:22:09 +02003407 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003408 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003409
Ronald Cron5425a212020-08-04 14:58:35 +02003410 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003411 hash_data->x, hash_data->len,
3412 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003413
3414#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003415 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003416 hash_data->x, hash_data->len,
3417 signature_data->x,
3418 signature_data->len ) );
3419
3420#endif /* MBEDTLS_TEST_DEPRECATED */
3421
itayzafrir5c753392018-05-08 11:18:38 +03003422exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003423 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003424 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003425 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003426}
3427/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003428
3429/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003430void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3431 int alg_arg, data_t *hash_data,
3432 data_t *signature_data,
3433 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003434{
Ronald Cron5425a212020-08-04 14:58:35 +02003435 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003436 psa_key_type_t key_type = key_type_arg;
3437 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003438 psa_status_t actual_status;
3439 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003440 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003441
Gilles Peskine8817f612018-12-18 00:18:46 +01003442 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003443
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003444 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003445 psa_set_key_algorithm( &attributes, alg );
3446 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003447
Gilles Peskine049c7532019-05-15 20:22:09 +02003448 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003449 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003450
Ronald Cron5425a212020-08-04 14:58:35 +02003451 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003452 hash_data->x, hash_data->len,
3453 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003454 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003455
Gilles Peskine895242b2019-11-29 12:15:40 +01003456#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003457 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003458 hash_data->x, hash_data->len,
3459 signature_data->x, signature_data->len ),
3460 expected_status );
3461#endif /* MBEDTLS_TEST_DEPRECATED */
3462
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003463exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003464 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003465 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003466 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003467}
3468/* END_CASE */
3469
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003470/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003471void asymmetric_encrypt( int key_type_arg,
3472 data_t *key_data,
3473 int alg_arg,
3474 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003475 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003476 int expected_output_length_arg,
3477 int expected_status_arg )
3478{
Ronald Cron5425a212020-08-04 14:58:35 +02003479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003480 psa_key_type_t key_type = key_type_arg;
3481 psa_algorithm_t alg = alg_arg;
3482 size_t expected_output_length = expected_output_length_arg;
3483 size_t key_bits;
3484 unsigned char *output = NULL;
3485 size_t output_size;
3486 size_t output_length = ~0;
3487 psa_status_t actual_status;
3488 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003490
Gilles Peskine8817f612018-12-18 00:18:46 +01003491 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003492
Gilles Peskine656896e2018-06-29 19:12:28 +02003493 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003494 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3495 psa_set_key_algorithm( &attributes, alg );
3496 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003497 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003498 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003499
3500 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003501 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003502 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003503
Gilles Peskine656896e2018-06-29 19:12:28 +02003504 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003505 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003506 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003507
3508 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003509 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003510 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003511 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003512 output, output_size,
3513 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003514 TEST_EQUAL( actual_status, expected_status );
3515 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003516
Gilles Peskine68428122018-06-30 18:42:41 +02003517 /* If the label is empty, the test framework puts a non-null pointer
3518 * in label->x. Test that a null pointer works as well. */
3519 if( label->len == 0 )
3520 {
3521 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003522 if( output_size != 0 )
3523 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003524 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003525 input_data->x, input_data->len,
3526 NULL, label->len,
3527 output, output_size,
3528 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003529 TEST_EQUAL( actual_status, expected_status );
3530 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003531 }
3532
Gilles Peskine656896e2018-06-29 19:12:28 +02003533exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003534 /*
3535 * Key attributes may have been returned by psa_get_key_attributes()
3536 * thus reset them as required.
3537 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003538 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003539
Ronald Cron5425a212020-08-04 14:58:35 +02003540 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003541 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003542 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003543}
3544/* END_CASE */
3545
3546/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003547void asymmetric_encrypt_decrypt( int key_type_arg,
3548 data_t *key_data,
3549 int alg_arg,
3550 data_t *input_data,
3551 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003552{
Ronald Cron5425a212020-08-04 14:58:35 +02003553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003554 psa_key_type_t key_type = key_type_arg;
3555 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003556 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003557 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003558 size_t output_size;
3559 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003560 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003561 size_t output2_size;
3562 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003564
Gilles Peskine8817f612018-12-18 00:18:46 +01003565 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003566
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003567 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3568 psa_set_key_algorithm( &attributes, alg );
3569 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003570
Gilles Peskine049c7532019-05-15 20:22:09 +02003571 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003572 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003573
3574 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003575 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003576 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003577
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003578 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003579 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003580 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003581
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003582 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003583 TEST_ASSERT( output2_size <=
3584 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3585 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003586 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003587
Gilles Peskineeebd7382018-06-08 18:11:54 +02003588 /* We test encryption by checking that encrypt-then-decrypt gives back
3589 * the original plaintext because of the non-optional random
3590 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003591 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003592 input_data->x, input_data->len,
3593 label->x, label->len,
3594 output, output_size,
3595 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003596 /* We don't know what ciphertext length to expect, but check that
3597 * it looks sensible. */
3598 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003599
Ronald Cron5425a212020-08-04 14:58:35 +02003600 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003601 output, output_length,
3602 label->x, label->len,
3603 output2, output2_size,
3604 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003605 ASSERT_COMPARE( input_data->x, input_data->len,
3606 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003607
3608exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003609 /*
3610 * Key attributes may have been returned by psa_get_key_attributes()
3611 * thus reset them as required.
3612 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003613 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003614
Ronald Cron5425a212020-08-04 14:58:35 +02003615 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003616 mbedtls_free( output );
3617 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003618 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003619}
3620/* END_CASE */
3621
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003622/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003623void asymmetric_decrypt( int key_type_arg,
3624 data_t *key_data,
3625 int alg_arg,
3626 data_t *input_data,
3627 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003628 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003629{
Ronald Cron5425a212020-08-04 14:58:35 +02003630 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003631 psa_key_type_t key_type = key_type_arg;
3632 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003633 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003635 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003636 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003637 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003638
Gilles Peskine8817f612018-12-18 00:18:46 +01003639 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003640
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003641 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3642 psa_set_key_algorithm( &attributes, alg );
3643 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003644
Gilles Peskine049c7532019-05-15 20:22:09 +02003645 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003646 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003647
gabor-mezei-armceface22021-01-21 12:26:17 +01003648 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3649 key_bits = psa_get_key_bits( &attributes );
3650
3651 /* Determine the maximum ciphertext length */
3652 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3653 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3654 ASSERT_ALLOC( output, output_size );
3655
Ronald Cron5425a212020-08-04 14:58:35 +02003656 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003657 input_data->x, input_data->len,
3658 label->x, label->len,
3659 output,
3660 output_size,
3661 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003662 ASSERT_COMPARE( expected_data->x, expected_data->len,
3663 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664
Gilles Peskine68428122018-06-30 18:42:41 +02003665 /* If the label is empty, the test framework puts a non-null pointer
3666 * in label->x. Test that a null pointer works as well. */
3667 if( label->len == 0 )
3668 {
3669 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003670 if( output_size != 0 )
3671 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003672 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003673 input_data->x, input_data->len,
3674 NULL, label->len,
3675 output,
3676 output_size,
3677 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003678 ASSERT_COMPARE( expected_data->x, expected_data->len,
3679 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003680 }
3681
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003682exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003683 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003684 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003685 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003686 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003687}
3688/* END_CASE */
3689
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003690/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003691void asymmetric_decrypt_fail( int key_type_arg,
3692 data_t *key_data,
3693 int alg_arg,
3694 data_t *input_data,
3695 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003696 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003697 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003698{
Ronald Cron5425a212020-08-04 14:58:35 +02003699 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003700 psa_key_type_t key_type = key_type_arg;
3701 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003702 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003703 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003704 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003705 psa_status_t actual_status;
3706 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003708
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003709 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003710
Gilles Peskine8817f612018-12-18 00:18:46 +01003711 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003712
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003713 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3714 psa_set_key_algorithm( &attributes, alg );
3715 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003716
Gilles Peskine049c7532019-05-15 20:22:09 +02003717 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003718 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003719
Ronald Cron5425a212020-08-04 14:58:35 +02003720 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003721 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003722 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003723 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003724 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003725 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003726 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727
Gilles Peskine68428122018-06-30 18:42:41 +02003728 /* If the label is empty, the test framework puts a non-null pointer
3729 * in label->x. Test that a null pointer works as well. */
3730 if( label->len == 0 )
3731 {
3732 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003733 if( output_size != 0 )
3734 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003735 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003736 input_data->x, input_data->len,
3737 NULL, label->len,
3738 output, output_size,
3739 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003740 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003741 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003742 }
3743
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003744exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003745 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003746 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02003747 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003748 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003749}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003750/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003751
3752/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003753void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00003754{
3755 /* Test each valid way of initializing the object, except for `= {0}`, as
3756 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3757 * though it's OK by the C standard. We could test for this, but we'd need
3758 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003759 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003760 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
3761 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
3762 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003763
3764 memset( &zero, 0, sizeof( zero ) );
3765
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003766 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003767 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003768 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003769 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003770 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003771 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003772 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003773
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003774 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003775 PSA_ASSERT( psa_key_derivation_abort(&func) );
3776 PSA_ASSERT( psa_key_derivation_abort(&init) );
3777 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003778}
3779/* END_CASE */
3780
Janos Follath16de4a42019-06-13 16:32:24 +01003781/* BEGIN_CASE */
3782void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003783{
Gilles Peskineea0fb492018-07-12 17:17:20 +02003784 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003785 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003786 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003787
Gilles Peskine8817f612018-12-18 00:18:46 +01003788 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003789
Janos Follath16de4a42019-06-13 16:32:24 +01003790 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003791 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003792
3793exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003794 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003795 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003796}
3797/* END_CASE */
3798
Janos Follathaf3c2a02019-06-12 12:34:34 +01003799/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01003800void derive_set_capacity( int alg_arg, int capacity_arg,
3801 int expected_status_arg )
3802{
3803 psa_algorithm_t alg = alg_arg;
3804 size_t capacity = capacity_arg;
3805 psa_status_t expected_status = expected_status_arg;
3806 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3807
3808 PSA_ASSERT( psa_crypto_init( ) );
3809
3810 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3811
3812 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
3813 expected_status );
3814
3815exit:
3816 psa_key_derivation_abort( &operation );
3817 PSA_DONE( );
3818}
3819/* END_CASE */
3820
3821/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01003822void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02003823 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003824 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02003825 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003826 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02003827 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003828 int expected_status_arg3,
3829 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003830{
3831 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02003832 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
3833 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01003834 psa_status_t expected_statuses[] = {expected_status_arg1,
3835 expected_status_arg2,
3836 expected_status_arg3};
3837 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003838 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3839 MBEDTLS_SVC_KEY_ID_INIT,
3840 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01003841 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3842 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3843 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003844 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003845 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003846 psa_status_t expected_output_status = expected_output_status_arg;
3847 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01003848
3849 PSA_ASSERT( psa_crypto_init( ) );
3850
3851 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3852 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003853
3854 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3855
3856 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
3857 {
Gilles Peskineb8965192019-09-24 16:21:10 +02003858 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003859 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02003860 psa_set_key_type( &attributes, key_types[i] );
3861 PSA_ASSERT( psa_import_key( &attributes,
3862 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003863 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003864 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
3865 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
3866 {
3867 // When taking a private key as secret input, use key agreement
3868 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003869 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
3870 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003871 expected_statuses[i] );
3872 }
3873 else
3874 {
3875 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02003876 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003877 expected_statuses[i] );
3878 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02003879 }
3880 else
3881 {
3882 TEST_EQUAL( psa_key_derivation_input_bytes(
3883 &operation, steps[i],
3884 inputs[i]->x, inputs[i]->len ),
3885 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003886 }
3887 }
3888
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003889 if( output_key_type != PSA_KEY_TYPE_NONE )
3890 {
3891 psa_reset_key_attributes( &attributes );
3892 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
3893 psa_set_key_bits( &attributes, 8 );
3894 actual_output_status =
3895 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02003896 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003897 }
3898 else
3899 {
3900 uint8_t buffer[1];
3901 actual_output_status =
3902 psa_key_derivation_output_bytes( &operation,
3903 buffer, sizeof( buffer ) );
3904 }
3905 TEST_EQUAL( actual_output_status, expected_output_status );
3906
Janos Follathaf3c2a02019-06-12 12:34:34 +01003907exit:
3908 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003909 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3910 psa_destroy_key( keys[i] );
3911 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003912 PSA_DONE( );
3913}
3914/* END_CASE */
3915
Janos Follathd958bb72019-07-03 15:02:16 +01003916/* BEGIN_CASE */
3917void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003918{
Janos Follathd958bb72019-07-03 15:02:16 +01003919 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003920 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003921 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003922 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01003923 unsigned char input1[] = "Input 1";
3924 size_t input1_length = sizeof( input1 );
3925 unsigned char input2[] = "Input 2";
3926 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003927 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003928 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003929 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3930 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3931 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003933
Gilles Peskine8817f612018-12-18 00:18:46 +01003934 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003935
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003936 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3937 psa_set_key_algorithm( &attributes, alg );
3938 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003939
Gilles Peskine73676cb2019-05-15 20:15:10 +02003940 PSA_ASSERT( psa_import_key( &attributes,
3941 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02003942 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003943
3944 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003945 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
3946 input1, input1_length,
3947 input2, input2_length,
3948 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01003949 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003950
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003951 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01003952 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003953 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003954
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003955 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003956
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003957 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02003958 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003959
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003960exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003961 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003962 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003963 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003964}
3965/* END_CASE */
3966
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003967/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003968void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003969{
3970 uint8_t output_buffer[16];
3971 size_t buffer_size = 16;
3972 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003973 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003974
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003975 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3976 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003977 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003978
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003979 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003980 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003981
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003982 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003983
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003984 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3985 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003986 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003987
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003988 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003989 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003990
3991exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003992 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003993}
3994/* END_CASE */
3995
3996/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003997void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02003998 int step1_arg, data_t *input1,
3999 int step2_arg, data_t *input2,
4000 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004001 int requested_capacity_arg,
4002 data_t *expected_output1,
4003 data_t *expected_output2 )
4004{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004005 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004006 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4007 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004008 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4009 MBEDTLS_SVC_KEY_ID_INIT,
4010 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004011 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004012 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004013 uint8_t *expected_outputs[2] =
4014 {expected_output1->x, expected_output2->x};
4015 size_t output_sizes[2] =
4016 {expected_output1->len, expected_output2->len};
4017 size_t output_buffer_size = 0;
4018 uint8_t *output_buffer = NULL;
4019 size_t expected_capacity;
4020 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004022 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004023 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004024
4025 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4026 {
4027 if( output_sizes[i] > output_buffer_size )
4028 output_buffer_size = output_sizes[i];
4029 if( output_sizes[i] == 0 )
4030 expected_outputs[i] = NULL;
4031 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004032 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004033 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004034
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004035 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4036 psa_set_key_algorithm( &attributes, alg );
4037 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004038
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004039 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004040 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4041 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4042 requested_capacity ) );
4043 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004044 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004045 switch( steps[i] )
4046 {
4047 case 0:
4048 break;
4049 case PSA_KEY_DERIVATION_INPUT_SECRET:
4050 PSA_ASSERT( psa_import_key( &attributes,
4051 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004052 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004053
4054 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4055 {
4056 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4057 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4058 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4059 }
4060
Gilles Peskine1468da72019-05-29 17:35:49 +02004061 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004062 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004063 break;
4064 default:
4065 PSA_ASSERT( psa_key_derivation_input_bytes(
4066 &operation, steps[i],
4067 inputs[i]->x, inputs[i]->len ) );
4068 break;
4069 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004070 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004071
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004072 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004073 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004074 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004075 expected_capacity = requested_capacity;
4076
4077 /* Expansion phase. */
4078 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4079 {
4080 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004081 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004082 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004083 if( expected_capacity == 0 && output_sizes[i] == 0 )
4084 {
4085 /* Reading 0 bytes when 0 bytes are available can go either way. */
4086 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004087 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004088 continue;
4089 }
4090 else if( expected_capacity == 0 ||
4091 output_sizes[i] > expected_capacity )
4092 {
4093 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004094 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004095 expected_capacity = 0;
4096 continue;
4097 }
4098 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004099 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004100 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004101 ASSERT_COMPARE( output_buffer, output_sizes[i],
4102 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004103 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004104 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004105 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004106 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004107 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004108 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004109 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004110
4111exit:
4112 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004113 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004114 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4115 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004116 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004117}
4118/* END_CASE */
4119
4120/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004121void derive_full( int alg_arg,
4122 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004123 data_t *input1,
4124 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004125 int requested_capacity_arg )
4126{
Ronald Cron5425a212020-08-04 14:58:35 +02004127 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004128 psa_algorithm_t alg = alg_arg;
4129 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004130 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004131 unsigned char output_buffer[16];
4132 size_t expected_capacity = requested_capacity;
4133 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004135
Gilles Peskine8817f612018-12-18 00:18:46 +01004136 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004137
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4139 psa_set_key_algorithm( &attributes, alg );
4140 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004141
Gilles Peskine049c7532019-05-15 20:22:09 +02004142 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004143 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004144
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004145 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4146 input1->x, input1->len,
4147 input2->x, input2->len,
4148 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004149 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004150
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004151 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004152 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004153 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004154
4155 /* Expansion phase. */
4156 while( current_capacity > 0 )
4157 {
4158 size_t read_size = sizeof( output_buffer );
4159 if( read_size > current_capacity )
4160 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004161 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004162 output_buffer,
4163 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004164 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004165 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004166 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004167 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004168 }
4169
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004170 /* Check that the operation refuses to go over capacity. */
4171 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004172 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004173
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004174 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004175
4176exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004177 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004178 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004179 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004180}
4181/* END_CASE */
4182
Janos Follathe60c9052019-07-03 13:51:30 +01004183/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004184void derive_key_exercise( int alg_arg,
4185 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004186 data_t *input1,
4187 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004188 int derived_type_arg,
4189 int derived_bits_arg,
4190 int derived_usage_arg,
4191 int derived_alg_arg )
4192{
Ronald Cron5425a212020-08-04 14:58:35 +02004193 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4194 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004195 psa_algorithm_t alg = alg_arg;
4196 psa_key_type_t derived_type = derived_type_arg;
4197 size_t derived_bits = derived_bits_arg;
4198 psa_key_usage_t derived_usage = derived_usage_arg;
4199 psa_algorithm_t derived_alg = derived_alg_arg;
4200 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004201 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004202 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004203 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004204
Gilles Peskine8817f612018-12-18 00:18:46 +01004205 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004206
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4208 psa_set_key_algorithm( &attributes, alg );
4209 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004210 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004211 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004212
4213 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004214 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4215 input1->x, input1->len,
4216 input2->x, input2->len,
4217 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004218 goto exit;
4219
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004220 psa_set_key_usage_flags( &attributes, derived_usage );
4221 psa_set_key_algorithm( &attributes, derived_alg );
4222 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004223 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004224 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004225 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004226
4227 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004228 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004229 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4230 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004231
4232 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004233 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004234 goto exit;
4235
4236exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004237 /*
4238 * Key attributes may have been returned by psa_get_key_attributes()
4239 * thus reset them as required.
4240 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004241 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004242
4243 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004244 psa_destroy_key( base_key );
4245 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004246 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004247}
4248/* END_CASE */
4249
Janos Follath42fd8882019-07-03 14:17:09 +01004250/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004251void derive_key_export( int alg_arg,
4252 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004253 data_t *input1,
4254 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004255 int bytes1_arg,
4256 int bytes2_arg )
4257{
Ronald Cron5425a212020-08-04 14:58:35 +02004258 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4259 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004260 psa_algorithm_t alg = alg_arg;
4261 size_t bytes1 = bytes1_arg;
4262 size_t bytes2 = bytes2_arg;
4263 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004264 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004265 uint8_t *output_buffer = NULL;
4266 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004267 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4268 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004269 size_t length;
4270
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004271 ASSERT_ALLOC( output_buffer, capacity );
4272 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004273 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004274
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004275 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4276 psa_set_key_algorithm( &base_attributes, alg );
4277 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004278 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004279 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004280
4281 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004282 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4283 input1->x, input1->len,
4284 input2->x, input2->len,
4285 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004286 goto exit;
4287
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004288 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004289 output_buffer,
4290 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004291 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004292
4293 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004294 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4295 input1->x, input1->len,
4296 input2->x, input2->len,
4297 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004298 goto exit;
4299
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004300 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4301 psa_set_key_algorithm( &derived_attributes, 0 );
4302 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004303 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004304 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004305 &derived_key ) );
4306 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004307 export_buffer, bytes1,
4308 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004309 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004310 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004311 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004312 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004313 &derived_key ) );
4314 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004315 export_buffer + bytes1, bytes2,
4316 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004317 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004318
4319 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004320 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4321 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004322
4323exit:
4324 mbedtls_free( output_buffer );
4325 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004326 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004327 psa_destroy_key( base_key );
4328 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004329 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004330}
4331/* END_CASE */
4332
4333/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004334void derive_key( int alg_arg,
4335 data_t *key_data, data_t *input1, data_t *input2,
4336 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004337 int expected_status_arg,
4338 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004339{
Ronald Cron5425a212020-08-04 14:58:35 +02004340 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4341 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004342 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004343 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004344 size_t bits = bits_arg;
4345 psa_status_t expected_status = expected_status_arg;
4346 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4347 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4348 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4349
4350 PSA_ASSERT( psa_crypto_init( ) );
4351
4352 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4353 psa_set_key_algorithm( &base_attributes, alg );
4354 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4355 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004356 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004357
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004358 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4359 input1->x, input1->len,
4360 input2->x, input2->len,
4361 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004362 goto exit;
4363
4364 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4365 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004366 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004367 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004368
4369 psa_status_t status =
4370 psa_key_derivation_output_key( &derived_attributes,
4371 &operation,
4372 &derived_key );
4373 if( is_large_output > 0 )
4374 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4375 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004376
4377exit:
4378 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004379 psa_destroy_key( base_key );
4380 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004381 PSA_DONE( );
4382}
4383/* END_CASE */
4384
4385/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004386void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004387 int our_key_type_arg, int our_key_alg_arg,
4388 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004389 int expected_status_arg )
4390{
Ronald Cron5425a212020-08-04 14:58:35 +02004391 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004392 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004393 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004394 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004395 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004397 psa_status_t expected_status = expected_status_arg;
4398 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004399
Gilles Peskine8817f612018-12-18 00:18:46 +01004400 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004401
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004402 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004403 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004404 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004405 PSA_ASSERT( psa_import_key( &attributes,
4406 our_key_data->x, our_key_data->len,
4407 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004408
Gilles Peskine77f40d82019-04-11 21:27:06 +02004409 /* The tests currently include inputs that should fail at either step.
4410 * Test cases that fail at the setup step should be changed to call
4411 * key_derivation_setup instead, and this function should be renamed
4412 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004413 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004414 if( status == PSA_SUCCESS )
4415 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004416 TEST_EQUAL( psa_key_derivation_key_agreement(
4417 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4418 our_key,
4419 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004420 expected_status );
4421 }
4422 else
4423 {
4424 TEST_ASSERT( status == expected_status );
4425 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004426
4427exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004429 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004430 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004431}
4432/* END_CASE */
4433
4434/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004435void raw_key_agreement( int alg_arg,
4436 int our_key_type_arg, data_t *our_key_data,
4437 data_t *peer_key_data,
4438 data_t *expected_output )
4439{
Ronald Cron5425a212020-08-04 14:58:35 +02004440 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004441 psa_algorithm_t alg = alg_arg;
4442 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004444 unsigned char *output = NULL;
4445 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004446 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004447
4448 ASSERT_ALLOC( output, expected_output->len );
4449 PSA_ASSERT( psa_crypto_init( ) );
4450
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4452 psa_set_key_algorithm( &attributes, alg );
4453 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004454 PSA_ASSERT( psa_import_key( &attributes,
4455 our_key_data->x, our_key_data->len,
4456 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004457
gabor-mezei-armceface22021-01-21 12:26:17 +01004458 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4459 key_bits = psa_get_key_bits( &attributes );
4460
Gilles Peskinebe697d82019-05-16 18:00:41 +02004461 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4462 peer_key_data->x, peer_key_data->len,
4463 output, expected_output->len,
4464 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004465 ASSERT_COMPARE( output, output_length,
4466 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004467 TEST_ASSERT( output_length <=
4468 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4469 TEST_ASSERT( output_length <=
4470 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004471
4472exit:
4473 mbedtls_free( output );
4474 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004475 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004476}
4477/* END_CASE */
4478
4479/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004480void key_agreement_capacity( int alg_arg,
4481 int our_key_type_arg, data_t *our_key_data,
4482 data_t *peer_key_data,
4483 int expected_capacity_arg )
4484{
Ronald Cron5425a212020-08-04 14:58:35 +02004485 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004486 psa_algorithm_t alg = alg_arg;
4487 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004490 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004491 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004492
Gilles Peskine8817f612018-12-18 00:18:46 +01004493 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004494
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004495 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4496 psa_set_key_algorithm( &attributes, alg );
4497 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004498 PSA_ASSERT( psa_import_key( &attributes,
4499 our_key_data->x, our_key_data->len,
4500 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004501
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004502 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004503 PSA_ASSERT( psa_key_derivation_key_agreement(
4504 &operation,
4505 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4506 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004507 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4508 {
4509 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004510 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004511 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004512 NULL, 0 ) );
4513 }
Gilles Peskine59685592018-09-18 12:11:34 +02004514
Gilles Peskinebf491972018-10-25 22:36:12 +02004515 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004516 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004517 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004518 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004519
Gilles Peskinebf491972018-10-25 22:36:12 +02004520 /* Test the actual capacity by reading the output. */
4521 while( actual_capacity > sizeof( output ) )
4522 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004523 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004524 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004525 actual_capacity -= sizeof( output );
4526 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004527 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004528 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004529 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004530 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004531
Gilles Peskine59685592018-09-18 12:11:34 +02004532exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004533 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004534 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004535 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004536}
4537/* END_CASE */
4538
4539/* BEGIN_CASE */
4540void key_agreement_output( int alg_arg,
4541 int our_key_type_arg, data_t *our_key_data,
4542 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004543 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004544{
Ronald Cron5425a212020-08-04 14:58:35 +02004545 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004546 psa_algorithm_t alg = alg_arg;
4547 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004548 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004549 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004550 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004551
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004552 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4553 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004554
Gilles Peskine8817f612018-12-18 00:18:46 +01004555 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004556
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004557 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4558 psa_set_key_algorithm( &attributes, alg );
4559 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004560 PSA_ASSERT( psa_import_key( &attributes,
4561 our_key_data->x, our_key_data->len,
4562 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004563
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004564 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004565 PSA_ASSERT( psa_key_derivation_key_agreement(
4566 &operation,
4567 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4568 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004569 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4570 {
4571 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004572 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004573 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004574 NULL, 0 ) );
4575 }
Gilles Peskine59685592018-09-18 12:11:34 +02004576
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004578 actual_output,
4579 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004580 ASSERT_COMPARE( actual_output, expected_output1->len,
4581 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004582 if( expected_output2->len != 0 )
4583 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004584 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004585 actual_output,
4586 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004587 ASSERT_COMPARE( actual_output, expected_output2->len,
4588 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004589 }
Gilles Peskine59685592018-09-18 12:11:34 +02004590
4591exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004592 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004593 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004594 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004595 mbedtls_free( actual_output );
4596}
4597/* END_CASE */
4598
4599/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004600void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004601{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004602 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004603 unsigned char *output = NULL;
4604 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004605 size_t i;
4606 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004607
Simon Butcher49f8e312020-03-03 15:51:50 +00004608 TEST_ASSERT( bytes_arg >= 0 );
4609
Gilles Peskine91892022021-02-08 19:50:26 +01004610 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004611 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004612
Gilles Peskine8817f612018-12-18 00:18:46 +01004613 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004614
Gilles Peskinea50d7392018-06-21 10:22:13 +02004615 /* Run several times, to ensure that every output byte will be
4616 * nonzero at least once with overwhelming probability
4617 * (2^(-8*number_of_runs)). */
4618 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004619 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004620 if( bytes != 0 )
4621 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004622 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004623
Gilles Peskinea50d7392018-06-21 10:22:13 +02004624 for( i = 0; i < bytes; i++ )
4625 {
4626 if( output[i] != 0 )
4627 ++changed[i];
4628 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004629 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004630
4631 /* Check that every byte was changed to nonzero at least once. This
4632 * validates that psa_generate_random is overwriting every byte of
4633 * the output buffer. */
4634 for( i = 0; i < bytes; i++ )
4635 {
4636 TEST_ASSERT( changed[i] != 0 );
4637 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004638
4639exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004640 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004641 mbedtls_free( output );
4642 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004643}
4644/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004645
4646/* BEGIN_CASE */
4647void generate_key( int type_arg,
4648 int bits_arg,
4649 int usage_arg,
4650 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004651 int expected_status_arg,
4652 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004653{
Ronald Cron5425a212020-08-04 14:58:35 +02004654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004655 psa_key_type_t type = type_arg;
4656 psa_key_usage_t usage = usage_arg;
4657 size_t bits = bits_arg;
4658 psa_algorithm_t alg = alg_arg;
4659 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004661 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004662
Gilles Peskine8817f612018-12-18 00:18:46 +01004663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004664
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004665 psa_set_key_usage_flags( &attributes, usage );
4666 psa_set_key_algorithm( &attributes, alg );
4667 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004668 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004669
4670 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004671 psa_status_t status = psa_generate_key( &attributes, &key );
4672
4673 if( is_large_key > 0 )
4674 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4675 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004676 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004677 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004678
4679 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004680 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004681 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4682 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004683
Gilles Peskine818ca122018-06-20 18:16:48 +02004684 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004685 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004686 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004687
4688exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004689 /*
4690 * Key attributes may have been returned by psa_get_key_attributes()
4691 * thus reset them as required.
4692 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004693 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004694
Ronald Cron5425a212020-08-04 14:58:35 +02004695 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004696 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004697}
4698/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004699
Gilles Peskinee56e8782019-04-26 17:34:02 +02004700/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4701void generate_key_rsa( int bits_arg,
4702 data_t *e_arg,
4703 int expected_status_arg )
4704{
Ronald Cron5425a212020-08-04 14:58:35 +02004705 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004706 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004707 size_t bits = bits_arg;
4708 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4709 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4710 psa_status_t expected_status = expected_status_arg;
4711 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4712 uint8_t *exported = NULL;
4713 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004714 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004715 size_t exported_length = SIZE_MAX;
4716 uint8_t *e_read_buffer = NULL;
4717 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004718 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004719 size_t e_read_length = SIZE_MAX;
4720
4721 if( e_arg->len == 0 ||
4722 ( e_arg->len == 3 &&
4723 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4724 {
4725 is_default_public_exponent = 1;
4726 e_read_size = 0;
4727 }
4728 ASSERT_ALLOC( e_read_buffer, e_read_size );
4729 ASSERT_ALLOC( exported, exported_size );
4730
4731 PSA_ASSERT( psa_crypto_init( ) );
4732
4733 psa_set_key_usage_flags( &attributes, usage );
4734 psa_set_key_algorithm( &attributes, alg );
4735 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4736 e_arg->x, e_arg->len ) );
4737 psa_set_key_bits( &attributes, bits );
4738
4739 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004740 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004741 if( expected_status != PSA_SUCCESS )
4742 goto exit;
4743
4744 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004745 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004746 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4747 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4748 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4749 e_read_buffer, e_read_size,
4750 &e_read_length ) );
4751 if( is_default_public_exponent )
4752 TEST_EQUAL( e_read_length, 0 );
4753 else
4754 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4755
4756 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004757 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02004758 goto exit;
4759
4760 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02004761 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02004762 exported, exported_size,
4763 &exported_length ) );
4764 {
4765 uint8_t *p = exported;
4766 uint8_t *end = exported + exported_length;
4767 size_t len;
4768 /* RSAPublicKey ::= SEQUENCE {
4769 * modulus INTEGER, -- n
4770 * publicExponent INTEGER } -- e
4771 */
4772 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004773 MBEDTLS_ASN1_SEQUENCE |
4774 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01004775 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004776 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4777 MBEDTLS_ASN1_INTEGER ) );
4778 if( len >= 1 && p[0] == 0 )
4779 {
4780 ++p;
4781 --len;
4782 }
4783 if( e_arg->len == 0 )
4784 {
4785 TEST_EQUAL( len, 3 );
4786 TEST_EQUAL( p[0], 1 );
4787 TEST_EQUAL( p[1], 0 );
4788 TEST_EQUAL( p[2], 1 );
4789 }
4790 else
4791 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4792 }
4793
4794exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004795 /*
4796 * Key attributes may have been returned by psa_get_key_attributes() or
4797 * set by psa_set_key_domain_parameters() thus reset them as required.
4798 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004799 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004800
Ronald Cron5425a212020-08-04 14:58:35 +02004801 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004802 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004803 mbedtls_free( e_read_buffer );
4804 mbedtls_free( exported );
4805}
4806/* END_CASE */
4807
Darryl Greend49a4992018-06-18 17:27:26 +01004808/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004809void persistent_key_load_key_from_storage( data_t *data,
4810 int type_arg, int bits_arg,
4811 int usage_flags_arg, int alg_arg,
4812 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004813{
Ronald Cron71016a92020-08-28 19:01:50 +02004814 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004815 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02004816 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4817 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004818 psa_key_type_t type = type_arg;
4819 size_t bits = bits_arg;
4820 psa_key_usage_t usage_flags = usage_flags_arg;
4821 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004822 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004823 unsigned char *first_export = NULL;
4824 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004825 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004826 size_t first_exported_length;
4827 size_t second_exported_length;
4828
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004829 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4830 {
4831 ASSERT_ALLOC( first_export, export_size );
4832 ASSERT_ALLOC( second_export, export_size );
4833 }
Darryl Greend49a4992018-06-18 17:27:26 +01004834
Gilles Peskine8817f612018-12-18 00:18:46 +01004835 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004836
Gilles Peskinec87af662019-05-15 16:12:22 +02004837 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004838 psa_set_key_usage_flags( &attributes, usage_flags );
4839 psa_set_key_algorithm( &attributes, alg );
4840 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004841 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004842
Darryl Green0c6575a2018-11-07 16:05:30 +00004843 switch( generation_method )
4844 {
4845 case IMPORT_KEY:
4846 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004847 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004848 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004849 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004850
Darryl Green0c6575a2018-11-07 16:05:30 +00004851 case GENERATE_KEY:
4852 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004853 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004854 break;
4855
4856 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01004857#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004858 {
4859 /* Create base key */
4860 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4861 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4862 psa_set_key_usage_flags( &base_attributes,
4863 PSA_KEY_USAGE_DERIVE );
4864 psa_set_key_algorithm( &base_attributes, derive_alg );
4865 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004866 PSA_ASSERT( psa_import_key( &base_attributes,
4867 data->x, data->len,
4868 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004869 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004871 PSA_ASSERT( psa_key_derivation_input_key(
4872 &operation,
4873 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004874 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004875 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004876 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004877 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
4878 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004879 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004880 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004881 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02004882 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004883 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01004884#else
4885 TEST_ASSUME( ! "KDF not supported in this configuration" );
4886#endif
4887 break;
4888
4889 default:
4890 TEST_ASSERT( ! "generation_method not implemented in test" );
4891 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00004892 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004893 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004894
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004895 /* Export the key if permitted by the key policy. */
4896 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4897 {
Ronald Cron5425a212020-08-04 14:58:35 +02004898 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004899 first_export, export_size,
4900 &first_exported_length ) );
4901 if( generation_method == IMPORT_KEY )
4902 ASSERT_COMPARE( data->x, data->len,
4903 first_export, first_exported_length );
4904 }
Darryl Greend49a4992018-06-18 17:27:26 +01004905
4906 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02004907 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004908 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01004909 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004910
Darryl Greend49a4992018-06-18 17:27:26 +01004911 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02004912 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02004913 TEST_ASSERT( mbedtls_svc_key_id_equal(
4914 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004915 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4916 PSA_KEY_LIFETIME_PERSISTENT );
4917 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4918 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4919 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4920 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004921
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004922 /* Export the key again if permitted by the key policy. */
4923 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004924 {
Ronald Cron5425a212020-08-04 14:58:35 +02004925 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004926 second_export, export_size,
4927 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004928 ASSERT_COMPARE( first_export, first_exported_length,
4929 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004930 }
4931
4932 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004933 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004934 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004935
4936exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004937 /*
4938 * Key attributes may have been returned by psa_get_key_attributes()
4939 * thus reset them as required.
4940 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004941 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004942
Darryl Greend49a4992018-06-18 17:27:26 +01004943 mbedtls_free( first_export );
4944 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004945 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004946 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02004947 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004948 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01004949}
4950/* END_CASE */