blob: 484e97f770ea19806f46650001805064dbb0e25e [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,
Ronald Cron88a55462021-03-31 09:39:07 +02001375 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001376 int expected_status_arg )
1377{
1378 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1379 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001380 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1381 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001382 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001383
1384 PSA_ASSERT( psa_crypto_init( ) );
1385
1386 /* Prepare the source key. */
1387 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1388 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001389 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001390 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001391 PSA_ASSERT( psa_import_key( &source_attributes,
1392 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001393 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001394
1395 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001396 psa_set_key_id( &target_attributes, key_id );
1397 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001398 psa_set_key_type( &target_attributes, target_type_arg );
1399 psa_set_key_bits( &target_attributes, target_bits_arg );
1400 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1401 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001402 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001403
1404 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001405 TEST_EQUAL( psa_copy_key( source_key,
1406 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001407 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001408
Ronald Cron5425a212020-08-04 14:58:35 +02001409 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001410
Gilles Peskine4a644642019-05-03 17:14:08 +02001411exit:
1412 psa_reset_key_attributes( &source_attributes );
1413 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001414 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001415}
1416/* END_CASE */
1417
1418/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001419void hash_operation_init( )
1420{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001421 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001422 /* Test each valid way of initializing the object, except for `= {0}`, as
1423 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1424 * though it's OK by the C standard. We could test for this, but we'd need
1425 * to supress the Clang warning for the test. */
1426 psa_hash_operation_t func = psa_hash_operation_init( );
1427 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1428 psa_hash_operation_t zero;
1429
1430 memset( &zero, 0, sizeof( zero ) );
1431
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001432 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001433 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1434 PSA_ERROR_BAD_STATE );
1435 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1436 PSA_ERROR_BAD_STATE );
1437 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1438 PSA_ERROR_BAD_STATE );
1439
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001440 /* A default hash operation should be abortable without error. */
1441 PSA_ASSERT( psa_hash_abort( &func ) );
1442 PSA_ASSERT( psa_hash_abort( &init ) );
1443 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001444}
1445/* END_CASE */
1446
1447/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001448void hash_setup( int alg_arg,
1449 int expected_status_arg )
1450{
1451 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001452 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001453 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001454 psa_status_t status;
1455
Gilles Peskine8817f612018-12-18 00:18:46 +01001456 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001457
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001458 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001459 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001461 /* Whether setup succeeded or failed, abort must succeed. */
1462 PSA_ASSERT( psa_hash_abort( &operation ) );
1463
1464 /* If setup failed, reproduce the failure, so as to
1465 * test the resulting state of the operation object. */
1466 if( status != PSA_SUCCESS )
1467 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1468
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001469 /* Now the operation object should be reusable. */
1470#if defined(KNOWN_SUPPORTED_HASH_ALG)
1471 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1472 PSA_ASSERT( psa_hash_abort( &operation ) );
1473#endif
1474
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001475exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001476 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001477}
1478/* END_CASE */
1479
1480/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001481void hash_compute_fail( int alg_arg, data_t *input,
1482 int output_size_arg, int expected_status_arg )
1483{
1484 psa_algorithm_t alg = alg_arg;
1485 uint8_t *output = NULL;
1486 size_t output_size = output_size_arg;
1487 size_t output_length = INVALID_EXPORT_LENGTH;
1488 psa_status_t expected_status = expected_status_arg;
1489 psa_status_t status;
1490
1491 ASSERT_ALLOC( output, output_size );
1492
1493 PSA_ASSERT( psa_crypto_init( ) );
1494
1495 status = psa_hash_compute( alg, input->x, input->len,
1496 output, output_size, &output_length );
1497 TEST_EQUAL( status, expected_status );
1498 TEST_ASSERT( output_length <= output_size );
1499
1500exit:
1501 mbedtls_free( output );
1502 PSA_DONE( );
1503}
1504/* END_CASE */
1505
1506/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001507void hash_compare_fail( int alg_arg, data_t *input,
1508 data_t *reference_hash,
1509 int expected_status_arg )
1510{
1511 psa_algorithm_t alg = alg_arg;
1512 psa_status_t expected_status = expected_status_arg;
1513 psa_status_t status;
1514
1515 PSA_ASSERT( psa_crypto_init( ) );
1516
1517 status = psa_hash_compare( alg, input->x, input->len,
1518 reference_hash->x, reference_hash->len );
1519 TEST_EQUAL( status, expected_status );
1520
1521exit:
1522 PSA_DONE( );
1523}
1524/* END_CASE */
1525
1526/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001527void hash_compute_compare( int alg_arg, data_t *input,
1528 data_t *expected_output )
1529{
1530 psa_algorithm_t alg = alg_arg;
1531 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1532 size_t output_length = INVALID_EXPORT_LENGTH;
1533 size_t i;
1534
1535 PSA_ASSERT( psa_crypto_init( ) );
1536
1537 /* Compute with tight buffer */
1538 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001539 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001540 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001541 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001542 ASSERT_COMPARE( output, output_length,
1543 expected_output->x, expected_output->len );
1544
1545 /* Compute with larger buffer */
1546 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1547 output, sizeof( output ),
1548 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001549 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001550 ASSERT_COMPARE( output, output_length,
1551 expected_output->x, expected_output->len );
1552
1553 /* Compare with correct hash */
1554 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1555 output, output_length ) );
1556
1557 /* Compare with trailing garbage */
1558 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1559 output, output_length + 1 ),
1560 PSA_ERROR_INVALID_SIGNATURE );
1561
1562 /* Compare with truncated hash */
1563 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1564 output, output_length - 1 ),
1565 PSA_ERROR_INVALID_SIGNATURE );
1566
1567 /* Compare with corrupted value */
1568 for( i = 0; i < output_length; i++ )
1569 {
Chris Jones9634bb12021-01-20 15:56:42 +00001570 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001571 output[i] ^= 1;
1572 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1573 output, output_length ),
1574 PSA_ERROR_INVALID_SIGNATURE );
1575 output[i] ^= 1;
1576 }
1577
1578exit:
1579 PSA_DONE( );
1580}
1581/* END_CASE */
1582
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001583/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001584void hash_bad_order( )
1585{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001586 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001587 unsigned char input[] = "";
1588 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001589 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001590 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1591 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1592 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001593 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001594 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001595 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001596
Gilles Peskine8817f612018-12-18 00:18:46 +01001597 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001598
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001599 /* Call setup twice in a row. */
1600 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1601 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1602 PSA_ERROR_BAD_STATE );
1603 PSA_ASSERT( psa_hash_abort( &operation ) );
1604
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001605 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001606 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001607 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001608 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001609
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001610 /* Call update after finish. */
1611 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1612 PSA_ASSERT( psa_hash_finish( &operation,
1613 hash, sizeof( hash ), &hash_len ) );
1614 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001615 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001616 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001617
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001618 /* Call verify without calling setup beforehand. */
1619 TEST_EQUAL( psa_hash_verify( &operation,
1620 valid_hash, sizeof( valid_hash ) ),
1621 PSA_ERROR_BAD_STATE );
1622 PSA_ASSERT( psa_hash_abort( &operation ) );
1623
1624 /* Call verify after finish. */
1625 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1626 PSA_ASSERT( psa_hash_finish( &operation,
1627 hash, sizeof( hash ), &hash_len ) );
1628 TEST_EQUAL( psa_hash_verify( &operation,
1629 valid_hash, sizeof( valid_hash ) ),
1630 PSA_ERROR_BAD_STATE );
1631 PSA_ASSERT( psa_hash_abort( &operation ) );
1632
1633 /* Call verify twice in a row. */
1634 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1635 PSA_ASSERT( psa_hash_verify( &operation,
1636 valid_hash, sizeof( valid_hash ) ) );
1637 TEST_EQUAL( psa_hash_verify( &operation,
1638 valid_hash, sizeof( valid_hash ) ),
1639 PSA_ERROR_BAD_STATE );
1640 PSA_ASSERT( psa_hash_abort( &operation ) );
1641
1642 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001643 TEST_EQUAL( psa_hash_finish( &operation,
1644 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001645 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001646 PSA_ASSERT( psa_hash_abort( &operation ) );
1647
1648 /* Call finish twice in a row. */
1649 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1650 PSA_ASSERT( psa_hash_finish( &operation,
1651 hash, sizeof( hash ), &hash_len ) );
1652 TEST_EQUAL( psa_hash_finish( &operation,
1653 hash, sizeof( hash ), &hash_len ),
1654 PSA_ERROR_BAD_STATE );
1655 PSA_ASSERT( psa_hash_abort( &operation ) );
1656
1657 /* Call finish after calling verify. */
1658 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1659 PSA_ASSERT( psa_hash_verify( &operation,
1660 valid_hash, sizeof( valid_hash ) ) );
1661 TEST_EQUAL( psa_hash_finish( &operation,
1662 hash, sizeof( hash ), &hash_len ),
1663 PSA_ERROR_BAD_STATE );
1664 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001665
1666exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001667 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001668}
1669/* END_CASE */
1670
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001671/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001672void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001673{
1674 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001675 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1676 * appended to it */
1677 unsigned char hash[] = {
1678 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1679 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1680 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001681 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001682 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001683
Gilles Peskine8817f612018-12-18 00:18:46 +01001684 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001685
itayzafrir27e69452018-11-01 14:26:34 +02001686 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001688 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001689 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001690
itayzafrir27e69452018-11-01 14:26:34 +02001691 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001692 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001693 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001694 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001695
itayzafrir27e69452018-11-01 14:26:34 +02001696 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001697 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001698 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001699 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001700
itayzafrirec93d302018-10-18 18:01:10 +03001701exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001702 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001703}
1704/* END_CASE */
1705
Ronald Cronee414c72021-03-18 18:50:08 +01001706/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001707void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001708{
1709 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001710 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001711 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001712 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001713 size_t hash_len;
1714
Gilles Peskine8817f612018-12-18 00:18:46 +01001715 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001716
itayzafrir58028322018-10-25 10:22:01 +03001717 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001719 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001720 hash, expected_size - 1, &hash_len ),
1721 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001722
1723exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001724 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001725}
1726/* END_CASE */
1727
Ronald Cronee414c72021-03-18 18:50:08 +01001728/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001729void hash_clone_source_state( )
1730{
1731 psa_algorithm_t alg = PSA_ALG_SHA_256;
1732 unsigned char hash[PSA_HASH_MAX_SIZE];
1733 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1734 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1735 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1736 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1737 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1738 size_t hash_len;
1739
1740 PSA_ASSERT( psa_crypto_init( ) );
1741 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1742
1743 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1744 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1745 PSA_ASSERT( psa_hash_finish( &op_finished,
1746 hash, sizeof( hash ), &hash_len ) );
1747 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1748 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1749
1750 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1751 PSA_ERROR_BAD_STATE );
1752
1753 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1754 PSA_ASSERT( psa_hash_finish( &op_init,
1755 hash, sizeof( hash ), &hash_len ) );
1756 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1757 PSA_ASSERT( psa_hash_finish( &op_finished,
1758 hash, sizeof( hash ), &hash_len ) );
1759 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1760 PSA_ASSERT( psa_hash_finish( &op_aborted,
1761 hash, sizeof( hash ), &hash_len ) );
1762
1763exit:
1764 psa_hash_abort( &op_source );
1765 psa_hash_abort( &op_init );
1766 psa_hash_abort( &op_setup );
1767 psa_hash_abort( &op_finished );
1768 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001769 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001770}
1771/* END_CASE */
1772
Ronald Cronee414c72021-03-18 18:50:08 +01001773/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001774void hash_clone_target_state( )
1775{
1776 psa_algorithm_t alg = PSA_ALG_SHA_256;
1777 unsigned char hash[PSA_HASH_MAX_SIZE];
1778 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1779 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1780 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1781 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1782 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1783 size_t hash_len;
1784
1785 PSA_ASSERT( psa_crypto_init( ) );
1786
1787 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1788 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1789 PSA_ASSERT( psa_hash_finish( &op_finished,
1790 hash, sizeof( hash ), &hash_len ) );
1791 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1792 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1793
1794 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1795 PSA_ASSERT( psa_hash_finish( &op_target,
1796 hash, sizeof( hash ), &hash_len ) );
1797
1798 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1799 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1800 PSA_ERROR_BAD_STATE );
1801 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1802 PSA_ERROR_BAD_STATE );
1803
1804exit:
1805 psa_hash_abort( &op_target );
1806 psa_hash_abort( &op_init );
1807 psa_hash_abort( &op_setup );
1808 psa_hash_abort( &op_finished );
1809 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001810 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001811}
1812/* END_CASE */
1813
itayzafrir58028322018-10-25 10:22:01 +03001814/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001815void mac_operation_init( )
1816{
Jaeden Amero252ef282019-02-15 14:05:35 +00001817 const uint8_t input[1] = { 0 };
1818
Jaeden Amero769ce272019-01-04 11:48:03 +00001819 /* Test each valid way of initializing the object, except for `= {0}`, as
1820 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1821 * though it's OK by the C standard. We could test for this, but we'd need
1822 * to supress the Clang warning for the test. */
1823 psa_mac_operation_t func = psa_mac_operation_init( );
1824 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1825 psa_mac_operation_t zero;
1826
1827 memset( &zero, 0, sizeof( zero ) );
1828
Jaeden Amero252ef282019-02-15 14:05:35 +00001829 /* A freshly-initialized MAC operation should not be usable. */
1830 TEST_EQUAL( psa_mac_update( &func,
1831 input, sizeof( input ) ),
1832 PSA_ERROR_BAD_STATE );
1833 TEST_EQUAL( psa_mac_update( &init,
1834 input, sizeof( input ) ),
1835 PSA_ERROR_BAD_STATE );
1836 TEST_EQUAL( psa_mac_update( &zero,
1837 input, sizeof( input ) ),
1838 PSA_ERROR_BAD_STATE );
1839
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001840 /* A default MAC operation should be abortable without error. */
1841 PSA_ASSERT( psa_mac_abort( &func ) );
1842 PSA_ASSERT( psa_mac_abort( &init ) );
1843 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001844}
1845/* END_CASE */
1846
1847/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001848void mac_setup( int key_type_arg,
1849 data_t *key,
1850 int alg_arg,
1851 int expected_status_arg )
1852{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001853 psa_key_type_t key_type = key_type_arg;
1854 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001855 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001856 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001857 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1858#if defined(KNOWN_SUPPORTED_MAC_ALG)
1859 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1860#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001861
Gilles Peskine8817f612018-12-18 00:18:46 +01001862 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001863
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001864 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1865 &operation, &status ) )
1866 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001867 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001868
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001869 /* The operation object should be reusable. */
1870#if defined(KNOWN_SUPPORTED_MAC_ALG)
1871 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1872 smoke_test_key_data,
1873 sizeof( smoke_test_key_data ),
1874 KNOWN_SUPPORTED_MAC_ALG,
1875 &operation, &status ) )
1876 goto exit;
1877 TEST_EQUAL( status, PSA_SUCCESS );
1878#endif
1879
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001880exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001881 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001882}
1883/* END_CASE */
1884
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001885/* 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 +00001886void mac_bad_order( )
1887{
Ronald Cron5425a212020-08-04 14:58:35 +02001888 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001889 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1890 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001891 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001892 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1893 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1894 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001895 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001896 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1897 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1898 size_t sign_mac_length = 0;
1899 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1900 const uint8_t verify_mac[] = {
1901 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1902 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1903 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1904
1905 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001906 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001907 psa_set_key_algorithm( &attributes, alg );
1908 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001909
Ronald Cron5425a212020-08-04 14:58:35 +02001910 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1911 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001912
Jaeden Amero252ef282019-02-15 14:05:35 +00001913 /* Call update without calling setup beforehand. */
1914 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1915 PSA_ERROR_BAD_STATE );
1916 PSA_ASSERT( psa_mac_abort( &operation ) );
1917
1918 /* Call sign finish without calling setup beforehand. */
1919 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1920 &sign_mac_length),
1921 PSA_ERROR_BAD_STATE );
1922 PSA_ASSERT( psa_mac_abort( &operation ) );
1923
1924 /* Call verify finish without calling setup beforehand. */
1925 TEST_EQUAL( psa_mac_verify_finish( &operation,
1926 verify_mac, sizeof( verify_mac ) ),
1927 PSA_ERROR_BAD_STATE );
1928 PSA_ASSERT( psa_mac_abort( &operation ) );
1929
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001930 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001931 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1932 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001933 PSA_ERROR_BAD_STATE );
1934 PSA_ASSERT( psa_mac_abort( &operation ) );
1935
Jaeden Amero252ef282019-02-15 14:05:35 +00001936 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001937 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001938 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1939 PSA_ASSERT( psa_mac_sign_finish( &operation,
1940 sign_mac, sizeof( sign_mac ),
1941 &sign_mac_length ) );
1942 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1943 PSA_ERROR_BAD_STATE );
1944 PSA_ASSERT( psa_mac_abort( &operation ) );
1945
1946 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001947 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001948 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1949 PSA_ASSERT( psa_mac_verify_finish( &operation,
1950 verify_mac, sizeof( verify_mac ) ) );
1951 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1952 PSA_ERROR_BAD_STATE );
1953 PSA_ASSERT( psa_mac_abort( &operation ) );
1954
1955 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001956 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001957 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1958 PSA_ASSERT( psa_mac_sign_finish( &operation,
1959 sign_mac, sizeof( sign_mac ),
1960 &sign_mac_length ) );
1961 TEST_EQUAL( psa_mac_sign_finish( &operation,
1962 sign_mac, sizeof( sign_mac ),
1963 &sign_mac_length ),
1964 PSA_ERROR_BAD_STATE );
1965 PSA_ASSERT( psa_mac_abort( &operation ) );
1966
1967 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001968 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001969 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1970 PSA_ASSERT( psa_mac_verify_finish( &operation,
1971 verify_mac, sizeof( verify_mac ) ) );
1972 TEST_EQUAL( psa_mac_verify_finish( &operation,
1973 verify_mac, sizeof( verify_mac ) ),
1974 PSA_ERROR_BAD_STATE );
1975 PSA_ASSERT( psa_mac_abort( &operation ) );
1976
1977 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001978 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001979 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1980 TEST_EQUAL( psa_mac_verify_finish( &operation,
1981 verify_mac, sizeof( verify_mac ) ),
1982 PSA_ERROR_BAD_STATE );
1983 PSA_ASSERT( psa_mac_abort( &operation ) );
1984
1985 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001986 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001987 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1988 TEST_EQUAL( psa_mac_sign_finish( &operation,
1989 sign_mac, sizeof( sign_mac ),
1990 &sign_mac_length ),
1991 PSA_ERROR_BAD_STATE );
1992 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001993
Ronald Cron5425a212020-08-04 14:58:35 +02001994 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001995
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001996exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001997 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001998}
1999/* END_CASE */
2000
2001/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002002void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002003 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002004 int alg_arg,
2005 data_t *input,
2006 data_t *expected_mac )
2007{
Ronald Cron5425a212020-08-04 14:58:35 +02002008 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002009 psa_key_type_t key_type = key_type_arg;
2010 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002011 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002012 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002013 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002014 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002015 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002016 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002017 const size_t output_sizes_to_test[] = {
2018 0,
2019 1,
2020 expected_mac->len - 1,
2021 expected_mac->len,
2022 expected_mac->len + 1,
2023 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002024
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002025 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002026 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002027 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002028
Gilles Peskine8817f612018-12-18 00:18:46 +01002029 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002030
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002031 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002032 psa_set_key_algorithm( &attributes, alg );
2033 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002034
Ronald Cron5425a212020-08-04 14:58:35 +02002035 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2036 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002037
Gilles Peskine8b356b52020-08-25 23:44:59 +02002038 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2039 {
2040 const size_t output_size = output_sizes_to_test[i];
2041 psa_status_t expected_status =
2042 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2043 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002044
Chris Jones9634bb12021-01-20 15:56:42 +00002045 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002046 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002047
Gilles Peskine8b356b52020-08-25 23:44:59 +02002048 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002049 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002050 PSA_ASSERT( psa_mac_update( &operation,
2051 input->x, input->len ) );
2052 TEST_EQUAL( psa_mac_sign_finish( &operation,
2053 actual_mac, output_size,
2054 &mac_length ),
2055 expected_status );
2056 PSA_ASSERT( psa_mac_abort( &operation ) );
2057
2058 if( expected_status == PSA_SUCCESS )
2059 {
2060 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2061 actual_mac, mac_length );
2062 }
2063 mbedtls_free( actual_mac );
2064 actual_mac = NULL;
2065 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002066
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002067exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002068 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002069 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002070 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002071 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002072}
2073/* END_CASE */
2074
2075/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002076void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002077 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002078 int alg_arg,
2079 data_t *input,
2080 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081{
Ronald Cron5425a212020-08-04 14:58:35 +02002082 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002083 psa_key_type_t key_type = key_type_arg;
2084 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002085 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002086 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002087 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002088
Gilles Peskine69c12672018-06-28 00:07:19 +02002089 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2090
Gilles Peskine8817f612018-12-18 00:18:46 +01002091 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002092
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002093 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002094 psa_set_key_algorithm( &attributes, alg );
2095 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002096
Ronald Cron5425a212020-08-04 14:58:35 +02002097 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2098 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002099
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002100 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002101 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002102 PSA_ASSERT( psa_mac_update( &operation,
2103 input->x, input->len ) );
2104 PSA_ASSERT( psa_mac_verify_finish( &operation,
2105 expected_mac->x,
2106 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002107
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002108 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002109 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002110 PSA_ASSERT( psa_mac_update( &operation,
2111 input->x, input->len ) );
2112 TEST_EQUAL( psa_mac_verify_finish( &operation,
2113 expected_mac->x,
2114 expected_mac->len - 1 ),
2115 PSA_ERROR_INVALID_SIGNATURE );
2116
2117 /* Test a MAC that's too long. */
2118 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2119 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002120 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002121 PSA_ASSERT( psa_mac_update( &operation,
2122 input->x, input->len ) );
2123 TEST_EQUAL( psa_mac_verify_finish( &operation,
2124 perturbed_mac,
2125 expected_mac->len + 1 ),
2126 PSA_ERROR_INVALID_SIGNATURE );
2127
2128 /* Test changing one byte. */
2129 for( size_t i = 0; i < expected_mac->len; i++ )
2130 {
Chris Jones9634bb12021-01-20 15:56:42 +00002131 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002132 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002133 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002134 PSA_ASSERT( psa_mac_update( &operation,
2135 input->x, input->len ) );
2136 TEST_EQUAL( psa_mac_verify_finish( &operation,
2137 perturbed_mac,
2138 expected_mac->len ),
2139 PSA_ERROR_INVALID_SIGNATURE );
2140 perturbed_mac[i] ^= 1;
2141 }
2142
Gilles Peskine8c9def32018-02-08 10:02:12 +01002143exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002144 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002145 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002146 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002147 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002148}
2149/* END_CASE */
2150
2151/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002152void cipher_operation_init( )
2153{
Jaeden Ameroab439972019-02-15 14:12:05 +00002154 const uint8_t input[1] = { 0 };
2155 unsigned char output[1] = { 0 };
2156 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002157 /* Test each valid way of initializing the object, except for `= {0}`, as
2158 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2159 * though it's OK by the C standard. We could test for this, but we'd need
2160 * to supress the Clang warning for the test. */
2161 psa_cipher_operation_t func = psa_cipher_operation_init( );
2162 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2163 psa_cipher_operation_t zero;
2164
2165 memset( &zero, 0, sizeof( zero ) );
2166
Jaeden Ameroab439972019-02-15 14:12:05 +00002167 /* A freshly-initialized cipher operation should not be usable. */
2168 TEST_EQUAL( psa_cipher_update( &func,
2169 input, sizeof( input ),
2170 output, sizeof( output ),
2171 &output_length ),
2172 PSA_ERROR_BAD_STATE );
2173 TEST_EQUAL( psa_cipher_update( &init,
2174 input, sizeof( input ),
2175 output, sizeof( output ),
2176 &output_length ),
2177 PSA_ERROR_BAD_STATE );
2178 TEST_EQUAL( psa_cipher_update( &zero,
2179 input, sizeof( input ),
2180 output, sizeof( output ),
2181 &output_length ),
2182 PSA_ERROR_BAD_STATE );
2183
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002184 /* A default cipher operation should be abortable without error. */
2185 PSA_ASSERT( psa_cipher_abort( &func ) );
2186 PSA_ASSERT( psa_cipher_abort( &init ) );
2187 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002188}
2189/* END_CASE */
2190
2191/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002192void cipher_setup( int key_type_arg,
2193 data_t *key,
2194 int alg_arg,
2195 int expected_status_arg )
2196{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002197 psa_key_type_t key_type = key_type_arg;
2198 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002199 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002200 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002201 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002202#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002203 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2204#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002205
Gilles Peskine8817f612018-12-18 00:18:46 +01002206 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002207
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002208 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2209 &operation, &status ) )
2210 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002211 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002212
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002213 /* The operation object should be reusable. */
2214#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2215 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2216 smoke_test_key_data,
2217 sizeof( smoke_test_key_data ),
2218 KNOWN_SUPPORTED_CIPHER_ALG,
2219 &operation, &status ) )
2220 goto exit;
2221 TEST_EQUAL( status, PSA_SUCCESS );
2222#endif
2223
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002224exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002225 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002226 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002227}
2228/* END_CASE */
2229
Ronald Cronee414c72021-03-18 18:50:08 +01002230/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002231void cipher_bad_order( )
2232{
Ronald Cron5425a212020-08-04 14:58:35 +02002233 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002234 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2235 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002236 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002237 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002238 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002239 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002240 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2241 0xaa, 0xaa, 0xaa, 0xaa };
2242 const uint8_t text[] = {
2243 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2244 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002245 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002246 size_t length = 0;
2247
2248 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002249 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2250 psa_set_key_algorithm( &attributes, alg );
2251 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002252 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2253 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002254
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002255 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002256 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2257 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002258 PSA_ERROR_BAD_STATE );
2259 PSA_ASSERT( psa_cipher_abort( &operation ) );
2260
2261 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002262 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2263 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002264 PSA_ERROR_BAD_STATE );
2265 PSA_ASSERT( psa_cipher_abort( &operation ) );
2266
Jaeden Ameroab439972019-02-15 14:12:05 +00002267 /* Generate an IV without calling setup beforehand. */
2268 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2269 buffer, sizeof( buffer ),
2270 &length ),
2271 PSA_ERROR_BAD_STATE );
2272 PSA_ASSERT( psa_cipher_abort( &operation ) );
2273
2274 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002275 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002276 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2277 buffer, sizeof( buffer ),
2278 &length ) );
2279 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2280 buffer, sizeof( buffer ),
2281 &length ),
2282 PSA_ERROR_BAD_STATE );
2283 PSA_ASSERT( psa_cipher_abort( &operation ) );
2284
2285 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002286 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002287 PSA_ASSERT( psa_cipher_set_iv( &operation,
2288 iv, sizeof( iv ) ) );
2289 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2290 buffer, sizeof( buffer ),
2291 &length ),
2292 PSA_ERROR_BAD_STATE );
2293 PSA_ASSERT( psa_cipher_abort( &operation ) );
2294
2295 /* Set an IV without calling setup beforehand. */
2296 TEST_EQUAL( psa_cipher_set_iv( &operation,
2297 iv, sizeof( iv ) ),
2298 PSA_ERROR_BAD_STATE );
2299 PSA_ASSERT( psa_cipher_abort( &operation ) );
2300
2301 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002302 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002303 PSA_ASSERT( psa_cipher_set_iv( &operation,
2304 iv, sizeof( iv ) ) );
2305 TEST_EQUAL( psa_cipher_set_iv( &operation,
2306 iv, sizeof( iv ) ),
2307 PSA_ERROR_BAD_STATE );
2308 PSA_ASSERT( psa_cipher_abort( &operation ) );
2309
2310 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002311 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002312 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2313 buffer, sizeof( buffer ),
2314 &length ) );
2315 TEST_EQUAL( psa_cipher_set_iv( &operation,
2316 iv, sizeof( iv ) ),
2317 PSA_ERROR_BAD_STATE );
2318 PSA_ASSERT( psa_cipher_abort( &operation ) );
2319
2320 /* Call update without calling setup beforehand. */
2321 TEST_EQUAL( psa_cipher_update( &operation,
2322 text, sizeof( text ),
2323 buffer, sizeof( buffer ),
2324 &length ),
2325 PSA_ERROR_BAD_STATE );
2326 PSA_ASSERT( psa_cipher_abort( &operation ) );
2327
2328 /* Call update without an IV where an IV is required. */
2329 TEST_EQUAL( psa_cipher_update( &operation,
2330 text, sizeof( text ),
2331 buffer, sizeof( buffer ),
2332 &length ),
2333 PSA_ERROR_BAD_STATE );
2334 PSA_ASSERT( psa_cipher_abort( &operation ) );
2335
2336 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002337 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002338 PSA_ASSERT( psa_cipher_set_iv( &operation,
2339 iv, sizeof( iv ) ) );
2340 PSA_ASSERT( psa_cipher_finish( &operation,
2341 buffer, sizeof( buffer ), &length ) );
2342 TEST_EQUAL( psa_cipher_update( &operation,
2343 text, sizeof( text ),
2344 buffer, sizeof( buffer ),
2345 &length ),
2346 PSA_ERROR_BAD_STATE );
2347 PSA_ASSERT( psa_cipher_abort( &operation ) );
2348
2349 /* Call finish without calling setup beforehand. */
2350 TEST_EQUAL( psa_cipher_finish( &operation,
2351 buffer, sizeof( buffer ), &length ),
2352 PSA_ERROR_BAD_STATE );
2353 PSA_ASSERT( psa_cipher_abort( &operation ) );
2354
2355 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002356 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002357 /* Not calling update means we are encrypting an empty buffer, which is OK
2358 * for cipher modes with padding. */
2359 TEST_EQUAL( psa_cipher_finish( &operation,
2360 buffer, sizeof( buffer ), &length ),
2361 PSA_ERROR_BAD_STATE );
2362 PSA_ASSERT( psa_cipher_abort( &operation ) );
2363
2364 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002365 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002366 PSA_ASSERT( psa_cipher_set_iv( &operation,
2367 iv, sizeof( iv ) ) );
2368 PSA_ASSERT( psa_cipher_finish( &operation,
2369 buffer, sizeof( buffer ), &length ) );
2370 TEST_EQUAL( psa_cipher_finish( &operation,
2371 buffer, sizeof( buffer ), &length ),
2372 PSA_ERROR_BAD_STATE );
2373 PSA_ASSERT( psa_cipher_abort( &operation ) );
2374
Ronald Cron5425a212020-08-04 14:58:35 +02002375 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002376
Jaeden Ameroab439972019-02-15 14:12:05 +00002377exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002378 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002379 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002380}
2381/* END_CASE */
2382
2383/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002384void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002385 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002386 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002387 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002388{
Ronald Cron5425a212020-08-04 14:58:35 +02002389 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002390 psa_status_t status;
2391 psa_key_type_t key_type = key_type_arg;
2392 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002393 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002394 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002395 size_t output_buffer_size = 0;
2396 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002397 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002398 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002399 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002400
Gilles Peskine8817f612018-12-18 00:18:46 +01002401 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002402
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002403 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2404 psa_set_key_algorithm( &attributes, alg );
2405 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002406
Ronald Cron5425a212020-08-04 14:58:35 +02002407 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2408 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002409
Ronald Cron5425a212020-08-04 14:58:35 +02002410 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002411
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002412 if( iv->len > 0 )
2413 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002414 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002415 }
2416
gabor-mezei-armceface22021-01-21 12:26:17 +01002417 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2418 TEST_ASSERT( output_buffer_size <=
2419 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002420 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002421
Gilles Peskine8817f612018-12-18 00:18:46 +01002422 PSA_ASSERT( psa_cipher_update( &operation,
2423 input->x, input->len,
2424 output, output_buffer_size,
2425 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002426 TEST_ASSERT( function_output_length <=
2427 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2428 TEST_ASSERT( function_output_length <=
2429 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002430 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002431
Gilles Peskine50e586b2018-06-08 14:28:46 +02002432 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002433 ( output_buffer_size == 0 ? NULL :
2434 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002435 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002436 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002437 TEST_ASSERT( function_output_length <=
2438 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2439 TEST_ASSERT( function_output_length <=
2440 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002441 total_output_length += function_output_length;
2442
Gilles Peskinefe11b722018-12-18 00:24:04 +01002443 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002444 if( expected_status == PSA_SUCCESS )
2445 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002446 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002447 ASSERT_COMPARE( expected_output->x, expected_output->len,
2448 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002449 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002450
Gilles Peskine50e586b2018-06-08 14:28:46 +02002451exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002452 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002453 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002454 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002455 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002456}
2457/* END_CASE */
2458
2459/* BEGIN_CASE */
2460void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002461 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002462 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002463 int first_part_size_arg,
2464 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002465 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002466{
Ronald Cron5425a212020-08-04 14:58:35 +02002467 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002468 psa_key_type_t key_type = key_type_arg;
2469 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002470 size_t first_part_size = first_part_size_arg;
2471 size_t output1_length = output1_length_arg;
2472 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002473 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002474 size_t output_buffer_size = 0;
2475 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002476 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002477 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002479
Gilles Peskine8817f612018-12-18 00:18:46 +01002480 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002481
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002482 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2483 psa_set_key_algorithm( &attributes, alg );
2484 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002485
Ronald Cron5425a212020-08-04 14:58:35 +02002486 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2487 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002488
Ronald Cron5425a212020-08-04 14:58:35 +02002489 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002490
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002491 if( iv->len > 0 )
2492 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002493 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002494 }
2495
gabor-mezei-armceface22021-01-21 12:26:17 +01002496 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2497 TEST_ASSERT( output_buffer_size <=
2498 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002499 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002500
Gilles Peskinee0866522019-02-19 19:44:00 +01002501 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002502 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2503 output, output_buffer_size,
2504 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002505 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002506 TEST_ASSERT( function_output_length <=
2507 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2508 TEST_ASSERT( function_output_length <=
2509 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002510 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002511
Gilles Peskine8817f612018-12-18 00:18:46 +01002512 PSA_ASSERT( psa_cipher_update( &operation,
2513 input->x + first_part_size,
2514 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002515 ( output_buffer_size == 0 ? NULL :
2516 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002517 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002518 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002519 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002520 TEST_ASSERT( function_output_length <=
2521 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2522 alg,
2523 input->len - first_part_size ) );
2524 TEST_ASSERT( function_output_length <=
2525 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002526 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002527
Gilles Peskine8817f612018-12-18 00:18:46 +01002528 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002529 ( output_buffer_size == 0 ? NULL :
2530 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002531 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002532 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002533 TEST_ASSERT( function_output_length <=
2534 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2535 TEST_ASSERT( function_output_length <=
2536 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002537 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002538 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002539
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002540 ASSERT_COMPARE( expected_output->x, expected_output->len,
2541 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002542
2543exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002544 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002545 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002546 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002547 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002548}
2549/* END_CASE */
2550
2551/* BEGIN_CASE */
2552void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002553 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002554 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002555 int first_part_size_arg,
2556 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002557 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002558{
Ronald Cron5425a212020-08-04 14:58:35 +02002559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002560 psa_key_type_t key_type = key_type_arg;
2561 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002562 size_t first_part_size = first_part_size_arg;
2563 size_t output1_length = output1_length_arg;
2564 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002565 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002566 size_t output_buffer_size = 0;
2567 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002568 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002569 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002571
Gilles Peskine8817f612018-12-18 00:18:46 +01002572 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002573
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002574 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2575 psa_set_key_algorithm( &attributes, alg );
2576 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002577
Ronald Cron5425a212020-08-04 14:58:35 +02002578 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2579 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002580
Ronald Cron5425a212020-08-04 14:58:35 +02002581 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002582
Steven Cooreman177deba2020-09-07 17:14:14 +02002583 if( iv->len > 0 )
2584 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002585 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002586 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002587
gabor-mezei-armceface22021-01-21 12:26:17 +01002588 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2589 TEST_ASSERT( output_buffer_size <=
2590 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002591 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002592
Gilles Peskinee0866522019-02-19 19:44:00 +01002593 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002594 PSA_ASSERT( psa_cipher_update( &operation,
2595 input->x, first_part_size,
2596 output, output_buffer_size,
2597 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002598 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002599 TEST_ASSERT( function_output_length <=
2600 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2601 TEST_ASSERT( function_output_length <=
2602 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002603 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002604
Gilles Peskine8817f612018-12-18 00:18:46 +01002605 PSA_ASSERT( psa_cipher_update( &operation,
2606 input->x + first_part_size,
2607 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002608 ( output_buffer_size == 0 ? NULL :
2609 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002610 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002611 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002612 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002613 TEST_ASSERT( function_output_length <=
2614 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2615 alg,
2616 input->len - first_part_size ) );
2617 TEST_ASSERT( function_output_length <=
2618 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002619 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002620
Gilles Peskine8817f612018-12-18 00:18:46 +01002621 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002622 ( output_buffer_size == 0 ? NULL :
2623 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002624 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002626 TEST_ASSERT( function_output_length <=
2627 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2628 TEST_ASSERT( function_output_length <=
2629 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002630 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002631 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002632
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002633 ASSERT_COMPARE( expected_output->x, expected_output->len,
2634 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002635
2636exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002637 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002638 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002639 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002640 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002641}
2642/* END_CASE */
2643
Gilles Peskine50e586b2018-06-08 14:28:46 +02002644/* BEGIN_CASE */
2645void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002646 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002647 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002648 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002649{
Ronald Cron5425a212020-08-04 14:58:35 +02002650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002651 psa_status_t status;
2652 psa_key_type_t key_type = key_type_arg;
2653 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002654 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002655 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002656 size_t output_buffer_size = 0;
2657 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002658 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002659 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661
Gilles Peskine8817f612018-12-18 00:18:46 +01002662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002664 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2665 psa_set_key_algorithm( &attributes, alg );
2666 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002667
Ronald Cron5425a212020-08-04 14:58:35 +02002668 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2669 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002670
Ronald Cron5425a212020-08-04 14:58:35 +02002671 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002672
Steven Cooreman177deba2020-09-07 17:14:14 +02002673 if( iv->len > 0 )
2674 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002675 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002676 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002677
gabor-mezei-armceface22021-01-21 12:26:17 +01002678 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2679 TEST_ASSERT( output_buffer_size <=
2680 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002681 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002682
Gilles Peskine8817f612018-12-18 00:18:46 +01002683 PSA_ASSERT( psa_cipher_update( &operation,
2684 input->x, input->len,
2685 output, output_buffer_size,
2686 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002687 TEST_ASSERT( function_output_length <=
2688 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2689 TEST_ASSERT( function_output_length <=
2690 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002691 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002692
Gilles Peskine50e586b2018-06-08 14:28:46 +02002693 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002694 ( output_buffer_size == 0 ? NULL :
2695 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002696 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002697 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002698 TEST_ASSERT( function_output_length <=
2699 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2700 TEST_ASSERT( function_output_length <=
2701 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002702 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002703 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002704
2705 if( expected_status == PSA_SUCCESS )
2706 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002707 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002708 ASSERT_COMPARE( expected_output->x, expected_output->len,
2709 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710 }
2711
Gilles Peskine50e586b2018-06-08 14:28:46 +02002712exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002713 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002714 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002715 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002716 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002717}
2718/* END_CASE */
2719
Gilles Peskine50e586b2018-06-08 14:28:46 +02002720/* BEGIN_CASE */
2721void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002722 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002723 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002724{
Ronald Cron5425a212020-08-04 14:58:35 +02002725 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002726 psa_key_type_t key_type = key_type_arg;
2727 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002728 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002729 size_t iv_size = 16;
2730 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002731 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002732 size_t output1_size = 0;
2733 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002734 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002735 size_t output2_size = 0;
2736 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002737 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002738 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2739 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002740 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002741
Gilles Peskine8817f612018-12-18 00:18:46 +01002742 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002743
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002744 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2745 psa_set_key_algorithm( &attributes, alg );
2746 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002747
Ronald Cron5425a212020-08-04 14:58:35 +02002748 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2749 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002750
Ronald Cron5425a212020-08-04 14:58:35 +02002751 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2752 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002753
Steven Cooreman177deba2020-09-07 17:14:14 +02002754 if( alg != PSA_ALG_ECB_NO_PADDING )
2755 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002756 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2757 iv, iv_size,
2758 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002759 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002760 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2761 TEST_ASSERT( output1_size <=
2762 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002763 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002764
Gilles Peskine8817f612018-12-18 00:18:46 +01002765 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2766 output1, output1_size,
2767 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002768 TEST_ASSERT( output1_length <=
2769 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2770 TEST_ASSERT( output1_length <=
2771 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2772
Gilles Peskine8817f612018-12-18 00:18:46 +01002773 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002774 output1 + output1_length,
2775 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002777 TEST_ASSERT( function_output_length <=
2778 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2779 TEST_ASSERT( function_output_length <=
2780 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002781
Gilles Peskine048b7f02018-06-08 14:20:49 +02002782 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002783
Gilles Peskine8817f612018-12-18 00:18:46 +01002784 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002785
2786 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002787 TEST_ASSERT( output2_size <=
2788 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2789 TEST_ASSERT( output2_size <=
2790 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002791 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002792
Steven Cooreman177deba2020-09-07 17:14:14 +02002793 if( iv_length > 0 )
2794 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002795 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2796 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002797 }
2798
Gilles Peskine8817f612018-12-18 00:18:46 +01002799 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2800 output2, output2_size,
2801 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002802 TEST_ASSERT( output2_length <=
2803 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2804 TEST_ASSERT( output2_length <=
2805 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2806
Gilles Peskine048b7f02018-06-08 14:20:49 +02002807 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_cipher_finish( &operation2,
2809 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002810 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002811 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002812 TEST_ASSERT( function_output_length <=
2813 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2814 TEST_ASSERT( function_output_length <=
2815 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002816
Gilles Peskine048b7f02018-06-08 14:20:49 +02002817 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002818
Gilles Peskine8817f612018-12-18 00:18:46 +01002819 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002820
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002821 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002822
2823exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002824 psa_cipher_abort( &operation1 );
2825 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002826 mbedtls_free( output1 );
2827 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002828 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002829 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002830}
2831/* END_CASE */
2832
2833/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834void cipher_verify_output_multipart( int alg_arg,
2835 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002836 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002837 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002838 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002839{
Ronald Cron5425a212020-08-04 14:58:35 +02002840 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002841 psa_key_type_t key_type = key_type_arg;
2842 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002843 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002844 unsigned char iv[16] = {0};
2845 size_t iv_size = 16;
2846 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002847 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002848 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002849 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002850 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002851 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002852 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002853 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002854 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2855 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002857
Gilles Peskine8817f612018-12-18 00:18:46 +01002858 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002859
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002860 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2861 psa_set_key_algorithm( &attributes, alg );
2862 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002863
Ronald Cron5425a212020-08-04 14:58:35 +02002864 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2865 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002866
Ronald Cron5425a212020-08-04 14:58:35 +02002867 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2868 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002869
Steven Cooreman177deba2020-09-07 17:14:14 +02002870 if( alg != PSA_ALG_ECB_NO_PADDING )
2871 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002872 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2873 iv, iv_size,
2874 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002875 }
2876
gabor-mezei-armceface22021-01-21 12:26:17 +01002877 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2878 TEST_ASSERT( output1_buffer_size <=
2879 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002880 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002881
Gilles Peskinee0866522019-02-19 19:44:00 +01002882 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002883
Gilles Peskine8817f612018-12-18 00:18:46 +01002884 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2885 output1, output1_buffer_size,
2886 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002887 TEST_ASSERT( function_output_length <=
2888 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2889 TEST_ASSERT( function_output_length <=
2890 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002891 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002892
Gilles Peskine8817f612018-12-18 00:18:46 +01002893 PSA_ASSERT( psa_cipher_update( &operation1,
2894 input->x + first_part_size,
2895 input->len - first_part_size,
2896 output1, output1_buffer_size,
2897 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002898 TEST_ASSERT( function_output_length <=
2899 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2900 alg,
2901 input->len - first_part_size ) );
2902 TEST_ASSERT( function_output_length <=
2903 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002904 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002905
Gilles Peskine8817f612018-12-18 00:18:46 +01002906 PSA_ASSERT( psa_cipher_finish( &operation1,
2907 output1 + output1_length,
2908 output1_buffer_size - output1_length,
2909 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002910 TEST_ASSERT( function_output_length <=
2911 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2912 TEST_ASSERT( function_output_length <=
2913 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002914 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002915
Gilles Peskine8817f612018-12-18 00:18:46 +01002916 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002917
Gilles Peskine048b7f02018-06-08 14:20:49 +02002918 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002919 TEST_ASSERT( output2_buffer_size <=
2920 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2921 TEST_ASSERT( output2_buffer_size <=
2922 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002923 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002924
Steven Cooreman177deba2020-09-07 17:14:14 +02002925 if( iv_length > 0 )
2926 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002927 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2928 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002929 }
Moran Pekerded84402018-06-06 16:36:50 +03002930
Gilles Peskine8817f612018-12-18 00:18:46 +01002931 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2932 output2, output2_buffer_size,
2933 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002934 TEST_ASSERT( function_output_length <=
2935 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2936 TEST_ASSERT( function_output_length <=
2937 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002938 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002939
Gilles Peskine8817f612018-12-18 00:18:46 +01002940 PSA_ASSERT( psa_cipher_update( &operation2,
2941 output1 + first_part_size,
2942 output1_length - first_part_size,
2943 output2, output2_buffer_size,
2944 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002945 TEST_ASSERT( function_output_length <=
2946 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2947 alg,
2948 output1_length - first_part_size ) );
2949 TEST_ASSERT( function_output_length <=
2950 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002951 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002952
Gilles Peskine8817f612018-12-18 00:18:46 +01002953 PSA_ASSERT( psa_cipher_finish( &operation2,
2954 output2 + output2_length,
2955 output2_buffer_size - output2_length,
2956 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002957 TEST_ASSERT( function_output_length <=
2958 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2959 TEST_ASSERT( function_output_length <=
2960 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002961 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002962
Gilles Peskine8817f612018-12-18 00:18:46 +01002963 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002964
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002965 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002966
2967exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002968 psa_cipher_abort( &operation1 );
2969 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002970 mbedtls_free( output1 );
2971 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002972 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002973 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002974}
2975/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002976
Gilles Peskine20035e32018-02-03 22:44:14 +01002977/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002978void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002979 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002980 data_t *nonce,
2981 data_t *additional_data,
2982 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002983 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002984{
Ronald Cron5425a212020-08-04 14:58:35 +02002985 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002986 psa_key_type_t key_type = key_type_arg;
2987 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002988 unsigned char *output_data = NULL;
2989 size_t output_size = 0;
2990 size_t output_length = 0;
2991 unsigned char *output_data2 = NULL;
2992 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002993 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Steven Cooremanf49478b2021-02-15 15:19:25 +01002994 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002995 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002996 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002997
Gilles Peskine4abf7412018-06-18 16:35:34 +02002998 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002999 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3000 * should be exact. */
3001 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3002 TEST_EQUAL( output_size,
3003 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003004 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003005
Gilles Peskine8817f612018-12-18 00:18:46 +01003006 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003007
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003008 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3009 psa_set_key_algorithm( &attributes, alg );
3010 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003011
Gilles Peskine049c7532019-05-15 20:22:09 +02003012 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003013 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003014
Steven Cooremanf49478b2021-02-15 15:19:25 +01003015 status = psa_aead_encrypt( key, alg,
3016 nonce->x, nonce->len,
3017 additional_data->x,
3018 additional_data->len,
3019 input_data->x, input_data->len,
3020 output_data, output_size,
3021 &output_length );
3022
3023 /* If the operation is not supported, just skip and not fail in case the
3024 * encryption involves a common limitation of cryptography hardwares and
3025 * an alternative implementation. */
3026 if( status == PSA_ERROR_NOT_SUPPORTED )
3027 {
3028 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3029 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3030 }
3031
3032 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003033
3034 if( PSA_SUCCESS == expected_result )
3035 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003036 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003037
Gilles Peskine003a4a92019-05-14 16:09:40 +02003038 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3039 * should be exact. */
3040 TEST_EQUAL( input_data->len,
3041 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3042
gabor-mezei-armceface22021-01-21 12:26:17 +01003043 TEST_ASSERT( input_data->len <=
3044 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3045
Ronald Cron5425a212020-08-04 14:58:35 +02003046 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003047 nonce->x, nonce->len,
3048 additional_data->x,
3049 additional_data->len,
3050 output_data, output_length,
3051 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003052 &output_length2 ),
3053 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003054
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003055 ASSERT_COMPARE( input_data->x, input_data->len,
3056 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003057 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003058
Gilles Peskinea1cac842018-06-11 19:33:02 +02003059exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003060 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003061 mbedtls_free( output_data );
3062 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003063 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003064}
3065/* END_CASE */
3066
3067/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003068void aead_encrypt( int key_type_arg, data_t *key_data,
3069 int alg_arg,
3070 data_t *nonce,
3071 data_t *additional_data,
3072 data_t *input_data,
3073 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003074{
Ronald Cron5425a212020-08-04 14:58:35 +02003075 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003076 psa_key_type_t key_type = key_type_arg;
3077 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003078 unsigned char *output_data = NULL;
3079 size_t output_size = 0;
3080 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003081 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003083 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003084
Gilles Peskine4abf7412018-06-18 16:35:34 +02003085 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003086 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3087 * should be exact. */
3088 TEST_EQUAL( output_size,
3089 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003090 TEST_ASSERT( output_size <=
3091 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003092 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003093
Gilles Peskine8817f612018-12-18 00:18:46 +01003094 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003096 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3097 psa_set_key_algorithm( &attributes, alg );
3098 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003099
Gilles Peskine049c7532019-05-15 20:22:09 +02003100 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003101 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003102
Steven Cooremand588ea12021-01-11 19:36:04 +01003103 status = psa_aead_encrypt( key, alg,
3104 nonce->x, nonce->len,
3105 additional_data->x, additional_data->len,
3106 input_data->x, input_data->len,
3107 output_data, output_size,
3108 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003109
Ronald Cron28a45ed2021-02-09 20:35:42 +01003110 /* If the operation is not supported, just skip and not fail in case the
3111 * encryption involves a common limitation of cryptography hardwares and
3112 * an alternative implementation. */
3113 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003114 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003115 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3116 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003117 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003118
3119 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003120 ASSERT_COMPARE( expected_result->x, expected_result->len,
3121 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003122
Gilles Peskinea1cac842018-06-11 19:33:02 +02003123exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003124 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003125 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003126 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003127}
3128/* END_CASE */
3129
3130/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003131void aead_decrypt( int key_type_arg, data_t *key_data,
3132 int alg_arg,
3133 data_t *nonce,
3134 data_t *additional_data,
3135 data_t *input_data,
3136 data_t *expected_data,
3137 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003138{
Ronald Cron5425a212020-08-04 14:58:35 +02003139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003140 psa_key_type_t key_type = key_type_arg;
3141 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003142 unsigned char *output_data = NULL;
3143 size_t output_size = 0;
3144 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003145 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003146 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003147 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003148 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003149
Gilles Peskine003a4a92019-05-14 16:09:40 +02003150 output_size = input_data->len - tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003151 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
gabor-mezei-armceface22021-01-21 12:26:17 +01003152 {
3153 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3154 * should be exact. */
Gilles Peskine003a4a92019-05-14 16:09:40 +02003155 TEST_EQUAL( output_size,
3156 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003157 TEST_ASSERT( output_size <=
3158 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3159 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003160 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003161
Gilles Peskine8817f612018-12-18 00:18:46 +01003162 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003163
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003164 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3165 psa_set_key_algorithm( &attributes, alg );
3166 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003167
Gilles Peskine049c7532019-05-15 20:22:09 +02003168 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003169 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003170
Steven Cooremand588ea12021-01-11 19:36:04 +01003171 status = psa_aead_decrypt( key, alg,
3172 nonce->x, nonce->len,
3173 additional_data->x,
3174 additional_data->len,
3175 input_data->x, input_data->len,
3176 output_data, output_size,
3177 &output_length );
3178
Ronald Cron28a45ed2021-02-09 20:35:42 +01003179 /* If the operation is not supported, just skip and not fail in case the
3180 * decryption involves a common limitation of cryptography hardwares and
3181 * an alternative implementation. */
3182 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003183 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003184 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3185 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003186 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003187
3188 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003189
Gilles Peskine2d277862018-06-18 15:41:12 +02003190 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003191 ASSERT_COMPARE( expected_data->x, expected_data->len,
3192 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003193
Gilles Peskinea1cac842018-06-11 19:33:02 +02003194exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003195 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003196 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003197 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003198}
3199/* END_CASE */
3200
3201/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003202void signature_size( int type_arg,
3203 int bits,
3204 int alg_arg,
3205 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003206{
3207 psa_key_type_t type = type_arg;
3208 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003209 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003210
Gilles Peskinefe11b722018-12-18 00:24:04 +01003211 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003212#if defined(MBEDTLS_TEST_DEPRECATED)
3213 TEST_EQUAL( actual_size,
3214 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3215#endif /* MBEDTLS_TEST_DEPRECATED */
3216
Gilles Peskinee59236f2018-01-27 23:32:46 +01003217exit:
3218 ;
3219}
3220/* END_CASE */
3221
3222/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003223void sign_deterministic( int key_type_arg, data_t *key_data,
3224 int alg_arg, data_t *input_data,
3225 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003226{
Ronald Cron5425a212020-08-04 14:58:35 +02003227 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003228 psa_key_type_t key_type = key_type_arg;
3229 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003230 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003231 unsigned char *signature = NULL;
3232 size_t signature_size;
3233 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003235
Gilles Peskine8817f612018-12-18 00:18:46 +01003236 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003237
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003238 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003239 psa_set_key_algorithm( &attributes, alg );
3240 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003241
Gilles Peskine049c7532019-05-15 20:22:09 +02003242 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003243 &key ) );
3244 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003245 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003246
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003247 /* Allocate a buffer which has the size advertized by the
3248 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003249 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003250 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003251 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003252 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003253 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003254
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003255 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003256 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003257 input_data->x, input_data->len,
3258 signature, signature_size,
3259 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003260 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003261 ASSERT_COMPARE( output_data->x, output_data->len,
3262 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003263
Gilles Peskine0627f982019-11-26 19:12:16 +01003264#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003265 memset( signature, 0, signature_size );
3266 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003267 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003268 input_data->x, input_data->len,
3269 signature, signature_size,
3270 &signature_length ) );
3271 ASSERT_COMPARE( output_data->x, output_data->len,
3272 signature, signature_length );
3273#endif /* MBEDTLS_TEST_DEPRECATED */
3274
Gilles Peskine20035e32018-02-03 22:44:14 +01003275exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003276 /*
3277 * Key attributes may have been returned by psa_get_key_attributes()
3278 * thus reset them as required.
3279 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003280 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003281
Ronald Cron5425a212020-08-04 14:58:35 +02003282 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003283 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003284 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003285}
3286/* END_CASE */
3287
3288/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003289void sign_fail( int key_type_arg, data_t *key_data,
3290 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003291 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003292{
Ronald Cron5425a212020-08-04 14:58:35 +02003293 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003294 psa_key_type_t key_type = key_type_arg;
3295 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003296 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003297 psa_status_t actual_status;
3298 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003299 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003300 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003301 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003302
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003303 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003304
Gilles Peskine8817f612018-12-18 00:18:46 +01003305 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003306
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003307 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003308 psa_set_key_algorithm( &attributes, alg );
3309 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003310
Gilles Peskine049c7532019-05-15 20:22:09 +02003311 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003312 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003313
Ronald Cron5425a212020-08-04 14:58:35 +02003314 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003315 input_data->x, input_data->len,
3316 signature, signature_size,
3317 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003318 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003319 /* The value of *signature_length is unspecified on error, but
3320 * whatever it is, it should be less than signature_size, so that
3321 * if the caller tries to read *signature_length bytes without
3322 * checking the error code then they don't overflow a buffer. */
3323 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003324
Gilles Peskine895242b2019-11-29 12:15:40 +01003325#if defined(MBEDTLS_TEST_DEPRECATED)
3326 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003327 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003328 input_data->x, input_data->len,
3329 signature, signature_size,
3330 &signature_length ),
3331 expected_status );
3332 TEST_ASSERT( signature_length <= signature_size );
3333#endif /* MBEDTLS_TEST_DEPRECATED */
3334
Gilles Peskine20035e32018-02-03 22:44:14 +01003335exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003336 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003337 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003338 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003339 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003340}
3341/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003342
3343/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003344void sign_verify( int key_type_arg, data_t *key_data,
3345 int alg_arg, data_t *input_data )
3346{
Ronald Cron5425a212020-08-04 14:58:35 +02003347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003348 psa_key_type_t key_type = key_type_arg;
3349 psa_algorithm_t alg = alg_arg;
3350 size_t key_bits;
3351 unsigned char *signature = NULL;
3352 size_t signature_size;
3353 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003354 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003355
Gilles Peskine8817f612018-12-18 00:18:46 +01003356 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003357
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003358 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003359 psa_set_key_algorithm( &attributes, alg );
3360 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003361
Gilles Peskine049c7532019-05-15 20:22:09 +02003362 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003363 &key ) );
3364 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003365 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003366
3367 /* Allocate a buffer which has the size advertized by the
3368 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003369 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003370 key_bits, alg );
3371 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003372 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003373 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003374
3375 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003376 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003377 input_data->x, input_data->len,
3378 signature, signature_size,
3379 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003380 /* Check that the signature length looks sensible. */
3381 TEST_ASSERT( signature_length <= signature_size );
3382 TEST_ASSERT( signature_length > 0 );
3383
3384 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003385 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003386 input_data->x, input_data->len,
3387 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003388
3389 if( input_data->len != 0 )
3390 {
3391 /* Flip a bit in the input and verify that the signature is now
3392 * detected as invalid. Flip a bit at the beginning, not at the end,
3393 * because ECDSA may ignore the last few bits of the input. */
3394 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003395 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003396 input_data->x, input_data->len,
3397 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003398 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003399 }
3400
3401exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003402 /*
3403 * Key attributes may have been returned by psa_get_key_attributes()
3404 * thus reset them as required.
3405 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003406 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003407
Ronald Cron5425a212020-08-04 14:58:35 +02003408 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003409 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003410 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003411}
3412/* END_CASE */
3413
3414/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003415void asymmetric_verify( int key_type_arg, data_t *key_data,
3416 int alg_arg, data_t *hash_data,
3417 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003418{
Ronald Cron5425a212020-08-04 14:58:35 +02003419 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003420 psa_key_type_t key_type = key_type_arg;
3421 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003423
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003424 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003425
Gilles Peskine8817f612018-12-18 00:18:46 +01003426 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003427
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003428 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003429 psa_set_key_algorithm( &attributes, alg );
3430 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003431
Gilles Peskine049c7532019-05-15 20:22:09 +02003432 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003433 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003434
Ronald Cron5425a212020-08-04 14:58:35 +02003435 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003436 hash_data->x, hash_data->len,
3437 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003438
3439#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003440 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003441 hash_data->x, hash_data->len,
3442 signature_data->x,
3443 signature_data->len ) );
3444
3445#endif /* MBEDTLS_TEST_DEPRECATED */
3446
itayzafrir5c753392018-05-08 11:18:38 +03003447exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003448 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003449 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003450 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003451}
3452/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003453
3454/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003455void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3456 int alg_arg, data_t *hash_data,
3457 data_t *signature_data,
3458 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003459{
Ronald Cron5425a212020-08-04 14:58:35 +02003460 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003461 psa_key_type_t key_type = key_type_arg;
3462 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003463 psa_status_t actual_status;
3464 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003466
Gilles Peskine8817f612018-12-18 00:18:46 +01003467 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003468
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003469 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003470 psa_set_key_algorithm( &attributes, alg );
3471 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003472
Gilles Peskine049c7532019-05-15 20:22:09 +02003473 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003474 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003475
Ronald Cron5425a212020-08-04 14:58:35 +02003476 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003477 hash_data->x, hash_data->len,
3478 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003479 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003480
Gilles Peskine895242b2019-11-29 12:15:40 +01003481#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003482 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003483 hash_data->x, hash_data->len,
3484 signature_data->x, signature_data->len ),
3485 expected_status );
3486#endif /* MBEDTLS_TEST_DEPRECATED */
3487
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003488exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003489 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003490 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003491 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003492}
3493/* END_CASE */
3494
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003495/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003496void asymmetric_encrypt( int key_type_arg,
3497 data_t *key_data,
3498 int alg_arg,
3499 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003500 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003501 int expected_output_length_arg,
3502 int expected_status_arg )
3503{
Ronald Cron5425a212020-08-04 14:58:35 +02003504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003505 psa_key_type_t key_type = key_type_arg;
3506 psa_algorithm_t alg = alg_arg;
3507 size_t expected_output_length = expected_output_length_arg;
3508 size_t key_bits;
3509 unsigned char *output = NULL;
3510 size_t output_size;
3511 size_t output_length = ~0;
3512 psa_status_t actual_status;
3513 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003514 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003515
Gilles Peskine8817f612018-12-18 00:18:46 +01003516 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003517
Gilles Peskine656896e2018-06-29 19:12:28 +02003518 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003519 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3520 psa_set_key_algorithm( &attributes, alg );
3521 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003522 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003523 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003524
3525 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003526 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003527 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003528
Gilles Peskine656896e2018-06-29 19:12:28 +02003529 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003530 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003531 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003532
3533 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003534 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003535 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003536 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003537 output, output_size,
3538 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003539 TEST_EQUAL( actual_status, expected_status );
3540 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003541
Gilles Peskine68428122018-06-30 18:42:41 +02003542 /* If the label is empty, the test framework puts a non-null pointer
3543 * in label->x. Test that a null pointer works as well. */
3544 if( label->len == 0 )
3545 {
3546 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003547 if( output_size != 0 )
3548 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003549 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003550 input_data->x, input_data->len,
3551 NULL, label->len,
3552 output, output_size,
3553 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003554 TEST_EQUAL( actual_status, expected_status );
3555 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003556 }
3557
Gilles Peskine656896e2018-06-29 19:12:28 +02003558exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003559 /*
3560 * Key attributes may have been returned by psa_get_key_attributes()
3561 * thus reset them as required.
3562 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003563 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003564
Ronald Cron5425a212020-08-04 14:58:35 +02003565 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003566 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003567 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003568}
3569/* END_CASE */
3570
3571/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003572void asymmetric_encrypt_decrypt( int key_type_arg,
3573 data_t *key_data,
3574 int alg_arg,
3575 data_t *input_data,
3576 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003577{
Ronald Cron5425a212020-08-04 14:58:35 +02003578 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579 psa_key_type_t key_type = key_type_arg;
3580 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003581 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003582 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003583 size_t output_size;
3584 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003585 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003586 size_t output2_size;
3587 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003588 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003589
Gilles Peskine8817f612018-12-18 00:18:46 +01003590 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003591
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003592 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3593 psa_set_key_algorithm( &attributes, alg );
3594 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003595
Gilles Peskine049c7532019-05-15 20:22:09 +02003596 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003597 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003598
3599 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003600 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003601 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003602
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003603 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003604 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003605 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003606
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003607 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003608 TEST_ASSERT( output2_size <=
3609 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3610 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003611 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003612
Gilles Peskineeebd7382018-06-08 18:11:54 +02003613 /* We test encryption by checking that encrypt-then-decrypt gives back
3614 * the original plaintext because of the non-optional random
3615 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003616 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003617 input_data->x, input_data->len,
3618 label->x, label->len,
3619 output, output_size,
3620 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003621 /* We don't know what ciphertext length to expect, but check that
3622 * it looks sensible. */
3623 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003624
Ronald Cron5425a212020-08-04 14:58:35 +02003625 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003626 output, output_length,
3627 label->x, label->len,
3628 output2, output2_size,
3629 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003630 ASSERT_COMPARE( input_data->x, input_data->len,
3631 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003632
3633exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003634 /*
3635 * Key attributes may have been returned by psa_get_key_attributes()
3636 * thus reset them as required.
3637 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003638 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003639
Ronald Cron5425a212020-08-04 14:58:35 +02003640 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003641 mbedtls_free( output );
3642 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003643 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644}
3645/* END_CASE */
3646
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003647/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003648void asymmetric_decrypt( int key_type_arg,
3649 data_t *key_data,
3650 int alg_arg,
3651 data_t *input_data,
3652 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003653 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003654{
Ronald Cron5425a212020-08-04 14:58:35 +02003655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003656 psa_key_type_t key_type = key_type_arg;
3657 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003658 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003660 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003661 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003662 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003663
Gilles Peskine8817f612018-12-18 00:18:46 +01003664 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003665
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003666 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3667 psa_set_key_algorithm( &attributes, alg );
3668 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003669
Gilles Peskine049c7532019-05-15 20:22:09 +02003670 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003671 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003672
gabor-mezei-armceface22021-01-21 12:26:17 +01003673 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3674 key_bits = psa_get_key_bits( &attributes );
3675
3676 /* Determine the maximum ciphertext length */
3677 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3678 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3679 ASSERT_ALLOC( output, output_size );
3680
Ronald Cron5425a212020-08-04 14:58:35 +02003681 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003682 input_data->x, input_data->len,
3683 label->x, label->len,
3684 output,
3685 output_size,
3686 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003687 ASSERT_COMPARE( expected_data->x, expected_data->len,
3688 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003689
Gilles Peskine68428122018-06-30 18:42:41 +02003690 /* If the label is empty, the test framework puts a non-null pointer
3691 * in label->x. Test that a null pointer works as well. */
3692 if( label->len == 0 )
3693 {
3694 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003695 if( output_size != 0 )
3696 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003697 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003698 input_data->x, input_data->len,
3699 NULL, label->len,
3700 output,
3701 output_size,
3702 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003703 ASSERT_COMPARE( expected_data->x, expected_data->len,
3704 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003705 }
3706
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003707exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003708 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003709 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003710 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003711 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003712}
3713/* END_CASE */
3714
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003715/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003716void asymmetric_decrypt_fail( int key_type_arg,
3717 data_t *key_data,
3718 int alg_arg,
3719 data_t *input_data,
3720 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003721 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003722 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003723{
Ronald Cron5425a212020-08-04 14:58:35 +02003724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003725 psa_key_type_t key_type = key_type_arg;
3726 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003728 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003729 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003730 psa_status_t actual_status;
3731 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003732 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003733
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003734 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003735
Gilles Peskine8817f612018-12-18 00:18:46 +01003736 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003737
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003738 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3739 psa_set_key_algorithm( &attributes, alg );
3740 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003741
Gilles Peskine049c7532019-05-15 20:22:09 +02003742 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003743 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003744
Ronald Cron5425a212020-08-04 14:58:35 +02003745 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003746 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003747 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003748 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003749 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003750 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003751 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003752
Gilles Peskine68428122018-06-30 18:42:41 +02003753 /* If the label is empty, the test framework puts a non-null pointer
3754 * in label->x. Test that a null pointer works as well. */
3755 if( label->len == 0 )
3756 {
3757 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003758 if( output_size != 0 )
3759 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003760 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003761 input_data->x, input_data->len,
3762 NULL, label->len,
3763 output, output_size,
3764 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003765 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003766 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003767 }
3768
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003769exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003770 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003771 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02003772 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003773 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003774}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003775/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003776
3777/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003778void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00003779{
3780 /* Test each valid way of initializing the object, except for `= {0}`, as
3781 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3782 * though it's OK by the C standard. We could test for this, but we'd need
3783 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003784 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003785 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
3786 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
3787 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003788
3789 memset( &zero, 0, sizeof( zero ) );
3790
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003791 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003792 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003793 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003794 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003795 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003796 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003797 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003798
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003799 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003800 PSA_ASSERT( psa_key_derivation_abort(&func) );
3801 PSA_ASSERT( psa_key_derivation_abort(&init) );
3802 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003803}
3804/* END_CASE */
3805
Janos Follath16de4a42019-06-13 16:32:24 +01003806/* BEGIN_CASE */
3807void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003808{
Gilles Peskineea0fb492018-07-12 17:17:20 +02003809 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003810 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003811 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003812
Gilles Peskine8817f612018-12-18 00:18:46 +01003813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003814
Janos Follath16de4a42019-06-13 16:32:24 +01003815 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003816 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003817
3818exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003819 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003820 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003821}
3822/* END_CASE */
3823
Janos Follathaf3c2a02019-06-12 12:34:34 +01003824/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01003825void derive_set_capacity( int alg_arg, int capacity_arg,
3826 int expected_status_arg )
3827{
3828 psa_algorithm_t alg = alg_arg;
3829 size_t capacity = capacity_arg;
3830 psa_status_t expected_status = expected_status_arg;
3831 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3832
3833 PSA_ASSERT( psa_crypto_init( ) );
3834
3835 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3836
3837 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
3838 expected_status );
3839
3840exit:
3841 psa_key_derivation_abort( &operation );
3842 PSA_DONE( );
3843}
3844/* END_CASE */
3845
3846/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01003847void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02003848 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003849 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02003850 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003851 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02003852 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003853 int expected_status_arg3,
3854 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003855{
3856 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02003857 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
3858 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01003859 psa_status_t expected_statuses[] = {expected_status_arg1,
3860 expected_status_arg2,
3861 expected_status_arg3};
3862 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003863 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3864 MBEDTLS_SVC_KEY_ID_INIT,
3865 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01003866 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3868 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003869 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003870 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003871 psa_status_t expected_output_status = expected_output_status_arg;
3872 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01003873
3874 PSA_ASSERT( psa_crypto_init( ) );
3875
3876 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3877 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003878
3879 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3880
3881 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
3882 {
Gilles Peskineb8965192019-09-24 16:21:10 +02003883 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003884 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02003885 psa_set_key_type( &attributes, key_types[i] );
3886 PSA_ASSERT( psa_import_key( &attributes,
3887 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003888 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003889 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
3890 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
3891 {
3892 // When taking a private key as secret input, use key agreement
3893 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003894 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
3895 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003896 expected_statuses[i] );
3897 }
3898 else
3899 {
3900 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02003901 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003902 expected_statuses[i] );
3903 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02003904 }
3905 else
3906 {
3907 TEST_EQUAL( psa_key_derivation_input_bytes(
3908 &operation, steps[i],
3909 inputs[i]->x, inputs[i]->len ),
3910 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003911 }
3912 }
3913
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003914 if( output_key_type != PSA_KEY_TYPE_NONE )
3915 {
3916 psa_reset_key_attributes( &attributes );
3917 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
3918 psa_set_key_bits( &attributes, 8 );
3919 actual_output_status =
3920 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02003921 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003922 }
3923 else
3924 {
3925 uint8_t buffer[1];
3926 actual_output_status =
3927 psa_key_derivation_output_bytes( &operation,
3928 buffer, sizeof( buffer ) );
3929 }
3930 TEST_EQUAL( actual_output_status, expected_output_status );
3931
Janos Follathaf3c2a02019-06-12 12:34:34 +01003932exit:
3933 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003934 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3935 psa_destroy_key( keys[i] );
3936 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003937 PSA_DONE( );
3938}
3939/* END_CASE */
3940
Janos Follathd958bb72019-07-03 15:02:16 +01003941/* BEGIN_CASE */
3942void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003943{
Janos Follathd958bb72019-07-03 15:02:16 +01003944 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003945 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003946 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003947 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01003948 unsigned char input1[] = "Input 1";
3949 size_t input1_length = sizeof( input1 );
3950 unsigned char input2[] = "Input 2";
3951 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003952 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003953 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003954 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3955 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3956 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003958
Gilles Peskine8817f612018-12-18 00:18:46 +01003959 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003960
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3962 psa_set_key_algorithm( &attributes, alg );
3963 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003964
Gilles Peskine73676cb2019-05-15 20:15:10 +02003965 PSA_ASSERT( psa_import_key( &attributes,
3966 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02003967 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003968
3969 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003970 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
3971 input1, input1_length,
3972 input2, input2_length,
3973 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01003974 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003975
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003976 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01003977 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003978 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003979
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003980 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003981
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003982 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02003983 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003984
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003985exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003986 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003987 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003988 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003989}
3990/* END_CASE */
3991
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003992/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003993void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003994{
3995 uint8_t output_buffer[16];
3996 size_t buffer_size = 16;
3997 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003998 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003999
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004000 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4001 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004002 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004003
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004004 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004005 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004006
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004007 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004008
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004009 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4010 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004011 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004012
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004013 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004014 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004015
4016exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004017 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004018}
4019/* END_CASE */
4020
4021/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004022void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004023 int step1_arg, data_t *input1,
4024 int step2_arg, data_t *input2,
4025 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004026 int requested_capacity_arg,
4027 data_t *expected_output1,
4028 data_t *expected_output2 )
4029{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004030 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004031 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4032 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004033 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4034 MBEDTLS_SVC_KEY_ID_INIT,
4035 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004036 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004037 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004038 uint8_t *expected_outputs[2] =
4039 {expected_output1->x, expected_output2->x};
4040 size_t output_sizes[2] =
4041 {expected_output1->len, expected_output2->len};
4042 size_t output_buffer_size = 0;
4043 uint8_t *output_buffer = NULL;
4044 size_t expected_capacity;
4045 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004047 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004048 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004049
4050 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4051 {
4052 if( output_sizes[i] > output_buffer_size )
4053 output_buffer_size = output_sizes[i];
4054 if( output_sizes[i] == 0 )
4055 expected_outputs[i] = NULL;
4056 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004057 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004058 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004059
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004060 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4061 psa_set_key_algorithm( &attributes, alg );
4062 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004063
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004064 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004065 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4066 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4067 requested_capacity ) );
4068 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004069 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004070 switch( steps[i] )
4071 {
4072 case 0:
4073 break;
4074 case PSA_KEY_DERIVATION_INPUT_SECRET:
4075 PSA_ASSERT( psa_import_key( &attributes,
4076 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004077 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004078
4079 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4080 {
4081 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4082 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4083 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4084 }
4085
Gilles Peskine1468da72019-05-29 17:35:49 +02004086 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004087 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004088 break;
4089 default:
4090 PSA_ASSERT( psa_key_derivation_input_bytes(
4091 &operation, steps[i],
4092 inputs[i]->x, inputs[i]->len ) );
4093 break;
4094 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004095 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004096
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004097 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004098 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004099 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004100 expected_capacity = requested_capacity;
4101
4102 /* Expansion phase. */
4103 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4104 {
4105 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004106 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004107 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004108 if( expected_capacity == 0 && output_sizes[i] == 0 )
4109 {
4110 /* Reading 0 bytes when 0 bytes are available can go either way. */
4111 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004112 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004113 continue;
4114 }
4115 else if( expected_capacity == 0 ||
4116 output_sizes[i] > expected_capacity )
4117 {
4118 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004119 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004120 expected_capacity = 0;
4121 continue;
4122 }
4123 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004124 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004125 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004126 ASSERT_COMPARE( output_buffer, output_sizes[i],
4127 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004128 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004129 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004130 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004131 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004132 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004133 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004134 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004135
4136exit:
4137 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004138 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004139 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4140 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004141 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004142}
4143/* END_CASE */
4144
4145/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004146void derive_full( int alg_arg,
4147 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004148 data_t *input1,
4149 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004150 int requested_capacity_arg )
4151{
Ronald Cron5425a212020-08-04 14:58:35 +02004152 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004153 psa_algorithm_t alg = alg_arg;
4154 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004155 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004156 unsigned char output_buffer[16];
4157 size_t expected_capacity = requested_capacity;
4158 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004160
Gilles Peskine8817f612018-12-18 00:18:46 +01004161 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004162
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004163 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4164 psa_set_key_algorithm( &attributes, alg );
4165 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004166
Gilles Peskine049c7532019-05-15 20:22:09 +02004167 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004168 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004169
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004170 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4171 input1->x, input1->len,
4172 input2->x, input2->len,
4173 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004174 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004175
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004176 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004177 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004178 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004179
4180 /* Expansion phase. */
4181 while( current_capacity > 0 )
4182 {
4183 size_t read_size = sizeof( output_buffer );
4184 if( read_size > current_capacity )
4185 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004186 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004187 output_buffer,
4188 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004189 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004190 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004191 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004192 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004193 }
4194
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004195 /* Check that the operation refuses to go over capacity. */
4196 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004197 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004198
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004199 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004200
4201exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004202 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004203 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004204 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004205}
4206/* END_CASE */
4207
Janos Follathe60c9052019-07-03 13:51:30 +01004208/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004209void derive_key_exercise( int alg_arg,
4210 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004211 data_t *input1,
4212 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004213 int derived_type_arg,
4214 int derived_bits_arg,
4215 int derived_usage_arg,
4216 int derived_alg_arg )
4217{
Ronald Cron5425a212020-08-04 14:58:35 +02004218 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4219 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004220 psa_algorithm_t alg = alg_arg;
4221 psa_key_type_t derived_type = derived_type_arg;
4222 size_t derived_bits = derived_bits_arg;
4223 psa_key_usage_t derived_usage = derived_usage_arg;
4224 psa_algorithm_t derived_alg = derived_alg_arg;
4225 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004226 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004227 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004228 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004229
Gilles Peskine8817f612018-12-18 00:18:46 +01004230 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004231
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004232 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4233 psa_set_key_algorithm( &attributes, alg );
4234 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004235 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004236 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004237
4238 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004239 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4240 input1->x, input1->len,
4241 input2->x, input2->len,
4242 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004243 goto exit;
4244
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004245 psa_set_key_usage_flags( &attributes, derived_usage );
4246 psa_set_key_algorithm( &attributes, derived_alg );
4247 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004248 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004249 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004250 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004251
4252 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004253 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004254 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4255 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004256
4257 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004258 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004259 goto exit;
4260
4261exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004262 /*
4263 * Key attributes may have been returned by psa_get_key_attributes()
4264 * thus reset them as required.
4265 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004266 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004267
4268 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004269 psa_destroy_key( base_key );
4270 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004271 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004272}
4273/* END_CASE */
4274
Janos Follath42fd8882019-07-03 14:17:09 +01004275/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004276void derive_key_export( int alg_arg,
4277 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004278 data_t *input1,
4279 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004280 int bytes1_arg,
4281 int bytes2_arg )
4282{
Ronald Cron5425a212020-08-04 14:58:35 +02004283 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4284 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004285 psa_algorithm_t alg = alg_arg;
4286 size_t bytes1 = bytes1_arg;
4287 size_t bytes2 = bytes2_arg;
4288 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004289 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004290 uint8_t *output_buffer = NULL;
4291 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004292 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4293 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004294 size_t length;
4295
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004296 ASSERT_ALLOC( output_buffer, capacity );
4297 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004298 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004299
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004300 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4301 psa_set_key_algorithm( &base_attributes, alg );
4302 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004303 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004304 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004305
4306 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004307 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4308 input1->x, input1->len,
4309 input2->x, input2->len,
4310 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004311 goto exit;
4312
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004313 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004314 output_buffer,
4315 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004316 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004317
4318 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004319 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4320 input1->x, input1->len,
4321 input2->x, input2->len,
4322 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004323 goto exit;
4324
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004325 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4326 psa_set_key_algorithm( &derived_attributes, 0 );
4327 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004328 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004329 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004330 &derived_key ) );
4331 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004332 export_buffer, bytes1,
4333 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004334 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004335 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004336 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004337 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004338 &derived_key ) );
4339 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004340 export_buffer + bytes1, bytes2,
4341 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004342 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004343
4344 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004345 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4346 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004347
4348exit:
4349 mbedtls_free( output_buffer );
4350 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004351 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004352 psa_destroy_key( base_key );
4353 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004354 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004355}
4356/* END_CASE */
4357
4358/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004359void derive_key( int alg_arg,
4360 data_t *key_data, data_t *input1, data_t *input2,
4361 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004362 int expected_status_arg,
4363 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004364{
Ronald Cron5425a212020-08-04 14:58:35 +02004365 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4366 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004367 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004368 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004369 size_t bits = bits_arg;
4370 psa_status_t expected_status = expected_status_arg;
4371 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4372 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4373 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4374
4375 PSA_ASSERT( psa_crypto_init( ) );
4376
4377 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4378 psa_set_key_algorithm( &base_attributes, alg );
4379 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4380 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004381 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004382
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004383 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4384 input1->x, input1->len,
4385 input2->x, input2->len,
4386 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004387 goto exit;
4388
4389 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4390 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004391 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004392 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004393
4394 psa_status_t status =
4395 psa_key_derivation_output_key( &derived_attributes,
4396 &operation,
4397 &derived_key );
4398 if( is_large_output > 0 )
4399 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4400 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004401
4402exit:
4403 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004404 psa_destroy_key( base_key );
4405 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004406 PSA_DONE( );
4407}
4408/* END_CASE */
4409
4410/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004411void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004412 int our_key_type_arg, int our_key_alg_arg,
4413 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004414 int expected_status_arg )
4415{
Ronald Cron5425a212020-08-04 14:58:35 +02004416 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004417 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004418 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004419 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004422 psa_status_t expected_status = expected_status_arg;
4423 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004424
Gilles Peskine8817f612018-12-18 00:18:46 +01004425 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004426
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004427 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004428 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004429 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004430 PSA_ASSERT( psa_import_key( &attributes,
4431 our_key_data->x, our_key_data->len,
4432 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004433
Gilles Peskine77f40d82019-04-11 21:27:06 +02004434 /* The tests currently include inputs that should fail at either step.
4435 * Test cases that fail at the setup step should be changed to call
4436 * key_derivation_setup instead, and this function should be renamed
4437 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004439 if( status == PSA_SUCCESS )
4440 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004441 TEST_EQUAL( psa_key_derivation_key_agreement(
4442 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4443 our_key,
4444 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004445 expected_status );
4446 }
4447 else
4448 {
4449 TEST_ASSERT( status == expected_status );
4450 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004451
4452exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004453 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004454 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004455 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004456}
4457/* END_CASE */
4458
4459/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004460void raw_key_agreement( int alg_arg,
4461 int our_key_type_arg, data_t *our_key_data,
4462 data_t *peer_key_data,
4463 data_t *expected_output )
4464{
Ronald Cron5425a212020-08-04 14:58:35 +02004465 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004466 psa_algorithm_t alg = alg_arg;
4467 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004469 unsigned char *output = NULL;
4470 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004471 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004472
4473 ASSERT_ALLOC( output, expected_output->len );
4474 PSA_ASSERT( psa_crypto_init( ) );
4475
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004476 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4477 psa_set_key_algorithm( &attributes, alg );
4478 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004479 PSA_ASSERT( psa_import_key( &attributes,
4480 our_key_data->x, our_key_data->len,
4481 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004482
gabor-mezei-armceface22021-01-21 12:26:17 +01004483 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4484 key_bits = psa_get_key_bits( &attributes );
4485
Gilles Peskinebe697d82019-05-16 18:00:41 +02004486 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4487 peer_key_data->x, peer_key_data->len,
4488 output, expected_output->len,
4489 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004490 ASSERT_COMPARE( output, output_length,
4491 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004492 TEST_ASSERT( output_length <=
4493 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4494 TEST_ASSERT( output_length <=
4495 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004496
4497exit:
4498 mbedtls_free( output );
4499 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004500 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004501}
4502/* END_CASE */
4503
4504/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004505void key_agreement_capacity( int alg_arg,
4506 int our_key_type_arg, data_t *our_key_data,
4507 data_t *peer_key_data,
4508 int expected_capacity_arg )
4509{
Ronald Cron5425a212020-08-04 14:58:35 +02004510 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004511 psa_algorithm_t alg = alg_arg;
4512 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004513 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004514 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004515 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004516 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004517
Gilles Peskine8817f612018-12-18 00:18:46 +01004518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004519
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004520 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4521 psa_set_key_algorithm( &attributes, alg );
4522 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004523 PSA_ASSERT( psa_import_key( &attributes,
4524 our_key_data->x, our_key_data->len,
4525 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004526
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004527 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004528 PSA_ASSERT( psa_key_derivation_key_agreement(
4529 &operation,
4530 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4531 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004532 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4533 {
4534 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004535 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004536 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004537 NULL, 0 ) );
4538 }
Gilles Peskine59685592018-09-18 12:11:34 +02004539
Gilles Peskinebf491972018-10-25 22:36:12 +02004540 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004541 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004543 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004544
Gilles Peskinebf491972018-10-25 22:36:12 +02004545 /* Test the actual capacity by reading the output. */
4546 while( 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, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004550 actual_capacity -= sizeof( output );
4551 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004552 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004553 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004554 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004555 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004556
Gilles Peskine59685592018-09-18 12:11:34 +02004557exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004558 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004559 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004560 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004561}
4562/* END_CASE */
4563
4564/* BEGIN_CASE */
4565void key_agreement_output( int alg_arg,
4566 int our_key_type_arg, data_t *our_key_data,
4567 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004568 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004569{
Ronald Cron5425a212020-08-04 14:58:35 +02004570 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004571 psa_algorithm_t alg = alg_arg;
4572 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004573 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004575 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004576
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004577 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4578 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004579
Gilles Peskine8817f612018-12-18 00:18:46 +01004580 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004581
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004582 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4583 psa_set_key_algorithm( &attributes, alg );
4584 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004585 PSA_ASSERT( psa_import_key( &attributes,
4586 our_key_data->x, our_key_data->len,
4587 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004588
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004589 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004590 PSA_ASSERT( psa_key_derivation_key_agreement(
4591 &operation,
4592 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4593 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004594 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4595 {
4596 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004597 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004598 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004599 NULL, 0 ) );
4600 }
Gilles Peskine59685592018-09-18 12:11:34 +02004601
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004603 actual_output,
4604 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004605 ASSERT_COMPARE( actual_output, expected_output1->len,
4606 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004607 if( expected_output2->len != 0 )
4608 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004609 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004610 actual_output,
4611 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004612 ASSERT_COMPARE( actual_output, expected_output2->len,
4613 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004614 }
Gilles Peskine59685592018-09-18 12:11:34 +02004615
4616exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004617 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004618 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004619 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004620 mbedtls_free( actual_output );
4621}
4622/* END_CASE */
4623
4624/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004625void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004626{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004627 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004628 unsigned char *output = NULL;
4629 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004630 size_t i;
4631 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004632
Simon Butcher49f8e312020-03-03 15:51:50 +00004633 TEST_ASSERT( bytes_arg >= 0 );
4634
Gilles Peskine91892022021-02-08 19:50:26 +01004635 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004636 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004637
Gilles Peskine8817f612018-12-18 00:18:46 +01004638 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004639
Gilles Peskinea50d7392018-06-21 10:22:13 +02004640 /* Run several times, to ensure that every output byte will be
4641 * nonzero at least once with overwhelming probability
4642 * (2^(-8*number_of_runs)). */
4643 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004644 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004645 if( bytes != 0 )
4646 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004647 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004648
Gilles Peskinea50d7392018-06-21 10:22:13 +02004649 for( i = 0; i < bytes; i++ )
4650 {
4651 if( output[i] != 0 )
4652 ++changed[i];
4653 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004654 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004655
4656 /* Check that every byte was changed to nonzero at least once. This
4657 * validates that psa_generate_random is overwriting every byte of
4658 * the output buffer. */
4659 for( i = 0; i < bytes; i++ )
4660 {
4661 TEST_ASSERT( changed[i] != 0 );
4662 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004663
4664exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004665 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004666 mbedtls_free( output );
4667 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004668}
4669/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004670
4671/* BEGIN_CASE */
4672void generate_key( int type_arg,
4673 int bits_arg,
4674 int usage_arg,
4675 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004676 int expected_status_arg,
4677 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004678{
Ronald Cron5425a212020-08-04 14:58:35 +02004679 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004680 psa_key_type_t type = type_arg;
4681 psa_key_usage_t usage = usage_arg;
4682 size_t bits = bits_arg;
4683 psa_algorithm_t alg = alg_arg;
4684 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004686 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004687
Gilles Peskine8817f612018-12-18 00:18:46 +01004688 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004689
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004690 psa_set_key_usage_flags( &attributes, usage );
4691 psa_set_key_algorithm( &attributes, alg );
4692 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004693 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004694
4695 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004696 psa_status_t status = psa_generate_key( &attributes, &key );
4697
4698 if( is_large_key > 0 )
4699 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4700 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004701 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004702 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004703
4704 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004705 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004706 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4707 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004708
Gilles Peskine818ca122018-06-20 18:16:48 +02004709 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004710 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004711 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004712
4713exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004714 /*
4715 * Key attributes may have been returned by psa_get_key_attributes()
4716 * thus reset them as required.
4717 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004718 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004719
Ronald Cron5425a212020-08-04 14:58:35 +02004720 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004721 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004722}
4723/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004724
Ronald Cronee414c72021-03-18 18:50:08 +01004725/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004726void generate_key_rsa( int bits_arg,
4727 data_t *e_arg,
4728 int expected_status_arg )
4729{
Ronald Cron5425a212020-08-04 14:58:35 +02004730 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004731 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004732 size_t bits = bits_arg;
4733 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4734 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4735 psa_status_t expected_status = expected_status_arg;
4736 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4737 uint8_t *exported = NULL;
4738 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004739 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004740 size_t exported_length = SIZE_MAX;
4741 uint8_t *e_read_buffer = NULL;
4742 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004743 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004744 size_t e_read_length = SIZE_MAX;
4745
4746 if( e_arg->len == 0 ||
4747 ( e_arg->len == 3 &&
4748 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4749 {
4750 is_default_public_exponent = 1;
4751 e_read_size = 0;
4752 }
4753 ASSERT_ALLOC( e_read_buffer, e_read_size );
4754 ASSERT_ALLOC( exported, exported_size );
4755
4756 PSA_ASSERT( psa_crypto_init( ) );
4757
4758 psa_set_key_usage_flags( &attributes, usage );
4759 psa_set_key_algorithm( &attributes, alg );
4760 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4761 e_arg->x, e_arg->len ) );
4762 psa_set_key_bits( &attributes, bits );
4763
4764 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004765 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004766 if( expected_status != PSA_SUCCESS )
4767 goto exit;
4768
4769 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004770 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004771 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4772 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4773 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4774 e_read_buffer, e_read_size,
4775 &e_read_length ) );
4776 if( is_default_public_exponent )
4777 TEST_EQUAL( e_read_length, 0 );
4778 else
4779 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4780
4781 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004782 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02004783 goto exit;
4784
4785 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02004786 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02004787 exported, exported_size,
4788 &exported_length ) );
4789 {
4790 uint8_t *p = exported;
4791 uint8_t *end = exported + exported_length;
4792 size_t len;
4793 /* RSAPublicKey ::= SEQUENCE {
4794 * modulus INTEGER, -- n
4795 * publicExponent INTEGER } -- e
4796 */
4797 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004798 MBEDTLS_ASN1_SEQUENCE |
4799 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01004800 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004801 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4802 MBEDTLS_ASN1_INTEGER ) );
4803 if( len >= 1 && p[0] == 0 )
4804 {
4805 ++p;
4806 --len;
4807 }
4808 if( e_arg->len == 0 )
4809 {
4810 TEST_EQUAL( len, 3 );
4811 TEST_EQUAL( p[0], 1 );
4812 TEST_EQUAL( p[1], 0 );
4813 TEST_EQUAL( p[2], 1 );
4814 }
4815 else
4816 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4817 }
4818
4819exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004820 /*
4821 * Key attributes may have been returned by psa_get_key_attributes() or
4822 * set by psa_set_key_domain_parameters() thus reset them as required.
4823 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004824 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004825
Ronald Cron5425a212020-08-04 14:58:35 +02004826 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004827 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004828 mbedtls_free( e_read_buffer );
4829 mbedtls_free( exported );
4830}
4831/* END_CASE */
4832
Darryl Greend49a4992018-06-18 17:27:26 +01004833/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004834void persistent_key_load_key_from_storage( data_t *data,
4835 int type_arg, int bits_arg,
4836 int usage_flags_arg, int alg_arg,
4837 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004838{
Ronald Cron71016a92020-08-28 19:01:50 +02004839 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004840 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02004841 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4842 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004843 psa_key_type_t type = type_arg;
4844 size_t bits = bits_arg;
4845 psa_key_usage_t usage_flags = usage_flags_arg;
4846 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004847 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004848 unsigned char *first_export = NULL;
4849 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004850 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004851 size_t first_exported_length;
4852 size_t second_exported_length;
4853
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004854 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4855 {
4856 ASSERT_ALLOC( first_export, export_size );
4857 ASSERT_ALLOC( second_export, export_size );
4858 }
Darryl Greend49a4992018-06-18 17:27:26 +01004859
Gilles Peskine8817f612018-12-18 00:18:46 +01004860 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004861
Gilles Peskinec87af662019-05-15 16:12:22 +02004862 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004863 psa_set_key_usage_flags( &attributes, usage_flags );
4864 psa_set_key_algorithm( &attributes, alg );
4865 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004866 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004867
Darryl Green0c6575a2018-11-07 16:05:30 +00004868 switch( generation_method )
4869 {
4870 case IMPORT_KEY:
4871 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004872 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004873 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004874 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004875
Darryl Green0c6575a2018-11-07 16:05:30 +00004876 case GENERATE_KEY:
4877 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004878 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004879 break;
4880
4881 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01004882#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004883 {
4884 /* Create base key */
4885 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4886 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4887 psa_set_key_usage_flags( &base_attributes,
4888 PSA_KEY_USAGE_DERIVE );
4889 psa_set_key_algorithm( &base_attributes, derive_alg );
4890 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004891 PSA_ASSERT( psa_import_key( &base_attributes,
4892 data->x, data->len,
4893 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004894 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004896 PSA_ASSERT( psa_key_derivation_input_key(
4897 &operation,
4898 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004899 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004900 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004901 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004902 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
4903 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004904 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004905 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004906 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02004907 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004908 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01004909#else
4910 TEST_ASSUME( ! "KDF not supported in this configuration" );
4911#endif
4912 break;
4913
4914 default:
4915 TEST_ASSERT( ! "generation_method not implemented in test" );
4916 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00004917 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004918 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004919
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004920 /* Export the key if permitted by the key policy. */
4921 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4922 {
Ronald Cron5425a212020-08-04 14:58:35 +02004923 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004924 first_export, export_size,
4925 &first_exported_length ) );
4926 if( generation_method == IMPORT_KEY )
4927 ASSERT_COMPARE( data->x, data->len,
4928 first_export, first_exported_length );
4929 }
Darryl Greend49a4992018-06-18 17:27:26 +01004930
4931 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02004932 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004933 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01004934 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004935
Darryl Greend49a4992018-06-18 17:27:26 +01004936 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02004937 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02004938 TEST_ASSERT( mbedtls_svc_key_id_equal(
4939 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004940 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4941 PSA_KEY_LIFETIME_PERSISTENT );
4942 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4943 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4944 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4945 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004946
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004947 /* Export the key again if permitted by the key policy. */
4948 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004949 {
Ronald Cron5425a212020-08-04 14:58:35 +02004950 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004951 second_export, export_size,
4952 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004953 ASSERT_COMPARE( first_export, first_exported_length,
4954 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004955 }
4956
4957 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004958 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004959 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004960
4961exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004962 /*
4963 * Key attributes may have been returned by psa_get_key_attributes()
4964 * thus reset them as required.
4965 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004966 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004967
Darryl Greend49a4992018-06-18 17:27:26 +01004968 mbedtls_free( first_export );
4969 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004971 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02004972 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004973 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01004974}
4975/* END_CASE */