blob: 2ad07c403dd59e01670dc3ec6d35f43cc25f633c [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,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100844 int exercise_alg,
845 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200846{
Ronald Cron5425a212020-08-04 14:58:35 +0200847 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200848 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000849 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200850 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100851 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200852 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200853
Gilles Peskine8817f612018-12-18 00:18:46 +0100854 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200855
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200856 psa_set_key_usage_flags( &attributes, policy_usage );
857 psa_set_key_algorithm( &attributes, policy_alg );
858 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200859
Gilles Peskine049c7532019-05-15 20:22:09 +0200860 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200861 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200862
Ronald Cron5425a212020-08-04 14:58:35 +0200863 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100864 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100865 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100866 else
867 TEST_EQUAL( status, expected_status );
868
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200870
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200871 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200872 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100873 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100874 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100875 else
876 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200877
878exit:
879 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200880 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200881 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200882}
883/* END_CASE */
884
885/* BEGIN_CASE */
886void cipher_key_policy( int policy_usage,
887 int policy_alg,
888 int key_type,
889 data_t *key_data,
890 int exercise_alg )
891{
Ronald Cron5425a212020-08-04 14:58:35 +0200892 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000894 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200895 psa_status_t status;
896
Gilles Peskine8817f612018-12-18 00:18:46 +0100897 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200898
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200899 psa_set_key_usage_flags( &attributes, policy_usage );
900 psa_set_key_algorithm( &attributes, policy_alg );
901 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200902
Gilles Peskine049c7532019-05-15 20:22:09 +0200903 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200904 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905
Ronald Cron5425a212020-08-04 14:58:35 +0200906 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200907 if( policy_alg == exercise_alg &&
908 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100909 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100911 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200912 psa_cipher_abort( &operation );
913
Ronald Cron5425a212020-08-04 14:58:35 +0200914 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915 if( policy_alg == exercise_alg &&
916 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100917 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100919 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200920
921exit:
922 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200923 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200924 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200925}
926/* END_CASE */
927
928/* BEGIN_CASE */
929void aead_key_policy( int policy_usage,
930 int policy_alg,
931 int key_type,
932 data_t *key_data,
933 int nonce_length_arg,
934 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100935 int exercise_alg,
936 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937{
Ronald Cron5425a212020-08-04 14:58:35 +0200938 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100941 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200942 unsigned char nonce[16] = {0};
943 size_t nonce_length = nonce_length_arg;
944 unsigned char tag[16];
945 size_t tag_length = tag_length_arg;
946 size_t output_length;
947
948 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
949 TEST_ASSERT( tag_length <= sizeof( tag ) );
950
Gilles Peskine8817f612018-12-18 00:18:46 +0100951 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200952
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200953 psa_set_key_usage_flags( &attributes, policy_usage );
954 psa_set_key_algorithm( &attributes, policy_alg );
955 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200956
Gilles Peskine049c7532019-05-15 20:22:09 +0200957 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200958 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959
Ronald Cron5425a212020-08-04 14:58:35 +0200960 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961 nonce, nonce_length,
962 NULL, 0,
963 NULL, 0,
964 tag, tag_length,
965 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100966 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
967 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100969 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200970
971 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200972 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973 nonce, nonce_length,
974 NULL, 0,
975 tag, tag_length,
976 NULL, 0,
977 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100978 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
979 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
980 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100981 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200982 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100983 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200984
985exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200986 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200987 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200988}
989/* END_CASE */
990
991/* BEGIN_CASE */
992void asymmetric_encryption_key_policy( int policy_usage,
993 int policy_alg,
994 int key_type,
995 data_t *key_data,
996 int exercise_alg )
997{
Ronald Cron5425a212020-08-04 14:58:35 +0200998 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001000 psa_status_t status;
1001 size_t key_bits;
1002 size_t buffer_length;
1003 unsigned char *buffer = NULL;
1004 size_t output_length;
1005
Gilles Peskine8817f612018-12-18 00:18:46 +01001006 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001008 psa_set_key_usage_flags( &attributes, policy_usage );
1009 psa_set_key_algorithm( &attributes, policy_alg );
1010 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001011
Gilles Peskine049c7532019-05-15 20:22:09 +02001012 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001013 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014
Ronald Cron5425a212020-08-04 14:58:35 +02001015 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001017 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1018 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001019 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001020
Ronald Cron5425a212020-08-04 14:58:35 +02001021 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001022 NULL, 0,
1023 NULL, 0,
1024 buffer, buffer_length,
1025 &output_length );
1026 if( policy_alg == exercise_alg &&
1027 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001028 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001029 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001030 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001031
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001032 if( buffer_length != 0 )
1033 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001034 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001035 buffer, buffer_length,
1036 NULL, 0,
1037 buffer, buffer_length,
1038 &output_length );
1039 if( policy_alg == exercise_alg &&
1040 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001041 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001042 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001043 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001044
1045exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001046 /*
1047 * Key attributes may have been returned by psa_get_key_attributes()
1048 * thus reset them as required.
1049 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001050 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001051
1052 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001053 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001054 mbedtls_free( buffer );
1055}
1056/* END_CASE */
1057
1058/* BEGIN_CASE */
1059void asymmetric_signature_key_policy( int policy_usage,
1060 int policy_alg,
1061 int key_type,
1062 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001063 int exercise_alg,
1064 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001065{
Ronald Cron5425a212020-08-04 14:58:35 +02001066 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001069 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1070 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1071 * compatible with the policy and `payload_length_arg` is supposed to be
1072 * a valid input length to sign. If `payload_length_arg <= 0`,
1073 * `exercise_alg` is supposed to be forbidden by the policy. */
1074 int compatible_alg = payload_length_arg > 0;
1075 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001076 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077 size_t signature_length;
1078
Gilles Peskine8817f612018-12-18 00:18:46 +01001079 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001080
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001081 psa_set_key_usage_flags( &attributes, policy_usage );
1082 psa_set_key_algorithm( &attributes, policy_alg );
1083 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001084
Gilles Peskine049c7532019-05-15 20:22:09 +02001085 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001086 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087
Ronald Cron5425a212020-08-04 14:58:35 +02001088 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001089 payload, payload_length,
1090 signature, sizeof( signature ),
1091 &signature_length );
1092 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001093 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001094 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001095 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001096
1097 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001098 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001099 payload, payload_length,
1100 signature, sizeof( signature ) );
1101 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001102 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001103 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001104 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001105
1106exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001107 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001108 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001109}
1110/* END_CASE */
1111
Janos Follathba3fab92019-06-11 14:50:16 +01001112/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001113void derive_key_policy( int policy_usage,
1114 int policy_alg,
1115 int key_type,
1116 data_t *key_data,
1117 int exercise_alg )
1118{
Ronald Cron5425a212020-08-04 14:58:35 +02001119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001121 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001122 psa_status_t status;
1123
Gilles Peskine8817f612018-12-18 00:18:46 +01001124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001126 psa_set_key_usage_flags( &attributes, policy_usage );
1127 psa_set_key_algorithm( &attributes, policy_alg );
1128 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001129
Gilles Peskine049c7532019-05-15 20:22:09 +02001130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001131 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001132
Janos Follathba3fab92019-06-11 14:50:16 +01001133 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1134
1135 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1136 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001137 {
Janos Follathba3fab92019-06-11 14:50:16 +01001138 PSA_ASSERT( psa_key_derivation_input_bytes(
1139 &operation,
1140 PSA_KEY_DERIVATION_INPUT_SEED,
1141 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001142 }
Janos Follathba3fab92019-06-11 14:50:16 +01001143
1144 status = psa_key_derivation_input_key( &operation,
1145 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001146 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001147
Gilles Peskineea0fb492018-07-12 17:17:20 +02001148 if( policy_alg == exercise_alg &&
1149 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001150 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001151 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001152 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001153
1154exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001155 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001156 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001157 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001158}
1159/* END_CASE */
1160
1161/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001162void agreement_key_policy( int policy_usage,
1163 int policy_alg,
1164 int key_type_arg,
1165 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001166 int exercise_alg,
1167 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001168{
Ronald Cron5425a212020-08-04 14:58:35 +02001169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001171 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001172 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001173 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001174 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001175
Gilles Peskine8817f612018-12-18 00:18:46 +01001176 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001177
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001178 psa_set_key_usage_flags( &attributes, policy_usage );
1179 psa_set_key_algorithm( &attributes, policy_alg );
1180 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181
Gilles Peskine049c7532019-05-15 20:22:09 +02001182 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001183 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001184
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001185 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001186 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001187
Steven Cooremance48e852020-10-05 16:02:45 +02001188 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001189
1190exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001191 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001192 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001193 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001194}
1195/* END_CASE */
1196
1197/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001198void key_policy_alg2( int key_type_arg, data_t *key_data,
1199 int usage_arg, int alg_arg, int alg2_arg )
1200{
Ronald Cron5425a212020-08-04 14:58:35 +02001201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001202 psa_key_type_t key_type = key_type_arg;
1203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1204 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1205 psa_key_usage_t usage = usage_arg;
1206 psa_algorithm_t alg = alg_arg;
1207 psa_algorithm_t alg2 = alg2_arg;
1208
1209 PSA_ASSERT( psa_crypto_init( ) );
1210
1211 psa_set_key_usage_flags( &attributes, usage );
1212 psa_set_key_algorithm( &attributes, alg );
1213 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1214 psa_set_key_type( &attributes, key_type );
1215 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001216 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001217
Ronald Cron5425a212020-08-04 14:58:35 +02001218 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001219 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1220 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1221 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1222
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001223 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001224 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001225 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001226 goto exit;
1227
1228exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001229 /*
1230 * Key attributes may have been returned by psa_get_key_attributes()
1231 * thus reset them as required.
1232 */
1233 psa_reset_key_attributes( &got_attributes );
1234
Ronald Cron5425a212020-08-04 14:58:35 +02001235 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001236 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001237}
1238/* END_CASE */
1239
1240/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001241void raw_agreement_key_policy( int policy_usage,
1242 int policy_alg,
1243 int key_type_arg,
1244 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001245 int exercise_alg,
1246 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001247{
Ronald Cron5425a212020-08-04 14:58:35 +02001248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001250 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001251 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001252 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001253 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001254
1255 PSA_ASSERT( psa_crypto_init( ) );
1256
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001257 psa_set_key_usage_flags( &attributes, policy_usage );
1258 psa_set_key_algorithm( &attributes, policy_alg );
1259 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260
Gilles Peskine049c7532019-05-15 20:22:09 +02001261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001262 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001263
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001264 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001265
Steven Cooremance48e852020-10-05 16:02:45 +02001266 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001267
1268exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001269 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001270 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001271 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001272}
1273/* END_CASE */
1274
1275/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001276void copy_success( int source_usage_arg,
1277 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001278 int type_arg, data_t *material,
1279 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001280 int target_usage_arg,
1281 int target_alg_arg, int target_alg2_arg,
1282 int expected_usage_arg,
1283 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001284{
Gilles Peskineca25db92019-04-19 11:43:08 +02001285 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1286 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001287 psa_key_usage_t expected_usage = expected_usage_arg;
1288 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001289 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001290 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1291 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001292 uint8_t *export_buffer = NULL;
1293
Gilles Peskine57ab7212019-01-28 13:03:09 +01001294 PSA_ASSERT( psa_crypto_init( ) );
1295
Gilles Peskineca25db92019-04-19 11:43:08 +02001296 /* Prepare the source key. */
1297 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1298 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001299 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001300 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001301 PSA_ASSERT( psa_import_key( &source_attributes,
1302 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001303 &source_key ) );
1304 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001305
Gilles Peskineca25db92019-04-19 11:43:08 +02001306 /* Prepare the target attributes. */
1307 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001308 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001309 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001310 /* Set volatile lifetime to reset the key identifier to 0. */
1311 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1312 }
1313
Gilles Peskineca25db92019-04-19 11:43:08 +02001314 if( target_usage_arg != -1 )
1315 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1316 if( target_alg_arg != -1 )
1317 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001318 if( target_alg2_arg != -1 )
1319 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001320
1321 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001322 PSA_ASSERT( psa_copy_key( source_key,
1323 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001324
1325 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001326 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001327
1328 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001329 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001330 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1331 psa_get_key_type( &target_attributes ) );
1332 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1333 psa_get_key_bits( &target_attributes ) );
1334 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1335 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001336 TEST_EQUAL( expected_alg2,
1337 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001338 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1339 {
1340 size_t length;
1341 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001342 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001343 material->len, &length ) );
1344 ASSERT_COMPARE( material->x, material->len,
1345 export_buffer, length );
1346 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001347
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001348 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001349 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001350 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001351 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001352
Ronald Cron5425a212020-08-04 14:58:35 +02001353 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001354
1355exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001356 /*
1357 * Source and target key attributes may have been returned by
1358 * psa_get_key_attributes() thus reset them as required.
1359 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001360 psa_reset_key_attributes( &source_attributes );
1361 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001362
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001363 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001364 mbedtls_free( export_buffer );
1365}
1366/* END_CASE */
1367
1368/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001369void copy_fail( int source_usage_arg,
1370 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001371 int type_arg, data_t *material,
1372 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001373 int target_usage_arg,
1374 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001375 int expected_status_arg )
1376{
1377 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1378 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001379 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1380 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02001381
1382 PSA_ASSERT( psa_crypto_init( ) );
1383
1384 /* Prepare the source key. */
1385 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1386 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001387 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001388 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001389 PSA_ASSERT( psa_import_key( &source_attributes,
1390 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001391 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001392
1393 /* Prepare the target attributes. */
1394 psa_set_key_type( &target_attributes, target_type_arg );
1395 psa_set_key_bits( &target_attributes, target_bits_arg );
1396 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1397 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001398 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001399
1400 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001401 TEST_EQUAL( psa_copy_key( source_key,
1402 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001403 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001404
Ronald Cron5425a212020-08-04 14:58:35 +02001405 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001406
Gilles Peskine4a644642019-05-03 17:14:08 +02001407exit:
1408 psa_reset_key_attributes( &source_attributes );
1409 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001410 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001411}
1412/* END_CASE */
1413
1414/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001415void hash_operation_init( )
1416{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001417 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001418 /* Test each valid way of initializing the object, except for `= {0}`, as
1419 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1420 * though it's OK by the C standard. We could test for this, but we'd need
1421 * to supress the Clang warning for the test. */
1422 psa_hash_operation_t func = psa_hash_operation_init( );
1423 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1424 psa_hash_operation_t zero;
1425
1426 memset( &zero, 0, sizeof( zero ) );
1427
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001428 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001429 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1430 PSA_ERROR_BAD_STATE );
1431 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1432 PSA_ERROR_BAD_STATE );
1433 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1434 PSA_ERROR_BAD_STATE );
1435
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001436 /* A default hash operation should be abortable without error. */
1437 PSA_ASSERT( psa_hash_abort( &func ) );
1438 PSA_ASSERT( psa_hash_abort( &init ) );
1439 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001440}
1441/* END_CASE */
1442
1443/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001444void hash_setup( int alg_arg,
1445 int expected_status_arg )
1446{
1447 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001448 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001449 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001450 psa_status_t status;
1451
Gilles Peskine8817f612018-12-18 00:18:46 +01001452 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001453
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001454 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001455 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001456
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001457 /* Whether setup succeeded or failed, abort must succeed. */
1458 PSA_ASSERT( psa_hash_abort( &operation ) );
1459
1460 /* If setup failed, reproduce the failure, so as to
1461 * test the resulting state of the operation object. */
1462 if( status != PSA_SUCCESS )
1463 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1464
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001465 /* Now the operation object should be reusable. */
1466#if defined(KNOWN_SUPPORTED_HASH_ALG)
1467 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1468 PSA_ASSERT( psa_hash_abort( &operation ) );
1469#endif
1470
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001471exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001472 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001473}
1474/* END_CASE */
1475
1476/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001477void hash_compute_fail( int alg_arg, data_t *input,
1478 int output_size_arg, int expected_status_arg )
1479{
1480 psa_algorithm_t alg = alg_arg;
1481 uint8_t *output = NULL;
1482 size_t output_size = output_size_arg;
1483 size_t output_length = INVALID_EXPORT_LENGTH;
1484 psa_status_t expected_status = expected_status_arg;
1485 psa_status_t status;
1486
1487 ASSERT_ALLOC( output, output_size );
1488
1489 PSA_ASSERT( psa_crypto_init( ) );
1490
1491 status = psa_hash_compute( alg, input->x, input->len,
1492 output, output_size, &output_length );
1493 TEST_EQUAL( status, expected_status );
1494 TEST_ASSERT( output_length <= output_size );
1495
1496exit:
1497 mbedtls_free( output );
1498 PSA_DONE( );
1499}
1500/* END_CASE */
1501
1502/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001503void hash_compare_fail( int alg_arg, data_t *input,
1504 data_t *reference_hash,
1505 int expected_status_arg )
1506{
1507 psa_algorithm_t alg = alg_arg;
1508 psa_status_t expected_status = expected_status_arg;
1509 psa_status_t status;
1510
1511 PSA_ASSERT( psa_crypto_init( ) );
1512
1513 status = psa_hash_compare( alg, input->x, input->len,
1514 reference_hash->x, reference_hash->len );
1515 TEST_EQUAL( status, expected_status );
1516
1517exit:
1518 PSA_DONE( );
1519}
1520/* END_CASE */
1521
1522/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001523void hash_compute_compare( int alg_arg, data_t *input,
1524 data_t *expected_output )
1525{
1526 psa_algorithm_t alg = alg_arg;
1527 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1528 size_t output_length = INVALID_EXPORT_LENGTH;
1529 size_t i;
1530
1531 PSA_ASSERT( psa_crypto_init( ) );
1532
1533 /* Compute with tight buffer */
1534 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001535 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001536 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001537 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001538 ASSERT_COMPARE( output, output_length,
1539 expected_output->x, expected_output->len );
1540
1541 /* Compute with larger buffer */
1542 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1543 output, sizeof( output ),
1544 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001545 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001546 ASSERT_COMPARE( output, output_length,
1547 expected_output->x, expected_output->len );
1548
1549 /* Compare with correct hash */
1550 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1551 output, output_length ) );
1552
1553 /* Compare with trailing garbage */
1554 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1555 output, output_length + 1 ),
1556 PSA_ERROR_INVALID_SIGNATURE );
1557
1558 /* Compare with truncated hash */
1559 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1560 output, output_length - 1 ),
1561 PSA_ERROR_INVALID_SIGNATURE );
1562
1563 /* Compare with corrupted value */
1564 for( i = 0; i < output_length; i++ )
1565 {
Chris Jones9634bb12021-01-20 15:56:42 +00001566 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001567 output[i] ^= 1;
1568 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1569 output, output_length ),
1570 PSA_ERROR_INVALID_SIGNATURE );
1571 output[i] ^= 1;
1572 }
1573
1574exit:
1575 PSA_DONE( );
1576}
1577/* END_CASE */
1578
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001579/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001580void hash_bad_order( )
1581{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001582 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001583 unsigned char input[] = "";
1584 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001585 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001586 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1587 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1588 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001589 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001590 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001591 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001592
Gilles Peskine8817f612018-12-18 00:18:46 +01001593 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001594
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001595 /* Call setup twice in a row. */
1596 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1597 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1598 PSA_ERROR_BAD_STATE );
1599 PSA_ASSERT( psa_hash_abort( &operation ) );
1600
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001601 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001602 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001603 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001604 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001605
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001606 /* Call update after finish. */
1607 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1608 PSA_ASSERT( psa_hash_finish( &operation,
1609 hash, sizeof( hash ), &hash_len ) );
1610 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001611 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001612 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001613
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001614 /* Call verify without calling setup beforehand. */
1615 TEST_EQUAL( psa_hash_verify( &operation,
1616 valid_hash, sizeof( valid_hash ) ),
1617 PSA_ERROR_BAD_STATE );
1618 PSA_ASSERT( psa_hash_abort( &operation ) );
1619
1620 /* Call verify after finish. */
1621 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1622 PSA_ASSERT( psa_hash_finish( &operation,
1623 hash, sizeof( hash ), &hash_len ) );
1624 TEST_EQUAL( psa_hash_verify( &operation,
1625 valid_hash, sizeof( valid_hash ) ),
1626 PSA_ERROR_BAD_STATE );
1627 PSA_ASSERT( psa_hash_abort( &operation ) );
1628
1629 /* Call verify twice in a row. */
1630 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1631 PSA_ASSERT( psa_hash_verify( &operation,
1632 valid_hash, sizeof( valid_hash ) ) );
1633 TEST_EQUAL( psa_hash_verify( &operation,
1634 valid_hash, sizeof( valid_hash ) ),
1635 PSA_ERROR_BAD_STATE );
1636 PSA_ASSERT( psa_hash_abort( &operation ) );
1637
1638 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001639 TEST_EQUAL( psa_hash_finish( &operation,
1640 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001641 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001642 PSA_ASSERT( psa_hash_abort( &operation ) );
1643
1644 /* Call finish twice in a row. */
1645 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1646 PSA_ASSERT( psa_hash_finish( &operation,
1647 hash, sizeof( hash ), &hash_len ) );
1648 TEST_EQUAL( psa_hash_finish( &operation,
1649 hash, sizeof( hash ), &hash_len ),
1650 PSA_ERROR_BAD_STATE );
1651 PSA_ASSERT( psa_hash_abort( &operation ) );
1652
1653 /* Call finish after calling verify. */
1654 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1655 PSA_ASSERT( psa_hash_verify( &operation,
1656 valid_hash, sizeof( valid_hash ) ) );
1657 TEST_EQUAL( psa_hash_finish( &operation,
1658 hash, sizeof( hash ), &hash_len ),
1659 PSA_ERROR_BAD_STATE );
1660 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001661
1662exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001663 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001664}
1665/* END_CASE */
1666
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001667/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001668void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001669{
1670 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001671 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1672 * appended to it */
1673 unsigned char hash[] = {
1674 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1675 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1676 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001677 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001678 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001679
Gilles Peskine8817f612018-12-18 00:18:46 +01001680 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001681
itayzafrir27e69452018-11-01 14:26:34 +02001682 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001683 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001684 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001685 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001686
itayzafrir27e69452018-11-01 14:26:34 +02001687 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001688 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001689 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001690 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001691
itayzafrir27e69452018-11-01 14:26:34 +02001692 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001693 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001694 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001695 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001696
itayzafrirec93d302018-10-18 18:01:10 +03001697exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001698 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001699}
1700/* END_CASE */
1701
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001702/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1703void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001704{
1705 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001706 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001707 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001708 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001709 size_t hash_len;
1710
Gilles Peskine8817f612018-12-18 00:18:46 +01001711 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001712
itayzafrir58028322018-10-25 10:22:01 +03001713 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001714 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001715 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001716 hash, expected_size - 1, &hash_len ),
1717 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001718
1719exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001720 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001721}
1722/* END_CASE */
1723
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001724/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1725void hash_clone_source_state( )
1726{
1727 psa_algorithm_t alg = PSA_ALG_SHA_256;
1728 unsigned char hash[PSA_HASH_MAX_SIZE];
1729 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1730 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1731 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1732 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1733 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1734 size_t hash_len;
1735
1736 PSA_ASSERT( psa_crypto_init( ) );
1737 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1738
1739 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1740 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1741 PSA_ASSERT( psa_hash_finish( &op_finished,
1742 hash, sizeof( hash ), &hash_len ) );
1743 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1744 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1745
1746 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1747 PSA_ERROR_BAD_STATE );
1748
1749 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1750 PSA_ASSERT( psa_hash_finish( &op_init,
1751 hash, sizeof( hash ), &hash_len ) );
1752 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1753 PSA_ASSERT( psa_hash_finish( &op_finished,
1754 hash, sizeof( hash ), &hash_len ) );
1755 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1756 PSA_ASSERT( psa_hash_finish( &op_aborted,
1757 hash, sizeof( hash ), &hash_len ) );
1758
1759exit:
1760 psa_hash_abort( &op_source );
1761 psa_hash_abort( &op_init );
1762 psa_hash_abort( &op_setup );
1763 psa_hash_abort( &op_finished );
1764 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001765 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001766}
1767/* END_CASE */
1768
1769/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1770void hash_clone_target_state( )
1771{
1772 psa_algorithm_t alg = PSA_ALG_SHA_256;
1773 unsigned char hash[PSA_HASH_MAX_SIZE];
1774 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1775 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1776 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1777 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1778 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1779 size_t hash_len;
1780
1781 PSA_ASSERT( psa_crypto_init( ) );
1782
1783 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1784 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1785 PSA_ASSERT( psa_hash_finish( &op_finished,
1786 hash, sizeof( hash ), &hash_len ) );
1787 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1788 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1789
1790 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1791 PSA_ASSERT( psa_hash_finish( &op_target,
1792 hash, sizeof( hash ), &hash_len ) );
1793
1794 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1795 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1796 PSA_ERROR_BAD_STATE );
1797 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1798 PSA_ERROR_BAD_STATE );
1799
1800exit:
1801 psa_hash_abort( &op_target );
1802 psa_hash_abort( &op_init );
1803 psa_hash_abort( &op_setup );
1804 psa_hash_abort( &op_finished );
1805 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001806 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001807}
1808/* END_CASE */
1809
itayzafrir58028322018-10-25 10:22:01 +03001810/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001811void mac_operation_init( )
1812{
Jaeden Amero252ef282019-02-15 14:05:35 +00001813 const uint8_t input[1] = { 0 };
1814
Jaeden Amero769ce272019-01-04 11:48:03 +00001815 /* Test each valid way of initializing the object, except for `= {0}`, as
1816 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1817 * though it's OK by the C standard. We could test for this, but we'd need
1818 * to supress the Clang warning for the test. */
1819 psa_mac_operation_t func = psa_mac_operation_init( );
1820 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1821 psa_mac_operation_t zero;
1822
1823 memset( &zero, 0, sizeof( zero ) );
1824
Jaeden Amero252ef282019-02-15 14:05:35 +00001825 /* A freshly-initialized MAC operation should not be usable. */
1826 TEST_EQUAL( psa_mac_update( &func,
1827 input, sizeof( input ) ),
1828 PSA_ERROR_BAD_STATE );
1829 TEST_EQUAL( psa_mac_update( &init,
1830 input, sizeof( input ) ),
1831 PSA_ERROR_BAD_STATE );
1832 TEST_EQUAL( psa_mac_update( &zero,
1833 input, sizeof( input ) ),
1834 PSA_ERROR_BAD_STATE );
1835
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001836 /* A default MAC operation should be abortable without error. */
1837 PSA_ASSERT( psa_mac_abort( &func ) );
1838 PSA_ASSERT( psa_mac_abort( &init ) );
1839 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001840}
1841/* END_CASE */
1842
1843/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001844void mac_setup( int key_type_arg,
1845 data_t *key,
1846 int alg_arg,
1847 int expected_status_arg )
1848{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001849 psa_key_type_t key_type = key_type_arg;
1850 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001851 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001852 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001853 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1854#if defined(KNOWN_SUPPORTED_MAC_ALG)
1855 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1856#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001857
Gilles Peskine8817f612018-12-18 00:18:46 +01001858 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001859
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001860 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1861 &operation, &status ) )
1862 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001863 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001864
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001865 /* The operation object should be reusable. */
1866#if defined(KNOWN_SUPPORTED_MAC_ALG)
1867 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1868 smoke_test_key_data,
1869 sizeof( smoke_test_key_data ),
1870 KNOWN_SUPPORTED_MAC_ALG,
1871 &operation, &status ) )
1872 goto exit;
1873 TEST_EQUAL( status, PSA_SUCCESS );
1874#endif
1875
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001876exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001877 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001878}
1879/* END_CASE */
1880
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001881/* 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 +00001882void mac_bad_order( )
1883{
Ronald Cron5425a212020-08-04 14:58:35 +02001884 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001885 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1886 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001887 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001888 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1889 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1890 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001892 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1893 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1894 size_t sign_mac_length = 0;
1895 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1896 const uint8_t verify_mac[] = {
1897 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1898 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1899 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1900
1901 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001902 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001903 psa_set_key_algorithm( &attributes, alg );
1904 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001905
Ronald Cron5425a212020-08-04 14:58:35 +02001906 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1907 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001908
Jaeden Amero252ef282019-02-15 14:05:35 +00001909 /* Call update without calling setup beforehand. */
1910 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1911 PSA_ERROR_BAD_STATE );
1912 PSA_ASSERT( psa_mac_abort( &operation ) );
1913
1914 /* Call sign finish without calling setup beforehand. */
1915 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1916 &sign_mac_length),
1917 PSA_ERROR_BAD_STATE );
1918 PSA_ASSERT( psa_mac_abort( &operation ) );
1919
1920 /* Call verify finish without calling setup beforehand. */
1921 TEST_EQUAL( psa_mac_verify_finish( &operation,
1922 verify_mac, sizeof( verify_mac ) ),
1923 PSA_ERROR_BAD_STATE );
1924 PSA_ASSERT( psa_mac_abort( &operation ) );
1925
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001926 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001927 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1928 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001929 PSA_ERROR_BAD_STATE );
1930 PSA_ASSERT( psa_mac_abort( &operation ) );
1931
Jaeden Amero252ef282019-02-15 14:05:35 +00001932 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001933 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001934 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1935 PSA_ASSERT( psa_mac_sign_finish( &operation,
1936 sign_mac, sizeof( sign_mac ),
1937 &sign_mac_length ) );
1938 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1939 PSA_ERROR_BAD_STATE );
1940 PSA_ASSERT( psa_mac_abort( &operation ) );
1941
1942 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001943 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001944 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1945 PSA_ASSERT( psa_mac_verify_finish( &operation,
1946 verify_mac, sizeof( verify_mac ) ) );
1947 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1948 PSA_ERROR_BAD_STATE );
1949 PSA_ASSERT( psa_mac_abort( &operation ) );
1950
1951 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001952 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001953 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1954 PSA_ASSERT( psa_mac_sign_finish( &operation,
1955 sign_mac, sizeof( sign_mac ),
1956 &sign_mac_length ) );
1957 TEST_EQUAL( psa_mac_sign_finish( &operation,
1958 sign_mac, sizeof( sign_mac ),
1959 &sign_mac_length ),
1960 PSA_ERROR_BAD_STATE );
1961 PSA_ASSERT( psa_mac_abort( &operation ) );
1962
1963 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001964 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001965 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1966 PSA_ASSERT( psa_mac_verify_finish( &operation,
1967 verify_mac, sizeof( verify_mac ) ) );
1968 TEST_EQUAL( psa_mac_verify_finish( &operation,
1969 verify_mac, sizeof( verify_mac ) ),
1970 PSA_ERROR_BAD_STATE );
1971 PSA_ASSERT( psa_mac_abort( &operation ) );
1972
1973 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001974 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001975 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1976 TEST_EQUAL( psa_mac_verify_finish( &operation,
1977 verify_mac, sizeof( verify_mac ) ),
1978 PSA_ERROR_BAD_STATE );
1979 PSA_ASSERT( psa_mac_abort( &operation ) );
1980
1981 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001982 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001983 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1984 TEST_EQUAL( psa_mac_sign_finish( &operation,
1985 sign_mac, sizeof( sign_mac ),
1986 &sign_mac_length ),
1987 PSA_ERROR_BAD_STATE );
1988 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001989
Ronald Cron5425a212020-08-04 14:58:35 +02001990 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001991
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001992exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001993 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001994}
1995/* END_CASE */
1996
1997/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001998void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02001999 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002000 int alg_arg,
2001 data_t *input,
2002 data_t *expected_mac )
2003{
Ronald Cron5425a212020-08-04 14:58:35 +02002004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005 psa_key_type_t key_type = key_type_arg;
2006 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002007 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002008 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002009 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002010 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002011 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002012 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002013 const size_t output_sizes_to_test[] = {
2014 0,
2015 1,
2016 expected_mac->len - 1,
2017 expected_mac->len,
2018 expected_mac->len + 1,
2019 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002020
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002021 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002022 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002023 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002024
Gilles Peskine8817f612018-12-18 00:18:46 +01002025 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002026
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002027 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002028 psa_set_key_algorithm( &attributes, alg );
2029 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002030
Ronald Cron5425a212020-08-04 14:58:35 +02002031 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2032 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002033
Gilles Peskine8b356b52020-08-25 23:44:59 +02002034 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2035 {
2036 const size_t output_size = output_sizes_to_test[i];
2037 psa_status_t expected_status =
2038 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2039 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002040
Chris Jones9634bb12021-01-20 15:56:42 +00002041 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002042 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002043
Gilles Peskine8b356b52020-08-25 23:44:59 +02002044 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002045 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002046 PSA_ASSERT( psa_mac_update( &operation,
2047 input->x, input->len ) );
2048 TEST_EQUAL( psa_mac_sign_finish( &operation,
2049 actual_mac, output_size,
2050 &mac_length ),
2051 expected_status );
2052 PSA_ASSERT( psa_mac_abort( &operation ) );
2053
2054 if( expected_status == PSA_SUCCESS )
2055 {
2056 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2057 actual_mac, mac_length );
2058 }
2059 mbedtls_free( actual_mac );
2060 actual_mac = NULL;
2061 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002062
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002063exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002064 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002065 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002066 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002067 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002068}
2069/* END_CASE */
2070
2071/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002072void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002073 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002074 int alg_arg,
2075 data_t *input,
2076 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002077{
Ronald Cron5425a212020-08-04 14:58:35 +02002078 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002079 psa_key_type_t key_type = key_type_arg;
2080 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002081 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002083 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002084
Gilles Peskine69c12672018-06-28 00:07:19 +02002085 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2086
Gilles Peskine8817f612018-12-18 00:18:46 +01002087 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002088
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002089 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002090 psa_set_key_algorithm( &attributes, alg );
2091 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002092
Ronald Cron5425a212020-08-04 14:58:35 +02002093 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2094 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002095
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002096 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002097 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002098 PSA_ASSERT( psa_mac_update( &operation,
2099 input->x, input->len ) );
2100 PSA_ASSERT( psa_mac_verify_finish( &operation,
2101 expected_mac->x,
2102 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002103
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002104 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002105 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002106 PSA_ASSERT( psa_mac_update( &operation,
2107 input->x, input->len ) );
2108 TEST_EQUAL( psa_mac_verify_finish( &operation,
2109 expected_mac->x,
2110 expected_mac->len - 1 ),
2111 PSA_ERROR_INVALID_SIGNATURE );
2112
2113 /* Test a MAC that's too long. */
2114 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2115 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002116 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002117 PSA_ASSERT( psa_mac_update( &operation,
2118 input->x, input->len ) );
2119 TEST_EQUAL( psa_mac_verify_finish( &operation,
2120 perturbed_mac,
2121 expected_mac->len + 1 ),
2122 PSA_ERROR_INVALID_SIGNATURE );
2123
2124 /* Test changing one byte. */
2125 for( size_t i = 0; i < expected_mac->len; i++ )
2126 {
Chris Jones9634bb12021-01-20 15:56:42 +00002127 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002128 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002129 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002130 PSA_ASSERT( psa_mac_update( &operation,
2131 input->x, input->len ) );
2132 TEST_EQUAL( psa_mac_verify_finish( &operation,
2133 perturbed_mac,
2134 expected_mac->len ),
2135 PSA_ERROR_INVALID_SIGNATURE );
2136 perturbed_mac[i] ^= 1;
2137 }
2138
Gilles Peskine8c9def32018-02-08 10:02:12 +01002139exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002140 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002141 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002142 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002143 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002144}
2145/* END_CASE */
2146
2147/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002148void cipher_operation_init( )
2149{
Jaeden Ameroab439972019-02-15 14:12:05 +00002150 const uint8_t input[1] = { 0 };
2151 unsigned char output[1] = { 0 };
2152 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002153 /* Test each valid way of initializing the object, except for `= {0}`, as
2154 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2155 * though it's OK by the C standard. We could test for this, but we'd need
2156 * to supress the Clang warning for the test. */
2157 psa_cipher_operation_t func = psa_cipher_operation_init( );
2158 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2159 psa_cipher_operation_t zero;
2160
2161 memset( &zero, 0, sizeof( zero ) );
2162
Jaeden Ameroab439972019-02-15 14:12:05 +00002163 /* A freshly-initialized cipher operation should not be usable. */
2164 TEST_EQUAL( psa_cipher_update( &func,
2165 input, sizeof( input ),
2166 output, sizeof( output ),
2167 &output_length ),
2168 PSA_ERROR_BAD_STATE );
2169 TEST_EQUAL( psa_cipher_update( &init,
2170 input, sizeof( input ),
2171 output, sizeof( output ),
2172 &output_length ),
2173 PSA_ERROR_BAD_STATE );
2174 TEST_EQUAL( psa_cipher_update( &zero,
2175 input, sizeof( input ),
2176 output, sizeof( output ),
2177 &output_length ),
2178 PSA_ERROR_BAD_STATE );
2179
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002180 /* A default cipher operation should be abortable without error. */
2181 PSA_ASSERT( psa_cipher_abort( &func ) );
2182 PSA_ASSERT( psa_cipher_abort( &init ) );
2183 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002184}
2185/* END_CASE */
2186
2187/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002188void cipher_setup( int key_type_arg,
2189 data_t *key,
2190 int alg_arg,
2191 int expected_status_arg )
2192{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002193 psa_key_type_t key_type = key_type_arg;
2194 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002195 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002196 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002197 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002198#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002199 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2200#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002201
Gilles Peskine8817f612018-12-18 00:18:46 +01002202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002203
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002204 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2205 &operation, &status ) )
2206 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002207 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002208
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002209 /* The operation object should be reusable. */
2210#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2211 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2212 smoke_test_key_data,
2213 sizeof( smoke_test_key_data ),
2214 KNOWN_SUPPORTED_CIPHER_ALG,
2215 &operation, &status ) )
2216 goto exit;
2217 TEST_EQUAL( status, PSA_SUCCESS );
2218#endif
2219
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002220exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002221 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002222 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002223}
2224/* END_CASE */
2225
Steven Cooreman29eecbf2021-01-28 19:41:25 +01002226/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002227void cipher_bad_order( )
2228{
Ronald Cron5425a212020-08-04 14:58:35 +02002229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002230 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2231 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002233 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002234 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002235 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002236 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2237 0xaa, 0xaa, 0xaa, 0xaa };
2238 const uint8_t text[] = {
2239 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2240 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002241 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002242 size_t length = 0;
2243
2244 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002245 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2246 psa_set_key_algorithm( &attributes, alg );
2247 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002248 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2249 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002250
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002251 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002252 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2253 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002254 PSA_ERROR_BAD_STATE );
2255 PSA_ASSERT( psa_cipher_abort( &operation ) );
2256
2257 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002258 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2259 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002260 PSA_ERROR_BAD_STATE );
2261 PSA_ASSERT( psa_cipher_abort( &operation ) );
2262
Jaeden Ameroab439972019-02-15 14:12:05 +00002263 /* Generate an IV without calling setup beforehand. */
2264 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2265 buffer, sizeof( buffer ),
2266 &length ),
2267 PSA_ERROR_BAD_STATE );
2268 PSA_ASSERT( psa_cipher_abort( &operation ) );
2269
2270 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002271 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002272 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2273 buffer, sizeof( buffer ),
2274 &length ) );
2275 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2276 buffer, sizeof( buffer ),
2277 &length ),
2278 PSA_ERROR_BAD_STATE );
2279 PSA_ASSERT( psa_cipher_abort( &operation ) );
2280
2281 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002282 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002283 PSA_ASSERT( psa_cipher_set_iv( &operation,
2284 iv, sizeof( iv ) ) );
2285 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2286 buffer, sizeof( buffer ),
2287 &length ),
2288 PSA_ERROR_BAD_STATE );
2289 PSA_ASSERT( psa_cipher_abort( &operation ) );
2290
2291 /* Set an IV without calling setup beforehand. */
2292 TEST_EQUAL( psa_cipher_set_iv( &operation,
2293 iv, sizeof( iv ) ),
2294 PSA_ERROR_BAD_STATE );
2295 PSA_ASSERT( psa_cipher_abort( &operation ) );
2296
2297 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002298 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002299 PSA_ASSERT( psa_cipher_set_iv( &operation,
2300 iv, sizeof( iv ) ) );
2301 TEST_EQUAL( psa_cipher_set_iv( &operation,
2302 iv, sizeof( iv ) ),
2303 PSA_ERROR_BAD_STATE );
2304 PSA_ASSERT( psa_cipher_abort( &operation ) );
2305
2306 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002307 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002308 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2309 buffer, sizeof( buffer ),
2310 &length ) );
2311 TEST_EQUAL( psa_cipher_set_iv( &operation,
2312 iv, sizeof( iv ) ),
2313 PSA_ERROR_BAD_STATE );
2314 PSA_ASSERT( psa_cipher_abort( &operation ) );
2315
2316 /* Call update without calling setup beforehand. */
2317 TEST_EQUAL( psa_cipher_update( &operation,
2318 text, sizeof( text ),
2319 buffer, sizeof( buffer ),
2320 &length ),
2321 PSA_ERROR_BAD_STATE );
2322 PSA_ASSERT( psa_cipher_abort( &operation ) );
2323
2324 /* Call update without an IV where an IV is required. */
2325 TEST_EQUAL( psa_cipher_update( &operation,
2326 text, sizeof( text ),
2327 buffer, sizeof( buffer ),
2328 &length ),
2329 PSA_ERROR_BAD_STATE );
2330 PSA_ASSERT( psa_cipher_abort( &operation ) );
2331
2332 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002333 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002334 PSA_ASSERT( psa_cipher_set_iv( &operation,
2335 iv, sizeof( iv ) ) );
2336 PSA_ASSERT( psa_cipher_finish( &operation,
2337 buffer, sizeof( buffer ), &length ) );
2338 TEST_EQUAL( psa_cipher_update( &operation,
2339 text, sizeof( text ),
2340 buffer, sizeof( buffer ),
2341 &length ),
2342 PSA_ERROR_BAD_STATE );
2343 PSA_ASSERT( psa_cipher_abort( &operation ) );
2344
2345 /* Call finish without calling setup beforehand. */
2346 TEST_EQUAL( psa_cipher_finish( &operation,
2347 buffer, sizeof( buffer ), &length ),
2348 PSA_ERROR_BAD_STATE );
2349 PSA_ASSERT( psa_cipher_abort( &operation ) );
2350
2351 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002352 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002353 /* Not calling update means we are encrypting an empty buffer, which is OK
2354 * for cipher modes with padding. */
2355 TEST_EQUAL( psa_cipher_finish( &operation,
2356 buffer, sizeof( buffer ), &length ),
2357 PSA_ERROR_BAD_STATE );
2358 PSA_ASSERT( psa_cipher_abort( &operation ) );
2359
2360 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002361 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002362 PSA_ASSERT( psa_cipher_set_iv( &operation,
2363 iv, sizeof( iv ) ) );
2364 PSA_ASSERT( psa_cipher_finish( &operation,
2365 buffer, sizeof( buffer ), &length ) );
2366 TEST_EQUAL( psa_cipher_finish( &operation,
2367 buffer, sizeof( buffer ), &length ),
2368 PSA_ERROR_BAD_STATE );
2369 PSA_ASSERT( psa_cipher_abort( &operation ) );
2370
Ronald Cron5425a212020-08-04 14:58:35 +02002371 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002372
Jaeden Ameroab439972019-02-15 14:12:05 +00002373exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002374 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002375 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002376}
2377/* END_CASE */
2378
2379/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002380void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002381 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002382 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002383 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002384{
Ronald Cron5425a212020-08-04 14:58:35 +02002385 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002386 psa_status_t status;
2387 psa_key_type_t key_type = key_type_arg;
2388 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002389 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002390 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391 size_t output_buffer_size = 0;
2392 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002393 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002394 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002396
Gilles Peskine8817f612018-12-18 00:18:46 +01002397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002398
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002399 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2400 psa_set_key_algorithm( &attributes, alg );
2401 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002402
Ronald Cron5425a212020-08-04 14:58:35 +02002403 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2404 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405
Ronald Cron5425a212020-08-04 14:58:35 +02002406 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002407
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002408 if( iv->len > 0 )
2409 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002410 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002411 }
2412
gabor-mezei-armceface22021-01-21 12:26:17 +01002413 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2414 TEST_ASSERT( output_buffer_size <=
2415 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002416 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002417
Gilles Peskine8817f612018-12-18 00:18:46 +01002418 PSA_ASSERT( psa_cipher_update( &operation,
2419 input->x, input->len,
2420 output, output_buffer_size,
2421 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002422 TEST_ASSERT( function_output_length <=
2423 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2424 TEST_ASSERT( function_output_length <=
2425 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002426 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002427
Gilles Peskine50e586b2018-06-08 14:28:46 +02002428 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002429 ( output_buffer_size == 0 ? NULL :
2430 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002431 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002432 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002433 TEST_ASSERT( function_output_length <=
2434 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2435 TEST_ASSERT( function_output_length <=
2436 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002437 total_output_length += function_output_length;
2438
Gilles Peskinefe11b722018-12-18 00:24:04 +01002439 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002440 if( expected_status == PSA_SUCCESS )
2441 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002442 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002443 ASSERT_COMPARE( expected_output->x, expected_output->len,
2444 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002445 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002446
Gilles Peskine50e586b2018-06-08 14:28:46 +02002447exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002448 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002449 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002450 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002451 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002452}
2453/* END_CASE */
2454
2455/* BEGIN_CASE */
2456void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002457 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002458 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002459 int first_part_size_arg,
2460 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002461 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002462{
Ronald Cron5425a212020-08-04 14:58:35 +02002463 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002464 psa_key_type_t key_type = key_type_arg;
2465 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002466 size_t first_part_size = first_part_size_arg;
2467 size_t output1_length = output1_length_arg;
2468 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002469 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002470 size_t output_buffer_size = 0;
2471 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002472 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002473 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002474 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002475
Gilles Peskine8817f612018-12-18 00:18:46 +01002476 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002477
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002478 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2479 psa_set_key_algorithm( &attributes, alg );
2480 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002481
Ronald Cron5425a212020-08-04 14:58:35 +02002482 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2483 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002484
Ronald Cron5425a212020-08-04 14:58:35 +02002485 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002486
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002487 if( iv->len > 0 )
2488 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002489 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002490 }
2491
gabor-mezei-armceface22021-01-21 12:26:17 +01002492 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2493 TEST_ASSERT( output_buffer_size <=
2494 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002495 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002496
Gilles Peskinee0866522019-02-19 19:44:00 +01002497 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002498 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2499 output, output_buffer_size,
2500 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002501 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002502 TEST_ASSERT( function_output_length <=
2503 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2504 TEST_ASSERT( function_output_length <=
2505 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002506 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002507
Gilles Peskine8817f612018-12-18 00:18:46 +01002508 PSA_ASSERT( psa_cipher_update( &operation,
2509 input->x + first_part_size,
2510 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002511 ( output_buffer_size == 0 ? NULL :
2512 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002513 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002514 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002515 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002516 TEST_ASSERT( function_output_length <=
2517 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2518 alg,
2519 input->len - first_part_size ) );
2520 TEST_ASSERT( function_output_length <=
2521 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002522 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002523
Gilles Peskine8817f612018-12-18 00:18:46 +01002524 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002525 ( output_buffer_size == 0 ? NULL :
2526 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002527 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002528 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002529 TEST_ASSERT( function_output_length <=
2530 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2531 TEST_ASSERT( function_output_length <=
2532 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002533 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002534 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002535
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002536 ASSERT_COMPARE( expected_output->x, expected_output->len,
2537 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002538
2539exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002540 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002541 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002542 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002543 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002544}
2545/* END_CASE */
2546
2547/* BEGIN_CASE */
2548void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002549 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002550 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002551 int first_part_size_arg,
2552 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002553 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002554{
Ronald Cron5425a212020-08-04 14:58:35 +02002555 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002556 psa_key_type_t key_type = key_type_arg;
2557 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002558 size_t first_part_size = first_part_size_arg;
2559 size_t output1_length = output1_length_arg;
2560 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002561 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002562 size_t output_buffer_size = 0;
2563 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002564 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002565 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002567
Gilles Peskine8817f612018-12-18 00:18:46 +01002568 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002569
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002570 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2571 psa_set_key_algorithm( &attributes, alg );
2572 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002573
Ronald Cron5425a212020-08-04 14:58:35 +02002574 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2575 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002576
Ronald Cron5425a212020-08-04 14:58:35 +02002577 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002578
Steven Cooreman177deba2020-09-07 17:14:14 +02002579 if( iv->len > 0 )
2580 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002581 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002582 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002583
gabor-mezei-armceface22021-01-21 12:26:17 +01002584 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2585 TEST_ASSERT( output_buffer_size <=
2586 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002587 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002588
Gilles Peskinee0866522019-02-19 19:44:00 +01002589 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002590 PSA_ASSERT( psa_cipher_update( &operation,
2591 input->x, first_part_size,
2592 output, output_buffer_size,
2593 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002594 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002595 TEST_ASSERT( function_output_length <=
2596 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2597 TEST_ASSERT( function_output_length <=
2598 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002599 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002600
Gilles Peskine8817f612018-12-18 00:18:46 +01002601 PSA_ASSERT( psa_cipher_update( &operation,
2602 input->x + first_part_size,
2603 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002604 ( output_buffer_size == 0 ? NULL :
2605 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002606 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002607 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002608 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002609 TEST_ASSERT( function_output_length <=
2610 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2611 alg,
2612 input->len - first_part_size ) );
2613 TEST_ASSERT( function_output_length <=
2614 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002615 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002616
Gilles Peskine8817f612018-12-18 00:18:46 +01002617 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002618 ( output_buffer_size == 0 ? NULL :
2619 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002620 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002621 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002622 TEST_ASSERT( function_output_length <=
2623 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2624 TEST_ASSERT( function_output_length <=
2625 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002626 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002627 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002628
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002629 ASSERT_COMPARE( expected_output->x, expected_output->len,
2630 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002631
2632exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002633 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002634 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002635 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002636 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002637}
2638/* END_CASE */
2639
Gilles Peskine50e586b2018-06-08 14:28:46 +02002640/* BEGIN_CASE */
2641void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002642 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002643 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002644 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002645{
Ronald Cron5425a212020-08-04 14:58:35 +02002646 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647 psa_status_t status;
2648 psa_key_type_t key_type = key_type_arg;
2649 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002650 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002651 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652 size_t output_buffer_size = 0;
2653 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002654 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002655 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002657
Gilles Peskine8817f612018-12-18 00:18:46 +01002658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002659
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002660 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2661 psa_set_key_algorithm( &attributes, alg );
2662 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002663
Ronald Cron5425a212020-08-04 14:58:35 +02002664 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2665 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002666
Ronald Cron5425a212020-08-04 14:58:35 +02002667 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002668
Steven Cooreman177deba2020-09-07 17:14:14 +02002669 if( iv->len > 0 )
2670 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002671 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002672 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
gabor-mezei-armceface22021-01-21 12:26:17 +01002674 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2675 TEST_ASSERT( output_buffer_size <=
2676 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002677 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002678
Gilles Peskine8817f612018-12-18 00:18:46 +01002679 PSA_ASSERT( psa_cipher_update( &operation,
2680 input->x, input->len,
2681 output, output_buffer_size,
2682 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002683 TEST_ASSERT( function_output_length <=
2684 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2685 TEST_ASSERT( function_output_length <=
2686 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002687 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002688
Gilles Peskine50e586b2018-06-08 14:28:46 +02002689 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002690 ( output_buffer_size == 0 ? NULL :
2691 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002692 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002693 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002694 TEST_ASSERT( function_output_length <=
2695 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2696 TEST_ASSERT( function_output_length <=
2697 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002698 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002699 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700
2701 if( expected_status == PSA_SUCCESS )
2702 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002704 ASSERT_COMPARE( expected_output->x, expected_output->len,
2705 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002706 }
2707
Gilles Peskine50e586b2018-06-08 14:28:46 +02002708exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002709 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002710 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002711 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002712 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002713}
2714/* END_CASE */
2715
Gilles Peskine50e586b2018-06-08 14:28:46 +02002716/* BEGIN_CASE */
2717void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002718 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002719 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002720{
Ronald Cron5425a212020-08-04 14:58:35 +02002721 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002722 psa_key_type_t key_type = key_type_arg;
2723 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002724 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002725 size_t iv_size = 16;
2726 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002727 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002728 size_t output1_size = 0;
2729 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002730 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002731 size_t output2_size = 0;
2732 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002733 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002734 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2735 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002736 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002737
Gilles Peskine8817f612018-12-18 00:18:46 +01002738 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002739
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002740 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2741 psa_set_key_algorithm( &attributes, alg );
2742 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002743
Ronald Cron5425a212020-08-04 14:58:35 +02002744 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2745 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002746
Ronald Cron5425a212020-08-04 14:58:35 +02002747 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2748 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002749
Steven Cooreman177deba2020-09-07 17:14:14 +02002750 if( alg != PSA_ALG_ECB_NO_PADDING )
2751 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002752 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2753 iv, iv_size,
2754 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002755 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002756 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2757 TEST_ASSERT( output1_size <=
2758 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002759 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002760
Gilles Peskine8817f612018-12-18 00:18:46 +01002761 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2762 output1, output1_size,
2763 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002764 TEST_ASSERT( output1_length <=
2765 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2766 TEST_ASSERT( output1_length <=
2767 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2768
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002770 output1 + output1_length,
2771 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002772 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002773 TEST_ASSERT( function_output_length <=
2774 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2775 TEST_ASSERT( function_output_length <=
2776 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002777
Gilles Peskine048b7f02018-06-08 14:20:49 +02002778 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002779
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002781
2782 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002783 TEST_ASSERT( output2_size <=
2784 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2785 TEST_ASSERT( output2_size <=
2786 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002787 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002788
Steven Cooreman177deba2020-09-07 17:14:14 +02002789 if( iv_length > 0 )
2790 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002791 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2792 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002793 }
2794
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2796 output2, output2_size,
2797 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002798 TEST_ASSERT( output2_length <=
2799 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2800 TEST_ASSERT( output2_length <=
2801 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2802
Gilles Peskine048b7f02018-06-08 14:20:49 +02002803 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002804 PSA_ASSERT( psa_cipher_finish( &operation2,
2805 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002806 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002807 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002808 TEST_ASSERT( function_output_length <=
2809 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2810 TEST_ASSERT( function_output_length <=
2811 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002812
Gilles Peskine048b7f02018-06-08 14:20:49 +02002813 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002814
Gilles Peskine8817f612018-12-18 00:18:46 +01002815 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002816
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002817 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002818
2819exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002820 psa_cipher_abort( &operation1 );
2821 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002822 mbedtls_free( output1 );
2823 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002824 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002825 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002826}
2827/* END_CASE */
2828
2829/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830void cipher_verify_output_multipart( int alg_arg,
2831 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002832 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002833 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002834 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002835{
Ronald Cron5425a212020-08-04 14:58:35 +02002836 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002837 psa_key_type_t key_type = key_type_arg;
2838 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002839 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002840 unsigned char iv[16] = {0};
2841 size_t iv_size = 16;
2842 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002843 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002844 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002845 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002846 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002847 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002848 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002849 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002850 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2851 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002853
Gilles Peskine8817f612018-12-18 00:18:46 +01002854 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002855
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002856 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2857 psa_set_key_algorithm( &attributes, alg );
2858 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002859
Ronald Cron5425a212020-08-04 14:58:35 +02002860 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2861 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002862
Ronald Cron5425a212020-08-04 14:58:35 +02002863 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2864 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002865
Steven Cooreman177deba2020-09-07 17:14:14 +02002866 if( alg != PSA_ALG_ECB_NO_PADDING )
2867 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002868 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2869 iv, iv_size,
2870 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002871 }
2872
gabor-mezei-armceface22021-01-21 12:26:17 +01002873 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2874 TEST_ASSERT( output1_buffer_size <=
2875 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002876 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002877
Gilles Peskinee0866522019-02-19 19:44:00 +01002878 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002879
Gilles Peskine8817f612018-12-18 00:18:46 +01002880 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2881 output1, output1_buffer_size,
2882 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002883 TEST_ASSERT( function_output_length <=
2884 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2885 TEST_ASSERT( function_output_length <=
2886 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002887 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002888
Gilles Peskine8817f612018-12-18 00:18:46 +01002889 PSA_ASSERT( psa_cipher_update( &operation1,
2890 input->x + first_part_size,
2891 input->len - first_part_size,
2892 output1, output1_buffer_size,
2893 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002894 TEST_ASSERT( function_output_length <=
2895 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2896 alg,
2897 input->len - first_part_size ) );
2898 TEST_ASSERT( function_output_length <=
2899 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002900 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002901
Gilles Peskine8817f612018-12-18 00:18:46 +01002902 PSA_ASSERT( psa_cipher_finish( &operation1,
2903 output1 + output1_length,
2904 output1_buffer_size - output1_length,
2905 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002906 TEST_ASSERT( function_output_length <=
2907 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2908 TEST_ASSERT( function_output_length <=
2909 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002910 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002911
Gilles Peskine8817f612018-12-18 00:18:46 +01002912 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002913
Gilles Peskine048b7f02018-06-08 14:20:49 +02002914 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002915 TEST_ASSERT( output2_buffer_size <=
2916 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2917 TEST_ASSERT( output2_buffer_size <=
2918 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002919 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002920
Steven Cooreman177deba2020-09-07 17:14:14 +02002921 if( iv_length > 0 )
2922 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002923 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2924 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002925 }
Moran Pekerded84402018-06-06 16:36:50 +03002926
Gilles Peskine8817f612018-12-18 00:18:46 +01002927 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2928 output2, output2_buffer_size,
2929 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002930 TEST_ASSERT( function_output_length <=
2931 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2932 TEST_ASSERT( function_output_length <=
2933 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002934 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002935
Gilles Peskine8817f612018-12-18 00:18:46 +01002936 PSA_ASSERT( psa_cipher_update( &operation2,
2937 output1 + first_part_size,
2938 output1_length - first_part_size,
2939 output2, output2_buffer_size,
2940 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002941 TEST_ASSERT( function_output_length <=
2942 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2943 alg,
2944 output1_length - first_part_size ) );
2945 TEST_ASSERT( function_output_length <=
2946 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002947 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002948
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 PSA_ASSERT( psa_cipher_finish( &operation2,
2950 output2 + output2_length,
2951 output2_buffer_size - output2_length,
2952 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002953 TEST_ASSERT( function_output_length <=
2954 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2955 TEST_ASSERT( function_output_length <=
2956 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002957 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002958
Gilles Peskine8817f612018-12-18 00:18:46 +01002959 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002960
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002961 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002962
2963exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002964 psa_cipher_abort( &operation1 );
2965 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002966 mbedtls_free( output1 );
2967 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002968 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002969 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002970}
2971/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002972
Gilles Peskine20035e32018-02-03 22:44:14 +01002973/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002974void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002975 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002976 data_t *nonce,
2977 data_t *additional_data,
2978 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002979 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002980{
Ronald Cron5425a212020-08-04 14:58:35 +02002981 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002982 psa_key_type_t key_type = key_type_arg;
2983 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002984 unsigned char *output_data = NULL;
2985 size_t output_size = 0;
2986 size_t output_length = 0;
2987 unsigned char *output_data2 = NULL;
2988 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002989 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Steven Cooremanf49478b2021-02-15 15:19:25 +01002990 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002991 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002992 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002993
Gilles Peskine4abf7412018-06-18 16:35:34 +02002994 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002995 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2996 * should be exact. */
2997 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
2998 TEST_EQUAL( output_size,
2999 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003000 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003001
Gilles Peskine8817f612018-12-18 00:18:46 +01003002 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003003
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003004 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3005 psa_set_key_algorithm( &attributes, alg );
3006 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003007
Gilles Peskine049c7532019-05-15 20:22:09 +02003008 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003009 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003010
Steven Cooremanf49478b2021-02-15 15:19:25 +01003011 status = psa_aead_encrypt( key, alg,
3012 nonce->x, nonce->len,
3013 additional_data->x,
3014 additional_data->len,
3015 input_data->x, input_data->len,
3016 output_data, output_size,
3017 &output_length );
3018
3019 /* If the operation is not supported, just skip and not fail in case the
3020 * encryption involves a common limitation of cryptography hardwares and
3021 * an alternative implementation. */
3022 if( status == PSA_ERROR_NOT_SUPPORTED )
3023 {
3024 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3025 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3026 }
3027
3028 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003029
3030 if( PSA_SUCCESS == expected_result )
3031 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003032 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003033
Gilles Peskine003a4a92019-05-14 16:09:40 +02003034 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3035 * should be exact. */
3036 TEST_EQUAL( input_data->len,
3037 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3038
gabor-mezei-armceface22021-01-21 12:26:17 +01003039 TEST_ASSERT( input_data->len <=
3040 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3041
Ronald Cron5425a212020-08-04 14:58:35 +02003042 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003043 nonce->x, nonce->len,
3044 additional_data->x,
3045 additional_data->len,
3046 output_data, output_length,
3047 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003048 &output_length2 ),
3049 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003050
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003051 ASSERT_COMPARE( input_data->x, input_data->len,
3052 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003053 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003054
Gilles Peskinea1cac842018-06-11 19:33:02 +02003055exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003056 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003057 mbedtls_free( output_data );
3058 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003059 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003060}
3061/* END_CASE */
3062
3063/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003064void aead_encrypt( int key_type_arg, data_t *key_data,
3065 int alg_arg,
3066 data_t *nonce,
3067 data_t *additional_data,
3068 data_t *input_data,
3069 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003070{
Ronald Cron5425a212020-08-04 14:58:35 +02003071 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003072 psa_key_type_t key_type = key_type_arg;
3073 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003074 unsigned char *output_data = NULL;
3075 size_t output_size = 0;
3076 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003077 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003078 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003079 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003080
Gilles Peskine4abf7412018-06-18 16:35:34 +02003081 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003082 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3083 * should be exact. */
3084 TEST_EQUAL( output_size,
3085 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003086 TEST_ASSERT( output_size <=
3087 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003088 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003089
Gilles Peskine8817f612018-12-18 00:18:46 +01003090 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003091
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003092 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3093 psa_set_key_algorithm( &attributes, alg );
3094 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003095
Gilles Peskine049c7532019-05-15 20:22:09 +02003096 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003097 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003098
Steven Cooremand588ea12021-01-11 19:36:04 +01003099 status = psa_aead_encrypt( key, alg,
3100 nonce->x, nonce->len,
3101 additional_data->x, additional_data->len,
3102 input_data->x, input_data->len,
3103 output_data, output_size,
3104 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003105
Ronald Cron28a45ed2021-02-09 20:35:42 +01003106 /* If the operation is not supported, just skip and not fail in case the
3107 * encryption involves a common limitation of cryptography hardwares and
3108 * an alternative implementation. */
3109 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003110 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003111 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3112 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003113 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003114
3115 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003116 ASSERT_COMPARE( expected_result->x, expected_result->len,
3117 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003118
Gilles Peskinea1cac842018-06-11 19:33:02 +02003119exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003120 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003121 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003122 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003123}
3124/* END_CASE */
3125
3126/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003127void aead_decrypt( int key_type_arg, data_t *key_data,
3128 int alg_arg,
3129 data_t *nonce,
3130 data_t *additional_data,
3131 data_t *input_data,
3132 data_t *expected_data,
3133 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003134{
Ronald Cron5425a212020-08-04 14:58:35 +02003135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003136 psa_key_type_t key_type = key_type_arg;
3137 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003138 unsigned char *output_data = NULL;
3139 size_t output_size = 0;
3140 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003141 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003143 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003144 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003145
Gilles Peskine003a4a92019-05-14 16:09:40 +02003146 output_size = input_data->len - tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003147 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
gabor-mezei-armceface22021-01-21 12:26:17 +01003148 {
3149 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3150 * should be exact. */
Gilles Peskine003a4a92019-05-14 16:09:40 +02003151 TEST_EQUAL( output_size,
3152 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003153 TEST_ASSERT( output_size <=
3154 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3155 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003156 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003159
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003160 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3161 psa_set_key_algorithm( &attributes, alg );
3162 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003163
Gilles Peskine049c7532019-05-15 20:22:09 +02003164 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003165 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003166
Steven Cooremand588ea12021-01-11 19:36:04 +01003167 status = psa_aead_decrypt( key, alg,
3168 nonce->x, nonce->len,
3169 additional_data->x,
3170 additional_data->len,
3171 input_data->x, input_data->len,
3172 output_data, output_size,
3173 &output_length );
3174
Ronald Cron28a45ed2021-02-09 20:35:42 +01003175 /* If the operation is not supported, just skip and not fail in case the
3176 * decryption involves a common limitation of cryptography hardwares and
3177 * an alternative implementation. */
3178 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003179 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003180 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3181 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003182 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003183
3184 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003185
Gilles Peskine2d277862018-06-18 15:41:12 +02003186 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003187 ASSERT_COMPARE( expected_data->x, expected_data->len,
3188 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003189
Gilles Peskinea1cac842018-06-11 19:33:02 +02003190exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003191 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003192 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003193 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003194}
3195/* END_CASE */
3196
3197/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003198void signature_size( int type_arg,
3199 int bits,
3200 int alg_arg,
3201 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003202{
3203 psa_key_type_t type = type_arg;
3204 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003205 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003206
Gilles Peskinefe11b722018-12-18 00:24:04 +01003207 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003208#if defined(MBEDTLS_TEST_DEPRECATED)
3209 TEST_EQUAL( actual_size,
3210 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3211#endif /* MBEDTLS_TEST_DEPRECATED */
3212
Gilles Peskinee59236f2018-01-27 23:32:46 +01003213exit:
3214 ;
3215}
3216/* END_CASE */
3217
3218/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003219void sign_deterministic( int key_type_arg, data_t *key_data,
3220 int alg_arg, data_t *input_data,
3221 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003222{
Ronald Cron5425a212020-08-04 14:58:35 +02003223 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003224 psa_key_type_t key_type = key_type_arg;
3225 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003226 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003227 unsigned char *signature = NULL;
3228 size_t signature_size;
3229 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003231
Gilles Peskine8817f612018-12-18 00:18:46 +01003232 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003233
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003234 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003235 psa_set_key_algorithm( &attributes, alg );
3236 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003237
Gilles Peskine049c7532019-05-15 20:22:09 +02003238 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003239 &key ) );
3240 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003241 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003242
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003243 /* Allocate a buffer which has the size advertized by the
3244 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003245 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003246 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003247 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003248 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003249 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003250
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003251 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003252 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003253 input_data->x, input_data->len,
3254 signature, signature_size,
3255 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003256 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003257 ASSERT_COMPARE( output_data->x, output_data->len,
3258 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003259
Gilles Peskine0627f982019-11-26 19:12:16 +01003260#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003261 memset( signature, 0, signature_size );
3262 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003263 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003264 input_data->x, input_data->len,
3265 signature, signature_size,
3266 &signature_length ) );
3267 ASSERT_COMPARE( output_data->x, output_data->len,
3268 signature, signature_length );
3269#endif /* MBEDTLS_TEST_DEPRECATED */
3270
Gilles Peskine20035e32018-02-03 22:44:14 +01003271exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003272 /*
3273 * Key attributes may have been returned by psa_get_key_attributes()
3274 * thus reset them as required.
3275 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003276 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003277
Ronald Cron5425a212020-08-04 14:58:35 +02003278 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003279 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003280 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003281}
3282/* END_CASE */
3283
3284/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003285void sign_fail( int key_type_arg, data_t *key_data,
3286 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003287 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003288{
Ronald Cron5425a212020-08-04 14:58:35 +02003289 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003290 psa_key_type_t key_type = key_type_arg;
3291 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003292 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003293 psa_status_t actual_status;
3294 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003295 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003296 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003297 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003298
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003299 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003300
Gilles Peskine8817f612018-12-18 00:18:46 +01003301 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003302
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003303 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003304 psa_set_key_algorithm( &attributes, alg );
3305 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003306
Gilles Peskine049c7532019-05-15 20:22:09 +02003307 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003308 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003309
Ronald Cron5425a212020-08-04 14:58:35 +02003310 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003311 input_data->x, input_data->len,
3312 signature, signature_size,
3313 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003314 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003315 /* The value of *signature_length is unspecified on error, but
3316 * whatever it is, it should be less than signature_size, so that
3317 * if the caller tries to read *signature_length bytes without
3318 * checking the error code then they don't overflow a buffer. */
3319 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003320
Gilles Peskine895242b2019-11-29 12:15:40 +01003321#if defined(MBEDTLS_TEST_DEPRECATED)
3322 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003323 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003324 input_data->x, input_data->len,
3325 signature, signature_size,
3326 &signature_length ),
3327 expected_status );
3328 TEST_ASSERT( signature_length <= signature_size );
3329#endif /* MBEDTLS_TEST_DEPRECATED */
3330
Gilles Peskine20035e32018-02-03 22:44:14 +01003331exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003332 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003333 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003334 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003335 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003336}
3337/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003338
3339/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003340void sign_verify( int key_type_arg, data_t *key_data,
3341 int alg_arg, data_t *input_data )
3342{
Ronald Cron5425a212020-08-04 14:58:35 +02003343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003344 psa_key_type_t key_type = key_type_arg;
3345 psa_algorithm_t alg = alg_arg;
3346 size_t key_bits;
3347 unsigned char *signature = NULL;
3348 size_t signature_size;
3349 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003351
Gilles Peskine8817f612018-12-18 00:18:46 +01003352 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003353
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003354 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003355 psa_set_key_algorithm( &attributes, alg );
3356 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003357
Gilles Peskine049c7532019-05-15 20:22:09 +02003358 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003359 &key ) );
3360 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003361 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003362
3363 /* Allocate a buffer which has the size advertized by the
3364 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003365 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003366 key_bits, alg );
3367 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003368 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003369 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003370
3371 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003372 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003373 input_data->x, input_data->len,
3374 signature, signature_size,
3375 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003376 /* Check that the signature length looks sensible. */
3377 TEST_ASSERT( signature_length <= signature_size );
3378 TEST_ASSERT( signature_length > 0 );
3379
3380 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003381 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003382 input_data->x, input_data->len,
3383 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003384
3385 if( input_data->len != 0 )
3386 {
3387 /* Flip a bit in the input and verify that the signature is now
3388 * detected as invalid. Flip a bit at the beginning, not at the end,
3389 * because ECDSA may ignore the last few bits of the input. */
3390 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003391 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003392 input_data->x, input_data->len,
3393 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003394 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003395 }
3396
3397exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003398 /*
3399 * Key attributes may have been returned by psa_get_key_attributes()
3400 * thus reset them as required.
3401 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003402 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003403
Ronald Cron5425a212020-08-04 14:58:35 +02003404 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003405 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003406 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003407}
3408/* END_CASE */
3409
3410/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003411void asymmetric_verify( int key_type_arg, data_t *key_data,
3412 int alg_arg, data_t *hash_data,
3413 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003414{
Ronald Cron5425a212020-08-04 14:58:35 +02003415 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003416 psa_key_type_t key_type = key_type_arg;
3417 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003418 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003419
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003420 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003421
Gilles Peskine8817f612018-12-18 00:18:46 +01003422 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003423
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003424 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003425 psa_set_key_algorithm( &attributes, alg );
3426 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003427
Gilles Peskine049c7532019-05-15 20:22:09 +02003428 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003429 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003430
Ronald Cron5425a212020-08-04 14:58:35 +02003431 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003432 hash_data->x, hash_data->len,
3433 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003434
3435#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003436 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003437 hash_data->x, hash_data->len,
3438 signature_data->x,
3439 signature_data->len ) );
3440
3441#endif /* MBEDTLS_TEST_DEPRECATED */
3442
itayzafrir5c753392018-05-08 11:18:38 +03003443exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003444 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003445 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003446 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003447}
3448/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003449
3450/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003451void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3452 int alg_arg, data_t *hash_data,
3453 data_t *signature_data,
3454 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003455{
Ronald Cron5425a212020-08-04 14:58:35 +02003456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003457 psa_key_type_t key_type = key_type_arg;
3458 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003459 psa_status_t actual_status;
3460 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003462
Gilles Peskine8817f612018-12-18 00:18:46 +01003463 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003464
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003465 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003466 psa_set_key_algorithm( &attributes, alg );
3467 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003468
Gilles Peskine049c7532019-05-15 20:22:09 +02003469 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003470 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003471
Ronald Cron5425a212020-08-04 14:58:35 +02003472 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003473 hash_data->x, hash_data->len,
3474 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003475 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003476
Gilles Peskine895242b2019-11-29 12:15:40 +01003477#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003478 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003479 hash_data->x, hash_data->len,
3480 signature_data->x, signature_data->len ),
3481 expected_status );
3482#endif /* MBEDTLS_TEST_DEPRECATED */
3483
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003484exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003485 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003486 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003487 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003488}
3489/* END_CASE */
3490
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003491/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003492void asymmetric_encrypt( int key_type_arg,
3493 data_t *key_data,
3494 int alg_arg,
3495 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003496 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003497 int expected_output_length_arg,
3498 int expected_status_arg )
3499{
Ronald Cron5425a212020-08-04 14:58:35 +02003500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003501 psa_key_type_t key_type = key_type_arg;
3502 psa_algorithm_t alg = alg_arg;
3503 size_t expected_output_length = expected_output_length_arg;
3504 size_t key_bits;
3505 unsigned char *output = NULL;
3506 size_t output_size;
3507 size_t output_length = ~0;
3508 psa_status_t actual_status;
3509 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003511
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003513
Gilles Peskine656896e2018-06-29 19:12:28 +02003514 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003515 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3516 psa_set_key_algorithm( &attributes, alg );
3517 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003518 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003519 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003520
3521 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003522 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003523 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003524
Gilles Peskine656896e2018-06-29 19:12:28 +02003525 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003526 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003527 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003528
3529 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003530 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003531 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003532 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003533 output, output_size,
3534 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003535 TEST_EQUAL( actual_status, expected_status );
3536 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003537
Gilles Peskine68428122018-06-30 18:42:41 +02003538 /* If the label is empty, the test framework puts a non-null pointer
3539 * in label->x. Test that a null pointer works as well. */
3540 if( label->len == 0 )
3541 {
3542 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003543 if( output_size != 0 )
3544 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003545 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003546 input_data->x, input_data->len,
3547 NULL, label->len,
3548 output, output_size,
3549 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003550 TEST_EQUAL( actual_status, expected_status );
3551 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003552 }
3553
Gilles Peskine656896e2018-06-29 19:12:28 +02003554exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003555 /*
3556 * Key attributes may have been returned by psa_get_key_attributes()
3557 * thus reset them as required.
3558 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003559 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003560
Ronald Cron5425a212020-08-04 14:58:35 +02003561 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003562 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003563 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003564}
3565/* END_CASE */
3566
3567/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003568void asymmetric_encrypt_decrypt( int key_type_arg,
3569 data_t *key_data,
3570 int alg_arg,
3571 data_t *input_data,
3572 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003573{
Ronald Cron5425a212020-08-04 14:58:35 +02003574 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003575 psa_key_type_t key_type = key_type_arg;
3576 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003577 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003578 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003579 size_t output_size;
3580 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003581 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003582 size_t output2_size;
3583 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003585
Gilles Peskine8817f612018-12-18 00:18:46 +01003586 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003587
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003588 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3589 psa_set_key_algorithm( &attributes, alg );
3590 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003591
Gilles Peskine049c7532019-05-15 20:22:09 +02003592 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003593 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003594
3595 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003596 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003597 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003598
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003599 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003600 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003601 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003602
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003603 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003604 TEST_ASSERT( output2_size <=
3605 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3606 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003607 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003608
Gilles Peskineeebd7382018-06-08 18:11:54 +02003609 /* We test encryption by checking that encrypt-then-decrypt gives back
3610 * the original plaintext because of the non-optional random
3611 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003612 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 input_data->x, input_data->len,
3614 label->x, label->len,
3615 output, output_size,
3616 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003617 /* We don't know what ciphertext length to expect, but check that
3618 * it looks sensible. */
3619 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003620
Ronald Cron5425a212020-08-04 14:58:35 +02003621 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003622 output, output_length,
3623 label->x, label->len,
3624 output2, output2_size,
3625 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003626 ASSERT_COMPARE( input_data->x, input_data->len,
3627 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003628
3629exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003630 /*
3631 * Key attributes may have been returned by psa_get_key_attributes()
3632 * thus reset them as required.
3633 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003634 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003635
Ronald Cron5425a212020-08-04 14:58:35 +02003636 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003637 mbedtls_free( output );
3638 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003639 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003640}
3641/* END_CASE */
3642
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003644void asymmetric_decrypt( int key_type_arg,
3645 data_t *key_data,
3646 int alg_arg,
3647 data_t *input_data,
3648 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003649 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003650{
Ronald Cron5425a212020-08-04 14:58:35 +02003651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003652 psa_key_type_t key_type = key_type_arg;
3653 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003654 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003655 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003656 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003657 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659
Gilles Peskine8817f612018-12-18 00:18:46 +01003660 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003661
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003662 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3663 psa_set_key_algorithm( &attributes, alg );
3664 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003665
Gilles Peskine049c7532019-05-15 20:22:09 +02003666 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003667 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003668
gabor-mezei-armceface22021-01-21 12:26:17 +01003669 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3670 key_bits = psa_get_key_bits( &attributes );
3671
3672 /* Determine the maximum ciphertext length */
3673 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3674 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3675 ASSERT_ALLOC( output, output_size );
3676
Ronald Cron5425a212020-08-04 14:58:35 +02003677 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003678 input_data->x, input_data->len,
3679 label->x, label->len,
3680 output,
3681 output_size,
3682 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003683 ASSERT_COMPARE( expected_data->x, expected_data->len,
3684 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003685
Gilles Peskine68428122018-06-30 18:42:41 +02003686 /* If the label is empty, the test framework puts a non-null pointer
3687 * in label->x. Test that a null pointer works as well. */
3688 if( label->len == 0 )
3689 {
3690 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003691 if( output_size != 0 )
3692 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003693 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003694 input_data->x, input_data->len,
3695 NULL, label->len,
3696 output,
3697 output_size,
3698 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003699 ASSERT_COMPARE( expected_data->x, expected_data->len,
3700 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003701 }
3702
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003703exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003704 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003705 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003706 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003707 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003708}
3709/* END_CASE */
3710
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003711/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003712void asymmetric_decrypt_fail( int key_type_arg,
3713 data_t *key_data,
3714 int alg_arg,
3715 data_t *input_data,
3716 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003717 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003718 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003719{
Ronald Cron5425a212020-08-04 14:58:35 +02003720 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003721 psa_key_type_t key_type = key_type_arg;
3722 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003723 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003724 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003725 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003726 psa_status_t actual_status;
3727 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003728 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003729
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003730 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003731
Gilles Peskine8817f612018-12-18 00:18:46 +01003732 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003733
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003734 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3735 psa_set_key_algorithm( &attributes, alg );
3736 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003737
Gilles Peskine049c7532019-05-15 20:22:09 +02003738 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003739 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003740
Ronald Cron5425a212020-08-04 14:58:35 +02003741 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003742 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003743 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003744 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003745 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003746 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003747 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003748
Gilles Peskine68428122018-06-30 18:42:41 +02003749 /* If the label is empty, the test framework puts a non-null pointer
3750 * in label->x. Test that a null pointer works as well. */
3751 if( label->len == 0 )
3752 {
3753 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003754 if( output_size != 0 )
3755 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003756 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003757 input_data->x, input_data->len,
3758 NULL, label->len,
3759 output, output_size,
3760 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003761 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003762 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003763 }
3764
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003765exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003766 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003767 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02003768 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003769 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003770}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003771/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003772
3773/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003774void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00003775{
3776 /* Test each valid way of initializing the object, except for `= {0}`, as
3777 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3778 * though it's OK by the C standard. We could test for this, but we'd need
3779 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003780 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003781 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
3782 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
3783 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003784
3785 memset( &zero, 0, sizeof( zero ) );
3786
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003787 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003788 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003789 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003790 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003791 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003792 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003793 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003794
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003795 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003796 PSA_ASSERT( psa_key_derivation_abort(&func) );
3797 PSA_ASSERT( psa_key_derivation_abort(&init) );
3798 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003799}
3800/* END_CASE */
3801
Janos Follath16de4a42019-06-13 16:32:24 +01003802/* BEGIN_CASE */
3803void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003804{
Gilles Peskineea0fb492018-07-12 17:17:20 +02003805 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003806 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003807 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003808
Gilles Peskine8817f612018-12-18 00:18:46 +01003809 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003810
Janos Follath16de4a42019-06-13 16:32:24 +01003811 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003812 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003813
3814exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003815 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003816 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003817}
3818/* END_CASE */
3819
Janos Follathaf3c2a02019-06-12 12:34:34 +01003820/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01003821void derive_set_capacity( int alg_arg, int capacity_arg,
3822 int expected_status_arg )
3823{
3824 psa_algorithm_t alg = alg_arg;
3825 size_t capacity = capacity_arg;
3826 psa_status_t expected_status = expected_status_arg;
3827 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3828
3829 PSA_ASSERT( psa_crypto_init( ) );
3830
3831 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3832
3833 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
3834 expected_status );
3835
3836exit:
3837 psa_key_derivation_abort( &operation );
3838 PSA_DONE( );
3839}
3840/* END_CASE */
3841
3842/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01003843void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02003844 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003845 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02003846 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003847 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02003848 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003849 int expected_status_arg3,
3850 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003851{
3852 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02003853 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
3854 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01003855 psa_status_t expected_statuses[] = {expected_status_arg1,
3856 expected_status_arg2,
3857 expected_status_arg3};
3858 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003859 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3860 MBEDTLS_SVC_KEY_ID_INIT,
3861 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01003862 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3863 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3864 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003865 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003866 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003867 psa_status_t expected_output_status = expected_output_status_arg;
3868 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01003869
3870 PSA_ASSERT( psa_crypto_init( ) );
3871
3872 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3873 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003874
3875 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3876
3877 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
3878 {
Gilles Peskineb8965192019-09-24 16:21:10 +02003879 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003880 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02003881 psa_set_key_type( &attributes, key_types[i] );
3882 PSA_ASSERT( psa_import_key( &attributes,
3883 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003884 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003885 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
3886 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
3887 {
3888 // When taking a private key as secret input, use key agreement
3889 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003890 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
3891 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003892 expected_statuses[i] );
3893 }
3894 else
3895 {
3896 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02003897 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003898 expected_statuses[i] );
3899 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02003900 }
3901 else
3902 {
3903 TEST_EQUAL( psa_key_derivation_input_bytes(
3904 &operation, steps[i],
3905 inputs[i]->x, inputs[i]->len ),
3906 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003907 }
3908 }
3909
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003910 if( output_key_type != PSA_KEY_TYPE_NONE )
3911 {
3912 psa_reset_key_attributes( &attributes );
3913 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
3914 psa_set_key_bits( &attributes, 8 );
3915 actual_output_status =
3916 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02003917 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003918 }
3919 else
3920 {
3921 uint8_t buffer[1];
3922 actual_output_status =
3923 psa_key_derivation_output_bytes( &operation,
3924 buffer, sizeof( buffer ) );
3925 }
3926 TEST_EQUAL( actual_output_status, expected_output_status );
3927
Janos Follathaf3c2a02019-06-12 12:34:34 +01003928exit:
3929 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003930 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3931 psa_destroy_key( keys[i] );
3932 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003933 PSA_DONE( );
3934}
3935/* END_CASE */
3936
Janos Follathd958bb72019-07-03 15:02:16 +01003937/* BEGIN_CASE */
3938void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003939{
Janos Follathd958bb72019-07-03 15:02:16 +01003940 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003942 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003943 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01003944 unsigned char input1[] = "Input 1";
3945 size_t input1_length = sizeof( input1 );
3946 unsigned char input2[] = "Input 2";
3947 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003948 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003949 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003950 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3951 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3952 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003954
Gilles Peskine8817f612018-12-18 00:18:46 +01003955 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003956
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003957 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3958 psa_set_key_algorithm( &attributes, alg );
3959 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003960
Gilles Peskine73676cb2019-05-15 20:15:10 +02003961 PSA_ASSERT( psa_import_key( &attributes,
3962 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02003963 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003964
3965 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003966 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
3967 input1, input1_length,
3968 input2, input2_length,
3969 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01003970 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003971
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003972 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01003973 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003974 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003975
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003976 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003977
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003978 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02003979 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003980
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003981exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003982 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003983 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003984 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003985}
3986/* END_CASE */
3987
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003988/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003989void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003990{
3991 uint8_t output_buffer[16];
3992 size_t buffer_size = 16;
3993 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003994 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003995
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003996 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3997 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003998 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003999
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004000 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004001 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004002
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004003 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004004
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004005 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4006 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004007 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004008
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004009 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004010 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004011
4012exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004013 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004014}
4015/* END_CASE */
4016
4017/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004018void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004019 int step1_arg, data_t *input1,
4020 int step2_arg, data_t *input2,
4021 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004022 int requested_capacity_arg,
4023 data_t *expected_output1,
4024 data_t *expected_output2 )
4025{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004026 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004027 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4028 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004029 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4030 MBEDTLS_SVC_KEY_ID_INIT,
4031 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004032 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004033 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004034 uint8_t *expected_outputs[2] =
4035 {expected_output1->x, expected_output2->x};
4036 size_t output_sizes[2] =
4037 {expected_output1->len, expected_output2->len};
4038 size_t output_buffer_size = 0;
4039 uint8_t *output_buffer = NULL;
4040 size_t expected_capacity;
4041 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004042 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004043 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004044 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004045
4046 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4047 {
4048 if( output_sizes[i] > output_buffer_size )
4049 output_buffer_size = output_sizes[i];
4050 if( output_sizes[i] == 0 )
4051 expected_outputs[i] = NULL;
4052 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004053 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004054 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004055
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004056 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4057 psa_set_key_algorithm( &attributes, alg );
4058 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004059
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004060 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004061 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4062 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4063 requested_capacity ) );
4064 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004065 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004066 switch( steps[i] )
4067 {
4068 case 0:
4069 break;
4070 case PSA_KEY_DERIVATION_INPUT_SECRET:
4071 PSA_ASSERT( psa_import_key( &attributes,
4072 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004073 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004074
4075 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4076 {
4077 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4078 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4079 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4080 }
4081
Gilles Peskine1468da72019-05-29 17:35:49 +02004082 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004083 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004084 break;
4085 default:
4086 PSA_ASSERT( psa_key_derivation_input_bytes(
4087 &operation, steps[i],
4088 inputs[i]->x, inputs[i]->len ) );
4089 break;
4090 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004091 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004092
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004093 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004094 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004095 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004096 expected_capacity = requested_capacity;
4097
4098 /* Expansion phase. */
4099 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4100 {
4101 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004102 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004103 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004104 if( expected_capacity == 0 && output_sizes[i] == 0 )
4105 {
4106 /* Reading 0 bytes when 0 bytes are available can go either way. */
4107 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004108 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004109 continue;
4110 }
4111 else if( expected_capacity == 0 ||
4112 output_sizes[i] > expected_capacity )
4113 {
4114 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004115 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004116 expected_capacity = 0;
4117 continue;
4118 }
4119 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004120 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004121 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004122 ASSERT_COMPARE( output_buffer, output_sizes[i],
4123 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004124 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004125 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004126 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004127 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004128 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004129 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004130 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004131
4132exit:
4133 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004134 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004135 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4136 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004137 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004138}
4139/* END_CASE */
4140
4141/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004142void derive_full( int alg_arg,
4143 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004144 data_t *input1,
4145 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004146 int requested_capacity_arg )
4147{
Ronald Cron5425a212020-08-04 14:58:35 +02004148 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004149 psa_algorithm_t alg = alg_arg;
4150 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004151 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004152 unsigned char output_buffer[16];
4153 size_t expected_capacity = requested_capacity;
4154 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004155 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004156
Gilles Peskine8817f612018-12-18 00:18:46 +01004157 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004158
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004159 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4160 psa_set_key_algorithm( &attributes, alg );
4161 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004162
Gilles Peskine049c7532019-05-15 20:22:09 +02004163 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004164 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004165
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004166 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4167 input1->x, input1->len,
4168 input2->x, input2->len,
4169 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004170 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004171
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004172 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004173 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004174 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004175
4176 /* Expansion phase. */
4177 while( current_capacity > 0 )
4178 {
4179 size_t read_size = sizeof( output_buffer );
4180 if( read_size > current_capacity )
4181 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004182 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004183 output_buffer,
4184 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004185 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004186 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004187 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004188 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004189 }
4190
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191 /* Check that the operation refuses to go over capacity. */
4192 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004193 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004194
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004195 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004196
4197exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004198 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004199 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004200 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004201}
4202/* END_CASE */
4203
Janos Follathe60c9052019-07-03 13:51:30 +01004204/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004205void derive_key_exercise( int alg_arg,
4206 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004207 data_t *input1,
4208 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004209 int derived_type_arg,
4210 int derived_bits_arg,
4211 int derived_usage_arg,
4212 int derived_alg_arg )
4213{
Ronald Cron5425a212020-08-04 14:58:35 +02004214 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4215 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004216 psa_algorithm_t alg = alg_arg;
4217 psa_key_type_t derived_type = derived_type_arg;
4218 size_t derived_bits = derived_bits_arg;
4219 psa_key_usage_t derived_usage = derived_usage_arg;
4220 psa_algorithm_t derived_alg = derived_alg_arg;
4221 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004222 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004223 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004224 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004225
Gilles Peskine8817f612018-12-18 00:18:46 +01004226 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004227
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004228 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4229 psa_set_key_algorithm( &attributes, alg );
4230 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004231 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004232 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004233
4234 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004235 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4236 input1->x, input1->len,
4237 input2->x, input2->len,
4238 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004239 goto exit;
4240
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004241 psa_set_key_usage_flags( &attributes, derived_usage );
4242 psa_set_key_algorithm( &attributes, derived_alg );
4243 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004244 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004245 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004246 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004247
4248 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004249 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004250 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4251 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004252
4253 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004254 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004255 goto exit;
4256
4257exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004258 /*
4259 * Key attributes may have been returned by psa_get_key_attributes()
4260 * thus reset them as required.
4261 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004262 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004263
4264 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004265 psa_destroy_key( base_key );
4266 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004267 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004268}
4269/* END_CASE */
4270
Janos Follath42fd8882019-07-03 14:17:09 +01004271/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004272void derive_key_export( int alg_arg,
4273 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004274 data_t *input1,
4275 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004276 int bytes1_arg,
4277 int bytes2_arg )
4278{
Ronald Cron5425a212020-08-04 14:58:35 +02004279 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4280 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004281 psa_algorithm_t alg = alg_arg;
4282 size_t bytes1 = bytes1_arg;
4283 size_t bytes2 = bytes2_arg;
4284 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004285 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004286 uint8_t *output_buffer = NULL;
4287 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004288 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4289 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004290 size_t length;
4291
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004292 ASSERT_ALLOC( output_buffer, capacity );
4293 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004294 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004295
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004296 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4297 psa_set_key_algorithm( &base_attributes, alg );
4298 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004299 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004300 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004301
4302 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004303 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4304 input1->x, input1->len,
4305 input2->x, input2->len,
4306 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004307 goto exit;
4308
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004309 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004310 output_buffer,
4311 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004312 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004313
4314 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004315 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4316 input1->x, input1->len,
4317 input2->x, input2->len,
4318 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004319 goto exit;
4320
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004321 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4322 psa_set_key_algorithm( &derived_attributes, 0 );
4323 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004324 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004325 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004326 &derived_key ) );
4327 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004328 export_buffer, bytes1,
4329 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004330 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004331 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004332 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004333 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004334 &derived_key ) );
4335 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004336 export_buffer + bytes1, bytes2,
4337 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004338 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004339
4340 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004341 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4342 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004343
4344exit:
4345 mbedtls_free( output_buffer );
4346 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004347 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004348 psa_destroy_key( base_key );
4349 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004350 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004351}
4352/* END_CASE */
4353
4354/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004355void derive_key( int alg_arg,
4356 data_t *key_data, data_t *input1, data_t *input2,
4357 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004358 int expected_status_arg,
4359 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004360{
Ronald Cron5425a212020-08-04 14:58:35 +02004361 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4362 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004363 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004364 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004365 size_t bits = bits_arg;
4366 psa_status_t expected_status = expected_status_arg;
4367 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4368 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4369 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4370
4371 PSA_ASSERT( psa_crypto_init( ) );
4372
4373 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4374 psa_set_key_algorithm( &base_attributes, alg );
4375 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4376 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004377 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004378
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004379 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4380 input1->x, input1->len,
4381 input2->x, input2->len,
4382 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004383 goto exit;
4384
4385 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4386 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004387 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004388 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004389
4390 psa_status_t status =
4391 psa_key_derivation_output_key( &derived_attributes,
4392 &operation,
4393 &derived_key );
4394 if( is_large_output > 0 )
4395 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4396 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004397
4398exit:
4399 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004400 psa_destroy_key( base_key );
4401 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004402 PSA_DONE( );
4403}
4404/* END_CASE */
4405
4406/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004407void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004408 int our_key_type_arg, int our_key_alg_arg,
4409 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004410 int expected_status_arg )
4411{
Ronald Cron5425a212020-08-04 14:58:35 +02004412 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004413 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004414 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004415 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004417 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004418 psa_status_t expected_status = expected_status_arg;
4419 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004420
Gilles Peskine8817f612018-12-18 00:18:46 +01004421 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004422
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004423 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004424 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004425 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004426 PSA_ASSERT( psa_import_key( &attributes,
4427 our_key_data->x, our_key_data->len,
4428 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004429
Gilles Peskine77f40d82019-04-11 21:27:06 +02004430 /* The tests currently include inputs that should fail at either step.
4431 * Test cases that fail at the setup step should be changed to call
4432 * key_derivation_setup instead, and this function should be renamed
4433 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004435 if( status == PSA_SUCCESS )
4436 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004437 TEST_EQUAL( psa_key_derivation_key_agreement(
4438 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4439 our_key,
4440 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004441 expected_status );
4442 }
4443 else
4444 {
4445 TEST_ASSERT( status == expected_status );
4446 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004447
4448exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004449 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004450 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004451 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004452}
4453/* END_CASE */
4454
4455/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004456void raw_key_agreement( int alg_arg,
4457 int our_key_type_arg, data_t *our_key_data,
4458 data_t *peer_key_data,
4459 data_t *expected_output )
4460{
Ronald Cron5425a212020-08-04 14:58:35 +02004461 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004462 psa_algorithm_t alg = alg_arg;
4463 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004464 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004465 unsigned char *output = NULL;
4466 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004467 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004468
4469 ASSERT_ALLOC( output, expected_output->len );
4470 PSA_ASSERT( psa_crypto_init( ) );
4471
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4473 psa_set_key_algorithm( &attributes, alg );
4474 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004475 PSA_ASSERT( psa_import_key( &attributes,
4476 our_key_data->x, our_key_data->len,
4477 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004478
gabor-mezei-armceface22021-01-21 12:26:17 +01004479 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4480 key_bits = psa_get_key_bits( &attributes );
4481
Gilles Peskinebe697d82019-05-16 18:00:41 +02004482 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4483 peer_key_data->x, peer_key_data->len,
4484 output, expected_output->len,
4485 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004486 ASSERT_COMPARE( output, output_length,
4487 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004488 TEST_ASSERT( output_length <=
4489 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4490 TEST_ASSERT( output_length <=
4491 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004492
4493exit:
4494 mbedtls_free( output );
4495 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004496 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004497}
4498/* END_CASE */
4499
4500/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004501void key_agreement_capacity( int alg_arg,
4502 int our_key_type_arg, data_t *our_key_data,
4503 data_t *peer_key_data,
4504 int expected_capacity_arg )
4505{
Ronald Cron5425a212020-08-04 14:58:35 +02004506 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004507 psa_algorithm_t alg = alg_arg;
4508 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004509 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004511 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004512 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004513
Gilles Peskine8817f612018-12-18 00:18:46 +01004514 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004515
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004516 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4517 psa_set_key_algorithm( &attributes, alg );
4518 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004519 PSA_ASSERT( psa_import_key( &attributes,
4520 our_key_data->x, our_key_data->len,
4521 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004522
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004523 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004524 PSA_ASSERT( psa_key_derivation_key_agreement(
4525 &operation,
4526 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4527 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004528 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4529 {
4530 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004531 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004532 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004533 NULL, 0 ) );
4534 }
Gilles Peskine59685592018-09-18 12:11:34 +02004535
Gilles Peskinebf491972018-10-25 22:36:12 +02004536 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004537 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004538 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004539 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004540
Gilles Peskinebf491972018-10-25 22:36:12 +02004541 /* Test the actual capacity by reading the output. */
4542 while( actual_capacity > sizeof( output ) )
4543 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004544 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004545 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004546 actual_capacity -= sizeof( output );
4547 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004548 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004549 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004550 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004551 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004552
Gilles Peskine59685592018-09-18 12:11:34 +02004553exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004554 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004555 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004556 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004557}
4558/* END_CASE */
4559
4560/* BEGIN_CASE */
4561void key_agreement_output( int alg_arg,
4562 int our_key_type_arg, data_t *our_key_data,
4563 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004564 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004565{
Ronald Cron5425a212020-08-04 14:58:35 +02004566 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004567 psa_algorithm_t alg = alg_arg;
4568 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004569 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004571 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004572
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004573 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4574 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004575
Gilles Peskine8817f612018-12-18 00:18:46 +01004576 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004577
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004578 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4579 psa_set_key_algorithm( &attributes, alg );
4580 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004581 PSA_ASSERT( psa_import_key( &attributes,
4582 our_key_data->x, our_key_data->len,
4583 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004584
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004585 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004586 PSA_ASSERT( psa_key_derivation_key_agreement(
4587 &operation,
4588 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4589 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004590 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4591 {
4592 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004593 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004594 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004595 NULL, 0 ) );
4596 }
Gilles Peskine59685592018-09-18 12:11:34 +02004597
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004598 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004599 actual_output,
4600 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004601 ASSERT_COMPARE( actual_output, expected_output1->len,
4602 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004603 if( expected_output2->len != 0 )
4604 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004605 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004606 actual_output,
4607 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004608 ASSERT_COMPARE( actual_output, expected_output2->len,
4609 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004610 }
Gilles Peskine59685592018-09-18 12:11:34 +02004611
4612exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004613 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004614 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004615 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004616 mbedtls_free( actual_output );
4617}
4618/* END_CASE */
4619
4620/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004621void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004622{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004623 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004624 unsigned char *output = NULL;
4625 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004626 size_t i;
4627 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004628
Simon Butcher49f8e312020-03-03 15:51:50 +00004629 TEST_ASSERT( bytes_arg >= 0 );
4630
Gilles Peskine91892022021-02-08 19:50:26 +01004631 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004632 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004633
Gilles Peskine8817f612018-12-18 00:18:46 +01004634 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004635
Gilles Peskinea50d7392018-06-21 10:22:13 +02004636 /* Run several times, to ensure that every output byte will be
4637 * nonzero at least once with overwhelming probability
4638 * (2^(-8*number_of_runs)). */
4639 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004640 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004641 if( bytes != 0 )
4642 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004643 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004644
Gilles Peskinea50d7392018-06-21 10:22:13 +02004645 for( i = 0; i < bytes; i++ )
4646 {
4647 if( output[i] != 0 )
4648 ++changed[i];
4649 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004650 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004651
4652 /* Check that every byte was changed to nonzero at least once. This
4653 * validates that psa_generate_random is overwriting every byte of
4654 * the output buffer. */
4655 for( i = 0; i < bytes; i++ )
4656 {
4657 TEST_ASSERT( changed[i] != 0 );
4658 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004659
4660exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004661 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004662 mbedtls_free( output );
4663 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004664}
4665/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004666
4667/* BEGIN_CASE */
4668void generate_key( int type_arg,
4669 int bits_arg,
4670 int usage_arg,
4671 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004672 int expected_status_arg,
4673 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004674{
Ronald Cron5425a212020-08-04 14:58:35 +02004675 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004676 psa_key_type_t type = type_arg;
4677 psa_key_usage_t usage = usage_arg;
4678 size_t bits = bits_arg;
4679 psa_algorithm_t alg = alg_arg;
4680 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004682 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004683
Gilles Peskine8817f612018-12-18 00:18:46 +01004684 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004685
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004686 psa_set_key_usage_flags( &attributes, usage );
4687 psa_set_key_algorithm( &attributes, alg );
4688 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004689 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004690
4691 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004692 psa_status_t status = psa_generate_key( &attributes, &key );
4693
4694 if( is_large_key > 0 )
4695 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4696 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004697 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004698 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004699
4700 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004701 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004702 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4703 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004704
Gilles Peskine818ca122018-06-20 18:16:48 +02004705 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004706 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004707 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004708
4709exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004710 /*
4711 * Key attributes may have been returned by psa_get_key_attributes()
4712 * thus reset them as required.
4713 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004714 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004715
Ronald Cron5425a212020-08-04 14:58:35 +02004716 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004717 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004718}
4719/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004720
Gilles Peskinee56e8782019-04-26 17:34:02 +02004721/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4722void generate_key_rsa( int bits_arg,
4723 data_t *e_arg,
4724 int expected_status_arg )
4725{
Ronald Cron5425a212020-08-04 14:58:35 +02004726 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004727 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004728 size_t bits = bits_arg;
4729 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4730 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4731 psa_status_t expected_status = expected_status_arg;
4732 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4733 uint8_t *exported = NULL;
4734 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004735 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004736 size_t exported_length = SIZE_MAX;
4737 uint8_t *e_read_buffer = NULL;
4738 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004739 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004740 size_t e_read_length = SIZE_MAX;
4741
4742 if( e_arg->len == 0 ||
4743 ( e_arg->len == 3 &&
4744 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4745 {
4746 is_default_public_exponent = 1;
4747 e_read_size = 0;
4748 }
4749 ASSERT_ALLOC( e_read_buffer, e_read_size );
4750 ASSERT_ALLOC( exported, exported_size );
4751
4752 PSA_ASSERT( psa_crypto_init( ) );
4753
4754 psa_set_key_usage_flags( &attributes, usage );
4755 psa_set_key_algorithm( &attributes, alg );
4756 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4757 e_arg->x, e_arg->len ) );
4758 psa_set_key_bits( &attributes, bits );
4759
4760 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004761 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004762 if( expected_status != PSA_SUCCESS )
4763 goto exit;
4764
4765 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004766 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004767 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4768 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4769 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4770 e_read_buffer, e_read_size,
4771 &e_read_length ) );
4772 if( is_default_public_exponent )
4773 TEST_EQUAL( e_read_length, 0 );
4774 else
4775 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4776
4777 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004778 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02004779 goto exit;
4780
4781 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02004782 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02004783 exported, exported_size,
4784 &exported_length ) );
4785 {
4786 uint8_t *p = exported;
4787 uint8_t *end = exported + exported_length;
4788 size_t len;
4789 /* RSAPublicKey ::= SEQUENCE {
4790 * modulus INTEGER, -- n
4791 * publicExponent INTEGER } -- e
4792 */
4793 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004794 MBEDTLS_ASN1_SEQUENCE |
4795 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01004796 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004797 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4798 MBEDTLS_ASN1_INTEGER ) );
4799 if( len >= 1 && p[0] == 0 )
4800 {
4801 ++p;
4802 --len;
4803 }
4804 if( e_arg->len == 0 )
4805 {
4806 TEST_EQUAL( len, 3 );
4807 TEST_EQUAL( p[0], 1 );
4808 TEST_EQUAL( p[1], 0 );
4809 TEST_EQUAL( p[2], 1 );
4810 }
4811 else
4812 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4813 }
4814
4815exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004816 /*
4817 * Key attributes may have been returned by psa_get_key_attributes() or
4818 * set by psa_set_key_domain_parameters() thus reset them as required.
4819 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004820 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004821
Ronald Cron5425a212020-08-04 14:58:35 +02004822 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004823 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004824 mbedtls_free( e_read_buffer );
4825 mbedtls_free( exported );
4826}
4827/* END_CASE */
4828
Darryl Greend49a4992018-06-18 17:27:26 +01004829/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004830void persistent_key_load_key_from_storage( data_t *data,
4831 int type_arg, int bits_arg,
4832 int usage_flags_arg, int alg_arg,
4833 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004834{
Ronald Cron71016a92020-08-28 19:01:50 +02004835 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02004837 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4838 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004839 psa_key_type_t type = type_arg;
4840 size_t bits = bits_arg;
4841 psa_key_usage_t usage_flags = usage_flags_arg;
4842 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004843 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004844 unsigned char *first_export = NULL;
4845 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004846 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004847 size_t first_exported_length;
4848 size_t second_exported_length;
4849
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004850 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4851 {
4852 ASSERT_ALLOC( first_export, export_size );
4853 ASSERT_ALLOC( second_export, export_size );
4854 }
Darryl Greend49a4992018-06-18 17:27:26 +01004855
Gilles Peskine8817f612018-12-18 00:18:46 +01004856 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004857
Gilles Peskinec87af662019-05-15 16:12:22 +02004858 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004859 psa_set_key_usage_flags( &attributes, usage_flags );
4860 psa_set_key_algorithm( &attributes, alg );
4861 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004862 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004863
Darryl Green0c6575a2018-11-07 16:05:30 +00004864 switch( generation_method )
4865 {
4866 case IMPORT_KEY:
4867 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004868 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004869 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004870 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004871
Darryl Green0c6575a2018-11-07 16:05:30 +00004872 case GENERATE_KEY:
4873 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004874 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004875 break;
4876
4877 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01004878#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004879 {
4880 /* Create base key */
4881 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4882 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4883 psa_set_key_usage_flags( &base_attributes,
4884 PSA_KEY_USAGE_DERIVE );
4885 psa_set_key_algorithm( &base_attributes, derive_alg );
4886 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004887 PSA_ASSERT( psa_import_key( &base_attributes,
4888 data->x, data->len,
4889 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004890 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004891 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004892 PSA_ASSERT( psa_key_derivation_input_key(
4893 &operation,
4894 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004895 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004897 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004898 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
4899 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004900 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004902 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02004903 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004904 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01004905#else
4906 TEST_ASSUME( ! "KDF not supported in this configuration" );
4907#endif
4908 break;
4909
4910 default:
4911 TEST_ASSERT( ! "generation_method not implemented in test" );
4912 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00004913 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004914 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004915
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004916 /* Export the key if permitted by the key policy. */
4917 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4918 {
Ronald Cron5425a212020-08-04 14:58:35 +02004919 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004920 first_export, export_size,
4921 &first_exported_length ) );
4922 if( generation_method == IMPORT_KEY )
4923 ASSERT_COMPARE( data->x, data->len,
4924 first_export, first_exported_length );
4925 }
Darryl Greend49a4992018-06-18 17:27:26 +01004926
4927 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02004928 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004929 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01004930 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004931
Darryl Greend49a4992018-06-18 17:27:26 +01004932 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02004933 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02004934 TEST_ASSERT( mbedtls_svc_key_id_equal(
4935 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004936 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4937 PSA_KEY_LIFETIME_PERSISTENT );
4938 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4939 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4940 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4941 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004942
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004943 /* Export the key again if permitted by the key policy. */
4944 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004945 {
Ronald Cron5425a212020-08-04 14:58:35 +02004946 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004947 second_export, export_size,
4948 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004949 ASSERT_COMPARE( first_export, first_exported_length,
4950 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004951 }
4952
4953 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004954 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004955 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004956
4957exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004958 /*
4959 * Key attributes may have been returned by psa_get_key_attributes()
4960 * thus reset them as required.
4961 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004962 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004963
Darryl Greend49a4992018-06-18 17:27:26 +01004964 mbedtls_free( first_export );
4965 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004967 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02004968 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004969 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01004970}
4971/* END_CASE */