blob: d7e7b06f16eb101c3d6dd14c519d985295202798 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskinef6279312021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100131int exercise_mac_setup( psa_key_type_t key_type,
132 const unsigned char *key_bytes,
133 size_t key_length,
134 psa_algorithm_t alg,
135 psa_mac_operation_t *operation,
136 psa_status_t *status )
137{
Ronald Cron5425a212020-08-04 14:58:35 +0200138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100140
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200142 psa_set_key_algorithm( &attributes, alg );
143 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200144 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Ronald Cron5425a212020-08-04 14:58:35 +0200146 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100147 /* Whether setup succeeded or failed, abort must succeed. */
148 PSA_ASSERT( psa_mac_abort( operation ) );
149 /* If setup failed, reproduce the failure, so that the caller can
150 * test the resulting state of the operation object. */
151 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152 {
Ronald Cron5425a212020-08-04 14:58:35 +0200153 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 }
155
Ronald Cron5425a212020-08-04 14:58:35 +0200156 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 return( 1 );
158
159exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200160 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100161 return( 0 );
162}
163
164int exercise_cipher_setup( psa_key_type_t key_type,
165 const unsigned char *key_bytes,
166 size_t key_length,
167 psa_algorithm_t alg,
168 psa_cipher_operation_t *operation,
169 psa_status_t *status )
170{
Ronald Cron5425a212020-08-04 14:58:35 +0200171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
175 psa_set_key_algorithm( &attributes, alg );
176 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200177 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Ronald Cron5425a212020-08-04 14:58:35 +0200179 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100180 /* Whether setup succeeded or failed, abort must succeed. */
181 PSA_ASSERT( psa_cipher_abort( operation ) );
182 /* If setup failed, reproduce the failure, so that the caller can
183 * test the resulting state of the operation object. */
184 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 {
Ronald Cron5425a212020-08-04 14:58:35 +0200186 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 }
189
Ronald Cron5425a212020-08-04 14:58:35 +0200190 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191 return( 1 );
192
193exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200194 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 return( 0 );
196}
197
Ronald Cron5425a212020-08-04 14:58:35 +0200198static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199{
200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200201 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200202 uint8_t buffer[1];
203 size_t length;
204 int ok = 0;
205
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
208 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
209 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200210 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000211 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200212 TEST_EQUAL(
213 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
214 TEST_EQUAL(
215 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200216 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200217 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
218 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
219 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
220 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
221
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200224 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000226 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200227
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200228 ok = 1;
229
230exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100231 /*
232 * Key attributes may have been returned by psa_get_key_attributes()
233 * thus reset them as required.
234 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200235 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 return( ok );
238}
239
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200240/* Assert that a key isn't reported as having a slot number. */
241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
242#define ASSERT_NO_SLOT_NUMBER( attributes ) \
243 do \
244 { \
245 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
246 TEST_EQUAL( psa_get_key_slot_number( \
247 attributes, \
248 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
249 PSA_ERROR_INVALID_ARGUMENT ); \
250 } \
251 while( 0 )
252#else /* MBEDTLS_PSA_CRYPTO_SE_C */
253#define ASSERT_NO_SLOT_NUMBER( attributes ) \
254 ( (void) 0 )
255#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
256
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100257/* An overapproximation of the amount of storage needed for a key of the
258 * given type and with the given content. The API doesn't make it easy
259 * to find a good value for the size. The current implementation doesn't
260 * care about the value anyway. */
261#define KEY_BITS_FROM_DATA( type, data ) \
262 ( data )->len
263
Darryl Green0c6575a2018-11-07 16:05:30 +0000264typedef enum {
265 IMPORT_KEY = 0,
266 GENERATE_KEY = 1,
267 DERIVE_KEY = 2
268} generate_method;
269
Gilles Peskinee59236f2018-01-27 23:32:46 +0100270/* END_HEADER */
271
272/* BEGIN_DEPENDENCIES
273 * depends_on:MBEDTLS_PSA_CRYPTO_C
274 * END_DEPENDENCIES
275 */
276
277/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200278void static_checks( )
279{
280 size_t max_truncated_mac_size =
281 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
282
283 /* Check that the length for a truncated MAC always fits in the algorithm
284 * encoding. The shifted mask is the maximum truncated value. The
285 * untruncated algorithm may be one byte larger. */
286 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100287
288#if defined(MBEDTLS_TEST_DEPRECATED)
289 /* Check deprecated constants. */
290 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
291 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
292 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
293 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
294 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
295 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
296 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
297 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100298
Paul Elliott8ff510a2020-06-02 17:19:28 +0100299 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
300 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
301 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
302 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
303 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
304 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
321 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
323 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
324 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
325 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
326 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
327 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
328 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
329
330 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
331 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
333 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
334 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
335 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
336 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
337 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100338
Paul Elliott75e27032020-06-03 15:17:39 +0100339 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
340 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
341 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
342 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
343 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
344
345 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
346 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100347#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200348}
349/* END_CASE */
350
351/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200352void import_with_policy( int type_arg,
353 int usage_arg, int alg_arg,
354 int expected_status_arg )
355{
356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
357 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200359 psa_key_type_t type = type_arg;
360 psa_key_usage_t usage = usage_arg;
361 psa_algorithm_t alg = alg_arg;
362 psa_status_t expected_status = expected_status_arg;
363 const uint8_t key_material[16] = {0};
364 psa_status_t status;
365
366 PSA_ASSERT( psa_crypto_init( ) );
367
368 psa_set_key_type( &attributes, type );
369 psa_set_key_usage_flags( &attributes, usage );
370 psa_set_key_algorithm( &attributes, alg );
371
372 status = psa_import_key( &attributes,
373 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200374 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200375 TEST_EQUAL( status, expected_status );
376 if( status != PSA_SUCCESS )
377 goto exit;
378
Ronald Cron5425a212020-08-04 14:58:35 +0200379 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200380 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
381 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
382 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200383 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200384
Ronald Cron5425a212020-08-04 14:58:35 +0200385 PSA_ASSERT( psa_destroy_key( key ) );
386 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200387
388exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100389 /*
390 * Key attributes may have been returned by psa_get_key_attributes()
391 * thus reset them as required.
392 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200393 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100394
395 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200396 PSA_DONE( );
397}
398/* END_CASE */
399
400/* BEGIN_CASE */
401void import_with_data( data_t *data, int type_arg,
402 int attr_bits_arg,
403 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200404{
405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
406 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200408 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200409 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200410 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100411 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412
Gilles Peskine8817f612018-12-18 00:18:46 +0100413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100414
Gilles Peskine4747d192019-04-17 15:05:45 +0200415 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200416 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200417
Ronald Cron5425a212020-08-04 14:58:35 +0200418 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100419 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200420 if( status != PSA_SUCCESS )
421 goto exit;
422
Ronald Cron5425a212020-08-04 14:58:35 +0200423 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200424 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200425 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200426 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200427 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200428
Ronald Cron5425a212020-08-04 14:58:35 +0200429 PSA_ASSERT( psa_destroy_key( key ) );
430 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100431
432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100433 /*
434 * Key attributes may have been returned by psa_get_key_attributes()
435 * thus reset them as required.
436 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200437 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100438
439 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200440 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100441}
442/* END_CASE */
443
444/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200445void import_large_key( int type_arg, int byte_size_arg,
446 int expected_status_arg )
447{
448 psa_key_type_t type = type_arg;
449 size_t byte_size = byte_size_arg;
450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
451 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200452 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200453 psa_status_t status;
454 uint8_t *buffer = NULL;
455 size_t buffer_size = byte_size + 1;
456 size_t n;
457
Steven Cooreman69967ce2021-01-18 18:01:08 +0100458 /* Skip the test case if the target running the test cannot
459 * accomodate large keys due to heap size constraints */
460 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200461 memset( buffer, 'K', byte_size );
462
463 PSA_ASSERT( psa_crypto_init( ) );
464
465 /* Try importing the key */
466 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
467 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200468 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100469 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200470 TEST_EQUAL( status, expected_status );
471
472 if( status == PSA_SUCCESS )
473 {
Ronald Cron5425a212020-08-04 14:58:35 +0200474 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200475 TEST_EQUAL( psa_get_key_type( &attributes ), type );
476 TEST_EQUAL( psa_get_key_bits( &attributes ),
477 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200478 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200479 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200480 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200481 for( n = 0; n < byte_size; n++ )
482 TEST_EQUAL( buffer[n], 'K' );
483 for( n = byte_size; n < buffer_size; n++ )
484 TEST_EQUAL( buffer[n], 0 );
485 }
486
487exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100488 /*
489 * Key attributes may have been returned by psa_get_key_attributes()
490 * thus reset them as required.
491 */
492 psa_reset_key_attributes( &attributes );
493
Ronald Cron5425a212020-08-04 14:58:35 +0200494 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200495 PSA_DONE( );
496 mbedtls_free( buffer );
497}
498/* END_CASE */
499
500/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200501void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
502{
Ronald Cron5425a212020-08-04 14:58:35 +0200503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200504 size_t bits = bits_arg;
505 psa_status_t expected_status = expected_status_arg;
506 psa_status_t status;
507 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200508 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200509 size_t buffer_size = /* Slight overapproximations */
510 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200511 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200512 unsigned char *p;
513 int ret;
514 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200516
Gilles Peskine8817f612018-12-18 00:18:46 +0100517 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200518 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200519
520 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
521 bits, keypair ) ) >= 0 );
522 length = ret;
523
524 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200525 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200526 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100527 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200528
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200529 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200530 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200531
532exit:
533 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200534 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200535}
536/* END_CASE */
537
538/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300539void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300540 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200541 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 int expected_bits,
543 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200544 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 int canonical_input )
546{
Ronald Cron5425a212020-08-04 14:58:35 +0200547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200549 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200550 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 unsigned char *exported = NULL;
553 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100555 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200558 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559
Moran Pekercb088e72018-07-17 17:36:59 +0300560 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200561 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200563 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100564 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565
Gilles Peskine4747d192019-04-17 15:05:45 +0200566 psa_set_key_usage_flags( &attributes, usage_arg );
567 psa_set_key_algorithm( &attributes, alg );
568 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700569
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572
573 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200574 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200575 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
576 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200577 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578
579 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200580 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100581 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100582
583 /* The exported length must be set by psa_export_key() to a value between 0
584 * and export_size. On errors, the exported length must be 0. */
585 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
586 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
587 TEST_ASSERT( exported_length <= export_size );
588
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200589 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200590 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200592 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100593 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200595 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596
Gilles Peskineea38a922021-02-13 00:05:16 +0100597 /* Run sanity checks on the exported key. For non-canonical inputs,
598 * this validates the canonical representations. For canonical inputs,
599 * this doesn't directly validate the implementation, but it still helps
600 * by cross-validating the test data with the sanity check code. */
601 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200602 goto exit;
603
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200605 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606 else
607 {
Ronald Cron5425a212020-08-04 14:58:35 +0200608 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200609 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200610 &key2 ) );
611 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100612 reexported,
613 export_size,
614 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200615 ASSERT_COMPARE( exported, exported_length,
616 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200617 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100619 TEST_ASSERT( exported_length <=
620 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
621 psa_get_key_bits( &got_attributes ) ) );
622 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623
624destroy:
625 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200626 PSA_ASSERT( psa_destroy_key( key ) );
627 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100628
629exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100630 /*
631 * Key attributes may have been returned by psa_get_key_attributes()
632 * thus reset them as required.
633 */
634 psa_reset_key_attributes( &got_attributes );
635
itayzafrir3e02b3b2018-06-12 17:06:52 +0300636 mbedtls_free( exported );
637 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200638 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100639}
640/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100641
Moran Pekerf709f4a2018-06-06 17:26:04 +0300642/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200644 int type_arg,
645 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100646 int export_size_delta,
647 int expected_export_status_arg,
648 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300649{
Ronald Cron5425a212020-08-04 14:58:35 +0200650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300651 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200652 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200653 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300654 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100656 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100657 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300659
Gilles Peskine8817f612018-12-18 00:18:46 +0100660 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300661
Gilles Peskine4747d192019-04-17 15:05:45 +0200662 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
663 psa_set_key_algorithm( &attributes, alg );
664 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300665
666 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200667 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300668
Gilles Peskine49c25912018-10-29 15:15:31 +0100669 /* Export the public key */
670 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200671 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200672 exported, export_size,
673 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100674 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100675 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100676 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200677 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100678 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200679 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200680 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100681 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100682 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100683 TEST_ASSERT( expected_public_key->len <=
684 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
685 TEST_ASSERT( expected_public_key->len <=
686 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100687 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
688 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100689 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300690
691exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100692 /*
693 * Key attributes may have been returned by psa_get_key_attributes()
694 * thus reset them as required.
695 */
696 psa_reset_key_attributes( &attributes );
697
itayzafrir3e02b3b2018-06-12 17:06:52 +0300698 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200699 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200700 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300701}
702/* END_CASE */
703
Gilles Peskine20035e32018-02-03 22:44:14 +0100704/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200705void import_and_exercise_key( data_t *data,
706 int type_arg,
707 int bits_arg,
708 int alg_arg )
709{
Ronald Cron5425a212020-08-04 14:58:35 +0200710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200711 psa_key_type_t type = type_arg;
712 size_t bits = bits_arg;
713 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100714 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200715 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200716 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200717
Gilles Peskine8817f612018-12-18 00:18:46 +0100718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200719
Gilles Peskine4747d192019-04-17 15:05:45 +0200720 psa_set_key_usage_flags( &attributes, usage );
721 psa_set_key_algorithm( &attributes, alg );
722 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200723
724 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200725 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200726
727 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200728 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200729 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
730 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200731
732 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100733 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200734 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200735
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_destroy_key( key ) );
737 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200738
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200739exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100740 /*
741 * Key attributes may have been returned by psa_get_key_attributes()
742 * thus reset them as required.
743 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200744 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100745
746 psa_reset_key_attributes( &attributes );
747 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200748 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200749}
750/* END_CASE */
751
752/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100753void effective_key_attributes( int type_arg, int expected_type_arg,
754 int bits_arg, int expected_bits_arg,
755 int usage_arg, int expected_usage_arg,
756 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200757{
Ronald Cron5425a212020-08-04 14:58:35 +0200758 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100759 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100760 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100761 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100762 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200763 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100764 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200765 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100766 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200767 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200768
Gilles Peskine8817f612018-12-18 00:18:46 +0100769 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200770
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200771 psa_set_key_usage_flags( &attributes, usage );
772 psa_set_key_algorithm( &attributes, alg );
773 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100774 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200775
Ronald Cron5425a212020-08-04 14:58:35 +0200776 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100777 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200778
Ronald Cron5425a212020-08-04 14:58:35 +0200779 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100780 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
781 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
782 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
783 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200784
785exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100786 /*
787 * Key attributes may have been returned by psa_get_key_attributes()
788 * thus reset them as required.
789 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200790 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100791
792 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200793 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200794}
795/* END_CASE */
796
797/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100798void check_key_policy( int type_arg, int bits_arg,
799 int usage_arg, int alg_arg )
800{
801 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
802 usage_arg, usage_arg, alg_arg, alg_arg );
803 goto exit;
804}
805/* END_CASE */
806
807/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200808void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000809{
810 /* Test each valid way of initializing the object, except for `= {0}`, as
811 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
812 * though it's OK by the C standard. We could test for this, but we'd need
813 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200814 psa_key_attributes_t func = psa_key_attributes_init( );
815 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
816 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000817
818 memset( &zero, 0, sizeof( zero ) );
819
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200820 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
821 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
822 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000823
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200824 TEST_EQUAL( psa_get_key_type( &func ), 0 );
825 TEST_EQUAL( psa_get_key_type( &init ), 0 );
826 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
827
828 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
829 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
830 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
831
832 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
833 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
834 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
835
836 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
837 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
838 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000839}
840/* END_CASE */
841
842/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200843void mac_key_policy( int policy_usage,
844 int policy_alg,
845 int key_type,
846 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100847 int exercise_alg,
848 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200849{
Ronald Cron5425a212020-08-04 14:58:35 +0200850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200851 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000852 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200853 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100854 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200855 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200856
Gilles Peskine8817f612018-12-18 00:18:46 +0100857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200858
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200859 psa_set_key_usage_flags( &attributes, policy_usage );
860 psa_set_key_algorithm( &attributes, policy_alg );
861 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200862
Gilles Peskine049c7532019-05-15 20:22:09 +0200863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200864 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200865
Ronald Cron5425a212020-08-04 14:58:35 +0200866 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100867 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100868 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100869 else
870 TEST_EQUAL( status, expected_status );
871
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200872 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200873
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200874 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200875 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100876 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100877 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100878 else
879 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200880
881exit:
882 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200883 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200884 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200885}
886/* END_CASE */
887
888/* BEGIN_CASE */
889void cipher_key_policy( int policy_usage,
890 int policy_alg,
891 int key_type,
892 data_t *key_data,
893 int exercise_alg )
894{
Ronald Cron5425a212020-08-04 14:58:35 +0200895 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000897 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200898 psa_status_t status;
899
Gilles Peskine8817f612018-12-18 00:18:46 +0100900 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200901
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200902 psa_set_key_usage_flags( &attributes, policy_usage );
903 psa_set_key_algorithm( &attributes, policy_alg );
904 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905
Gilles Peskine049c7532019-05-15 20:22:09 +0200906 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200907 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200908
Ronald Cron5425a212020-08-04 14:58:35 +0200909 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 if( policy_alg == exercise_alg &&
911 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100912 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200913 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100914 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915 psa_cipher_abort( &operation );
916
Ronald Cron5425a212020-08-04 14:58:35 +0200917 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918 if( policy_alg == exercise_alg &&
919 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100920 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200921 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100922 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200923
924exit:
925 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200926 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200927 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200928}
929/* END_CASE */
930
931/* BEGIN_CASE */
932void aead_key_policy( int policy_usage,
933 int policy_alg,
934 int key_type,
935 data_t *key_data,
936 int nonce_length_arg,
937 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100938 int exercise_alg,
939 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940{
Ronald Cron5425a212020-08-04 14:58:35 +0200941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100944 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200945 unsigned char nonce[16] = {0};
946 size_t nonce_length = nonce_length_arg;
947 unsigned char tag[16];
948 size_t tag_length = tag_length_arg;
949 size_t output_length;
950
951 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
952 TEST_ASSERT( tag_length <= sizeof( tag ) );
953
Gilles Peskine8817f612018-12-18 00:18:46 +0100954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200956 psa_set_key_usage_flags( &attributes, policy_usage );
957 psa_set_key_algorithm( &attributes, policy_alg );
958 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959
Gilles Peskine049c7532019-05-15 20:22:09 +0200960 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200961 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200962
Ronald Cron5425a212020-08-04 14:58:35 +0200963 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964 nonce, nonce_length,
965 NULL, 0,
966 NULL, 0,
967 tag, tag_length,
968 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100969 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
970 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200971 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100972 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973
974 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200975 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200976 nonce, nonce_length,
977 NULL, 0,
978 tag, tag_length,
979 NULL, 0,
980 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100981 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
982 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
983 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100984 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200985 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100986 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200987
988exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200989 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200990 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200991}
992/* END_CASE */
993
994/* BEGIN_CASE */
995void asymmetric_encryption_key_policy( int policy_usage,
996 int policy_alg,
997 int key_type,
998 data_t *key_data,
999 int exercise_alg )
1000{
Ronald Cron5425a212020-08-04 14:58:35 +02001001 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001002 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001003 psa_status_t status;
1004 size_t key_bits;
1005 size_t buffer_length;
1006 unsigned char *buffer = NULL;
1007 size_t output_length;
1008
Gilles Peskine8817f612018-12-18 00:18:46 +01001009 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001011 psa_set_key_usage_flags( &attributes, policy_usage );
1012 psa_set_key_algorithm( &attributes, policy_alg );
1013 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014
Gilles Peskine049c7532019-05-15 20:22:09 +02001015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001016 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001017
Ronald Cron5425a212020-08-04 14:58:35 +02001018 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001019 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001020 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1021 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001022 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001023
Ronald Cron5425a212020-08-04 14:58:35 +02001024 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025 NULL, 0,
1026 NULL, 0,
1027 buffer, buffer_length,
1028 &output_length );
1029 if( policy_alg == exercise_alg &&
1030 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001031 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001034
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001035 if( buffer_length != 0 )
1036 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001037 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001038 buffer, buffer_length,
1039 NULL, 0,
1040 buffer, buffer_length,
1041 &output_length );
1042 if( policy_alg == exercise_alg &&
1043 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001044 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001045 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001046 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001047
1048exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001049 /*
1050 * Key attributes may have been returned by psa_get_key_attributes()
1051 * thus reset them as required.
1052 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001053 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001054
1055 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001056 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001057 mbedtls_free( buffer );
1058}
1059/* END_CASE */
1060
1061/* BEGIN_CASE */
1062void asymmetric_signature_key_policy( int policy_usage,
1063 int policy_alg,
1064 int key_type,
1065 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001066 int exercise_alg,
1067 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068{
Ronald Cron5425a212020-08-04 14:58:35 +02001069 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001071 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001072 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1073 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1074 * compatible with the policy and `payload_length_arg` is supposed to be
1075 * a valid input length to sign. If `payload_length_arg <= 0`,
1076 * `exercise_alg` is supposed to be forbidden by the policy. */
1077 int compatible_alg = payload_length_arg > 0;
1078 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001079 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001080 size_t signature_length;
1081
Gilles Peskine8817f612018-12-18 00:18:46 +01001082 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001083
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001084 psa_set_key_usage_flags( &attributes, policy_usage );
1085 psa_set_key_algorithm( &attributes, policy_alg );
1086 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087
Gilles Peskine049c7532019-05-15 20:22:09 +02001088 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001089 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001090
Ronald Cron5425a212020-08-04 14:58:35 +02001091 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001092 payload, payload_length,
1093 signature, sizeof( signature ),
1094 &signature_length );
1095 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001096 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001097 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001098 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001099
1100 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001101 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001102 payload, payload_length,
1103 signature, sizeof( signature ) );
1104 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001105 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001106 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001107 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001108
1109exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001110 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001111 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001112}
1113/* END_CASE */
1114
Janos Follathba3fab92019-06-11 14:50:16 +01001115/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001116void derive_key_policy( int policy_usage,
1117 int policy_alg,
1118 int key_type,
1119 data_t *key_data,
1120 int exercise_alg )
1121{
Ronald Cron5425a212020-08-04 14:58:35 +02001122 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001124 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001125 psa_status_t status;
1126
Gilles Peskine8817f612018-12-18 00:18:46 +01001127 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001128
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001129 psa_set_key_usage_flags( &attributes, policy_usage );
1130 psa_set_key_algorithm( &attributes, policy_alg );
1131 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001132
Gilles Peskine049c7532019-05-15 20:22:09 +02001133 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001134 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001135
Janos Follathba3fab92019-06-11 14:50:16 +01001136 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1137
1138 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1139 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001140 {
Janos Follathba3fab92019-06-11 14:50:16 +01001141 PSA_ASSERT( psa_key_derivation_input_bytes(
1142 &operation,
1143 PSA_KEY_DERIVATION_INPUT_SEED,
1144 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001145 }
Janos Follathba3fab92019-06-11 14:50:16 +01001146
1147 status = psa_key_derivation_input_key( &operation,
1148 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001149 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001150
Gilles Peskineea0fb492018-07-12 17:17:20 +02001151 if( policy_alg == exercise_alg &&
1152 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001153 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001154 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001155 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001156
1157exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001158 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001159 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001160 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001161}
1162/* END_CASE */
1163
1164/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001165void agreement_key_policy( int policy_usage,
1166 int policy_alg,
1167 int key_type_arg,
1168 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001169 int exercise_alg,
1170 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001171{
Ronald Cron5425a212020-08-04 14:58:35 +02001172 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001174 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001175 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001176 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001177 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001178
Gilles Peskine8817f612018-12-18 00:18:46 +01001179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001180
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001181 psa_set_key_usage_flags( &attributes, policy_usage );
1182 psa_set_key_algorithm( &attributes, policy_alg );
1183 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001184
Gilles Peskine049c7532019-05-15 20:22:09 +02001185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001186 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001187
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001188 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001189 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001190
Steven Cooremance48e852020-10-05 16:02:45 +02001191 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001192
1193exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001194 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001195 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001196 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001197}
1198/* END_CASE */
1199
1200/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001201void key_policy_alg2( int key_type_arg, data_t *key_data,
1202 int usage_arg, int alg_arg, int alg2_arg )
1203{
Ronald Cron5425a212020-08-04 14:58:35 +02001204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001205 psa_key_type_t key_type = key_type_arg;
1206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1207 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1208 psa_key_usage_t usage = usage_arg;
1209 psa_algorithm_t alg = alg_arg;
1210 psa_algorithm_t alg2 = alg2_arg;
1211
1212 PSA_ASSERT( psa_crypto_init( ) );
1213
1214 psa_set_key_usage_flags( &attributes, usage );
1215 psa_set_key_algorithm( &attributes, alg );
1216 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1217 psa_set_key_type( &attributes, key_type );
1218 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001219 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001220
Ronald Cron5425a212020-08-04 14:58:35 +02001221 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001222 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1223 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1224 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1225
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001226 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001227 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001228 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001229 goto exit;
1230
1231exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001232 /*
1233 * Key attributes may have been returned by psa_get_key_attributes()
1234 * thus reset them as required.
1235 */
1236 psa_reset_key_attributes( &got_attributes );
1237
Ronald Cron5425a212020-08-04 14:58:35 +02001238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001239 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001244void raw_agreement_key_policy( int policy_usage,
1245 int policy_alg,
1246 int key_type_arg,
1247 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001248 int exercise_alg,
1249 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001250{
Ronald Cron5425a212020-08-04 14:58:35 +02001251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001253 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001254 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001255 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001256 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001257
1258 PSA_ASSERT( psa_crypto_init( ) );
1259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001260 psa_set_key_usage_flags( &attributes, policy_usage );
1261 psa_set_key_algorithm( &attributes, policy_alg );
1262 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001263
Gilles Peskine049c7532019-05-15 20:22:09 +02001264 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001265 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001266
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001267 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001268
Steven Cooremance48e852020-10-05 16:02:45 +02001269 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001270
1271exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001272 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001273 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001274 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001275}
1276/* END_CASE */
1277
1278/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001279void copy_success( int source_usage_arg,
1280 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001281 int type_arg, data_t *material,
1282 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001283 int target_usage_arg,
1284 int target_alg_arg, int target_alg2_arg,
1285 int expected_usage_arg,
1286 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001287{
Gilles Peskineca25db92019-04-19 11:43:08 +02001288 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1289 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001290 psa_key_usage_t expected_usage = expected_usage_arg;
1291 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001292 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001293 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1294 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001295 uint8_t *export_buffer = NULL;
1296
Gilles Peskine57ab7212019-01-28 13:03:09 +01001297 PSA_ASSERT( psa_crypto_init( ) );
1298
Gilles Peskineca25db92019-04-19 11:43:08 +02001299 /* Prepare the source key. */
1300 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1301 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001302 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001303 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001304 PSA_ASSERT( psa_import_key( &source_attributes,
1305 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001306 &source_key ) );
1307 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001308
Gilles Peskineca25db92019-04-19 11:43:08 +02001309 /* Prepare the target attributes. */
1310 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001311 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001312 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001313 /* Set volatile lifetime to reset the key identifier to 0. */
1314 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1315 }
1316
Gilles Peskineca25db92019-04-19 11:43:08 +02001317 if( target_usage_arg != -1 )
1318 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1319 if( target_alg_arg != -1 )
1320 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001321 if( target_alg2_arg != -1 )
1322 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001323
1324 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001325 PSA_ASSERT( psa_copy_key( source_key,
1326 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001327
1328 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001329 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001330
1331 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001332 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001333 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1334 psa_get_key_type( &target_attributes ) );
1335 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1336 psa_get_key_bits( &target_attributes ) );
1337 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1338 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001339 TEST_EQUAL( expected_alg2,
1340 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001341 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1342 {
1343 size_t length;
1344 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001345 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001346 material->len, &length ) );
1347 ASSERT_COMPARE( material->x, material->len,
1348 export_buffer, length );
1349 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001350
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001351 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001352 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001353 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001354 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001355
Ronald Cron5425a212020-08-04 14:58:35 +02001356 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001357
1358exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001359 /*
1360 * Source and target key attributes may have been returned by
1361 * psa_get_key_attributes() thus reset them as required.
1362 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001363 psa_reset_key_attributes( &source_attributes );
1364 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001365
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001366 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001367 mbedtls_free( export_buffer );
1368}
1369/* END_CASE */
1370
1371/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001372void copy_fail( int source_usage_arg,
1373 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001374 int type_arg, data_t *material,
1375 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001376 int target_usage_arg,
1377 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001378 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001379 int expected_status_arg )
1380{
1381 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1382 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001383 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1384 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001385 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001386
1387 PSA_ASSERT( psa_crypto_init( ) );
1388
1389 /* Prepare the source key. */
1390 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1391 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001392 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001393 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001394 PSA_ASSERT( psa_import_key( &source_attributes,
1395 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001396 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001397
1398 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001399 psa_set_key_id( &target_attributes, key_id );
1400 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001401 psa_set_key_type( &target_attributes, target_type_arg );
1402 psa_set_key_bits( &target_attributes, target_bits_arg );
1403 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1404 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001405 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001406
1407 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001408 TEST_EQUAL( psa_copy_key( source_key,
1409 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001410 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001411
Ronald Cron5425a212020-08-04 14:58:35 +02001412 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001413
Gilles Peskine4a644642019-05-03 17:14:08 +02001414exit:
1415 psa_reset_key_attributes( &source_attributes );
1416 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001417 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001418}
1419/* END_CASE */
1420
1421/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001422void hash_operation_init( )
1423{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001424 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001425 /* Test each valid way of initializing the object, except for `= {0}`, as
1426 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1427 * though it's OK by the C standard. We could test for this, but we'd need
1428 * to supress the Clang warning for the test. */
1429 psa_hash_operation_t func = psa_hash_operation_init( );
1430 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1431 psa_hash_operation_t zero;
1432
1433 memset( &zero, 0, sizeof( zero ) );
1434
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001435 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001436 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1437 PSA_ERROR_BAD_STATE );
1438 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1439 PSA_ERROR_BAD_STATE );
1440 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1441 PSA_ERROR_BAD_STATE );
1442
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001443 /* A default hash operation should be abortable without error. */
1444 PSA_ASSERT( psa_hash_abort( &func ) );
1445 PSA_ASSERT( psa_hash_abort( &init ) );
1446 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001447}
1448/* END_CASE */
1449
1450/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001451void hash_setup( int alg_arg,
1452 int expected_status_arg )
1453{
1454 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001455 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001456 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001457 psa_status_t status;
1458
Gilles Peskine8817f612018-12-18 00:18:46 +01001459 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001460
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001461 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001462 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001463
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001464 /* Whether setup succeeded or failed, abort must succeed. */
1465 PSA_ASSERT( psa_hash_abort( &operation ) );
1466
1467 /* If setup failed, reproduce the failure, so as to
1468 * test the resulting state of the operation object. */
1469 if( status != PSA_SUCCESS )
1470 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1471
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001472 /* Now the operation object should be reusable. */
1473#if defined(KNOWN_SUPPORTED_HASH_ALG)
1474 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1475 PSA_ASSERT( psa_hash_abort( &operation ) );
1476#endif
1477
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001478exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001479 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001480}
1481/* END_CASE */
1482
1483/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001484void hash_compute_fail( int alg_arg, data_t *input,
1485 int output_size_arg, int expected_status_arg )
1486{
1487 psa_algorithm_t alg = alg_arg;
1488 uint8_t *output = NULL;
1489 size_t output_size = output_size_arg;
1490 size_t output_length = INVALID_EXPORT_LENGTH;
1491 psa_status_t expected_status = expected_status_arg;
1492 psa_status_t status;
1493
1494 ASSERT_ALLOC( output, output_size );
1495
1496 PSA_ASSERT( psa_crypto_init( ) );
1497
1498 status = psa_hash_compute( alg, input->x, input->len,
1499 output, output_size, &output_length );
1500 TEST_EQUAL( status, expected_status );
1501 TEST_ASSERT( output_length <= output_size );
1502
1503exit:
1504 mbedtls_free( output );
1505 PSA_DONE( );
1506}
1507/* END_CASE */
1508
1509/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001510void hash_compare_fail( int alg_arg, data_t *input,
1511 data_t *reference_hash,
1512 int expected_status_arg )
1513{
1514 psa_algorithm_t alg = alg_arg;
1515 psa_status_t expected_status = expected_status_arg;
1516 psa_status_t status;
1517
1518 PSA_ASSERT( psa_crypto_init( ) );
1519
1520 status = psa_hash_compare( alg, input->x, input->len,
1521 reference_hash->x, reference_hash->len );
1522 TEST_EQUAL( status, expected_status );
1523
1524exit:
1525 PSA_DONE( );
1526}
1527/* END_CASE */
1528
1529/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001530void hash_compute_compare( int alg_arg, data_t *input,
1531 data_t *expected_output )
1532{
1533 psa_algorithm_t alg = alg_arg;
1534 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1535 size_t output_length = INVALID_EXPORT_LENGTH;
1536 size_t i;
1537
1538 PSA_ASSERT( psa_crypto_init( ) );
1539
1540 /* Compute with tight buffer */
1541 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001542 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001543 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001544 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001545 ASSERT_COMPARE( output, output_length,
1546 expected_output->x, expected_output->len );
1547
1548 /* Compute with larger buffer */
1549 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1550 output, sizeof( output ),
1551 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001552 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001553 ASSERT_COMPARE( output, output_length,
1554 expected_output->x, expected_output->len );
1555
1556 /* Compare with correct hash */
1557 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1558 output, output_length ) );
1559
1560 /* Compare with trailing garbage */
1561 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1562 output, output_length + 1 ),
1563 PSA_ERROR_INVALID_SIGNATURE );
1564
1565 /* Compare with truncated hash */
1566 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1567 output, output_length - 1 ),
1568 PSA_ERROR_INVALID_SIGNATURE );
1569
1570 /* Compare with corrupted value */
1571 for( i = 0; i < output_length; i++ )
1572 {
Chris Jones9634bb12021-01-20 15:56:42 +00001573 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001574 output[i] ^= 1;
1575 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1576 output, output_length ),
1577 PSA_ERROR_INVALID_SIGNATURE );
1578 output[i] ^= 1;
1579 }
1580
1581exit:
1582 PSA_DONE( );
1583}
1584/* END_CASE */
1585
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001586/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001587void hash_bad_order( )
1588{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001589 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001590 unsigned char input[] = "";
1591 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001592 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001593 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1594 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1595 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001596 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001597 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001598 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001599
Gilles Peskine8817f612018-12-18 00:18:46 +01001600 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001601
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001602 /* Call setup twice in a row. */
1603 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1604 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1605 PSA_ERROR_BAD_STATE );
1606 PSA_ASSERT( psa_hash_abort( &operation ) );
1607
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001608 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001609 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001610 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001611 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001612
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001613 /* Call update after finish. */
1614 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1615 PSA_ASSERT( psa_hash_finish( &operation,
1616 hash, sizeof( hash ), &hash_len ) );
1617 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001618 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001619 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001620
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001621 /* Call verify without calling setup beforehand. */
1622 TEST_EQUAL( psa_hash_verify( &operation,
1623 valid_hash, sizeof( valid_hash ) ),
1624 PSA_ERROR_BAD_STATE );
1625 PSA_ASSERT( psa_hash_abort( &operation ) );
1626
1627 /* Call verify after finish. */
1628 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1629 PSA_ASSERT( psa_hash_finish( &operation,
1630 hash, sizeof( hash ), &hash_len ) );
1631 TEST_EQUAL( psa_hash_verify( &operation,
1632 valid_hash, sizeof( valid_hash ) ),
1633 PSA_ERROR_BAD_STATE );
1634 PSA_ASSERT( psa_hash_abort( &operation ) );
1635
1636 /* Call verify twice in a row. */
1637 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1638 PSA_ASSERT( psa_hash_verify( &operation,
1639 valid_hash, sizeof( valid_hash ) ) );
1640 TEST_EQUAL( psa_hash_verify( &operation,
1641 valid_hash, sizeof( valid_hash ) ),
1642 PSA_ERROR_BAD_STATE );
1643 PSA_ASSERT( psa_hash_abort( &operation ) );
1644
1645 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001646 TEST_EQUAL( psa_hash_finish( &operation,
1647 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001648 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001649 PSA_ASSERT( psa_hash_abort( &operation ) );
1650
1651 /* Call finish twice in a row. */
1652 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1653 PSA_ASSERT( psa_hash_finish( &operation,
1654 hash, sizeof( hash ), &hash_len ) );
1655 TEST_EQUAL( psa_hash_finish( &operation,
1656 hash, sizeof( hash ), &hash_len ),
1657 PSA_ERROR_BAD_STATE );
1658 PSA_ASSERT( psa_hash_abort( &operation ) );
1659
1660 /* Call finish after calling verify. */
1661 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1662 PSA_ASSERT( psa_hash_verify( &operation,
1663 valid_hash, sizeof( valid_hash ) ) );
1664 TEST_EQUAL( psa_hash_finish( &operation,
1665 hash, sizeof( hash ), &hash_len ),
1666 PSA_ERROR_BAD_STATE );
1667 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001668
1669exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001670 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001671}
1672/* END_CASE */
1673
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001674/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001675void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001676{
1677 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001678 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1679 * appended to it */
1680 unsigned char hash[] = {
1681 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1682 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1683 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001684 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001685 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001686
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001688
itayzafrir27e69452018-11-01 14:26:34 +02001689 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001690 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001691 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001692 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001693
itayzafrir27e69452018-11-01 14:26:34 +02001694 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001695 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001696 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001697 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001698
itayzafrir27e69452018-11-01 14:26:34 +02001699 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001700 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001701 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001703
itayzafrirec93d302018-10-18 18:01:10 +03001704exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001705 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001706}
1707/* END_CASE */
1708
Ronald Cronee414c72021-03-18 18:50:08 +01001709/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001710void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001711{
1712 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001713 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001714 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001715 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001716 size_t hash_len;
1717
Gilles Peskine8817f612018-12-18 00:18:46 +01001718 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001719
itayzafrir58028322018-10-25 10:22:01 +03001720 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001721 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001722 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001723 hash, expected_size - 1, &hash_len ),
1724 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001725
1726exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001727 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001728}
1729/* END_CASE */
1730
Ronald Cronee414c72021-03-18 18:50:08 +01001731/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001732void hash_clone_source_state( )
1733{
1734 psa_algorithm_t alg = PSA_ALG_SHA_256;
1735 unsigned char hash[PSA_HASH_MAX_SIZE];
1736 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1737 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1738 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1739 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1740 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1741 size_t hash_len;
1742
1743 PSA_ASSERT( psa_crypto_init( ) );
1744 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1745
1746 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1747 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1748 PSA_ASSERT( psa_hash_finish( &op_finished,
1749 hash, sizeof( hash ), &hash_len ) );
1750 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1751 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1752
1753 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1754 PSA_ERROR_BAD_STATE );
1755
1756 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1757 PSA_ASSERT( psa_hash_finish( &op_init,
1758 hash, sizeof( hash ), &hash_len ) );
1759 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1760 PSA_ASSERT( psa_hash_finish( &op_finished,
1761 hash, sizeof( hash ), &hash_len ) );
1762 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1763 PSA_ASSERT( psa_hash_finish( &op_aborted,
1764 hash, sizeof( hash ), &hash_len ) );
1765
1766exit:
1767 psa_hash_abort( &op_source );
1768 psa_hash_abort( &op_init );
1769 psa_hash_abort( &op_setup );
1770 psa_hash_abort( &op_finished );
1771 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001772 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001773}
1774/* END_CASE */
1775
Ronald Cronee414c72021-03-18 18:50:08 +01001776/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001777void hash_clone_target_state( )
1778{
1779 psa_algorithm_t alg = PSA_ALG_SHA_256;
1780 unsigned char hash[PSA_HASH_MAX_SIZE];
1781 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1782 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1783 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1784 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1785 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1786 size_t hash_len;
1787
1788 PSA_ASSERT( psa_crypto_init( ) );
1789
1790 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1791 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1792 PSA_ASSERT( psa_hash_finish( &op_finished,
1793 hash, sizeof( hash ), &hash_len ) );
1794 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1795 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1796
1797 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1798 PSA_ASSERT( psa_hash_finish( &op_target,
1799 hash, sizeof( hash ), &hash_len ) );
1800
1801 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1802 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1803 PSA_ERROR_BAD_STATE );
1804 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1805 PSA_ERROR_BAD_STATE );
1806
1807exit:
1808 psa_hash_abort( &op_target );
1809 psa_hash_abort( &op_init );
1810 psa_hash_abort( &op_setup );
1811 psa_hash_abort( &op_finished );
1812 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001813 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001814}
1815/* END_CASE */
1816
itayzafrir58028322018-10-25 10:22:01 +03001817/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001818void mac_operation_init( )
1819{
Jaeden Amero252ef282019-02-15 14:05:35 +00001820 const uint8_t input[1] = { 0 };
1821
Jaeden Amero769ce272019-01-04 11:48:03 +00001822 /* Test each valid way of initializing the object, except for `= {0}`, as
1823 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1824 * though it's OK by the C standard. We could test for this, but we'd need
1825 * to supress the Clang warning for the test. */
1826 psa_mac_operation_t func = psa_mac_operation_init( );
1827 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1828 psa_mac_operation_t zero;
1829
1830 memset( &zero, 0, sizeof( zero ) );
1831
Jaeden Amero252ef282019-02-15 14:05:35 +00001832 /* A freshly-initialized MAC operation should not be usable. */
1833 TEST_EQUAL( psa_mac_update( &func,
1834 input, sizeof( input ) ),
1835 PSA_ERROR_BAD_STATE );
1836 TEST_EQUAL( psa_mac_update( &init,
1837 input, sizeof( input ) ),
1838 PSA_ERROR_BAD_STATE );
1839 TEST_EQUAL( psa_mac_update( &zero,
1840 input, sizeof( input ) ),
1841 PSA_ERROR_BAD_STATE );
1842
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001843 /* A default MAC operation should be abortable without error. */
1844 PSA_ASSERT( psa_mac_abort( &func ) );
1845 PSA_ASSERT( psa_mac_abort( &init ) );
1846 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001847}
1848/* END_CASE */
1849
1850/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001851void mac_setup( int key_type_arg,
1852 data_t *key,
1853 int alg_arg,
1854 int expected_status_arg )
1855{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001856 psa_key_type_t key_type = key_type_arg;
1857 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001858 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001859 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001860 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1861#if defined(KNOWN_SUPPORTED_MAC_ALG)
1862 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1863#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001864
Gilles Peskine8817f612018-12-18 00:18:46 +01001865 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001866
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001867 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1868 &operation, &status ) )
1869 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001870 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001871
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001872 /* The operation object should be reusable. */
1873#if defined(KNOWN_SUPPORTED_MAC_ALG)
1874 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1875 smoke_test_key_data,
1876 sizeof( smoke_test_key_data ),
1877 KNOWN_SUPPORTED_MAC_ALG,
1878 &operation, &status ) )
1879 goto exit;
1880 TEST_EQUAL( status, PSA_SUCCESS );
1881#endif
1882
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001883exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001884 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001885}
1886/* END_CASE */
1887
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001888/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00001889void mac_bad_order( )
1890{
Ronald Cron5425a212020-08-04 14:58:35 +02001891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001892 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1893 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001894 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001895 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1896 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1897 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001899 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1900 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1901 size_t sign_mac_length = 0;
1902 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1903 const uint8_t verify_mac[] = {
1904 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1905 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1906 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1907
1908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001909 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001910 psa_set_key_algorithm( &attributes, alg );
1911 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001912
Ronald Cron5425a212020-08-04 14:58:35 +02001913 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1914 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001915
Jaeden Amero252ef282019-02-15 14:05:35 +00001916 /* Call update without calling setup beforehand. */
1917 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1918 PSA_ERROR_BAD_STATE );
1919 PSA_ASSERT( psa_mac_abort( &operation ) );
1920
1921 /* Call sign finish without calling setup beforehand. */
1922 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1923 &sign_mac_length),
1924 PSA_ERROR_BAD_STATE );
1925 PSA_ASSERT( psa_mac_abort( &operation ) );
1926
1927 /* Call verify finish without calling setup beforehand. */
1928 TEST_EQUAL( psa_mac_verify_finish( &operation,
1929 verify_mac, sizeof( verify_mac ) ),
1930 PSA_ERROR_BAD_STATE );
1931 PSA_ASSERT( psa_mac_abort( &operation ) );
1932
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001933 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001934 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1935 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001936 PSA_ERROR_BAD_STATE );
1937 PSA_ASSERT( psa_mac_abort( &operation ) );
1938
Jaeden Amero252ef282019-02-15 14:05:35 +00001939 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001940 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001941 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1942 PSA_ASSERT( psa_mac_sign_finish( &operation,
1943 sign_mac, sizeof( sign_mac ),
1944 &sign_mac_length ) );
1945 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1946 PSA_ERROR_BAD_STATE );
1947 PSA_ASSERT( psa_mac_abort( &operation ) );
1948
1949 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001950 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001951 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1952 PSA_ASSERT( psa_mac_verify_finish( &operation,
1953 verify_mac, sizeof( verify_mac ) ) );
1954 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1955 PSA_ERROR_BAD_STATE );
1956 PSA_ASSERT( psa_mac_abort( &operation ) );
1957
1958 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001959 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001960 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1961 PSA_ASSERT( psa_mac_sign_finish( &operation,
1962 sign_mac, sizeof( sign_mac ),
1963 &sign_mac_length ) );
1964 TEST_EQUAL( psa_mac_sign_finish( &operation,
1965 sign_mac, sizeof( sign_mac ),
1966 &sign_mac_length ),
1967 PSA_ERROR_BAD_STATE );
1968 PSA_ASSERT( psa_mac_abort( &operation ) );
1969
1970 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001971 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001972 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1973 PSA_ASSERT( psa_mac_verify_finish( &operation,
1974 verify_mac, sizeof( verify_mac ) ) );
1975 TEST_EQUAL( psa_mac_verify_finish( &operation,
1976 verify_mac, sizeof( verify_mac ) ),
1977 PSA_ERROR_BAD_STATE );
1978 PSA_ASSERT( psa_mac_abort( &operation ) );
1979
1980 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001981 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001982 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1983 TEST_EQUAL( psa_mac_verify_finish( &operation,
1984 verify_mac, sizeof( verify_mac ) ),
1985 PSA_ERROR_BAD_STATE );
1986 PSA_ASSERT( psa_mac_abort( &operation ) );
1987
1988 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001989 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001990 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1991 TEST_EQUAL( psa_mac_sign_finish( &operation,
1992 sign_mac, sizeof( sign_mac ),
1993 &sign_mac_length ),
1994 PSA_ERROR_BAD_STATE );
1995 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001996
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001998
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001999exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002000 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002001}
2002/* END_CASE */
2003
2004/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002006 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002007 int alg_arg,
2008 data_t *input,
2009 data_t *expected_mac )
2010{
Ronald Cron5425a212020-08-04 14:58:35 +02002011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002012 psa_key_type_t key_type = key_type_arg;
2013 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002014 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002016 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002017 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002018 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002019 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002020 const size_t output_sizes_to_test[] = {
2021 0,
2022 1,
2023 expected_mac->len - 1,
2024 expected_mac->len,
2025 expected_mac->len + 1,
2026 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002027
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002028 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002029 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002030 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002031
Gilles Peskine8817f612018-12-18 00:18:46 +01002032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002033
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002034 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002035 psa_set_key_algorithm( &attributes, alg );
2036 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002037
Ronald Cron5425a212020-08-04 14:58:35 +02002038 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2039 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002040
Gilles Peskine8b356b52020-08-25 23:44:59 +02002041 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2042 {
2043 const size_t output_size = output_sizes_to_test[i];
2044 psa_status_t expected_status =
2045 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2046 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002047
Chris Jones9634bb12021-01-20 15:56:42 +00002048 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002049 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002050
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002051 /* Calculate the MAC, one-shot case. */
2052 TEST_EQUAL( psa_mac_compute( key, alg,
2053 input->x, input->len,
2054 actual_mac, output_size, &mac_length ),
2055 expected_status );
2056 if( expected_status == PSA_SUCCESS )
2057 {
2058 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2059 actual_mac, mac_length );
2060 }
2061
2062 if( output_size > 0 )
2063 memset( actual_mac, 0, output_size );
2064
2065 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002066 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002067 PSA_ASSERT( psa_mac_update( &operation,
2068 input->x, input->len ) );
2069 TEST_EQUAL( psa_mac_sign_finish( &operation,
2070 actual_mac, output_size,
2071 &mac_length ),
2072 expected_status );
2073 PSA_ASSERT( psa_mac_abort( &operation ) );
2074
2075 if( expected_status == PSA_SUCCESS )
2076 {
2077 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2078 actual_mac, mac_length );
2079 }
2080 mbedtls_free( actual_mac );
2081 actual_mac = NULL;
2082 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002083
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002084exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002085 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002086 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002087 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002088 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002089}
2090/* END_CASE */
2091
2092/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002093void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002094 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002095 int alg_arg,
2096 data_t *input,
2097 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002098{
Ronald Cron5425a212020-08-04 14:58:35 +02002099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002100 psa_key_type_t key_type = key_type_arg;
2101 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002102 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002103 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002104 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002105
Gilles Peskine69c12672018-06-28 00:07:19 +02002106 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2107
Gilles Peskine8817f612018-12-18 00:18:46 +01002108 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002109
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002110 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002111 psa_set_key_algorithm( &attributes, alg );
2112 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002113
Ronald Cron5425a212020-08-04 14:58:35 +02002114 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2115 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002116
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002117 /* Verify correct MAC, one-shot case. */
2118 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2119 expected_mac->x, expected_mac->len ) );
2120
2121 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002122 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002123 PSA_ASSERT( psa_mac_update( &operation,
2124 input->x, input->len ) );
2125 PSA_ASSERT( psa_mac_verify_finish( &operation,
2126 expected_mac->x,
2127 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002129 /* Test a MAC that's too short, one-shot case. */
2130 TEST_EQUAL( psa_mac_verify( key, alg,
2131 input->x, input->len,
2132 expected_mac->x,
2133 expected_mac->len - 1 ),
2134 PSA_ERROR_INVALID_SIGNATURE );
2135
2136 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002137 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002138 PSA_ASSERT( psa_mac_update( &operation,
2139 input->x, input->len ) );
2140 TEST_EQUAL( psa_mac_verify_finish( &operation,
2141 expected_mac->x,
2142 expected_mac->len - 1 ),
2143 PSA_ERROR_INVALID_SIGNATURE );
2144
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002145 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002146 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2147 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002148 TEST_EQUAL( psa_mac_verify( key, alg,
2149 input->x, input->len,
2150 perturbed_mac, expected_mac->len + 1 ),
2151 PSA_ERROR_INVALID_SIGNATURE );
2152
2153 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002154 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002155 PSA_ASSERT( psa_mac_update( &operation,
2156 input->x, input->len ) );
2157 TEST_EQUAL( psa_mac_verify_finish( &operation,
2158 perturbed_mac,
2159 expected_mac->len + 1 ),
2160 PSA_ERROR_INVALID_SIGNATURE );
2161
2162 /* Test changing one byte. */
2163 for( size_t i = 0; i < expected_mac->len; i++ )
2164 {
Chris Jones9634bb12021-01-20 15:56:42 +00002165 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002166 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002167
2168 TEST_EQUAL( psa_mac_verify( key, alg,
2169 input->x, input->len,
2170 perturbed_mac, expected_mac->len ),
2171 PSA_ERROR_INVALID_SIGNATURE );
2172
Ronald Cron5425a212020-08-04 14:58:35 +02002173 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002174 PSA_ASSERT( psa_mac_update( &operation,
2175 input->x, input->len ) );
2176 TEST_EQUAL( psa_mac_verify_finish( &operation,
2177 perturbed_mac,
2178 expected_mac->len ),
2179 PSA_ERROR_INVALID_SIGNATURE );
2180 perturbed_mac[i] ^= 1;
2181 }
2182
Gilles Peskine8c9def32018-02-08 10:02:12 +01002183exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002184 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002186 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002187 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002188}
2189/* END_CASE */
2190
2191/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002192void cipher_operation_init( )
2193{
Jaeden Ameroab439972019-02-15 14:12:05 +00002194 const uint8_t input[1] = { 0 };
2195 unsigned char output[1] = { 0 };
2196 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002197 /* Test each valid way of initializing the object, except for `= {0}`, as
2198 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2199 * though it's OK by the C standard. We could test for this, but we'd need
2200 * to supress the Clang warning for the test. */
2201 psa_cipher_operation_t func = psa_cipher_operation_init( );
2202 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2203 psa_cipher_operation_t zero;
2204
2205 memset( &zero, 0, sizeof( zero ) );
2206
Jaeden Ameroab439972019-02-15 14:12:05 +00002207 /* A freshly-initialized cipher operation should not be usable. */
2208 TEST_EQUAL( psa_cipher_update( &func,
2209 input, sizeof( input ),
2210 output, sizeof( output ),
2211 &output_length ),
2212 PSA_ERROR_BAD_STATE );
2213 TEST_EQUAL( psa_cipher_update( &init,
2214 input, sizeof( input ),
2215 output, sizeof( output ),
2216 &output_length ),
2217 PSA_ERROR_BAD_STATE );
2218 TEST_EQUAL( psa_cipher_update( &zero,
2219 input, sizeof( input ),
2220 output, sizeof( output ),
2221 &output_length ),
2222 PSA_ERROR_BAD_STATE );
2223
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002224 /* A default cipher operation should be abortable without error. */
2225 PSA_ASSERT( psa_cipher_abort( &func ) );
2226 PSA_ASSERT( psa_cipher_abort( &init ) );
2227 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002228}
2229/* END_CASE */
2230
2231/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002232void cipher_setup( int key_type_arg,
2233 data_t *key,
2234 int alg_arg,
2235 int expected_status_arg )
2236{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002237 psa_key_type_t key_type = key_type_arg;
2238 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002239 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002240 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002241 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002242#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002243 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2244#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002245
Gilles Peskine8817f612018-12-18 00:18:46 +01002246 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002247
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002248 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2249 &operation, &status ) )
2250 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002251 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002252
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002253 /* The operation object should be reusable. */
2254#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2255 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2256 smoke_test_key_data,
2257 sizeof( smoke_test_key_data ),
2258 KNOWN_SUPPORTED_CIPHER_ALG,
2259 &operation, &status ) )
2260 goto exit;
2261 TEST_EQUAL( status, PSA_SUCCESS );
2262#endif
2263
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002265 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002266 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002267}
2268/* END_CASE */
2269
Ronald Cronee414c72021-03-18 18:50:08 +01002270/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002271void cipher_bad_order( )
2272{
Ronald Cron5425a212020-08-04 14:58:35 +02002273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002274 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2275 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002277 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002278 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002279 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002280 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2281 0xaa, 0xaa, 0xaa, 0xaa };
2282 const uint8_t text[] = {
2283 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2284 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002285 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002286 size_t length = 0;
2287
2288 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2290 psa_set_key_algorithm( &attributes, alg );
2291 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002292 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2293 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002294
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002295 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002296 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2297 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002298 PSA_ERROR_BAD_STATE );
2299 PSA_ASSERT( psa_cipher_abort( &operation ) );
2300
2301 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002302 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2303 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002304 PSA_ERROR_BAD_STATE );
2305 PSA_ASSERT( psa_cipher_abort( &operation ) );
2306
Jaeden Ameroab439972019-02-15 14:12:05 +00002307 /* Generate an IV without calling setup beforehand. */
2308 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2309 buffer, sizeof( buffer ),
2310 &length ),
2311 PSA_ERROR_BAD_STATE );
2312 PSA_ASSERT( psa_cipher_abort( &operation ) );
2313
2314 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002315 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002316 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2317 buffer, sizeof( buffer ),
2318 &length ) );
2319 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2320 buffer, sizeof( buffer ),
2321 &length ),
2322 PSA_ERROR_BAD_STATE );
2323 PSA_ASSERT( psa_cipher_abort( &operation ) );
2324
2325 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002326 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002327 PSA_ASSERT( psa_cipher_set_iv( &operation,
2328 iv, sizeof( iv ) ) );
2329 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2330 buffer, sizeof( buffer ),
2331 &length ),
2332 PSA_ERROR_BAD_STATE );
2333 PSA_ASSERT( psa_cipher_abort( &operation ) );
2334
2335 /* Set an IV without calling setup beforehand. */
2336 TEST_EQUAL( psa_cipher_set_iv( &operation,
2337 iv, sizeof( iv ) ),
2338 PSA_ERROR_BAD_STATE );
2339 PSA_ASSERT( psa_cipher_abort( &operation ) );
2340
2341 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002342 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002343 PSA_ASSERT( psa_cipher_set_iv( &operation,
2344 iv, sizeof( iv ) ) );
2345 TEST_EQUAL( psa_cipher_set_iv( &operation,
2346 iv, sizeof( iv ) ),
2347 PSA_ERROR_BAD_STATE );
2348 PSA_ASSERT( psa_cipher_abort( &operation ) );
2349
2350 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002351 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002352 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2353 buffer, sizeof( buffer ),
2354 &length ) );
2355 TEST_EQUAL( psa_cipher_set_iv( &operation,
2356 iv, sizeof( iv ) ),
2357 PSA_ERROR_BAD_STATE );
2358 PSA_ASSERT( psa_cipher_abort( &operation ) );
2359
2360 /* Call update without calling setup beforehand. */
2361 TEST_EQUAL( psa_cipher_update( &operation,
2362 text, sizeof( text ),
2363 buffer, sizeof( buffer ),
2364 &length ),
2365 PSA_ERROR_BAD_STATE );
2366 PSA_ASSERT( psa_cipher_abort( &operation ) );
2367
2368 /* Call update without an IV where an IV is required. */
2369 TEST_EQUAL( psa_cipher_update( &operation,
2370 text, sizeof( text ),
2371 buffer, sizeof( buffer ),
2372 &length ),
2373 PSA_ERROR_BAD_STATE );
2374 PSA_ASSERT( psa_cipher_abort( &operation ) );
2375
2376 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002377 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002378 PSA_ASSERT( psa_cipher_set_iv( &operation,
2379 iv, sizeof( iv ) ) );
2380 PSA_ASSERT( psa_cipher_finish( &operation,
2381 buffer, sizeof( buffer ), &length ) );
2382 TEST_EQUAL( psa_cipher_update( &operation,
2383 text, sizeof( text ),
2384 buffer, sizeof( buffer ),
2385 &length ),
2386 PSA_ERROR_BAD_STATE );
2387 PSA_ASSERT( psa_cipher_abort( &operation ) );
2388
2389 /* Call finish without calling setup beforehand. */
2390 TEST_EQUAL( psa_cipher_finish( &operation,
2391 buffer, sizeof( buffer ), &length ),
2392 PSA_ERROR_BAD_STATE );
2393 PSA_ASSERT( psa_cipher_abort( &operation ) );
2394
2395 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002396 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002397 /* Not calling update means we are encrypting an empty buffer, which is OK
2398 * for cipher modes with padding. */
2399 TEST_EQUAL( psa_cipher_finish( &operation,
2400 buffer, sizeof( buffer ), &length ),
2401 PSA_ERROR_BAD_STATE );
2402 PSA_ASSERT( psa_cipher_abort( &operation ) );
2403
2404 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002405 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002406 PSA_ASSERT( psa_cipher_set_iv( &operation,
2407 iv, sizeof( iv ) ) );
2408 PSA_ASSERT( psa_cipher_finish( &operation,
2409 buffer, sizeof( buffer ), &length ) );
2410 TEST_EQUAL( psa_cipher_finish( &operation,
2411 buffer, sizeof( buffer ), &length ),
2412 PSA_ERROR_BAD_STATE );
2413 PSA_ASSERT( psa_cipher_abort( &operation ) );
2414
Ronald Cron5425a212020-08-04 14:58:35 +02002415 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002416
Jaeden Ameroab439972019-02-15 14:12:05 +00002417exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002418 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002419 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002420}
2421/* END_CASE */
2422
2423/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002424void cipher_encrypt_fail( int alg_arg,
2425 int key_type_arg,
2426 data_t *key_data,
2427 data_t *input,
2428 int expected_status_arg )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002429{
2430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2431 psa_status_t status;
2432 psa_key_type_t key_type = key_type_arg;
2433 psa_algorithm_t alg = alg_arg;
2434 psa_status_t expected_status = expected_status_arg;
2435 unsigned char *output = NULL;
2436 size_t output_buffer_size = 0;
2437 size_t output_length = 0;
2438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2439
2440 if ( PSA_ERROR_BAD_STATE != expected_status )
2441 {
2442 PSA_ASSERT( psa_crypto_init( ) );
2443
2444 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2445 psa_set_key_algorithm( &attributes, alg );
2446 psa_set_key_type( &attributes, key_type );
2447
2448 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2449 input->len );
2450 ASSERT_ALLOC( output, output_buffer_size );
2451
2452 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2453 &key ) );
2454 }
2455
2456 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2457 output_buffer_size, &output_length );
2458
2459 TEST_EQUAL( status, expected_status );
2460
2461exit:
2462 mbedtls_free( output );
2463 psa_destroy_key( key );
2464 PSA_DONE( );
2465}
2466/* END_CASE */
2467
2468/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002469void cipher_encrypt_alg_without_iv( int alg_arg,
2470 int key_type_arg,
2471 data_t *key_data,
2472 data_t *input,
2473 data_t *expected_output )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002474{
2475 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2476 psa_key_type_t key_type = key_type_arg;
2477 psa_algorithm_t alg = alg_arg;
2478 unsigned char *output = NULL;
2479 size_t output_buffer_size = 0;
2480 size_t output_length = 0;
2481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2482
2483 PSA_ASSERT( psa_crypto_init( ) );
2484
2485 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2486 psa_set_key_algorithm( &attributes, alg );
2487 psa_set_key_type( &attributes, key_type );
2488
2489 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2490 ASSERT_ALLOC( output, output_buffer_size );
2491
2492 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2493 &key ) );
2494
2495 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2496 output_buffer_size, &output_length ) );
2497 TEST_ASSERT( output_length <=
2498 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2499 TEST_ASSERT( output_length <=
2500 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2501
2502 ASSERT_COMPARE( expected_output->x, expected_output->len,
2503 output, output_length );
2504exit:
2505 mbedtls_free( output );
2506 psa_destroy_key( key );
2507 PSA_DONE( );
2508}
2509/* END_CASE */
2510
2511/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002512void cipher_encrypt_validation( int alg_arg,
2513 int key_type_arg,
2514 data_t *key_data,
2515 data_t *input )
2516{
2517 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2518 psa_key_type_t key_type = key_type_arg;
2519 psa_algorithm_t alg = alg_arg;
2520 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2521 unsigned char *output1 = NULL;
2522 size_t output1_buffer_size = 0;
2523 size_t output1_length = 0;
2524 unsigned char *output2 = NULL;
2525 size_t output2_buffer_size = 0;
2526 size_t output2_length = 0;
2527 size_t function_output_length = 0;
2528 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2530
2531 PSA_ASSERT( psa_crypto_init( ) );
2532
2533 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2534 psa_set_key_algorithm( &attributes, alg );
2535 psa_set_key_type( &attributes, key_type );
2536
2537 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2538 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2539 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2540 ASSERT_ALLOC( output1, output1_buffer_size );
2541 ASSERT_ALLOC( output2, output2_buffer_size );
2542
2543 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2544 &key ) );
2545
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002546 /* The one-shot cipher encryption uses generated iv so validating
2547 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002548 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2549 output1_buffer_size, &output1_length ) );
2550 TEST_ASSERT( output1_length <=
2551 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2552 TEST_ASSERT( output1_length <=
2553 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2554
2555 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2556 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
2557
2558 PSA_ASSERT( psa_cipher_update( &operation,
2559 input->x, input->len,
2560 output2, output2_buffer_size,
2561 &function_output_length ) );
2562 TEST_ASSERT( function_output_length <=
2563 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2564 TEST_ASSERT( function_output_length <=
2565 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2566 output2_length += function_output_length;
2567
2568 PSA_ASSERT( psa_cipher_finish( &operation,
2569 output2 + output2_length,
2570 output2_buffer_size - output2_length,
2571 &function_output_length ) );
2572 TEST_ASSERT( function_output_length <=
2573 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2574 TEST_ASSERT( function_output_length <=
2575 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
2576 output2_length += function_output_length;
2577
2578 PSA_ASSERT( psa_cipher_abort( &operation ) );
2579 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2580 output2, output2_length );
2581
2582exit:
2583 psa_cipher_abort( &operation );
2584 mbedtls_free( output1 );
2585 mbedtls_free( output2 );
2586 psa_destroy_key( key );
2587 PSA_DONE( );
2588}
2589/* END_CASE */
2590
2591/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002592void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002593 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002594 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002595 int first_part_size_arg,
2596 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002597 data_t *expected_output,
2598 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002599{
Ronald Cron5425a212020-08-04 14:58:35 +02002600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002601 psa_key_type_t key_type = key_type_arg;
2602 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002603 psa_status_t status;
2604 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002605 size_t first_part_size = first_part_size_arg;
2606 size_t output1_length = output1_length_arg;
2607 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002608 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002609 size_t output_buffer_size = 0;
2610 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002611 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002612 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002614
Gilles Peskine8817f612018-12-18 00:18:46 +01002615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002616
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2618 psa_set_key_algorithm( &attributes, alg );
2619 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002620
Ronald Cron5425a212020-08-04 14:58:35 +02002621 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2622 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002623
Ronald Cron5425a212020-08-04 14:58:35 +02002624 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002625
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002626 if( iv->len > 0 )
2627 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002628 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002629 }
2630
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002631 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2632 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002633 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002634
Gilles Peskinee0866522019-02-19 19:44:00 +01002635 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002636 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2637 output, output_buffer_size,
2638 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002639 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002640 TEST_ASSERT( function_output_length <=
2641 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2642 TEST_ASSERT( function_output_length <=
2643 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002644 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002645
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002646 if( first_part_size < input->len )
2647 {
2648 PSA_ASSERT( psa_cipher_update( &operation,
2649 input->x + first_part_size,
2650 input->len - first_part_size,
2651 ( output_buffer_size == 0 ? NULL :
2652 output + total_output_length ),
2653 output_buffer_size - total_output_length,
2654 &function_output_length ) );
2655 TEST_ASSERT( function_output_length == output2_length );
2656 TEST_ASSERT( function_output_length <=
2657 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2658 alg,
2659 input->len - first_part_size ) );
2660 TEST_ASSERT( function_output_length <=
2661 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2662 total_output_length += function_output_length;
2663 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002664
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002665 status = psa_cipher_finish( &operation,
2666 ( output_buffer_size == 0 ? NULL :
2667 output + total_output_length ),
2668 output_buffer_size - total_output_length,
2669 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002670 TEST_ASSERT( function_output_length <=
2671 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2672 TEST_ASSERT( function_output_length <=
2673 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002674 total_output_length += function_output_length;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002675 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002676
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002677 if( expected_status == PSA_SUCCESS )
2678 {
2679 PSA_ASSERT( psa_cipher_abort( &operation ) );
2680
2681 ASSERT_COMPARE( expected_output->x, expected_output->len,
2682 output, total_output_length );
2683 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684
2685exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002686 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002687 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002688 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002689 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002690}
2691/* END_CASE */
2692
2693/* BEGIN_CASE */
2694void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002695 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002696 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002697 int first_part_size_arg,
2698 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002699 data_t *expected_output,
2700 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002701{
Ronald Cron5425a212020-08-04 14:58:35 +02002702 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002703 psa_key_type_t key_type = key_type_arg;
2704 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002705 psa_status_t status;
2706 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002707 size_t first_part_size = first_part_size_arg;
2708 size_t output1_length = output1_length_arg;
2709 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002710 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002711 size_t output_buffer_size = 0;
2712 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002713 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002714 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002715 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002716
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002718
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002719 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2720 psa_set_key_algorithm( &attributes, alg );
2721 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002722
Ronald Cron5425a212020-08-04 14:58:35 +02002723 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2724 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002725
Ronald Cron5425a212020-08-04 14:58:35 +02002726 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002727
Steven Cooreman177deba2020-09-07 17:14:14 +02002728 if( iv->len > 0 )
2729 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002730 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002731 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002732
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002733 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2734 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002735 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002736
Gilles Peskinee0866522019-02-19 19:44:00 +01002737 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002738 PSA_ASSERT( psa_cipher_update( &operation,
2739 input->x, first_part_size,
2740 output, output_buffer_size,
2741 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002742 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002743 TEST_ASSERT( function_output_length <=
2744 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2745 TEST_ASSERT( function_output_length <=
2746 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002747 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002748
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002749 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002750 {
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002751 PSA_ASSERT( psa_cipher_update( &operation,
2752 input->x + first_part_size,
2753 input->len - first_part_size,
2754 ( output_buffer_size == 0 ? NULL :
2755 output + total_output_length ),
2756 output_buffer_size - total_output_length,
2757 &function_output_length ) );
2758 TEST_ASSERT( function_output_length == output2_length );
2759 TEST_ASSERT( function_output_length <=
2760 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2761 alg,
2762 input->len - first_part_size ) );
2763 TEST_ASSERT( function_output_length <=
2764 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2765 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002766 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002767
Gilles Peskine50e586b2018-06-08 14:28:46 +02002768 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002769 ( output_buffer_size == 0 ? NULL :
2770 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002771 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002772 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002773 TEST_ASSERT( function_output_length <=
2774 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2775 TEST_ASSERT( function_output_length <=
2776 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002777 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002778 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002779
2780 if( expected_status == PSA_SUCCESS )
2781 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002782 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002783
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002784 ASSERT_COMPARE( expected_output->x, expected_output->len,
2785 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002786 }
2787
Gilles Peskine50e586b2018-06-08 14:28:46 +02002788exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002789 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002790 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002791 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002792 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002793}
2794/* END_CASE */
2795
Gilles Peskine50e586b2018-06-08 14:28:46 +02002796/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002797void cipher_decrypt_fail( int alg_arg,
2798 int key_type_arg,
2799 data_t *key_data,
2800 data_t *iv,
2801 data_t *input_arg,
2802 int expected_status_arg )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002803{
2804 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2805 psa_status_t status;
2806 psa_key_type_t key_type = key_type_arg;
2807 psa_algorithm_t alg = alg_arg;
2808 psa_status_t expected_status = expected_status_arg;
2809 unsigned char *input = NULL;
2810 size_t input_buffer_size = 0;
2811 unsigned char *output = NULL;
2812 size_t output_buffer_size = 0;
2813 size_t output_length = 0;
2814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2815
2816 if ( PSA_ERROR_BAD_STATE != expected_status )
2817 {
2818 PSA_ASSERT( psa_crypto_init( ) );
2819
2820 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2821 psa_set_key_algorithm( &attributes, alg );
2822 psa_set_key_type( &attributes, key_type );
2823
2824 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2825 &key ) );
2826 }
2827
2828 /* Allocate input buffer and copy the iv and the plaintext */
2829 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2830 if ( input_buffer_size > 0 )
2831 {
2832 ASSERT_ALLOC( input, input_buffer_size );
2833 memcpy( input, iv->x, iv->len );
2834 memcpy( input + iv->len, input_arg->x, input_arg->len );
2835 }
2836
2837 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2838 ASSERT_ALLOC( output, output_buffer_size );
2839
2840 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2841 output_buffer_size, &output_length );
2842 TEST_EQUAL( status, expected_status );
2843
2844exit:
2845 mbedtls_free( input );
2846 mbedtls_free( output );
2847 psa_destroy_key( key );
2848 PSA_DONE( );
2849}
2850/* END_CASE */
2851
2852/* BEGIN_CASE */
2853void cipher_decrypt( int alg_arg,
2854 int key_type_arg,
2855 data_t *key_data,
2856 data_t *iv,
2857 data_t *input_arg,
2858 data_t *expected_output )
2859{
2860 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2861 psa_key_type_t key_type = key_type_arg;
2862 psa_algorithm_t alg = alg_arg;
2863 unsigned char *input = NULL;
2864 size_t input_buffer_size = 0;
2865 unsigned char *output = NULL;
2866 size_t output_buffer_size = 0;
2867 size_t output_length = 0;
2868 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2869
2870 PSA_ASSERT( psa_crypto_init( ) );
2871
2872 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2873 psa_set_key_algorithm( &attributes, alg );
2874 psa_set_key_type( &attributes, key_type );
2875
2876 /* Allocate input buffer and copy the iv and the plaintext */
2877 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2878 if ( input_buffer_size > 0 )
2879 {
2880 ASSERT_ALLOC( input, input_buffer_size );
2881 memcpy( input, iv->x, iv->len );
2882 memcpy( input + iv->len, input_arg->x, input_arg->len );
2883 }
2884
2885 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2886 ASSERT_ALLOC( output, output_buffer_size );
2887
2888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2889 &key ) );
2890
2891 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2892 output_buffer_size, &output_length ) );
2893 TEST_ASSERT( output_length <=
2894 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
2895 TEST_ASSERT( output_length <=
2896 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
2897
2898 ASSERT_COMPARE( expected_output->x, expected_output->len,
2899 output, output_length );
2900exit:
2901 mbedtls_free( input );
2902 mbedtls_free( output );
2903 psa_destroy_key( key );
2904 PSA_DONE( );
2905}
2906/* END_CASE */
2907
2908/* BEGIN_CASE */
2909void cipher_verify_output( int alg_arg,
2910 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002911 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002912 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002913{
Ronald Cron5425a212020-08-04 14:58:35 +02002914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002915 psa_key_type_t key_type = key_type_arg;
2916 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002917 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002918 size_t output1_size = 0;
2919 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002920 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002921 size_t output2_size = 0;
2922 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002923 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002924
Gilles Peskine8817f612018-12-18 00:18:46 +01002925 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002926
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002927 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2928 psa_set_key_algorithm( &attributes, alg );
2929 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002930
Ronald Cron5425a212020-08-04 14:58:35 +02002931 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2932 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002933 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002934 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002935
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002936 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
2937 output1, output1_size,
2938 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002939 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002940 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002941 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002942 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002943
2944 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002945 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002946
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002947 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
2948 output2, output2_size,
2949 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002950 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002951 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002952 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002953 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002954
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002955 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002956
2957exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002958 mbedtls_free( output1 );
2959 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002960 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002961 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002962}
2963/* END_CASE */
2964
2965/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002966void cipher_verify_output_multipart( int alg_arg,
2967 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002968 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002969 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002970 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002971{
Ronald Cron5425a212020-08-04 14:58:35 +02002972 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002973 psa_key_type_t key_type = key_type_arg;
2974 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002975 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002976 unsigned char iv[16] = {0};
2977 size_t iv_size = 16;
2978 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002979 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002980 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002981 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002982 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002983 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002984 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002985 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002986 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2987 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002989
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002991
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002992 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2993 psa_set_key_algorithm( &attributes, alg );
2994 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002995
Ronald Cron5425a212020-08-04 14:58:35 +02002996 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2997 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002998
Ronald Cron5425a212020-08-04 14:58:35 +02002999 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3000 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003001
Steven Cooreman177deba2020-09-07 17:14:14 +02003002 if( alg != PSA_ALG_ECB_NO_PADDING )
3003 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003004 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3005 iv, iv_size,
3006 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003007 }
3008
gabor-mezei-armceface22021-01-21 12:26:17 +01003009 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3010 TEST_ASSERT( output1_buffer_size <=
3011 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003012 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003013
Gilles Peskinee0866522019-02-19 19:44:00 +01003014 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003015
Gilles Peskine8817f612018-12-18 00:18:46 +01003016 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3017 output1, output1_buffer_size,
3018 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003019 TEST_ASSERT( function_output_length <=
3020 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3021 TEST_ASSERT( function_output_length <=
3022 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003023 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003024
Gilles Peskine8817f612018-12-18 00:18:46 +01003025 PSA_ASSERT( psa_cipher_update( &operation1,
3026 input->x + first_part_size,
3027 input->len - first_part_size,
3028 output1, output1_buffer_size,
3029 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003030 TEST_ASSERT( function_output_length <=
3031 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3032 alg,
3033 input->len - first_part_size ) );
3034 TEST_ASSERT( function_output_length <=
3035 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003036 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003037
Gilles Peskine8817f612018-12-18 00:18:46 +01003038 PSA_ASSERT( psa_cipher_finish( &operation1,
3039 output1 + output1_length,
3040 output1_buffer_size - output1_length,
3041 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003042 TEST_ASSERT( function_output_length <=
3043 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3044 TEST_ASSERT( function_output_length <=
3045 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003046 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003047
Gilles Peskine8817f612018-12-18 00:18:46 +01003048 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003049
Gilles Peskine048b7f02018-06-08 14:20:49 +02003050 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003051 TEST_ASSERT( output2_buffer_size <=
3052 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3053 TEST_ASSERT( output2_buffer_size <=
3054 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003055 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003056
Steven Cooreman177deba2020-09-07 17:14:14 +02003057 if( iv_length > 0 )
3058 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003059 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3060 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003061 }
Moran Pekerded84402018-06-06 16:36:50 +03003062
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3064 output2, output2_buffer_size,
3065 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003066 TEST_ASSERT( function_output_length <=
3067 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3068 TEST_ASSERT( function_output_length <=
3069 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003070 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003071
Gilles Peskine8817f612018-12-18 00:18:46 +01003072 PSA_ASSERT( psa_cipher_update( &operation2,
3073 output1 + first_part_size,
3074 output1_length - first_part_size,
3075 output2, output2_buffer_size,
3076 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003077 TEST_ASSERT( function_output_length <=
3078 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3079 alg,
3080 output1_length - first_part_size ) );
3081 TEST_ASSERT( function_output_length <=
3082 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003083 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003084
Gilles Peskine8817f612018-12-18 00:18:46 +01003085 PSA_ASSERT( psa_cipher_finish( &operation2,
3086 output2 + output2_length,
3087 output2_buffer_size - output2_length,
3088 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003089 TEST_ASSERT( function_output_length <=
3090 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3091 TEST_ASSERT( function_output_length <=
3092 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003093 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003094
Gilles Peskine8817f612018-12-18 00:18:46 +01003095 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003096
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003097 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003098
3099exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003100 psa_cipher_abort( &operation1 );
3101 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003102 mbedtls_free( output1 );
3103 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003104 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003105 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003106}
3107/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003108
Gilles Peskine20035e32018-02-03 22:44:14 +01003109/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003110void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003111 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003112 data_t *nonce,
3113 data_t *additional_data,
3114 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003115 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003116{
Ronald Cron5425a212020-08-04 14:58:35 +02003117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003118 psa_key_type_t key_type = key_type_arg;
3119 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003120 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003121 unsigned char *output_data = NULL;
3122 size_t output_size = 0;
3123 size_t output_length = 0;
3124 unsigned char *output_data2 = NULL;
3125 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003126 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003127 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003129
Gilles Peskine8817f612018-12-18 00:18:46 +01003130 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003131
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003132 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3133 psa_set_key_algorithm( &attributes, alg );
3134 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003135
Gilles Peskine049c7532019-05-15 20:22:09 +02003136 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003137 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003138 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3139 key_bits = psa_get_key_bits( &attributes );
3140
3141 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3142 alg );
3143 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3144 * should be exact. */
3145 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3146 expected_result != PSA_ERROR_NOT_SUPPORTED )
3147 {
3148 TEST_EQUAL( output_size,
3149 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3150 TEST_ASSERT( output_size <=
3151 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3152 }
3153 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003154
Steven Cooremanf49478b2021-02-15 15:19:25 +01003155 status = psa_aead_encrypt( key, alg,
3156 nonce->x, nonce->len,
3157 additional_data->x,
3158 additional_data->len,
3159 input_data->x, input_data->len,
3160 output_data, output_size,
3161 &output_length );
3162
3163 /* If the operation is not supported, just skip and not fail in case the
3164 * encryption involves a common limitation of cryptography hardwares and
3165 * an alternative implementation. */
3166 if( status == PSA_ERROR_NOT_SUPPORTED )
3167 {
3168 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3169 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3170 }
3171
3172 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003173
3174 if( PSA_SUCCESS == expected_result )
3175 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003176 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003177
Gilles Peskine003a4a92019-05-14 16:09:40 +02003178 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3179 * should be exact. */
3180 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003181 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003182
gabor-mezei-armceface22021-01-21 12:26:17 +01003183 TEST_ASSERT( input_data->len <=
3184 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3185
Ronald Cron5425a212020-08-04 14:58:35 +02003186 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003187 nonce->x, nonce->len,
3188 additional_data->x,
3189 additional_data->len,
3190 output_data, output_length,
3191 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003192 &output_length2 ),
3193 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003194
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003195 ASSERT_COMPARE( input_data->x, input_data->len,
3196 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003197 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003198
Gilles Peskinea1cac842018-06-11 19:33:02 +02003199exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003200 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003201 mbedtls_free( output_data );
3202 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003203 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003204}
3205/* END_CASE */
3206
3207/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003208void aead_encrypt( int key_type_arg, data_t *key_data,
3209 int alg_arg,
3210 data_t *nonce,
3211 data_t *additional_data,
3212 data_t *input_data,
3213 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003214{
Ronald Cron5425a212020-08-04 14:58:35 +02003215 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003216 psa_key_type_t key_type = key_type_arg;
3217 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003218 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003219 unsigned char *output_data = NULL;
3220 size_t output_size = 0;
3221 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003222 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003223 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003224
Gilles Peskine8817f612018-12-18 00:18:46 +01003225 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003226
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003227 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3228 psa_set_key_algorithm( &attributes, alg );
3229 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003230
Gilles Peskine049c7532019-05-15 20:22:09 +02003231 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003232 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003233 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3234 key_bits = psa_get_key_bits( &attributes );
3235
3236 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3237 alg );
3238 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3239 * should be exact. */
3240 TEST_EQUAL( output_size,
3241 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3242 TEST_ASSERT( output_size <=
3243 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3244 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003245
Steven Cooremand588ea12021-01-11 19:36:04 +01003246 status = psa_aead_encrypt( key, alg,
3247 nonce->x, nonce->len,
3248 additional_data->x, additional_data->len,
3249 input_data->x, input_data->len,
3250 output_data, output_size,
3251 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003252
Ronald Cron28a45ed2021-02-09 20:35:42 +01003253 /* If the operation is not supported, just skip and not fail in case the
3254 * encryption involves a common limitation of cryptography hardwares and
3255 * an alternative implementation. */
3256 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003257 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003258 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3259 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003260 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003261
3262 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003263 ASSERT_COMPARE( expected_result->x, expected_result->len,
3264 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003265
Gilles Peskinea1cac842018-06-11 19:33:02 +02003266exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003267 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003268 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003269 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003270}
3271/* END_CASE */
3272
3273/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003274void aead_decrypt( int key_type_arg, data_t *key_data,
3275 int alg_arg,
3276 data_t *nonce,
3277 data_t *additional_data,
3278 data_t *input_data,
3279 data_t *expected_data,
3280 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003281{
Ronald Cron5425a212020-08-04 14:58:35 +02003282 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003283 psa_key_type_t key_type = key_type_arg;
3284 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003285 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003286 unsigned char *output_data = NULL;
3287 size_t output_size = 0;
3288 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003290 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003291 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003292
Gilles Peskine8817f612018-12-18 00:18:46 +01003293 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003294
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003295 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3296 psa_set_key_algorithm( &attributes, alg );
3297 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003298
Gilles Peskine049c7532019-05-15 20:22:09 +02003299 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003300 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003301 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3302 key_bits = psa_get_key_bits( &attributes );
3303
3304 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3305 alg );
3306 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3307 expected_result != PSA_ERROR_NOT_SUPPORTED )
3308 {
3309 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3310 * should be exact. */
3311 TEST_EQUAL( output_size,
3312 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3313 TEST_ASSERT( output_size <=
3314 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3315 }
3316 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317
Steven Cooremand588ea12021-01-11 19:36:04 +01003318 status = psa_aead_decrypt( key, alg,
3319 nonce->x, nonce->len,
3320 additional_data->x,
3321 additional_data->len,
3322 input_data->x, input_data->len,
3323 output_data, output_size,
3324 &output_length );
3325
Ronald Cron28a45ed2021-02-09 20:35:42 +01003326 /* If the operation is not supported, just skip and not fail in case the
3327 * decryption involves a common limitation of cryptography hardwares and
3328 * an alternative implementation. */
3329 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003330 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003331 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3332 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003333 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003334
3335 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336
Gilles Peskine2d277862018-06-18 15:41:12 +02003337 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003338 ASSERT_COMPARE( expected_data->x, expected_data->len,
3339 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003340
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003342 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003344 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003345}
3346/* END_CASE */
3347
3348/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003349void signature_size( int type_arg,
3350 int bits,
3351 int alg_arg,
3352 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003353{
3354 psa_key_type_t type = type_arg;
3355 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003356 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003357
Gilles Peskinefe11b722018-12-18 00:24:04 +01003358 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003359#if defined(MBEDTLS_TEST_DEPRECATED)
3360 TEST_EQUAL( actual_size,
3361 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3362#endif /* MBEDTLS_TEST_DEPRECATED */
3363
Gilles Peskinee59236f2018-01-27 23:32:46 +01003364exit:
3365 ;
3366}
3367/* END_CASE */
3368
3369/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003370void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3371 int alg_arg, data_t *input_data,
3372 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003373{
Ronald Cron5425a212020-08-04 14:58:35 +02003374 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003375 psa_key_type_t key_type = key_type_arg;
3376 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003377 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003378 unsigned char *signature = NULL;
3379 size_t signature_size;
3380 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003381 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003382
Gilles Peskine8817f612018-12-18 00:18:46 +01003383 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003384
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003385 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003386 psa_set_key_algorithm( &attributes, alg );
3387 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003388
Gilles Peskine049c7532019-05-15 20:22:09 +02003389 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003390 &key ) );
3391 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003392 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003393
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003394 /* Allocate a buffer which has the size advertized by the
3395 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003396 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003397 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003398 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003399 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003400 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003401
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003402 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003403 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003404 input_data->x, input_data->len,
3405 signature, signature_size,
3406 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003407 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003408 ASSERT_COMPARE( output_data->x, output_data->len,
3409 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003410
Gilles Peskine0627f982019-11-26 19:12:16 +01003411#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003412 memset( signature, 0, signature_size );
3413 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003414 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003415 input_data->x, input_data->len,
3416 signature, signature_size,
3417 &signature_length ) );
3418 ASSERT_COMPARE( output_data->x, output_data->len,
3419 signature, signature_length );
3420#endif /* MBEDTLS_TEST_DEPRECATED */
3421
Gilles Peskine20035e32018-02-03 22:44:14 +01003422exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003423 /*
3424 * Key attributes may have been returned by psa_get_key_attributes()
3425 * thus reset them as required.
3426 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003427 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003428
Ronald Cron5425a212020-08-04 14:58:35 +02003429 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003430 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003431 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003432}
3433/* END_CASE */
3434
3435/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003436void sign_hash_fail( int key_type_arg, data_t *key_data,
3437 int alg_arg, data_t *input_data,
3438 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003439{
Ronald Cron5425a212020-08-04 14:58:35 +02003440 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003441 psa_key_type_t key_type = key_type_arg;
3442 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003443 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003444 psa_status_t actual_status;
3445 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003446 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003447 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003449
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003450 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003451
Gilles Peskine8817f612018-12-18 00:18:46 +01003452 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003453
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003454 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003455 psa_set_key_algorithm( &attributes, alg );
3456 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003457
Gilles Peskine049c7532019-05-15 20:22:09 +02003458 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003459 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003460
Ronald Cron5425a212020-08-04 14:58:35 +02003461 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003462 input_data->x, input_data->len,
3463 signature, signature_size,
3464 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003465 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003466 /* The value of *signature_length is unspecified on error, but
3467 * whatever it is, it should be less than signature_size, so that
3468 * if the caller tries to read *signature_length bytes without
3469 * checking the error code then they don't overflow a buffer. */
3470 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003471
Gilles Peskine895242b2019-11-29 12:15:40 +01003472#if defined(MBEDTLS_TEST_DEPRECATED)
3473 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003474 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003475 input_data->x, input_data->len,
3476 signature, signature_size,
3477 &signature_length ),
3478 expected_status );
3479 TEST_ASSERT( signature_length <= signature_size );
3480#endif /* MBEDTLS_TEST_DEPRECATED */
3481
Gilles Peskine20035e32018-02-03 22:44:14 +01003482exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003483 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003484 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003485 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003486 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003487}
3488/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003489
3490/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003491void sign_verify_hash( int key_type_arg, data_t *key_data,
3492 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003493{
Ronald Cron5425a212020-08-04 14:58:35 +02003494 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003495 psa_key_type_t key_type = key_type_arg;
3496 psa_algorithm_t alg = alg_arg;
3497 size_t key_bits;
3498 unsigned char *signature = NULL;
3499 size_t signature_size;
3500 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003502
Gilles Peskine8817f612018-12-18 00:18:46 +01003503 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003504
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003505 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003506 psa_set_key_algorithm( &attributes, alg );
3507 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003508
Gilles Peskine049c7532019-05-15 20:22:09 +02003509 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003510 &key ) );
3511 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003512 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003513
3514 /* Allocate a buffer which has the size advertized by the
3515 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003516 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003517 key_bits, alg );
3518 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003519 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003520 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003521
3522 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003523 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003524 input_data->x, input_data->len,
3525 signature, signature_size,
3526 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003527 /* Check that the signature length looks sensible. */
3528 TEST_ASSERT( signature_length <= signature_size );
3529 TEST_ASSERT( signature_length > 0 );
3530
3531 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003532 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003533 input_data->x, input_data->len,
3534 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003535
3536 if( input_data->len != 0 )
3537 {
3538 /* Flip a bit in the input and verify that the signature is now
3539 * detected as invalid. Flip a bit at the beginning, not at the end,
3540 * because ECDSA may ignore the last few bits of the input. */
3541 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003542 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003543 input_data->x, input_data->len,
3544 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003545 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003546 }
3547
3548exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003549 /*
3550 * Key attributes may have been returned by psa_get_key_attributes()
3551 * thus reset them as required.
3552 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003553 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003554
Ronald Cron5425a212020-08-04 14:58:35 +02003555 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003556 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003557 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003558}
3559/* END_CASE */
3560
3561/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003562void verify_hash( int key_type_arg, data_t *key_data,
3563 int alg_arg, data_t *hash_data,
3564 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003565{
Ronald Cron5425a212020-08-04 14:58:35 +02003566 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003567 psa_key_type_t key_type = key_type_arg;
3568 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003570
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003571 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003572
Gilles Peskine8817f612018-12-18 00:18:46 +01003573 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003574
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003575 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003576 psa_set_key_algorithm( &attributes, alg );
3577 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003578
Gilles Peskine049c7532019-05-15 20:22:09 +02003579 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003580 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003581
Ronald Cron5425a212020-08-04 14:58:35 +02003582 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003583 hash_data->x, hash_data->len,
3584 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003585
3586#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003587 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003588 hash_data->x, hash_data->len,
3589 signature_data->x,
3590 signature_data->len ) );
3591
3592#endif /* MBEDTLS_TEST_DEPRECATED */
3593
itayzafrir5c753392018-05-08 11:18:38 +03003594exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003595 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003596 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003597 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003598}
3599/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600
3601/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003602void verify_hash_fail( int key_type_arg, data_t *key_data,
3603 int alg_arg, data_t *hash_data,
3604 data_t *signature_data,
3605 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003606{
Ronald Cron5425a212020-08-04 14:58:35 +02003607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003608 psa_key_type_t key_type = key_type_arg;
3609 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003610 psa_status_t actual_status;
3611 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003613
Gilles Peskine8817f612018-12-18 00:18:46 +01003614 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003616 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003617 psa_set_key_algorithm( &attributes, alg );
3618 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003619
Gilles Peskine049c7532019-05-15 20:22:09 +02003620 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003621 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003622
Ronald Cron5425a212020-08-04 14:58:35 +02003623 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003624 hash_data->x, hash_data->len,
3625 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003626 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003627
Gilles Peskine895242b2019-11-29 12:15:40 +01003628#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003629 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003630 hash_data->x, hash_data->len,
3631 signature_data->x, signature_data->len ),
3632 expected_status );
3633#endif /* MBEDTLS_TEST_DEPRECATED */
3634
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003635exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003636 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003637 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003638 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003639}
3640/* END_CASE */
3641
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003643void sign_message_deterministic( int key_type_arg,
3644 data_t *key_data,
3645 int alg_arg,
3646 data_t *input_data,
3647 data_t *output_data )
3648{
3649 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3650 psa_key_type_t key_type = key_type_arg;
3651 psa_algorithm_t alg = alg_arg;
3652 size_t key_bits;
3653 unsigned char *signature = NULL;
3654 size_t signature_size;
3655 size_t signature_length = 0xdeadbeef;
3656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3657
3658 PSA_ASSERT( psa_crypto_init( ) );
3659
3660 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3661 psa_set_key_algorithm( &attributes, alg );
3662 psa_set_key_type( &attributes, key_type );
3663
3664 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3665 &key ) );
3666 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3667 key_bits = psa_get_key_bits( &attributes );
3668
3669 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3670 TEST_ASSERT( signature_size != 0 );
3671 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3672 ASSERT_ALLOC( signature, signature_size );
3673
3674 PSA_ASSERT( psa_sign_message( key, alg,
3675 input_data->x, input_data->len,
3676 signature, signature_size,
3677 &signature_length ) );
3678
3679 ASSERT_COMPARE( output_data->x, output_data->len,
3680 signature, signature_length );
3681
3682exit:
3683 psa_reset_key_attributes( &attributes );
3684
3685 psa_destroy_key( key );
3686 mbedtls_free( signature );
3687 PSA_DONE( );
3688
3689}
3690/* END_CASE */
3691
3692/* BEGIN_CASE */
3693void sign_message_fail( int key_type_arg,
3694 data_t *key_data,
3695 int alg_arg,
3696 data_t *input_data,
3697 int signature_size_arg,
3698 int expected_status_arg )
3699{
3700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3701 psa_key_type_t key_type = key_type_arg;
3702 psa_algorithm_t alg = alg_arg;
3703 size_t signature_size = signature_size_arg;
3704 psa_status_t actual_status;
3705 psa_status_t expected_status = expected_status_arg;
3706 unsigned char *signature = NULL;
3707 size_t signature_length = 0xdeadbeef;
3708 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3709
3710 ASSERT_ALLOC( signature, signature_size );
3711
3712 PSA_ASSERT( psa_crypto_init( ) );
3713
3714 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3715 psa_set_key_algorithm( &attributes, alg );
3716 psa_set_key_type( &attributes, key_type );
3717
3718 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3719 &key ) );
3720
3721 actual_status = psa_sign_message( key, alg,
3722 input_data->x, input_data->len,
3723 signature, signature_size,
3724 &signature_length );
3725 TEST_EQUAL( actual_status, expected_status );
3726 /* The value of *signature_length is unspecified on error, but
3727 * whatever it is, it should be less than signature_size, so that
3728 * if the caller tries to read *signature_length bytes without
3729 * checking the error code then they don't overflow a buffer. */
3730 TEST_ASSERT( signature_length <= signature_size );
3731
3732exit:
3733 psa_reset_key_attributes( &attributes );
3734 psa_destroy_key( key );
3735 mbedtls_free( signature );
3736 PSA_DONE( );
3737}
3738/* END_CASE */
3739
3740/* BEGIN_CASE */
3741void sign_verify_message( int key_type_arg,
3742 data_t *key_data,
3743 int alg_arg,
3744 data_t *input_data )
3745{
3746 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3747 psa_key_type_t key_type = key_type_arg;
3748 psa_algorithm_t alg = alg_arg;
3749 size_t key_bits;
3750 unsigned char *signature = NULL;
3751 size_t signature_size;
3752 size_t signature_length = 0xdeadbeef;
3753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3754
3755 PSA_ASSERT( psa_crypto_init( ) );
3756
3757 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3758 PSA_KEY_USAGE_VERIFY_MESSAGE );
3759 psa_set_key_algorithm( &attributes, alg );
3760 psa_set_key_type( &attributes, key_type );
3761
3762 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3763 &key ) );
3764 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3765 key_bits = psa_get_key_bits( &attributes );
3766
3767 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3768 TEST_ASSERT( signature_size != 0 );
3769 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3770 ASSERT_ALLOC( signature, signature_size );
3771
3772 PSA_ASSERT( psa_sign_message( key, alg,
3773 input_data->x, input_data->len,
3774 signature, signature_size,
3775 &signature_length ) );
3776 TEST_ASSERT( signature_length <= signature_size );
3777 TEST_ASSERT( signature_length > 0 );
3778
3779 PSA_ASSERT( psa_verify_message( key, alg,
3780 input_data->x, input_data->len,
3781 signature, signature_length ) );
3782
3783 if( input_data->len != 0 )
3784 {
3785 /* Flip a bit in the input and verify that the signature is now
3786 * detected as invalid. Flip a bit at the beginning, not at the end,
3787 * because ECDSA may ignore the last few bits of the input. */
3788 input_data->x[0] ^= 1;
3789 TEST_EQUAL( psa_verify_message( key, alg,
3790 input_data->x, input_data->len,
3791 signature, signature_length ),
3792 PSA_ERROR_INVALID_SIGNATURE );
3793 }
3794
3795exit:
3796 psa_reset_key_attributes( &attributes );
3797
3798 psa_destroy_key( key );
3799 mbedtls_free( signature );
3800 PSA_DONE( );
3801}
3802/* END_CASE */
3803
3804/* BEGIN_CASE */
3805void verify_message( int key_type_arg,
3806 data_t *key_data,
3807 int alg_arg,
3808 data_t *input_data,
3809 data_t *signature_data )
3810{
3811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3812 psa_key_type_t key_type = key_type_arg;
3813 psa_algorithm_t alg = alg_arg;
3814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3815
3816 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3817
3818 PSA_ASSERT( psa_crypto_init( ) );
3819
3820 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3821 psa_set_key_algorithm( &attributes, alg );
3822 psa_set_key_type( &attributes, key_type );
3823
3824 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3825 &key ) );
3826
3827 PSA_ASSERT( psa_verify_message( key, alg,
3828 input_data->x, input_data->len,
3829 signature_data->x, signature_data->len ) );
3830
3831exit:
3832 psa_reset_key_attributes( &attributes );
3833 psa_destroy_key( key );
3834 PSA_DONE( );
3835}
3836/* END_CASE */
3837
3838/* BEGIN_CASE */
3839void verify_message_fail( int key_type_arg,
3840 data_t *key_data,
3841 int alg_arg,
3842 data_t *hash_data,
3843 data_t *signature_data,
3844 int expected_status_arg )
3845{
3846 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3847 psa_key_type_t key_type = key_type_arg;
3848 psa_algorithm_t alg = alg_arg;
3849 psa_status_t actual_status;
3850 psa_status_t expected_status = expected_status_arg;
3851 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3852
3853 PSA_ASSERT( psa_crypto_init( ) );
3854
3855 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3856 psa_set_key_algorithm( &attributes, alg );
3857 psa_set_key_type( &attributes, key_type );
3858
3859 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3860 &key ) );
3861
3862 actual_status = psa_verify_message( key, alg,
3863 hash_data->x, hash_data->len,
3864 signature_data->x,
3865 signature_data->len );
3866 TEST_EQUAL( actual_status, expected_status );
3867
3868exit:
3869 psa_reset_key_attributes( &attributes );
3870 psa_destroy_key( key );
3871 PSA_DONE( );
3872}
3873/* END_CASE */
3874
3875/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003876void asymmetric_encrypt( int key_type_arg,
3877 data_t *key_data,
3878 int alg_arg,
3879 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003880 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003881 int expected_output_length_arg,
3882 int expected_status_arg )
3883{
Ronald Cron5425a212020-08-04 14:58:35 +02003884 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003885 psa_key_type_t key_type = key_type_arg;
3886 psa_algorithm_t alg = alg_arg;
3887 size_t expected_output_length = expected_output_length_arg;
3888 size_t key_bits;
3889 unsigned char *output = NULL;
3890 size_t output_size;
3891 size_t output_length = ~0;
3892 psa_status_t actual_status;
3893 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003895
Gilles Peskine8817f612018-12-18 00:18:46 +01003896 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003897
Gilles Peskine656896e2018-06-29 19:12:28 +02003898 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003899 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3900 psa_set_key_algorithm( &attributes, alg );
3901 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003902 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003903 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003904
3905 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003906 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003907 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003908
Gilles Peskine656896e2018-06-29 19:12:28 +02003909 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003910 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003911 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003912
3913 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003914 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003915 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003916 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003917 output, output_size,
3918 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003919 TEST_EQUAL( actual_status, expected_status );
3920 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003921
Gilles Peskine68428122018-06-30 18:42:41 +02003922 /* If the label is empty, the test framework puts a non-null pointer
3923 * in label->x. Test that a null pointer works as well. */
3924 if( label->len == 0 )
3925 {
3926 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003927 if( output_size != 0 )
3928 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003929 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003930 input_data->x, input_data->len,
3931 NULL, label->len,
3932 output, output_size,
3933 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003934 TEST_EQUAL( actual_status, expected_status );
3935 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003936 }
3937
Gilles Peskine656896e2018-06-29 19:12:28 +02003938exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003939 /*
3940 * Key attributes may have been returned by psa_get_key_attributes()
3941 * thus reset them as required.
3942 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003943 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003944
Ronald Cron5425a212020-08-04 14:58:35 +02003945 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003946 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003947 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003948}
3949/* END_CASE */
3950
3951/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003952void asymmetric_encrypt_decrypt( int key_type_arg,
3953 data_t *key_data,
3954 int alg_arg,
3955 data_t *input_data,
3956 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003957{
Ronald Cron5425a212020-08-04 14:58:35 +02003958 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003959 psa_key_type_t key_type = key_type_arg;
3960 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003961 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003963 size_t output_size;
3964 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003965 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003966 size_t output2_size;
3967 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003968 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003969
Gilles Peskine8817f612018-12-18 00:18:46 +01003970 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003971
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003972 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3973 psa_set_key_algorithm( &attributes, alg );
3974 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003975
Gilles Peskine049c7532019-05-15 20:22:09 +02003976 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003977 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003978
3979 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003980 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003981 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003982
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003983 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003984 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003985 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003986
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003987 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003988 TEST_ASSERT( output2_size <=
3989 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3990 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003991 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003992
Gilles Peskineeebd7382018-06-08 18:11:54 +02003993 /* We test encryption by checking that encrypt-then-decrypt gives back
3994 * the original plaintext because of the non-optional random
3995 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003996 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003997 input_data->x, input_data->len,
3998 label->x, label->len,
3999 output, output_size,
4000 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004001 /* We don't know what ciphertext length to expect, but check that
4002 * it looks sensible. */
4003 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004004
Ronald Cron5425a212020-08-04 14:58:35 +02004005 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004006 output, output_length,
4007 label->x, label->len,
4008 output2, output2_size,
4009 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004010 ASSERT_COMPARE( input_data->x, input_data->len,
4011 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012
4013exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004014 /*
4015 * Key attributes may have been returned by psa_get_key_attributes()
4016 * thus reset them as required.
4017 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004018 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004019
Ronald Cron5425a212020-08-04 14:58:35 +02004020 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004021 mbedtls_free( output );
4022 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004023 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024}
4025/* END_CASE */
4026
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004028void asymmetric_decrypt( int key_type_arg,
4029 data_t *key_data,
4030 int alg_arg,
4031 data_t *input_data,
4032 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004033 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004034{
Ronald Cron5425a212020-08-04 14:58:35 +02004035 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004036 psa_key_type_t key_type = key_type_arg;
4037 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004038 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004039 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004040 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004041 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004042 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004045
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004046 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4047 psa_set_key_algorithm( &attributes, alg );
4048 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004049
Gilles Peskine049c7532019-05-15 20:22:09 +02004050 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004051 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004052
gabor-mezei-armceface22021-01-21 12:26:17 +01004053 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4054 key_bits = psa_get_key_bits( &attributes );
4055
4056 /* Determine the maximum ciphertext length */
4057 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4058 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4059 ASSERT_ALLOC( output, output_size );
4060
Ronald Cron5425a212020-08-04 14:58:35 +02004061 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004062 input_data->x, input_data->len,
4063 label->x, label->len,
4064 output,
4065 output_size,
4066 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004067 ASSERT_COMPARE( expected_data->x, expected_data->len,
4068 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004069
Gilles Peskine68428122018-06-30 18:42:41 +02004070 /* If the label is empty, the test framework puts a non-null pointer
4071 * in label->x. Test that a null pointer works as well. */
4072 if( label->len == 0 )
4073 {
4074 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004075 if( output_size != 0 )
4076 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004077 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004078 input_data->x, input_data->len,
4079 NULL, label->len,
4080 output,
4081 output_size,
4082 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004083 ASSERT_COMPARE( expected_data->x, expected_data->len,
4084 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004085 }
4086
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004087exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004088 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004089 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004090 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004091 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004092}
4093/* END_CASE */
4094
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004095/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004096void asymmetric_decrypt_fail( int key_type_arg,
4097 data_t *key_data,
4098 int alg_arg,
4099 data_t *input_data,
4100 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004101 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004102 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004103{
Ronald Cron5425a212020-08-04 14:58:35 +02004104 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004105 psa_key_type_t key_type = key_type_arg;
4106 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004107 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004108 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004109 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004110 psa_status_t actual_status;
4111 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004113
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004114 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004115
Gilles Peskine8817f612018-12-18 00:18:46 +01004116 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004117
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004118 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4119 psa_set_key_algorithm( &attributes, alg );
4120 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004121
Gilles Peskine049c7532019-05-15 20:22:09 +02004122 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004123 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004124
Ronald Cron5425a212020-08-04 14:58:35 +02004125 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004126 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004127 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004128 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004129 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004130 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004131 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004132
Gilles Peskine68428122018-06-30 18:42:41 +02004133 /* If the label is empty, the test framework puts a non-null pointer
4134 * in label->x. Test that a null pointer works as well. */
4135 if( label->len == 0 )
4136 {
4137 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004138 if( output_size != 0 )
4139 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004140 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004141 input_data->x, input_data->len,
4142 NULL, label->len,
4143 output, output_size,
4144 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004145 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004146 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004147 }
4148
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004149exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004150 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004151 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004152 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004153 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004154}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004155/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004156
4157/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004158void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004159{
4160 /* Test each valid way of initializing the object, except for `= {0}`, as
4161 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4162 * though it's OK by the C standard. We could test for this, but we'd need
4163 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004164 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004165 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4166 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4167 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004168
4169 memset( &zero, 0, sizeof( zero ) );
4170
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004171 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004172 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004173 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004174 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004175 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004176 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004177 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004178
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004179 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004180 PSA_ASSERT( psa_key_derivation_abort(&func) );
4181 PSA_ASSERT( psa_key_derivation_abort(&init) );
4182 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004183}
4184/* END_CASE */
4185
Janos Follath16de4a42019-06-13 16:32:24 +01004186/* BEGIN_CASE */
4187void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004188{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004189 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004190 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004192
Gilles Peskine8817f612018-12-18 00:18:46 +01004193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004194
Janos Follath16de4a42019-06-13 16:32:24 +01004195 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004196 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004197
4198exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004199 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004200 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004201}
4202/* END_CASE */
4203
Janos Follathaf3c2a02019-06-12 12:34:34 +01004204/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004205void derive_set_capacity( int alg_arg, int capacity_arg,
4206 int expected_status_arg )
4207{
4208 psa_algorithm_t alg = alg_arg;
4209 size_t capacity = capacity_arg;
4210 psa_status_t expected_status = expected_status_arg;
4211 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4212
4213 PSA_ASSERT( psa_crypto_init( ) );
4214
4215 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4216
4217 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4218 expected_status );
4219
4220exit:
4221 psa_key_derivation_abort( &operation );
4222 PSA_DONE( );
4223}
4224/* END_CASE */
4225
4226/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004227void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004228 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004229 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004230 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004231 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004232 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004233 int expected_status_arg3,
4234 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004235{
4236 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004237 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4238 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004239 psa_status_t expected_statuses[] = {expected_status_arg1,
4240 expected_status_arg2,
4241 expected_status_arg3};
4242 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004243 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4244 MBEDTLS_SVC_KEY_ID_INIT,
4245 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004246 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4248 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004249 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004250 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004251 psa_status_t expected_output_status = expected_output_status_arg;
4252 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004253
4254 PSA_ASSERT( psa_crypto_init( ) );
4255
4256 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4257 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004258
4259 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4260
4261 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4262 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004263 mbedtls_test_set_step( i );
4264 if( steps[i] == 0 )
4265 {
4266 /* Skip this step */
4267 }
4268 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004269 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004270 psa_set_key_type( &attributes, key_types[i] );
4271 PSA_ASSERT( psa_import_key( &attributes,
4272 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004273 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004274 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4275 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4276 {
4277 // When taking a private key as secret input, use key agreement
4278 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004279 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4280 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004281 expected_statuses[i] );
4282 }
4283 else
4284 {
4285 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004286 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004287 expected_statuses[i] );
4288 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004289 }
4290 else
4291 {
4292 TEST_EQUAL( psa_key_derivation_input_bytes(
4293 &operation, steps[i],
4294 inputs[i]->x, inputs[i]->len ),
4295 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004296 }
4297 }
4298
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004299 if( output_key_type != PSA_KEY_TYPE_NONE )
4300 {
4301 psa_reset_key_attributes( &attributes );
4302 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4303 psa_set_key_bits( &attributes, 8 );
4304 actual_output_status =
4305 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004306 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004307 }
4308 else
4309 {
4310 uint8_t buffer[1];
4311 actual_output_status =
4312 psa_key_derivation_output_bytes( &operation,
4313 buffer, sizeof( buffer ) );
4314 }
4315 TEST_EQUAL( actual_output_status, expected_output_status );
4316
Janos Follathaf3c2a02019-06-12 12:34:34 +01004317exit:
4318 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004319 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4320 psa_destroy_key( keys[i] );
4321 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004322 PSA_DONE( );
4323}
4324/* END_CASE */
4325
Janos Follathd958bb72019-07-03 15:02:16 +01004326/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004327void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004328{
Janos Follathd958bb72019-07-03 15:02:16 +01004329 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004330 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004331 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004332 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004333 unsigned char input1[] = "Input 1";
4334 size_t input1_length = sizeof( input1 );
4335 unsigned char input2[] = "Input 2";
4336 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004337 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004338 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004339 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4340 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4341 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004343
Gilles Peskine8817f612018-12-18 00:18:46 +01004344 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004345
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4347 psa_set_key_algorithm( &attributes, alg );
4348 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004349
Gilles Peskine73676cb2019-05-15 20:15:10 +02004350 PSA_ASSERT( psa_import_key( &attributes,
4351 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004352 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004353
4354 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004355 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4356 input1, input1_length,
4357 input2, input2_length,
4358 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004359 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004360
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004361 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004362 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004363 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004364
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004365 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004366
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004367 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004368 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004369
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004370exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004371 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004372 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004373 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004374}
4375/* END_CASE */
4376
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004377/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004378void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004379{
4380 uint8_t output_buffer[16];
4381 size_t buffer_size = 16;
4382 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004383 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004384
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004385 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4386 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004387 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004388
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004389 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004390 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004391
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004392 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004393
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004394 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4395 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004396 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004397
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004398 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004399 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004400
4401exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004402 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004403}
4404/* END_CASE */
4405
4406/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004407void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004408 int step1_arg, data_t *input1,
4409 int step2_arg, data_t *input2,
4410 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004411 int requested_capacity_arg,
4412 data_t *expected_output1,
4413 data_t *expected_output2 )
4414{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004415 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004416 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4417 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004418 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4419 MBEDTLS_SVC_KEY_ID_INIT,
4420 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004421 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004423 uint8_t *expected_outputs[2] =
4424 {expected_output1->x, expected_output2->x};
4425 size_t output_sizes[2] =
4426 {expected_output1->len, expected_output2->len};
4427 size_t output_buffer_size = 0;
4428 uint8_t *output_buffer = NULL;
4429 size_t expected_capacity;
4430 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004432 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004433 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004434
4435 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4436 {
4437 if( output_sizes[i] > output_buffer_size )
4438 output_buffer_size = output_sizes[i];
4439 if( output_sizes[i] == 0 )
4440 expected_outputs[i] = NULL;
4441 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004442 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004443 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004444
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004445 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4446 psa_set_key_algorithm( &attributes, alg );
4447 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004448
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004449 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004450 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4451 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4452 requested_capacity ) );
4453 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004454 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004455 switch( steps[i] )
4456 {
4457 case 0:
4458 break;
4459 case PSA_KEY_DERIVATION_INPUT_SECRET:
4460 PSA_ASSERT( psa_import_key( &attributes,
4461 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004462 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004463
4464 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4465 {
4466 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4467 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4468 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4469 }
4470
Gilles Peskine1468da72019-05-29 17:35:49 +02004471 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004472 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004473 break;
4474 default:
4475 PSA_ASSERT( psa_key_derivation_input_bytes(
4476 &operation, steps[i],
4477 inputs[i]->x, inputs[i]->len ) );
4478 break;
4479 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004480 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004481
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004482 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004483 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004484 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004485 expected_capacity = requested_capacity;
4486
4487 /* Expansion phase. */
4488 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4489 {
4490 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004492 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004493 if( expected_capacity == 0 && output_sizes[i] == 0 )
4494 {
4495 /* Reading 0 bytes when 0 bytes are available can go either way. */
4496 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004497 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004498 continue;
4499 }
4500 else if( expected_capacity == 0 ||
4501 output_sizes[i] > expected_capacity )
4502 {
4503 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004504 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004505 expected_capacity = 0;
4506 continue;
4507 }
4508 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004509 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004510 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004511 ASSERT_COMPARE( output_buffer, output_sizes[i],
4512 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004513 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004514 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004515 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004516 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004517 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004518 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004519 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004520
4521exit:
4522 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004523 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004524 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4525 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004526 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004527}
4528/* END_CASE */
4529
4530/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004531void derive_full( int alg_arg,
4532 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004533 data_t *input1,
4534 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004535 int requested_capacity_arg )
4536{
Ronald Cron5425a212020-08-04 14:58:35 +02004537 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004538 psa_algorithm_t alg = alg_arg;
4539 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004540 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004541 unsigned char output_buffer[16];
4542 size_t expected_capacity = requested_capacity;
4543 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004545
Gilles Peskine8817f612018-12-18 00:18:46 +01004546 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004547
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004548 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4549 psa_set_key_algorithm( &attributes, alg );
4550 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004551
Gilles Peskine049c7532019-05-15 20:22:09 +02004552 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004553 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004554
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004555 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4556 input1->x, input1->len,
4557 input2->x, input2->len,
4558 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004559 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004560
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004561 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004562 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004563 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004564
4565 /* Expansion phase. */
4566 while( current_capacity > 0 )
4567 {
4568 size_t read_size = sizeof( output_buffer );
4569 if( read_size > current_capacity )
4570 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004571 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004572 output_buffer,
4573 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004574 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004575 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004576 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004577 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004578 }
4579
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004580 /* Check that the operation refuses to go over capacity. */
4581 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004582 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004583
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004584 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004585
4586exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004588 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004589 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004590}
4591/* END_CASE */
4592
Janos Follathe60c9052019-07-03 13:51:30 +01004593/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004594void derive_key_exercise( int alg_arg,
4595 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004596 data_t *input1,
4597 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004598 int derived_type_arg,
4599 int derived_bits_arg,
4600 int derived_usage_arg,
4601 int derived_alg_arg )
4602{
Ronald Cron5425a212020-08-04 14:58:35 +02004603 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4604 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004605 psa_algorithm_t alg = alg_arg;
4606 psa_key_type_t derived_type = derived_type_arg;
4607 size_t derived_bits = derived_bits_arg;
4608 psa_key_usage_t derived_usage = derived_usage_arg;
4609 psa_algorithm_t derived_alg = derived_alg_arg;
4610 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004611 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004613 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004614
Gilles Peskine8817f612018-12-18 00:18:46 +01004615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004616
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4618 psa_set_key_algorithm( &attributes, alg );
4619 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004620 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004621 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004622
4623 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004624 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4625 input1->x, input1->len,
4626 input2->x, input2->len,
4627 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004628 goto exit;
4629
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004630 psa_set_key_usage_flags( &attributes, derived_usage );
4631 psa_set_key_algorithm( &attributes, derived_alg );
4632 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004633 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004634 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004635 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004636
4637 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004638 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004639 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4640 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004641
4642 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004643 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004644 goto exit;
4645
4646exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004647 /*
4648 * Key attributes may have been returned by psa_get_key_attributes()
4649 * thus reset them as required.
4650 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004651 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004652
4653 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004654 psa_destroy_key( base_key );
4655 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004656 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004657}
4658/* END_CASE */
4659
Janos Follath42fd8882019-07-03 14:17:09 +01004660/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004661void derive_key_export( int alg_arg,
4662 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004663 data_t *input1,
4664 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004665 int bytes1_arg,
4666 int bytes2_arg )
4667{
Ronald Cron5425a212020-08-04 14:58:35 +02004668 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4669 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004670 psa_algorithm_t alg = alg_arg;
4671 size_t bytes1 = bytes1_arg;
4672 size_t bytes2 = bytes2_arg;
4673 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004675 uint8_t *output_buffer = NULL;
4676 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004677 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4678 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004679 size_t length;
4680
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004681 ASSERT_ALLOC( output_buffer, capacity );
4682 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004683 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004684
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004685 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4686 psa_set_key_algorithm( &base_attributes, alg );
4687 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004688 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004689 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004690
4691 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004692 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4693 input1->x, input1->len,
4694 input2->x, input2->len,
4695 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004696 goto exit;
4697
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004698 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004699 output_buffer,
4700 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004701 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004702
4703 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004704 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4705 input1->x, input1->len,
4706 input2->x, input2->len,
4707 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004708 goto exit;
4709
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004710 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4711 psa_set_key_algorithm( &derived_attributes, 0 );
4712 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004713 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004715 &derived_key ) );
4716 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004717 export_buffer, bytes1,
4718 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004719 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004720 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004721 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004723 &derived_key ) );
4724 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004725 export_buffer + bytes1, bytes2,
4726 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004727 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004728
4729 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004730 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4731 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004732
4733exit:
4734 mbedtls_free( output_buffer );
4735 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004737 psa_destroy_key( base_key );
4738 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004739 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004740}
4741/* END_CASE */
4742
4743/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004744void derive_key( int alg_arg,
4745 data_t *key_data, data_t *input1, data_t *input2,
4746 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004747 int expected_status_arg,
4748 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004749{
Ronald Cron5425a212020-08-04 14:58:35 +02004750 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4751 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004752 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004753 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004754 size_t bits = bits_arg;
4755 psa_status_t expected_status = expected_status_arg;
4756 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4757 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4758 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4759
4760 PSA_ASSERT( psa_crypto_init( ) );
4761
4762 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4763 psa_set_key_algorithm( &base_attributes, alg );
4764 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4765 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004766 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004767
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004768 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4769 input1->x, input1->len,
4770 input2->x, input2->len,
4771 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004772 goto exit;
4773
4774 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4775 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004776 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004777 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004778
4779 psa_status_t status =
4780 psa_key_derivation_output_key( &derived_attributes,
4781 &operation,
4782 &derived_key );
4783 if( is_large_output > 0 )
4784 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4785 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004786
4787exit:
4788 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004789 psa_destroy_key( base_key );
4790 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004791 PSA_DONE( );
4792}
4793/* END_CASE */
4794
4795/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004796void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004797 int our_key_type_arg, int our_key_alg_arg,
4798 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004799 int expected_status_arg )
4800{
Ronald Cron5425a212020-08-04 14:58:35 +02004801 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004802 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004803 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004804 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004807 psa_status_t expected_status = expected_status_arg;
4808 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004809
Gilles Peskine8817f612018-12-18 00:18:46 +01004810 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004811
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004812 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004813 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004814 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004815 PSA_ASSERT( psa_import_key( &attributes,
4816 our_key_data->x, our_key_data->len,
4817 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004818
Gilles Peskine77f40d82019-04-11 21:27:06 +02004819 /* The tests currently include inputs that should fail at either step.
4820 * Test cases that fail at the setup step should be changed to call
4821 * key_derivation_setup instead, and this function should be renamed
4822 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004823 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004824 if( status == PSA_SUCCESS )
4825 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004826 TEST_EQUAL( psa_key_derivation_key_agreement(
4827 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4828 our_key,
4829 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004830 expected_status );
4831 }
4832 else
4833 {
4834 TEST_ASSERT( status == expected_status );
4835 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004836
4837exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004838 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004839 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004840 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004841}
4842/* END_CASE */
4843
4844/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004845void raw_key_agreement( int alg_arg,
4846 int our_key_type_arg, data_t *our_key_data,
4847 data_t *peer_key_data,
4848 data_t *expected_output )
4849{
Ronald Cron5425a212020-08-04 14:58:35 +02004850 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004851 psa_algorithm_t alg = alg_arg;
4852 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004854 unsigned char *output = NULL;
4855 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004856 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004857
4858 ASSERT_ALLOC( output, expected_output->len );
4859 PSA_ASSERT( psa_crypto_init( ) );
4860
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004861 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4862 psa_set_key_algorithm( &attributes, alg );
4863 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004864 PSA_ASSERT( psa_import_key( &attributes,
4865 our_key_data->x, our_key_data->len,
4866 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004867
gabor-mezei-armceface22021-01-21 12:26:17 +01004868 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4869 key_bits = psa_get_key_bits( &attributes );
4870
Gilles Peskinebe697d82019-05-16 18:00:41 +02004871 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4872 peer_key_data->x, peer_key_data->len,
4873 output, expected_output->len,
4874 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004875 ASSERT_COMPARE( output, output_length,
4876 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004877 TEST_ASSERT( output_length <=
4878 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4879 TEST_ASSERT( output_length <=
4880 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004881
4882exit:
4883 mbedtls_free( output );
4884 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004885 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004886}
4887/* END_CASE */
4888
4889/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004890void key_agreement_capacity( int alg_arg,
4891 int our_key_type_arg, data_t *our_key_data,
4892 data_t *peer_key_data,
4893 int expected_capacity_arg )
4894{
Ronald Cron5425a212020-08-04 14:58:35 +02004895 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004896 psa_algorithm_t alg = alg_arg;
4897 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004899 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004900 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004901 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004902
Gilles Peskine8817f612018-12-18 00:18:46 +01004903 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004904
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004905 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4906 psa_set_key_algorithm( &attributes, alg );
4907 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004908 PSA_ASSERT( psa_import_key( &attributes,
4909 our_key_data->x, our_key_data->len,
4910 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004911
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004913 PSA_ASSERT( psa_key_derivation_key_agreement(
4914 &operation,
4915 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4916 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004917 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4918 {
4919 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004921 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004922 NULL, 0 ) );
4923 }
Gilles Peskine59685592018-09-18 12:11:34 +02004924
Gilles Peskinebf491972018-10-25 22:36:12 +02004925 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004926 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004927 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004928 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004929
Gilles Peskinebf491972018-10-25 22:36:12 +02004930 /* Test the actual capacity by reading the output. */
4931 while( actual_capacity > sizeof( output ) )
4932 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004933 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004934 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004935 actual_capacity -= sizeof( output );
4936 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004937 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004938 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004939 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004940 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004941
Gilles Peskine59685592018-09-18 12:11:34 +02004942exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004943 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004944 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004945 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004946}
4947/* END_CASE */
4948
4949/* BEGIN_CASE */
4950void key_agreement_output( int alg_arg,
4951 int our_key_type_arg, data_t *our_key_data,
4952 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004953 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004954{
Ronald Cron5425a212020-08-04 14:58:35 +02004955 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004956 psa_algorithm_t alg = alg_arg;
4957 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004960 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004961
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004962 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4963 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004964
Gilles Peskine8817f612018-12-18 00:18:46 +01004965 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004966
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004967 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4968 psa_set_key_algorithm( &attributes, alg );
4969 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004970 PSA_ASSERT( psa_import_key( &attributes,
4971 our_key_data->x, our_key_data->len,
4972 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004973
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004974 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004975 PSA_ASSERT( psa_key_derivation_key_agreement(
4976 &operation,
4977 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4978 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004979 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4980 {
4981 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004983 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004984 NULL, 0 ) );
4985 }
Gilles Peskine59685592018-09-18 12:11:34 +02004986
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004987 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004988 actual_output,
4989 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004990 ASSERT_COMPARE( actual_output, expected_output1->len,
4991 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004992 if( expected_output2->len != 0 )
4993 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004994 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004995 actual_output,
4996 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004997 ASSERT_COMPARE( actual_output, expected_output2->len,
4998 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004999 }
Gilles Peskine59685592018-09-18 12:11:34 +02005000
5001exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005002 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005003 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005004 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005005 mbedtls_free( actual_output );
5006}
5007/* END_CASE */
5008
5009/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005010void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005011{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005012 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005013 unsigned char *output = NULL;
5014 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005015 size_t i;
5016 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005017
Simon Butcher49f8e312020-03-03 15:51:50 +00005018 TEST_ASSERT( bytes_arg >= 0 );
5019
Gilles Peskine91892022021-02-08 19:50:26 +01005020 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005021 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005022
Gilles Peskine8817f612018-12-18 00:18:46 +01005023 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005024
Gilles Peskinea50d7392018-06-21 10:22:13 +02005025 /* Run several times, to ensure that every output byte will be
5026 * nonzero at least once with overwhelming probability
5027 * (2^(-8*number_of_runs)). */
5028 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005029 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005030 if( bytes != 0 )
5031 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005032 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005033
Gilles Peskinea50d7392018-06-21 10:22:13 +02005034 for( i = 0; i < bytes; i++ )
5035 {
5036 if( output[i] != 0 )
5037 ++changed[i];
5038 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005039 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005040
5041 /* Check that every byte was changed to nonzero at least once. This
5042 * validates that psa_generate_random is overwriting every byte of
5043 * the output buffer. */
5044 for( i = 0; i < bytes; i++ )
5045 {
5046 TEST_ASSERT( changed[i] != 0 );
5047 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005048
5049exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005050 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005051 mbedtls_free( output );
5052 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005053}
5054/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005055
5056/* BEGIN_CASE */
5057void generate_key( int type_arg,
5058 int bits_arg,
5059 int usage_arg,
5060 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005061 int expected_status_arg,
5062 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005063{
Ronald Cron5425a212020-08-04 14:58:35 +02005064 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005065 psa_key_type_t type = type_arg;
5066 psa_key_usage_t usage = usage_arg;
5067 size_t bits = bits_arg;
5068 psa_algorithm_t alg = alg_arg;
5069 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005071 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005072
Gilles Peskine8817f612018-12-18 00:18:46 +01005073 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005074
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005075 psa_set_key_usage_flags( &attributes, usage );
5076 psa_set_key_algorithm( &attributes, alg );
5077 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005078 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005079
5080 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005081 psa_status_t status = psa_generate_key( &attributes, &key );
5082
5083 if( is_large_key > 0 )
5084 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5085 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005086 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005087 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005088
5089 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005090 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005091 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5092 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005093
Gilles Peskine818ca122018-06-20 18:16:48 +02005094 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005095 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005096 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005097
5098exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005099 /*
5100 * Key attributes may have been returned by psa_get_key_attributes()
5101 * thus reset them as required.
5102 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005103 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005104
Ronald Cron5425a212020-08-04 14:58:35 +02005105 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005106 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005107}
5108/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005109
Ronald Cronee414c72021-03-18 18:50:08 +01005110/* 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 +02005111void generate_key_rsa( int bits_arg,
5112 data_t *e_arg,
5113 int expected_status_arg )
5114{
Ronald Cron5425a212020-08-04 14:58:35 +02005115 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005116 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005117 size_t bits = bits_arg;
5118 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5119 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5120 psa_status_t expected_status = expected_status_arg;
5121 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5122 uint8_t *exported = NULL;
5123 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005124 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005125 size_t exported_length = SIZE_MAX;
5126 uint8_t *e_read_buffer = NULL;
5127 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005128 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005129 size_t e_read_length = SIZE_MAX;
5130
5131 if( e_arg->len == 0 ||
5132 ( e_arg->len == 3 &&
5133 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5134 {
5135 is_default_public_exponent = 1;
5136 e_read_size = 0;
5137 }
5138 ASSERT_ALLOC( e_read_buffer, e_read_size );
5139 ASSERT_ALLOC( exported, exported_size );
5140
5141 PSA_ASSERT( psa_crypto_init( ) );
5142
5143 psa_set_key_usage_flags( &attributes, usage );
5144 psa_set_key_algorithm( &attributes, alg );
5145 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5146 e_arg->x, e_arg->len ) );
5147 psa_set_key_bits( &attributes, bits );
5148
5149 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005150 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005151 if( expected_status != PSA_SUCCESS )
5152 goto exit;
5153
5154 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005155 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005156 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5157 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5158 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5159 e_read_buffer, e_read_size,
5160 &e_read_length ) );
5161 if( is_default_public_exponent )
5162 TEST_EQUAL( e_read_length, 0 );
5163 else
5164 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5165
5166 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005167 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005168 goto exit;
5169
5170 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005171 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005172 exported, exported_size,
5173 &exported_length ) );
5174 {
5175 uint8_t *p = exported;
5176 uint8_t *end = exported + exported_length;
5177 size_t len;
5178 /* RSAPublicKey ::= SEQUENCE {
5179 * modulus INTEGER, -- n
5180 * publicExponent INTEGER } -- e
5181 */
5182 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005183 MBEDTLS_ASN1_SEQUENCE |
5184 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005185 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005186 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5187 MBEDTLS_ASN1_INTEGER ) );
5188 if( len >= 1 && p[0] == 0 )
5189 {
5190 ++p;
5191 --len;
5192 }
5193 if( e_arg->len == 0 )
5194 {
5195 TEST_EQUAL( len, 3 );
5196 TEST_EQUAL( p[0], 1 );
5197 TEST_EQUAL( p[1], 0 );
5198 TEST_EQUAL( p[2], 1 );
5199 }
5200 else
5201 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5202 }
5203
5204exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005205 /*
5206 * Key attributes may have been returned by psa_get_key_attributes() or
5207 * set by psa_set_key_domain_parameters() thus reset them as required.
5208 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005209 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005210
Ronald Cron5425a212020-08-04 14:58:35 +02005211 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005212 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005213 mbedtls_free( e_read_buffer );
5214 mbedtls_free( exported );
5215}
5216/* END_CASE */
5217
Darryl Greend49a4992018-06-18 17:27:26 +01005218/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005219void persistent_key_load_key_from_storage( data_t *data,
5220 int type_arg, int bits_arg,
5221 int usage_flags_arg, int alg_arg,
5222 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005223{
Ronald Cron71016a92020-08-28 19:01:50 +02005224 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005225 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005226 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5227 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005228 psa_key_type_t type = type_arg;
5229 size_t bits = bits_arg;
5230 psa_key_usage_t usage_flags = usage_flags_arg;
5231 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005232 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005233 unsigned char *first_export = NULL;
5234 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005235 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005236 size_t first_exported_length;
5237 size_t second_exported_length;
5238
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005239 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5240 {
5241 ASSERT_ALLOC( first_export, export_size );
5242 ASSERT_ALLOC( second_export, export_size );
5243 }
Darryl Greend49a4992018-06-18 17:27:26 +01005244
Gilles Peskine8817f612018-12-18 00:18:46 +01005245 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005246
Gilles Peskinec87af662019-05-15 16:12:22 +02005247 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005248 psa_set_key_usage_flags( &attributes, usage_flags );
5249 psa_set_key_algorithm( &attributes, alg );
5250 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005251 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005252
Darryl Green0c6575a2018-11-07 16:05:30 +00005253 switch( generation_method )
5254 {
5255 case IMPORT_KEY:
5256 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005257 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005258 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005259 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005260
Darryl Green0c6575a2018-11-07 16:05:30 +00005261 case GENERATE_KEY:
5262 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005263 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005264 break;
5265
5266 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005267#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005268 {
5269 /* Create base key */
5270 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5271 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5272 psa_set_key_usage_flags( &base_attributes,
5273 PSA_KEY_USAGE_DERIVE );
5274 psa_set_key_algorithm( &base_attributes, derive_alg );
5275 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005276 PSA_ASSERT( psa_import_key( &base_attributes,
5277 data->x, data->len,
5278 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005279 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005281 PSA_ASSERT( psa_key_derivation_input_key(
5282 &operation,
5283 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005284 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005285 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005286 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005287 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5288 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005289 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005291 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005292 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005293 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005294#else
5295 TEST_ASSUME( ! "KDF not supported in this configuration" );
5296#endif
5297 break;
5298
5299 default:
5300 TEST_ASSERT( ! "generation_method not implemented in test" );
5301 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005302 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005303 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005304
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005305 /* Export the key if permitted by the key policy. */
5306 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5307 {
Ronald Cron5425a212020-08-04 14:58:35 +02005308 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005309 first_export, export_size,
5310 &first_exported_length ) );
5311 if( generation_method == IMPORT_KEY )
5312 ASSERT_COMPARE( data->x, data->len,
5313 first_export, first_exported_length );
5314 }
Darryl Greend49a4992018-06-18 17:27:26 +01005315
5316 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005317 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005318 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005319 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005320
Darryl Greend49a4992018-06-18 17:27:26 +01005321 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005322 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005323 TEST_ASSERT( mbedtls_svc_key_id_equal(
5324 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005325 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5326 PSA_KEY_LIFETIME_PERSISTENT );
5327 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5328 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5329 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5330 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005331
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005332 /* Export the key again if permitted by the key policy. */
5333 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005334 {
Ronald Cron5425a212020-08-04 14:58:35 +02005335 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005336 second_export, export_size,
5337 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005338 ASSERT_COMPARE( first_export, first_exported_length,
5339 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005340 }
5341
5342 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005343 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005344 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005345
5346exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005347 /*
5348 * Key attributes may have been returned by psa_get_key_attributes()
5349 * thus reset them as required.
5350 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005351 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005352
Darryl Greend49a4992018-06-18 17:27:26 +01005353 mbedtls_free( first_export );
5354 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005355 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005356 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005357 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005358 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005359}
5360/* END_CASE */