blob: 310b2a7b66f27bc59c59933a15fe60cb4d9878d3 [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 ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
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 ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
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 ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
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;
Bence Szépkútiec174e22021-03-19 18:46:15 +01002988 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002989 unsigned char *output_data = NULL;
2990 size_t output_size = 0;
2991 size_t output_length = 0;
2992 unsigned char *output_data2 = NULL;
2993 size_t output_length2 = 0;
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 Peskine8817f612018-12-18 00:18:46 +01002998 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002999
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003000 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3001 psa_set_key_algorithm( &attributes, alg );
3002 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003003
Gilles Peskine049c7532019-05-15 20:22:09 +02003004 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003005 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003006 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3007 key_bits = psa_get_key_bits( &attributes );
3008
3009 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3010 alg );
3011 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3012 * should be exact. */
3013 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3014 expected_result != PSA_ERROR_NOT_SUPPORTED )
3015 {
3016 TEST_EQUAL( output_size,
3017 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3018 TEST_ASSERT( output_size <=
3019 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3020 }
3021 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003022
Steven Cooremanf49478b2021-02-15 15:19:25 +01003023 status = psa_aead_encrypt( key, alg,
3024 nonce->x, nonce->len,
3025 additional_data->x,
3026 additional_data->len,
3027 input_data->x, input_data->len,
3028 output_data, output_size,
3029 &output_length );
3030
3031 /* If the operation is not supported, just skip and not fail in case the
3032 * encryption involves a common limitation of cryptography hardwares and
3033 * an alternative implementation. */
3034 if( status == PSA_ERROR_NOT_SUPPORTED )
3035 {
3036 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3037 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3038 }
3039
3040 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003041
3042 if( PSA_SUCCESS == expected_result )
3043 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003044 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003045
Gilles Peskine003a4a92019-05-14 16:09:40 +02003046 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3047 * should be exact. */
3048 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003049 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003050
gabor-mezei-armceface22021-01-21 12:26:17 +01003051 TEST_ASSERT( input_data->len <=
3052 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3053
Ronald Cron5425a212020-08-04 14:58:35 +02003054 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003055 nonce->x, nonce->len,
3056 additional_data->x,
3057 additional_data->len,
3058 output_data, output_length,
3059 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003060 &output_length2 ),
3061 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003062
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003063 ASSERT_COMPARE( input_data->x, input_data->len,
3064 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003065 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003066
Gilles Peskinea1cac842018-06-11 19:33:02 +02003067exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003068 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003069 mbedtls_free( output_data );
3070 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003071 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003072}
3073/* END_CASE */
3074
3075/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003076void aead_encrypt( int key_type_arg, data_t *key_data,
3077 int alg_arg,
3078 data_t *nonce,
3079 data_t *additional_data,
3080 data_t *input_data,
3081 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003082{
Ronald Cron5425a212020-08-04 14:58:35 +02003083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003084 psa_key_type_t key_type = key_type_arg;
3085 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003086 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003087 unsigned char *output_data = NULL;
3088 size_t output_size = 0;
3089 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003091 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003092
Gilles Peskine8817f612018-12-18 00:18:46 +01003093 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003094
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003095 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3096 psa_set_key_algorithm( &attributes, alg );
3097 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003098
Gilles Peskine049c7532019-05-15 20:22:09 +02003099 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003100 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003101 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3102 key_bits = psa_get_key_bits( &attributes );
3103
3104 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3105 alg );
3106 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3107 * should be exact. */
3108 TEST_EQUAL( output_size,
3109 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3110 TEST_ASSERT( output_size <=
3111 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3112 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003113
Steven Cooremand588ea12021-01-11 19:36:04 +01003114 status = psa_aead_encrypt( key, alg,
3115 nonce->x, nonce->len,
3116 additional_data->x, additional_data->len,
3117 input_data->x, input_data->len,
3118 output_data, output_size,
3119 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003120
Ronald Cron28a45ed2021-02-09 20:35:42 +01003121 /* If the operation is not supported, just skip and not fail in case the
3122 * encryption involves a common limitation of cryptography hardwares and
3123 * an alternative implementation. */
3124 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003125 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003126 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3127 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003128 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003129
3130 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003131 ASSERT_COMPARE( expected_result->x, expected_result->len,
3132 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003133
Gilles Peskinea1cac842018-06-11 19:33:02 +02003134exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003135 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003136 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003137 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003138}
3139/* END_CASE */
3140
3141/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003142void aead_decrypt( int key_type_arg, data_t *key_data,
3143 int alg_arg,
3144 data_t *nonce,
3145 data_t *additional_data,
3146 data_t *input_data,
3147 data_t *expected_data,
3148 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003149{
Ronald Cron5425a212020-08-04 14:58:35 +02003150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003151 psa_key_type_t key_type = key_type_arg;
3152 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003153 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003154 unsigned char *output_data = NULL;
3155 size_t output_size = 0;
3156 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003158 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003159 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003160
Gilles Peskine8817f612018-12-18 00:18:46 +01003161 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003162
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003163 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3164 psa_set_key_algorithm( &attributes, alg );
3165 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003166
Gilles Peskine049c7532019-05-15 20:22:09 +02003167 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003168 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003169 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3170 key_bits = psa_get_key_bits( &attributes );
3171
3172 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3173 alg );
3174 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3175 expected_result != PSA_ERROR_NOT_SUPPORTED )
3176 {
3177 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3178 * should be exact. */
3179 TEST_EQUAL( output_size,
3180 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3181 TEST_ASSERT( output_size <=
3182 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3183 }
3184 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003185
Steven Cooremand588ea12021-01-11 19:36:04 +01003186 status = psa_aead_decrypt( key, alg,
3187 nonce->x, nonce->len,
3188 additional_data->x,
3189 additional_data->len,
3190 input_data->x, input_data->len,
3191 output_data, output_size,
3192 &output_length );
3193
Ronald Cron28a45ed2021-02-09 20:35:42 +01003194 /* If the operation is not supported, just skip and not fail in case the
3195 * decryption involves a common limitation of cryptography hardwares and
3196 * an alternative implementation. */
3197 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003198 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003199 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3200 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003201 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003202
3203 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003204
Gilles Peskine2d277862018-06-18 15:41:12 +02003205 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003206 ASSERT_COMPARE( expected_data->x, expected_data->len,
3207 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003208
Gilles Peskinea1cac842018-06-11 19:33:02 +02003209exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003210 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003211 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003212 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003213}
3214/* END_CASE */
3215
3216/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003217void signature_size( int type_arg,
3218 int bits,
3219 int alg_arg,
3220 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003221{
3222 psa_key_type_t type = type_arg;
3223 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003224 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003225
Gilles Peskinefe11b722018-12-18 00:24:04 +01003226 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003227#if defined(MBEDTLS_TEST_DEPRECATED)
3228 TEST_EQUAL( actual_size,
3229 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3230#endif /* MBEDTLS_TEST_DEPRECATED */
3231
Gilles Peskinee59236f2018-01-27 23:32:46 +01003232exit:
3233 ;
3234}
3235/* END_CASE */
3236
3237/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003238void sign_deterministic( int key_type_arg, data_t *key_data,
3239 int alg_arg, data_t *input_data,
3240 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003241{
Ronald Cron5425a212020-08-04 14:58:35 +02003242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003243 psa_key_type_t key_type = key_type_arg;
3244 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003245 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003246 unsigned char *signature = NULL;
3247 size_t signature_size;
3248 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003250
Gilles Peskine8817f612018-12-18 00:18:46 +01003251 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003252
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003253 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003254 psa_set_key_algorithm( &attributes, alg );
3255 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003256
Gilles Peskine049c7532019-05-15 20:22:09 +02003257 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003258 &key ) );
3259 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003260 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003261
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003262 /* Allocate a buffer which has the size advertized by the
3263 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003264 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003265 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003266 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003267 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003268 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003269
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003270 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003271 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003272 input_data->x, input_data->len,
3273 signature, signature_size,
3274 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003275 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003276 ASSERT_COMPARE( output_data->x, output_data->len,
3277 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003278
Gilles Peskine0627f982019-11-26 19:12:16 +01003279#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003280 memset( signature, 0, signature_size );
3281 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003282 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003283 input_data->x, input_data->len,
3284 signature, signature_size,
3285 &signature_length ) );
3286 ASSERT_COMPARE( output_data->x, output_data->len,
3287 signature, signature_length );
3288#endif /* MBEDTLS_TEST_DEPRECATED */
3289
Gilles Peskine20035e32018-02-03 22:44:14 +01003290exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003291 /*
3292 * Key attributes may have been returned by psa_get_key_attributes()
3293 * thus reset them as required.
3294 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003295 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003296
Ronald Cron5425a212020-08-04 14:58:35 +02003297 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003298 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003299 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003300}
3301/* END_CASE */
3302
3303/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003304void sign_fail( int key_type_arg, data_t *key_data,
3305 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003306 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003307{
Ronald Cron5425a212020-08-04 14:58:35 +02003308 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003309 psa_key_type_t key_type = key_type_arg;
3310 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003311 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003312 psa_status_t actual_status;
3313 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003314 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003315 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003317
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003318 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003319
Gilles Peskine8817f612018-12-18 00:18:46 +01003320 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003321
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003322 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003323 psa_set_key_algorithm( &attributes, alg );
3324 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003325
Gilles Peskine049c7532019-05-15 20:22:09 +02003326 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003327 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003328
Ronald Cron5425a212020-08-04 14:58:35 +02003329 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003330 input_data->x, input_data->len,
3331 signature, signature_size,
3332 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003333 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003334 /* The value of *signature_length is unspecified on error, but
3335 * whatever it is, it should be less than signature_size, so that
3336 * if the caller tries to read *signature_length bytes without
3337 * checking the error code then they don't overflow a buffer. */
3338 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003339
Gilles Peskine895242b2019-11-29 12:15:40 +01003340#if defined(MBEDTLS_TEST_DEPRECATED)
3341 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003342 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003343 input_data->x, input_data->len,
3344 signature, signature_size,
3345 &signature_length ),
3346 expected_status );
3347 TEST_ASSERT( signature_length <= signature_size );
3348#endif /* MBEDTLS_TEST_DEPRECATED */
3349
Gilles Peskine20035e32018-02-03 22:44:14 +01003350exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003351 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003352 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003353 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003354 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003355}
3356/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003357
3358/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003359void sign_verify( int key_type_arg, data_t *key_data,
3360 int alg_arg, data_t *input_data )
3361{
Ronald Cron5425a212020-08-04 14:58:35 +02003362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003363 psa_key_type_t key_type = key_type_arg;
3364 psa_algorithm_t alg = alg_arg;
3365 size_t key_bits;
3366 unsigned char *signature = NULL;
3367 size_t signature_size;
3368 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003370
Gilles Peskine8817f612018-12-18 00:18:46 +01003371 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003372
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003373 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003374 psa_set_key_algorithm( &attributes, alg );
3375 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003376
Gilles Peskine049c7532019-05-15 20:22:09 +02003377 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003378 &key ) );
3379 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003380 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003381
3382 /* Allocate a buffer which has the size advertized by the
3383 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003384 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003385 key_bits, alg );
3386 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003387 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003388 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003389
3390 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003391 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003392 input_data->x, input_data->len,
3393 signature, signature_size,
3394 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003395 /* Check that the signature length looks sensible. */
3396 TEST_ASSERT( signature_length <= signature_size );
3397 TEST_ASSERT( signature_length > 0 );
3398
3399 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003400 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003401 input_data->x, input_data->len,
3402 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003403
3404 if( input_data->len != 0 )
3405 {
3406 /* Flip a bit in the input and verify that the signature is now
3407 * detected as invalid. Flip a bit at the beginning, not at the end,
3408 * because ECDSA may ignore the last few bits of the input. */
3409 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003410 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003411 input_data->x, input_data->len,
3412 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003413 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003414 }
3415
3416exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003417 /*
3418 * Key attributes may have been returned by psa_get_key_attributes()
3419 * thus reset them as required.
3420 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003421 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003422
Ronald Cron5425a212020-08-04 14:58:35 +02003423 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003424 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003425 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003426}
3427/* END_CASE */
3428
3429/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003430void asymmetric_verify( int key_type_arg, data_t *key_data,
3431 int alg_arg, data_t *hash_data,
3432 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003433{
Ronald Cron5425a212020-08-04 14:58:35 +02003434 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003435 psa_key_type_t key_type = key_type_arg;
3436 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003438
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003439 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003440
Gilles Peskine8817f612018-12-18 00:18:46 +01003441 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003442
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003443 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003444 psa_set_key_algorithm( &attributes, alg );
3445 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003446
Gilles Peskine049c7532019-05-15 20:22:09 +02003447 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003448 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003449
Ronald Cron5425a212020-08-04 14:58:35 +02003450 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003451 hash_data->x, hash_data->len,
3452 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003453
3454#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003455 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003456 hash_data->x, hash_data->len,
3457 signature_data->x,
3458 signature_data->len ) );
3459
3460#endif /* MBEDTLS_TEST_DEPRECATED */
3461
itayzafrir5c753392018-05-08 11:18:38 +03003462exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003463 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003464 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003465 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003466}
3467/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003468
3469/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003470void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3471 int alg_arg, data_t *hash_data,
3472 data_t *signature_data,
3473 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003474{
Ronald Cron5425a212020-08-04 14:58:35 +02003475 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003476 psa_key_type_t key_type = key_type_arg;
3477 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003478 psa_status_t actual_status;
3479 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003480 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003481
Gilles Peskine8817f612018-12-18 00:18:46 +01003482 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003483
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003484 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003485 psa_set_key_algorithm( &attributes, alg );
3486 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003487
Gilles Peskine049c7532019-05-15 20:22:09 +02003488 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003489 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003490
Ronald Cron5425a212020-08-04 14:58:35 +02003491 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003492 hash_data->x, hash_data->len,
3493 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003494 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003495
Gilles Peskine895242b2019-11-29 12:15:40 +01003496#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003497 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003498 hash_data->x, hash_data->len,
3499 signature_data->x, signature_data->len ),
3500 expected_status );
3501#endif /* MBEDTLS_TEST_DEPRECATED */
3502
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003503exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003504 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003505 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003506 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003507}
3508/* END_CASE */
3509
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003510/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003511void asymmetric_encrypt( int key_type_arg,
3512 data_t *key_data,
3513 int alg_arg,
3514 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003515 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003516 int expected_output_length_arg,
3517 int expected_status_arg )
3518{
Ronald Cron5425a212020-08-04 14:58:35 +02003519 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003520 psa_key_type_t key_type = key_type_arg;
3521 psa_algorithm_t alg = alg_arg;
3522 size_t expected_output_length = expected_output_length_arg;
3523 size_t key_bits;
3524 unsigned char *output = NULL;
3525 size_t output_size;
3526 size_t output_length = ~0;
3527 psa_status_t actual_status;
3528 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003530
Gilles Peskine8817f612018-12-18 00:18:46 +01003531 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003532
Gilles Peskine656896e2018-06-29 19:12:28 +02003533 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003534 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3535 psa_set_key_algorithm( &attributes, alg );
3536 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003537 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003538 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003539
3540 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003541 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003542 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003543
Gilles Peskine656896e2018-06-29 19:12:28 +02003544 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003545 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003546 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003547
3548 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003549 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003550 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003551 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003552 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 Peskine656896e2018-06-29 19:12:28 +02003556
Gilles Peskine68428122018-06-30 18:42:41 +02003557 /* If the label is empty, the test framework puts a non-null pointer
3558 * in label->x. Test that a null pointer works as well. */
3559 if( label->len == 0 )
3560 {
3561 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003562 if( output_size != 0 )
3563 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003564 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003565 input_data->x, input_data->len,
3566 NULL, label->len,
3567 output, output_size,
3568 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003569 TEST_EQUAL( actual_status, expected_status );
3570 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003571 }
3572
Gilles Peskine656896e2018-06-29 19:12:28 +02003573exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003574 /*
3575 * Key attributes may have been returned by psa_get_key_attributes()
3576 * thus reset them as required.
3577 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003578 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003579
Ronald Cron5425a212020-08-04 14:58:35 +02003580 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003581 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003582 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003583}
3584/* END_CASE */
3585
3586/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003587void asymmetric_encrypt_decrypt( int key_type_arg,
3588 data_t *key_data,
3589 int alg_arg,
3590 data_t *input_data,
3591 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003592{
Ronald Cron5425a212020-08-04 14:58:35 +02003593 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003594 psa_key_type_t key_type = key_type_arg;
3595 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003596 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003597 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003598 size_t output_size;
3599 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003600 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003601 size_t output2_size;
3602 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003604
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003606
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3608 psa_set_key_algorithm( &attributes, alg );
3609 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003610
Gilles Peskine049c7532019-05-15 20:22:09 +02003611 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003612 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003613
3614 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003615 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003616 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003617
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003618 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003619 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003620 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003621
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003622 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003623 TEST_ASSERT( output2_size <=
3624 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3625 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003626 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003627
Gilles Peskineeebd7382018-06-08 18:11:54 +02003628 /* We test encryption by checking that encrypt-then-decrypt gives back
3629 * the original plaintext because of the non-optional random
3630 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003631 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003632 input_data->x, input_data->len,
3633 label->x, label->len,
3634 output, output_size,
3635 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003636 /* We don't know what ciphertext length to expect, but check that
3637 * it looks sensible. */
3638 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003639
Ronald Cron5425a212020-08-04 14:58:35 +02003640 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003641 output, output_length,
3642 label->x, label->len,
3643 output2, output2_size,
3644 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003645 ASSERT_COMPARE( input_data->x, input_data->len,
3646 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003647
3648exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003649 /*
3650 * Key attributes may have been returned by psa_get_key_attributes()
3651 * thus reset them as required.
3652 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003653 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003654
Ronald Cron5425a212020-08-04 14:58:35 +02003655 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003656 mbedtls_free( output );
3657 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003658 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659}
3660/* END_CASE */
3661
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003663void asymmetric_decrypt( int key_type_arg,
3664 data_t *key_data,
3665 int alg_arg,
3666 data_t *input_data,
3667 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003668 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003669{
Ronald Cron5425a212020-08-04 14:58:35 +02003670 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003671 psa_key_type_t key_type = key_type_arg;
3672 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003673 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003674 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003675 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003676 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003678
Gilles Peskine8817f612018-12-18 00:18:46 +01003679 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003680
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003681 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3682 psa_set_key_algorithm( &attributes, alg );
3683 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003684
Gilles Peskine049c7532019-05-15 20:22:09 +02003685 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003686 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003687
gabor-mezei-armceface22021-01-21 12:26:17 +01003688 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3689 key_bits = psa_get_key_bits( &attributes );
3690
3691 /* Determine the maximum ciphertext length */
3692 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3693 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3694 ASSERT_ALLOC( output, output_size );
3695
Ronald Cron5425a212020-08-04 14:58:35 +02003696 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003697 input_data->x, input_data->len,
3698 label->x, label->len,
3699 output,
3700 output_size,
3701 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003702 ASSERT_COMPARE( expected_data->x, expected_data->len,
3703 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003704
Gilles Peskine68428122018-06-30 18:42:41 +02003705 /* If the label is empty, the test framework puts a non-null pointer
3706 * in label->x. Test that a null pointer works as well. */
3707 if( label->len == 0 )
3708 {
3709 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003710 if( output_size != 0 )
3711 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003712 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003713 input_data->x, input_data->len,
3714 NULL, label->len,
3715 output,
3716 output_size,
3717 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003718 ASSERT_COMPARE( expected_data->x, expected_data->len,
3719 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003720 }
3721
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003722exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003723 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003724 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003725 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003726 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727}
3728/* END_CASE */
3729
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003730/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003731void asymmetric_decrypt_fail( int key_type_arg,
3732 data_t *key_data,
3733 int alg_arg,
3734 data_t *input_data,
3735 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003736 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003737 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003738{
Ronald Cron5425a212020-08-04 14:58:35 +02003739 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003740 psa_key_type_t key_type = key_type_arg;
3741 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003742 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003743 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003744 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003745 psa_status_t actual_status;
3746 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003748
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003749 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003750
Gilles Peskine8817f612018-12-18 00:18:46 +01003751 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003752
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003753 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3754 psa_set_key_algorithm( &attributes, alg );
3755 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003756
Gilles Peskine049c7532019-05-15 20:22:09 +02003757 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003758 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003759
Ronald Cron5425a212020-08-04 14:58:35 +02003760 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003761 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003762 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003763 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003764 &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 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003767
Gilles Peskine68428122018-06-30 18:42:41 +02003768 /* If the label is empty, the test framework puts a non-null pointer
3769 * in label->x. Test that a null pointer works as well. */
3770 if( label->len == 0 )
3771 {
3772 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003773 if( output_size != 0 )
3774 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003775 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003776 input_data->x, input_data->len,
3777 NULL, label->len,
3778 output, output_size,
3779 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003780 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003781 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003782 }
3783
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003784exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003785 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003786 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02003787 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003788 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003789}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003790/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003791
3792/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003793void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00003794{
3795 /* Test each valid way of initializing the object, except for `= {0}`, as
3796 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3797 * though it's OK by the C standard. We could test for this, but we'd need
3798 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003799 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003800 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
3801 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
3802 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003803
3804 memset( &zero, 0, sizeof( zero ) );
3805
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003806 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003807 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003808 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003809 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003810 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003811 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003812 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003813
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003814 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003815 PSA_ASSERT( psa_key_derivation_abort(&func) );
3816 PSA_ASSERT( psa_key_derivation_abort(&init) );
3817 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003818}
3819/* END_CASE */
3820
Janos Follath16de4a42019-06-13 16:32:24 +01003821/* BEGIN_CASE */
3822void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003823{
Gilles Peskineea0fb492018-07-12 17:17:20 +02003824 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003825 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003826 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003827
Gilles Peskine8817f612018-12-18 00:18:46 +01003828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003829
Janos Follath16de4a42019-06-13 16:32:24 +01003830 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003831 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003832
3833exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003834 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003835 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003836}
3837/* END_CASE */
3838
Janos Follathaf3c2a02019-06-12 12:34:34 +01003839/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01003840void derive_set_capacity( int alg_arg, int capacity_arg,
3841 int expected_status_arg )
3842{
3843 psa_algorithm_t alg = alg_arg;
3844 size_t capacity = capacity_arg;
3845 psa_status_t expected_status = expected_status_arg;
3846 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3847
3848 PSA_ASSERT( psa_crypto_init( ) );
3849
3850 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3851
3852 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
3853 expected_status );
3854
3855exit:
3856 psa_key_derivation_abort( &operation );
3857 PSA_DONE( );
3858}
3859/* END_CASE */
3860
3861/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01003862void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02003863 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003864 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02003865 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003866 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02003867 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003868 int expected_status_arg3,
3869 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003870{
3871 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02003872 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
3873 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01003874 psa_status_t expected_statuses[] = {expected_status_arg1,
3875 expected_status_arg2,
3876 expected_status_arg3};
3877 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003878 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3879 MBEDTLS_SVC_KEY_ID_INIT,
3880 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01003881 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3883 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003884 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003885 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003886 psa_status_t expected_output_status = expected_output_status_arg;
3887 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01003888
3889 PSA_ASSERT( psa_crypto_init( ) );
3890
3891 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3892 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003893
3894 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3895
3896 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
3897 {
Gilles Peskineb8965192019-09-24 16:21:10 +02003898 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003899 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02003900 psa_set_key_type( &attributes, key_types[i] );
3901 PSA_ASSERT( psa_import_key( &attributes,
3902 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003903 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003904 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
3905 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
3906 {
3907 // When taking a private key as secret input, use key agreement
3908 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003909 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
3910 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003911 expected_statuses[i] );
3912 }
3913 else
3914 {
3915 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02003916 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003917 expected_statuses[i] );
3918 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02003919 }
3920 else
3921 {
3922 TEST_EQUAL( psa_key_derivation_input_bytes(
3923 &operation, steps[i],
3924 inputs[i]->x, inputs[i]->len ),
3925 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003926 }
3927 }
3928
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003929 if( output_key_type != PSA_KEY_TYPE_NONE )
3930 {
3931 psa_reset_key_attributes( &attributes );
3932 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
3933 psa_set_key_bits( &attributes, 8 );
3934 actual_output_status =
3935 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02003936 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003937 }
3938 else
3939 {
3940 uint8_t buffer[1];
3941 actual_output_status =
3942 psa_key_derivation_output_bytes( &operation,
3943 buffer, sizeof( buffer ) );
3944 }
3945 TEST_EQUAL( actual_output_status, expected_output_status );
3946
Janos Follathaf3c2a02019-06-12 12:34:34 +01003947exit:
3948 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003949 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3950 psa_destroy_key( keys[i] );
3951 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003952 PSA_DONE( );
3953}
3954/* END_CASE */
3955
Janos Follathd958bb72019-07-03 15:02:16 +01003956/* BEGIN_CASE */
3957void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003958{
Janos Follathd958bb72019-07-03 15:02:16 +01003959 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003960 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003961 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003962 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01003963 unsigned char input1[] = "Input 1";
3964 size_t input1_length = sizeof( input1 );
3965 unsigned char input2[] = "Input 2";
3966 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003967 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003968 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003969 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3970 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3971 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003972 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003973
Gilles Peskine8817f612018-12-18 00:18:46 +01003974 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003975
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003976 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3977 psa_set_key_algorithm( &attributes, alg );
3978 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003979
Gilles Peskine73676cb2019-05-15 20:15:10 +02003980 PSA_ASSERT( psa_import_key( &attributes,
3981 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02003982 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003983
3984 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003985 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
3986 input1, input1_length,
3987 input2, input2_length,
3988 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01003989 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003990
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003991 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01003992 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003993 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003994
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003995 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003996
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003997 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02003998 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003999
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004000exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004001 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004002 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004003 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004004}
4005/* END_CASE */
4006
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004007/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004008void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004009{
4010 uint8_t output_buffer[16];
4011 size_t buffer_size = 16;
4012 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004013 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004014
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004015 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4016 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004017 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004018
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004019 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004020 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004021
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004022 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004023
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004024 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4025 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004026 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004027
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004028 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004029 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004030
4031exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004032 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004033}
4034/* END_CASE */
4035
4036/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004037void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004038 int step1_arg, data_t *input1,
4039 int step2_arg, data_t *input2,
4040 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004041 int requested_capacity_arg,
4042 data_t *expected_output1,
4043 data_t *expected_output2 )
4044{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004045 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004046 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4047 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004048 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4049 MBEDTLS_SVC_KEY_ID_INIT,
4050 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004051 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004052 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004053 uint8_t *expected_outputs[2] =
4054 {expected_output1->x, expected_output2->x};
4055 size_t output_sizes[2] =
4056 {expected_output1->len, expected_output2->len};
4057 size_t output_buffer_size = 0;
4058 uint8_t *output_buffer = NULL;
4059 size_t expected_capacity;
4060 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004061 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004062 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004063 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004064
4065 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4066 {
4067 if( output_sizes[i] > output_buffer_size )
4068 output_buffer_size = output_sizes[i];
4069 if( output_sizes[i] == 0 )
4070 expected_outputs[i] = NULL;
4071 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004072 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004073 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004074
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004075 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4076 psa_set_key_algorithm( &attributes, alg );
4077 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004078
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004079 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004080 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4081 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4082 requested_capacity ) );
4083 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004084 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004085 switch( steps[i] )
4086 {
4087 case 0:
4088 break;
4089 case PSA_KEY_DERIVATION_INPUT_SECRET:
4090 PSA_ASSERT( psa_import_key( &attributes,
4091 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004092 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004093
4094 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4095 {
4096 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4097 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4098 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4099 }
4100
Gilles Peskine1468da72019-05-29 17:35:49 +02004101 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004102 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004103 break;
4104 default:
4105 PSA_ASSERT( psa_key_derivation_input_bytes(
4106 &operation, steps[i],
4107 inputs[i]->x, inputs[i]->len ) );
4108 break;
4109 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004110 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004111
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004112 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004113 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004114 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004115 expected_capacity = requested_capacity;
4116
4117 /* Expansion phase. */
4118 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4119 {
4120 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004121 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004122 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004123 if( expected_capacity == 0 && output_sizes[i] == 0 )
4124 {
4125 /* Reading 0 bytes when 0 bytes are available can go either way. */
4126 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004127 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004128 continue;
4129 }
4130 else if( expected_capacity == 0 ||
4131 output_sizes[i] > expected_capacity )
4132 {
4133 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004134 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004135 expected_capacity = 0;
4136 continue;
4137 }
4138 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004139 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004140 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004141 ASSERT_COMPARE( output_buffer, output_sizes[i],
4142 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004143 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004144 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004145 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004146 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004147 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004148 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004149 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004150
4151exit:
4152 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004153 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004154 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4155 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004156 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004157}
4158/* END_CASE */
4159
4160/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004161void derive_full( int alg_arg,
4162 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004163 data_t *input1,
4164 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004165 int requested_capacity_arg )
4166{
Ronald Cron5425a212020-08-04 14:58:35 +02004167 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004168 psa_algorithm_t alg = alg_arg;
4169 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004170 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004171 unsigned char output_buffer[16];
4172 size_t expected_capacity = requested_capacity;
4173 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004175
Gilles Peskine8817f612018-12-18 00:18:46 +01004176 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004177
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004178 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4179 psa_set_key_algorithm( &attributes, alg );
4180 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004181
Gilles Peskine049c7532019-05-15 20:22:09 +02004182 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004183 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004184
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004185 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4186 input1->x, input1->len,
4187 input2->x, input2->len,
4188 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004189 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004190
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004192 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004193 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004194
4195 /* Expansion phase. */
4196 while( current_capacity > 0 )
4197 {
4198 size_t read_size = sizeof( output_buffer );
4199 if( read_size > current_capacity )
4200 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004201 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004202 output_buffer,
4203 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004204 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004205 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004206 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004207 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004208 }
4209
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004210 /* Check that the operation refuses to go over capacity. */
4211 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004212 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004213
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004214 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004215
4216exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004217 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004218 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004219 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004220}
4221/* END_CASE */
4222
Janos Follathe60c9052019-07-03 13:51:30 +01004223/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004224void derive_key_exercise( int alg_arg,
4225 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004226 data_t *input1,
4227 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004228 int derived_type_arg,
4229 int derived_bits_arg,
4230 int derived_usage_arg,
4231 int derived_alg_arg )
4232{
Ronald Cron5425a212020-08-04 14:58:35 +02004233 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4234 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004235 psa_algorithm_t alg = alg_arg;
4236 psa_key_type_t derived_type = derived_type_arg;
4237 size_t derived_bits = derived_bits_arg;
4238 psa_key_usage_t derived_usage = derived_usage_arg;
4239 psa_algorithm_t derived_alg = derived_alg_arg;
4240 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004241 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004243 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004244
Gilles Peskine8817f612018-12-18 00:18:46 +01004245 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004246
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004247 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4248 psa_set_key_algorithm( &attributes, alg );
4249 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004250 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004251 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004252
4253 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004254 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4255 input1->x, input1->len,
4256 input2->x, input2->len,
4257 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004258 goto exit;
4259
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004260 psa_set_key_usage_flags( &attributes, derived_usage );
4261 psa_set_key_algorithm( &attributes, derived_alg );
4262 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004263 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004264 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004265 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004266
4267 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004268 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004269 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4270 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004271
4272 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004273 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004274 goto exit;
4275
4276exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004277 /*
4278 * Key attributes may have been returned by psa_get_key_attributes()
4279 * thus reset them as required.
4280 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004281 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004282
4283 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004284 psa_destroy_key( base_key );
4285 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004286 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004287}
4288/* END_CASE */
4289
Janos Follath42fd8882019-07-03 14:17:09 +01004290/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004291void derive_key_export( int alg_arg,
4292 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004293 data_t *input1,
4294 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004295 int bytes1_arg,
4296 int bytes2_arg )
4297{
Ronald Cron5425a212020-08-04 14:58:35 +02004298 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4299 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004300 psa_algorithm_t alg = alg_arg;
4301 size_t bytes1 = bytes1_arg;
4302 size_t bytes2 = bytes2_arg;
4303 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004304 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004305 uint8_t *output_buffer = NULL;
4306 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004307 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4308 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004309 size_t length;
4310
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004311 ASSERT_ALLOC( output_buffer, capacity );
4312 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004313 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004314
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004315 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4316 psa_set_key_algorithm( &base_attributes, alg );
4317 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004318 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004319 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004320
4321 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004322 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4323 input1->x, input1->len,
4324 input2->x, input2->len,
4325 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004326 goto exit;
4327
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004328 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004329 output_buffer,
4330 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004331 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004332
4333 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004334 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4335 input1->x, input1->len,
4336 input2->x, input2->len,
4337 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004338 goto exit;
4339
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004340 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4341 psa_set_key_algorithm( &derived_attributes, 0 );
4342 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004343 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004344 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004345 &derived_key ) );
4346 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004347 export_buffer, bytes1,
4348 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004349 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004350 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004351 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004352 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004353 &derived_key ) );
4354 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004355 export_buffer + bytes1, bytes2,
4356 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004357 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004358
4359 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004360 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4361 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004362
4363exit:
4364 mbedtls_free( output_buffer );
4365 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004366 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004367 psa_destroy_key( base_key );
4368 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004369 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004370}
4371/* END_CASE */
4372
4373/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004374void derive_key( int alg_arg,
4375 data_t *key_data, data_t *input1, data_t *input2,
4376 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004377 int expected_status_arg,
4378 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004379{
Ronald Cron5425a212020-08-04 14:58:35 +02004380 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4381 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004382 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004383 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004384 size_t bits = bits_arg;
4385 psa_status_t expected_status = expected_status_arg;
4386 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4387 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4388 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4389
4390 PSA_ASSERT( psa_crypto_init( ) );
4391
4392 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4393 psa_set_key_algorithm( &base_attributes, alg );
4394 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4395 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004396 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004397
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004398 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4399 input1->x, input1->len,
4400 input2->x, input2->len,
4401 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004402 goto exit;
4403
4404 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4405 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004406 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004407 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004408
4409 psa_status_t status =
4410 psa_key_derivation_output_key( &derived_attributes,
4411 &operation,
4412 &derived_key );
4413 if( is_large_output > 0 )
4414 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4415 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004416
4417exit:
4418 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004419 psa_destroy_key( base_key );
4420 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004421 PSA_DONE( );
4422}
4423/* END_CASE */
4424
4425/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004426void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004427 int our_key_type_arg, int our_key_alg_arg,
4428 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004429 int expected_status_arg )
4430{
Ronald Cron5425a212020-08-04 14:58:35 +02004431 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004432 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004433 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004434 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004435 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004437 psa_status_t expected_status = expected_status_arg;
4438 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004439
Gilles Peskine8817f612018-12-18 00:18:46 +01004440 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004441
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004442 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004443 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004444 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004445 PSA_ASSERT( psa_import_key( &attributes,
4446 our_key_data->x, our_key_data->len,
4447 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004448
Gilles Peskine77f40d82019-04-11 21:27:06 +02004449 /* The tests currently include inputs that should fail at either step.
4450 * Test cases that fail at the setup step should be changed to call
4451 * key_derivation_setup instead, and this function should be renamed
4452 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004453 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004454 if( status == PSA_SUCCESS )
4455 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004456 TEST_EQUAL( psa_key_derivation_key_agreement(
4457 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4458 our_key,
4459 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004460 expected_status );
4461 }
4462 else
4463 {
4464 TEST_ASSERT( status == expected_status );
4465 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004466
4467exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004468 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004469 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004470 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004471}
4472/* END_CASE */
4473
4474/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004475void raw_key_agreement( int alg_arg,
4476 int our_key_type_arg, data_t *our_key_data,
4477 data_t *peer_key_data,
4478 data_t *expected_output )
4479{
Ronald Cron5425a212020-08-04 14:58:35 +02004480 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004481 psa_algorithm_t alg = alg_arg;
4482 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004483 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004484 unsigned char *output = NULL;
4485 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004486 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004487
4488 ASSERT_ALLOC( output, expected_output->len );
4489 PSA_ASSERT( psa_crypto_init( ) );
4490
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004491 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4492 psa_set_key_algorithm( &attributes, alg );
4493 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004494 PSA_ASSERT( psa_import_key( &attributes,
4495 our_key_data->x, our_key_data->len,
4496 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004497
gabor-mezei-armceface22021-01-21 12:26:17 +01004498 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4499 key_bits = psa_get_key_bits( &attributes );
4500
Gilles Peskinebe697d82019-05-16 18:00:41 +02004501 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4502 peer_key_data->x, peer_key_data->len,
4503 output, expected_output->len,
4504 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004505 ASSERT_COMPARE( output, output_length,
4506 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004507 TEST_ASSERT( output_length <=
4508 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4509 TEST_ASSERT( output_length <=
4510 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004511
4512exit:
4513 mbedtls_free( output );
4514 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004515 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004516}
4517/* END_CASE */
4518
4519/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004520void key_agreement_capacity( int alg_arg,
4521 int our_key_type_arg, data_t *our_key_data,
4522 data_t *peer_key_data,
4523 int expected_capacity_arg )
4524{
Ronald Cron5425a212020-08-04 14:58:35 +02004525 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004526 psa_algorithm_t alg = alg_arg;
4527 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004528 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004530 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004531 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004532
Gilles Peskine8817f612018-12-18 00:18:46 +01004533 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004534
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004535 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4536 psa_set_key_algorithm( &attributes, alg );
4537 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004538 PSA_ASSERT( psa_import_key( &attributes,
4539 our_key_data->x, our_key_data->len,
4540 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004541
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004543 PSA_ASSERT( psa_key_derivation_key_agreement(
4544 &operation,
4545 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4546 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004547 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4548 {
4549 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004550 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004551 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004552 NULL, 0 ) );
4553 }
Gilles Peskine59685592018-09-18 12:11:34 +02004554
Gilles Peskinebf491972018-10-25 22:36:12 +02004555 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004556 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004557 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004558 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004559
Gilles Peskinebf491972018-10-25 22:36:12 +02004560 /* Test the actual capacity by reading the output. */
4561 while( actual_capacity > sizeof( output ) )
4562 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004563 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004564 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004565 actual_capacity -= sizeof( output );
4566 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004567 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004568 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004569 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004570 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004571
Gilles Peskine59685592018-09-18 12:11:34 +02004572exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004573 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004574 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004575 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004576}
4577/* END_CASE */
4578
4579/* BEGIN_CASE */
4580void key_agreement_output( int alg_arg,
4581 int our_key_type_arg, data_t *our_key_data,
4582 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004583 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004584{
Ronald Cron5425a212020-08-04 14:58:35 +02004585 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004586 psa_algorithm_t alg = alg_arg;
4587 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004588 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004589 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004590 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004591
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004592 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4593 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004594
Gilles Peskine8817f612018-12-18 00:18:46 +01004595 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004596
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004597 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4598 psa_set_key_algorithm( &attributes, alg );
4599 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004600 PSA_ASSERT( psa_import_key( &attributes,
4601 our_key_data->x, our_key_data->len,
4602 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004603
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004604 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004605 PSA_ASSERT( psa_key_derivation_key_agreement(
4606 &operation,
4607 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4608 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004609 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4610 {
4611 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004612 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004613 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004614 NULL, 0 ) );
4615 }
Gilles Peskine59685592018-09-18 12:11:34 +02004616
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004617 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004618 actual_output,
4619 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004620 ASSERT_COMPARE( actual_output, expected_output1->len,
4621 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004622 if( expected_output2->len != 0 )
4623 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004624 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004625 actual_output,
4626 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004627 ASSERT_COMPARE( actual_output, expected_output2->len,
4628 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004629 }
Gilles Peskine59685592018-09-18 12:11:34 +02004630
4631exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004632 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004633 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004634 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004635 mbedtls_free( actual_output );
4636}
4637/* END_CASE */
4638
4639/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004640void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004641{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004642 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004643 unsigned char *output = NULL;
4644 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004645 size_t i;
4646 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004647
Simon Butcher49f8e312020-03-03 15:51:50 +00004648 TEST_ASSERT( bytes_arg >= 0 );
4649
Gilles Peskine91892022021-02-08 19:50:26 +01004650 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004651 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004652
Gilles Peskine8817f612018-12-18 00:18:46 +01004653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004654
Gilles Peskinea50d7392018-06-21 10:22:13 +02004655 /* Run several times, to ensure that every output byte will be
4656 * nonzero at least once with overwhelming probability
4657 * (2^(-8*number_of_runs)). */
4658 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004659 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004660 if( bytes != 0 )
4661 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004662 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004663
Gilles Peskinea50d7392018-06-21 10:22:13 +02004664 for( i = 0; i < bytes; i++ )
4665 {
4666 if( output[i] != 0 )
4667 ++changed[i];
4668 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004669 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004670
4671 /* Check that every byte was changed to nonzero at least once. This
4672 * validates that psa_generate_random is overwriting every byte of
4673 * the output buffer. */
4674 for( i = 0; i < bytes; i++ )
4675 {
4676 TEST_ASSERT( changed[i] != 0 );
4677 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004678
4679exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004680 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004681 mbedtls_free( output );
4682 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004683}
4684/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004685
4686/* BEGIN_CASE */
4687void generate_key( int type_arg,
4688 int bits_arg,
4689 int usage_arg,
4690 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004691 int expected_status_arg,
4692 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004693{
Ronald Cron5425a212020-08-04 14:58:35 +02004694 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004695 psa_key_type_t type = type_arg;
4696 psa_key_usage_t usage = usage_arg;
4697 size_t bits = bits_arg;
4698 psa_algorithm_t alg = alg_arg;
4699 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004701 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004702
Gilles Peskine8817f612018-12-18 00:18:46 +01004703 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004704
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004705 psa_set_key_usage_flags( &attributes, usage );
4706 psa_set_key_algorithm( &attributes, alg );
4707 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004708 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004709
4710 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004711 psa_status_t status = psa_generate_key( &attributes, &key );
4712
4713 if( is_large_key > 0 )
4714 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4715 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004716 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004717 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004718
4719 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004720 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004721 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4722 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004723
Gilles Peskine818ca122018-06-20 18:16:48 +02004724 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004725 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004726 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004727
4728exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004729 /*
4730 * Key attributes may have been returned by psa_get_key_attributes()
4731 * thus reset them as required.
4732 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004733 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004734
Ronald Cron5425a212020-08-04 14:58:35 +02004735 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004736 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004737}
4738/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004739
Ronald Cronee414c72021-03-18 18:50:08 +01004740/* 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 +02004741void generate_key_rsa( int bits_arg,
4742 data_t *e_arg,
4743 int expected_status_arg )
4744{
Ronald Cron5425a212020-08-04 14:58:35 +02004745 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004746 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004747 size_t bits = bits_arg;
4748 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4749 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4750 psa_status_t expected_status = expected_status_arg;
4751 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4752 uint8_t *exported = NULL;
4753 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004754 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004755 size_t exported_length = SIZE_MAX;
4756 uint8_t *e_read_buffer = NULL;
4757 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004758 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004759 size_t e_read_length = SIZE_MAX;
4760
4761 if( e_arg->len == 0 ||
4762 ( e_arg->len == 3 &&
4763 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4764 {
4765 is_default_public_exponent = 1;
4766 e_read_size = 0;
4767 }
4768 ASSERT_ALLOC( e_read_buffer, e_read_size );
4769 ASSERT_ALLOC( exported, exported_size );
4770
4771 PSA_ASSERT( psa_crypto_init( ) );
4772
4773 psa_set_key_usage_flags( &attributes, usage );
4774 psa_set_key_algorithm( &attributes, alg );
4775 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4776 e_arg->x, e_arg->len ) );
4777 psa_set_key_bits( &attributes, bits );
4778
4779 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004780 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004781 if( expected_status != PSA_SUCCESS )
4782 goto exit;
4783
4784 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004785 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004786 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4787 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4788 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4789 e_read_buffer, e_read_size,
4790 &e_read_length ) );
4791 if( is_default_public_exponent )
4792 TEST_EQUAL( e_read_length, 0 );
4793 else
4794 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4795
4796 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004797 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02004798 goto exit;
4799
4800 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02004801 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02004802 exported, exported_size,
4803 &exported_length ) );
4804 {
4805 uint8_t *p = exported;
4806 uint8_t *end = exported + exported_length;
4807 size_t len;
4808 /* RSAPublicKey ::= SEQUENCE {
4809 * modulus INTEGER, -- n
4810 * publicExponent INTEGER } -- e
4811 */
4812 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004813 MBEDTLS_ASN1_SEQUENCE |
4814 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01004815 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004816 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4817 MBEDTLS_ASN1_INTEGER ) );
4818 if( len >= 1 && p[0] == 0 )
4819 {
4820 ++p;
4821 --len;
4822 }
4823 if( e_arg->len == 0 )
4824 {
4825 TEST_EQUAL( len, 3 );
4826 TEST_EQUAL( p[0], 1 );
4827 TEST_EQUAL( p[1], 0 );
4828 TEST_EQUAL( p[2], 1 );
4829 }
4830 else
4831 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4832 }
4833
4834exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004835 /*
4836 * Key attributes may have been returned by psa_get_key_attributes() or
4837 * set by psa_set_key_domain_parameters() thus reset them as required.
4838 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004839 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004840
Ronald Cron5425a212020-08-04 14:58:35 +02004841 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004842 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004843 mbedtls_free( e_read_buffer );
4844 mbedtls_free( exported );
4845}
4846/* END_CASE */
4847
Darryl Greend49a4992018-06-18 17:27:26 +01004848/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004849void persistent_key_load_key_from_storage( data_t *data,
4850 int type_arg, int bits_arg,
4851 int usage_flags_arg, int alg_arg,
4852 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004853{
Ronald Cron71016a92020-08-28 19:01:50 +02004854 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004855 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02004856 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4857 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004858 psa_key_type_t type = type_arg;
4859 size_t bits = bits_arg;
4860 psa_key_usage_t usage_flags = usage_flags_arg;
4861 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004863 unsigned char *first_export = NULL;
4864 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004865 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004866 size_t first_exported_length;
4867 size_t second_exported_length;
4868
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004869 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4870 {
4871 ASSERT_ALLOC( first_export, export_size );
4872 ASSERT_ALLOC( second_export, export_size );
4873 }
Darryl Greend49a4992018-06-18 17:27:26 +01004874
Gilles Peskine8817f612018-12-18 00:18:46 +01004875 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004876
Gilles Peskinec87af662019-05-15 16:12:22 +02004877 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004878 psa_set_key_usage_flags( &attributes, usage_flags );
4879 psa_set_key_algorithm( &attributes, alg );
4880 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004881 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004882
Darryl Green0c6575a2018-11-07 16:05:30 +00004883 switch( generation_method )
4884 {
4885 case IMPORT_KEY:
4886 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004887 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004888 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004889 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004890
Darryl Green0c6575a2018-11-07 16:05:30 +00004891 case GENERATE_KEY:
4892 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004893 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004894 break;
4895
4896 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01004897#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004898 {
4899 /* Create base key */
4900 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4901 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4902 psa_set_key_usage_flags( &base_attributes,
4903 PSA_KEY_USAGE_DERIVE );
4904 psa_set_key_algorithm( &base_attributes, derive_alg );
4905 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004906 PSA_ASSERT( psa_import_key( &base_attributes,
4907 data->x, data->len,
4908 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004909 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004911 PSA_ASSERT( psa_key_derivation_input_key(
4912 &operation,
4913 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004914 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004916 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004917 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
4918 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004919 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004921 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02004922 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004923 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01004924#else
4925 TEST_ASSUME( ! "KDF not supported in this configuration" );
4926#endif
4927 break;
4928
4929 default:
4930 TEST_ASSERT( ! "generation_method not implemented in test" );
4931 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00004932 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004933 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004934
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004935 /* Export the key if permitted by the key policy. */
4936 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4937 {
Ronald Cron5425a212020-08-04 14:58:35 +02004938 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004939 first_export, export_size,
4940 &first_exported_length ) );
4941 if( generation_method == IMPORT_KEY )
4942 ASSERT_COMPARE( data->x, data->len,
4943 first_export, first_exported_length );
4944 }
Darryl Greend49a4992018-06-18 17:27:26 +01004945
4946 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02004947 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004948 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01004949 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004950
Darryl Greend49a4992018-06-18 17:27:26 +01004951 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02004952 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02004953 TEST_ASSERT( mbedtls_svc_key_id_equal(
4954 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004955 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4956 PSA_KEY_LIFETIME_PERSISTENT );
4957 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4958 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4959 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4960 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004961
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004962 /* Export the key again if permitted by the key policy. */
4963 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004964 {
Ronald Cron5425a212020-08-04 14:58:35 +02004965 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004966 second_export, export_size,
4967 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004968 ASSERT_COMPARE( first_export, first_exported_length,
4969 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004970 }
4971
4972 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004973 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004974 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004975
4976exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004977 /*
4978 * Key attributes may have been returned by psa_get_key_attributes()
4979 * thus reset them as required.
4980 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004981 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004982
Darryl Greend49a4992018-06-18 17:27:26 +01004983 mbedtls_free( first_export );
4984 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004985 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004986 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02004987 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004988 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01004989}
4990/* END_CASE */