blob: 21985e976c02cd2182475d1ceaf2511ff3a4eaa7 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022/** Test if a buffer contains a constant byte value.
23 *
24 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020025 *
26 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020027 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 * \param size Size of the buffer in bytes.
29 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 * \return 1 if the buffer is all-bits-zero.
31 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020034{
35 size_t i;
36 for( i = 0; i < size; i++ )
37 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020038 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020039 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020041 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042}
Gilles Peskine818ca122018-06-20 18:16:48 +020043
Gilles Peskine0b352bc2018-06-28 00:16:11 +020044/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
45static int asn1_write_10x( unsigned char **p,
46 unsigned char *start,
47 size_t bits,
48 unsigned char x )
49{
50 int ret;
51 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020052 if( bits == 0 )
53 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
54 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020055 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030056 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
58 *p -= len;
59 ( *p )[len-1] = x;
60 if( bits % 8 == 0 )
61 ( *p )[1] |= 1;
62 else
63 ( *p )[0] |= 1 << ( bits % 8 );
64 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
65 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
66 MBEDTLS_ASN1_INTEGER ) );
67 return( len );
68}
69
70static int construct_fake_rsa_key( unsigned char *buffer,
71 size_t buffer_size,
72 unsigned char **p,
73 size_t bits,
74 int keypair )
75{
76 size_t half_bits = ( bits + 1 ) / 2;
77 int ret;
78 int len = 0;
79 /* Construct something that looks like a DER encoding of
80 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
81 * RSAPrivateKey ::= SEQUENCE {
82 * version Version,
83 * modulus INTEGER, -- n
84 * publicExponent INTEGER, -- e
85 * privateExponent INTEGER, -- d
86 * prime1 INTEGER, -- p
87 * prime2 INTEGER, -- q
88 * exponent1 INTEGER, -- d mod (p-1)
89 * exponent2 INTEGER, -- d mod (q-1)
90 * coefficient INTEGER, -- (inverse of q) mod p
91 * otherPrimeInfos OtherPrimeInfos OPTIONAL
92 * }
93 * Or, for a public key, the same structure with only
94 * version, modulus and publicExponent.
95 */
96 *p = buffer + buffer_size;
97 if( keypair )
98 {
99 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
100 asn1_write_10x( p, buffer, half_bits, 1 ) );
101 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* q */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
108 asn1_write_10x( p, buffer, half_bits, 3 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* d */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 }
112 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
113 asn1_write_10x( p, buffer, 17, 1 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, /* n */
115 asn1_write_10x( p, buffer, bits, 1 ) );
116 if( keypair )
117 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
118 mbedtls_asn1_write_int( p, buffer, 0 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
120 {
121 const unsigned char tag =
122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
124 }
125 return( len );
126}
127
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100128int exercise_mac_setup( psa_key_type_t key_type,
129 const unsigned char *key_bytes,
130 size_t key_length,
131 psa_algorithm_t alg,
132 psa_mac_operation_t *operation,
133 psa_status_t *status )
134{
Ronald Cron5425a212020-08-04 14:58:35 +0200135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100137
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100138 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200139 psa_set_key_algorithm( &attributes, alg );
140 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200141 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142
Ronald Cron5425a212020-08-04 14:58:35 +0200143 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100144 /* Whether setup succeeded or failed, abort must succeed. */
145 PSA_ASSERT( psa_mac_abort( operation ) );
146 /* If setup failed, reproduce the failure, so that the caller can
147 * test the resulting state of the operation object. */
148 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100149 {
Ronald Cron5425a212020-08-04 14:58:35 +0200150 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151 }
152
Ronald Cron5425a212020-08-04 14:58:35 +0200153 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100154 return( 1 );
155
156exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200157 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158 return( 0 );
159}
160
161int exercise_cipher_setup( psa_key_type_t key_type,
162 const unsigned char *key_bytes,
163 size_t key_length,
164 psa_algorithm_t alg,
165 psa_cipher_operation_t *operation,
166 psa_status_t *status )
167{
Ronald Cron5425a212020-08-04 14:58:35 +0200168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
172 psa_set_key_algorithm( &attributes, alg );
173 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200174 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100175
Ronald Cron5425a212020-08-04 14:58:35 +0200176 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100177 /* Whether setup succeeded or failed, abort must succeed. */
178 PSA_ASSERT( psa_cipher_abort( operation ) );
179 /* If setup failed, reproduce the failure, so that the caller can
180 * test the resulting state of the operation object. */
181 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182 {
Ronald Cron5425a212020-08-04 14:58:35 +0200183 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100184 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185 }
186
Ronald Cron5425a212020-08-04 14:58:35 +0200187 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 return( 1 );
189
190exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200191 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100192 return( 0 );
193}
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200196{
197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200198 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200199 uint8_t buffer[1];
200 size_t length;
201 int ok = 0;
202
Ronald Cronecfb2372020-07-23 17:13:42 +0200203 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
205 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
206 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200207 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000208 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200209 TEST_EQUAL(
210 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
211 TEST_EQUAL(
212 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200213 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
215 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
216 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
217 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
218
Ronald Cron5425a212020-08-04 14:58:35 +0200219 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000220 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200224
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200225 ok = 1;
226
227exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100228 /*
229 * Key attributes may have been returned by psa_get_key_attributes()
230 * thus reset them as required.
231 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100233
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200234 return( ok );
235}
236
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200237/* Assert that a key isn't reported as having a slot number. */
238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
239#define ASSERT_NO_SLOT_NUMBER( attributes ) \
240 do \
241 { \
242 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
243 TEST_EQUAL( psa_get_key_slot_number( \
244 attributes, \
245 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
246 PSA_ERROR_INVALID_ARGUMENT ); \
247 } \
248 while( 0 )
249#else /* MBEDTLS_PSA_CRYPTO_SE_C */
250#define ASSERT_NO_SLOT_NUMBER( attributes ) \
251 ( (void) 0 )
252#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
253
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100254/* An overapproximation of the amount of storage needed for a key of the
255 * given type and with the given content. The API doesn't make it easy
256 * to find a good value for the size. The current implementation doesn't
257 * care about the value anyway. */
258#define KEY_BITS_FROM_DATA( type, data ) \
259 ( data )->len
260
Darryl Green0c6575a2018-11-07 16:05:30 +0000261typedef enum {
262 IMPORT_KEY = 0,
263 GENERATE_KEY = 1,
264 DERIVE_KEY = 2
265} generate_method;
266
Gilles Peskinee59236f2018-01-27 23:32:46 +0100267/* END_HEADER */
268
269/* BEGIN_DEPENDENCIES
270 * depends_on:MBEDTLS_PSA_CRYPTO_C
271 * END_DEPENDENCIES
272 */
273
274/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200275void static_checks( )
276{
277 size_t max_truncated_mac_size =
278 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
279
280 /* Check that the length for a truncated MAC always fits in the algorithm
281 * encoding. The shifted mask is the maximum truncated value. The
282 * untruncated algorithm may be one byte larger. */
283 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100284
285#if defined(MBEDTLS_TEST_DEPRECATED)
286 /* Check deprecated constants. */
287 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
288 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
289 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
290 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
291 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
292 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
293 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
294 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100295
Paul Elliott8ff510a2020-06-02 17:19:28 +0100296 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
297 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
298 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
299 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
300 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
301 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
302 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
303 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
304 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
321 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
323 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
324 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
325 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
326
327 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
328 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
329 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
330 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
331 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
333 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
334 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100335
Paul Elliott75e27032020-06-03 15:17:39 +0100336 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
337 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
338 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
339 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
340 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
341
342 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
343 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100344#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200345}
346/* END_CASE */
347
348/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200349void import_with_policy( int type_arg,
350 int usage_arg, int alg_arg,
351 int expected_status_arg )
352{
353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
354 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200356 psa_key_type_t type = type_arg;
357 psa_key_usage_t usage = usage_arg;
358 psa_algorithm_t alg = alg_arg;
359 psa_status_t expected_status = expected_status_arg;
360 const uint8_t key_material[16] = {0};
361 psa_status_t status;
362
363 PSA_ASSERT( psa_crypto_init( ) );
364
365 psa_set_key_type( &attributes, type );
366 psa_set_key_usage_flags( &attributes, usage );
367 psa_set_key_algorithm( &attributes, alg );
368
369 status = psa_import_key( &attributes,
370 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200371 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200372 TEST_EQUAL( status, expected_status );
373 if( status != PSA_SUCCESS )
374 goto exit;
375
Ronald Cron5425a212020-08-04 14:58:35 +0200376 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200377 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
378 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
379 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200380 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200381
Ronald Cron5425a212020-08-04 14:58:35 +0200382 PSA_ASSERT( psa_destroy_key( key ) );
383 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200384
385exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100386 /*
387 * Key attributes may have been returned by psa_get_key_attributes()
388 * thus reset them as required.
389 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200390 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100391
392 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200393 PSA_DONE( );
394}
395/* END_CASE */
396
397/* BEGIN_CASE */
398void import_with_data( data_t *data, int type_arg,
399 int attr_bits_arg,
400 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200401{
402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
403 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200404 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200405 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200406 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200407 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100408 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409
Gilles Peskine8817f612018-12-18 00:18:46 +0100410 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100411
Gilles Peskine4747d192019-04-17 15:05:45 +0200412 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200413 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200414
Ronald Cron5425a212020-08-04 14:58:35 +0200415 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100416 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200417 if( status != PSA_SUCCESS )
418 goto exit;
419
Ronald Cron5425a212020-08-04 14:58:35 +0200420 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200421 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200422 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200423 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200424 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200425
Ronald Cron5425a212020-08-04 14:58:35 +0200426 PSA_ASSERT( psa_destroy_key( key ) );
427 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100428
429exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100430 /*
431 * Key attributes may have been returned by psa_get_key_attributes()
432 * thus reset them as required.
433 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200434 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100435
436 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200437 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100438}
439/* END_CASE */
440
441/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200442void import_large_key( int type_arg, int byte_size_arg,
443 int expected_status_arg )
444{
445 psa_key_type_t type = type_arg;
446 size_t byte_size = byte_size_arg;
447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
448 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200449 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200450 psa_status_t status;
451 uint8_t *buffer = NULL;
452 size_t buffer_size = byte_size + 1;
453 size_t n;
454
Steven Cooreman69967ce2021-01-18 18:01:08 +0100455 /* Skip the test case if the target running the test cannot
456 * accomodate large keys due to heap size constraints */
457 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200458 memset( buffer, 'K', byte_size );
459
460 PSA_ASSERT( psa_crypto_init( ) );
461
462 /* Try importing the key */
463 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
464 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200465 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100466 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200467 TEST_EQUAL( status, expected_status );
468
469 if( status == PSA_SUCCESS )
470 {
Ronald Cron5425a212020-08-04 14:58:35 +0200471 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200472 TEST_EQUAL( psa_get_key_type( &attributes ), type );
473 TEST_EQUAL( psa_get_key_bits( &attributes ),
474 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200475 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200476 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200477 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200478 for( n = 0; n < byte_size; n++ )
479 TEST_EQUAL( buffer[n], 'K' );
480 for( n = byte_size; n < buffer_size; n++ )
481 TEST_EQUAL( buffer[n], 0 );
482 }
483
484exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100485 /*
486 * Key attributes may have been returned by psa_get_key_attributes()
487 * thus reset them as required.
488 */
489 psa_reset_key_attributes( &attributes );
490
Ronald Cron5425a212020-08-04 14:58:35 +0200491 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200492 PSA_DONE( );
493 mbedtls_free( buffer );
494}
495/* END_CASE */
496
497/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200498void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
499{
Ronald Cron5425a212020-08-04 14:58:35 +0200500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200501 size_t bits = bits_arg;
502 psa_status_t expected_status = expected_status_arg;
503 psa_status_t status;
504 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200505 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200506 size_t buffer_size = /* Slight overapproximations */
507 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200508 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200509 unsigned char *p;
510 int ret;
511 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200513
Gilles Peskine8817f612018-12-18 00:18:46 +0100514 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200515 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200516
517 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
518 bits, keypair ) ) >= 0 );
519 length = ret;
520
521 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200522 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200523 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100524 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200525
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200526 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200527 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200528
529exit:
530 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200531 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200532}
533/* END_CASE */
534
535/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300536void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300537 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200538 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539 int expected_bits,
540 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200541 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 int canonical_input )
543{
Ronald Cron5425a212020-08-04 14:58:35 +0200544 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200546 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200547 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 unsigned char *exported = NULL;
550 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100552 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200555 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556
Moran Pekercb088e72018-07-17 17:36:59 +0300557 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200558 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200560 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100561 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562
Gilles Peskine4747d192019-04-17 15:05:45 +0200563 psa_set_key_usage_flags( &attributes, usage_arg );
564 psa_set_key_algorithm( &attributes, alg );
565 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700566
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200568 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569
570 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200572 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
573 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200574 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575
576 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200577 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100578 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100579
580 /* The exported length must be set by psa_export_key() to a value between 0
581 * and export_size. On errors, the exported length must be 0. */
582 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
583 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
584 TEST_ASSERT( exported_length <= export_size );
585
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200586 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200587 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100588 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200589 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100590 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200592 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100593
Gilles Peskineea38a922021-02-13 00:05:16 +0100594 /* Run sanity checks on the exported key. For non-canonical inputs,
595 * this validates the canonical representations. For canonical inputs,
596 * this doesn't directly validate the implementation, but it still helps
597 * by cross-validating the test data with the sanity check code. */
598 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200599 goto exit;
600
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200602 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603 else
604 {
Ronald Cron5425a212020-08-04 14:58:35 +0200605 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200606 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200607 &key2 ) );
608 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100609 reexported,
610 export_size,
611 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200612 ASSERT_COMPARE( exported, exported_length,
613 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200614 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100615 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100616 TEST_ASSERT( exported_length <=
617 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
618 psa_get_key_bits( &got_attributes ) ) );
619 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100620
621destroy:
622 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200623 PSA_ASSERT( psa_destroy_key( key ) );
624 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625
626exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100627 /*
628 * Key attributes may have been returned by psa_get_key_attributes()
629 * thus reset them as required.
630 */
631 psa_reset_key_attributes( &got_attributes );
632
itayzafrir3e02b3b2018-06-12 17:06:52 +0300633 mbedtls_free( exported );
634 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200635 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636}
637/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100638
Moran Pekerf709f4a2018-06-06 17:26:04 +0300639/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300640void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200641 int type_arg,
642 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100643 int export_size_delta,
644 int expected_export_status_arg,
645 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300646{
Ronald Cron5425a212020-08-04 14:58:35 +0200647 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300648 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200649 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200650 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300651 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300652 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100653 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100654 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200655 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300656
Gilles Peskine8817f612018-12-18 00:18:46 +0100657 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300658
Gilles Peskine4747d192019-04-17 15:05:45 +0200659 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
660 psa_set_key_algorithm( &attributes, alg );
661 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662
663 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200664 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300665
Gilles Peskine49c25912018-10-29 15:15:31 +0100666 /* Export the public key */
667 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200668 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200669 exported, export_size,
670 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100671 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100672 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100673 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200674 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100675 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200676 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200677 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100678 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100679 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100680 TEST_ASSERT( expected_public_key->len <=
681 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
682 TEST_ASSERT( expected_public_key->len <=
683 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100684 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
685 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100686 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300687
688exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100689 /*
690 * Key attributes may have been returned by psa_get_key_attributes()
691 * thus reset them as required.
692 */
693 psa_reset_key_attributes( &attributes );
694
itayzafrir3e02b3b2018-06-12 17:06:52 +0300695 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200696 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200697 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300698}
699/* END_CASE */
700
Gilles Peskine20035e32018-02-03 22:44:14 +0100701/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200702void import_and_exercise_key( data_t *data,
703 int type_arg,
704 int bits_arg,
705 int alg_arg )
706{
Ronald Cron5425a212020-08-04 14:58:35 +0200707 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200708 psa_key_type_t type = type_arg;
709 size_t bits = bits_arg;
710 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100711 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200713 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200714
Gilles Peskine8817f612018-12-18 00:18:46 +0100715 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200716
Gilles Peskine4747d192019-04-17 15:05:45 +0200717 psa_set_key_usage_flags( &attributes, usage );
718 psa_set_key_algorithm( &attributes, alg );
719 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200720
721 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200722 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200723
724 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200725 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200726 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
727 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200728
729 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100730 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200731 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200732
Ronald Cron5425a212020-08-04 14:58:35 +0200733 PSA_ASSERT( psa_destroy_key( key ) );
734 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200735
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200736exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100737 /*
738 * Key attributes may have been returned by psa_get_key_attributes()
739 * thus reset them as required.
740 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200741 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100742
743 psa_reset_key_attributes( &attributes );
744 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200745 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200746}
747/* END_CASE */
748
749/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100750void effective_key_attributes( int type_arg, int expected_type_arg,
751 int bits_arg, int expected_bits_arg,
752 int usage_arg, int expected_usage_arg,
753 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200754{
Ronald Cron5425a212020-08-04 14:58:35 +0200755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100756 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100757 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100758 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100759 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200760 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100761 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200762 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100763 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200765
Gilles Peskine8817f612018-12-18 00:18:46 +0100766 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200767
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200768 psa_set_key_usage_flags( &attributes, usage );
769 psa_set_key_algorithm( &attributes, alg );
770 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100771 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200772
Ronald Cron5425a212020-08-04 14:58:35 +0200773 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100774 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200775
Ronald Cron5425a212020-08-04 14:58:35 +0200776 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100777 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
778 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
779 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
780 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200781
782exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100783 /*
784 * Key attributes may have been returned by psa_get_key_attributes()
785 * thus reset them as required.
786 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200787 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100788
789 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200790 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200791}
792/* END_CASE */
793
794/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100795void check_key_policy( int type_arg, int bits_arg,
796 int usage_arg, int alg_arg )
797{
798 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
799 usage_arg, usage_arg, alg_arg, alg_arg );
800 goto exit;
801}
802/* END_CASE */
803
804/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200805void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000806{
807 /* Test each valid way of initializing the object, except for `= {0}`, as
808 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
809 * though it's OK by the C standard. We could test for this, but we'd need
810 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200811 psa_key_attributes_t func = psa_key_attributes_init( );
812 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
813 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000814
815 memset( &zero, 0, sizeof( zero ) );
816
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200817 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
818 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
819 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000820
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200821 TEST_EQUAL( psa_get_key_type( &func ), 0 );
822 TEST_EQUAL( psa_get_key_type( &init ), 0 );
823 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
824
825 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
826 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
827 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
828
829 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
830 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
831 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
832
833 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
834 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
835 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000836}
837/* END_CASE */
838
839/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200840void mac_key_policy( int policy_usage,
841 int policy_alg,
842 int key_type,
843 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100844 int exercise_alg,
845 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200846{
Ronald Cron5425a212020-08-04 14:58:35 +0200847 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200848 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000849 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200850 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100851 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200852 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200853
Gilles Peskine8817f612018-12-18 00:18:46 +0100854 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200855
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200856 psa_set_key_usage_flags( &attributes, policy_usage );
857 psa_set_key_algorithm( &attributes, policy_alg );
858 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200859
Gilles Peskine049c7532019-05-15 20:22:09 +0200860 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200861 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200862
Ronald Cron5425a212020-08-04 14:58:35 +0200863 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100864 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100865 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100866 else
867 TEST_EQUAL( status, expected_status );
868
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200869 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200870
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200871 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200872 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100873 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100874 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100875 else
876 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200877
878exit:
879 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200880 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200881 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200882}
883/* END_CASE */
884
885/* BEGIN_CASE */
886void cipher_key_policy( int policy_usage,
887 int policy_alg,
888 int key_type,
889 data_t *key_data,
890 int exercise_alg )
891{
Ronald Cron5425a212020-08-04 14:58:35 +0200892 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000894 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200895 psa_status_t status;
896
Gilles Peskine8817f612018-12-18 00:18:46 +0100897 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200898
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200899 psa_set_key_usage_flags( &attributes, policy_usage );
900 psa_set_key_algorithm( &attributes, policy_alg );
901 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200902
Gilles Peskine049c7532019-05-15 20:22:09 +0200903 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200904 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905
Ronald Cron5425a212020-08-04 14:58:35 +0200906 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200907 if( policy_alg == exercise_alg &&
908 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100909 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200910 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100911 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200912 psa_cipher_abort( &operation );
913
Ronald Cron5425a212020-08-04 14:58:35 +0200914 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200915 if( policy_alg == exercise_alg &&
916 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100917 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100919 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200920
921exit:
922 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200923 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200924 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200925}
926/* END_CASE */
927
928/* BEGIN_CASE */
929void aead_key_policy( int policy_usage,
930 int policy_alg,
931 int key_type,
932 data_t *key_data,
933 int nonce_length_arg,
934 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100935 int exercise_alg,
936 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200937{
Ronald Cron5425a212020-08-04 14:58:35 +0200938 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200940 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100941 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200942 unsigned char nonce[16] = {0};
943 size_t nonce_length = nonce_length_arg;
944 unsigned char tag[16];
945 size_t tag_length = tag_length_arg;
946 size_t output_length;
947
948 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
949 TEST_ASSERT( tag_length <= sizeof( tag ) );
950
Gilles Peskine8817f612018-12-18 00:18:46 +0100951 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200952
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200953 psa_set_key_usage_flags( &attributes, policy_usage );
954 psa_set_key_algorithm( &attributes, policy_alg );
955 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200956
Gilles Peskine049c7532019-05-15 20:22:09 +0200957 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200958 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959
Ronald Cron5425a212020-08-04 14:58:35 +0200960 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200961 nonce, nonce_length,
962 NULL, 0,
963 NULL, 0,
964 tag, tag_length,
965 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100966 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
967 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100969 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200970
971 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200972 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200973 nonce, nonce_length,
974 NULL, 0,
975 tag, tag_length,
976 NULL, 0,
977 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100978 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
979 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
980 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100981 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200982 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100983 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200984
985exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200986 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200987 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200988}
989/* END_CASE */
990
991/* BEGIN_CASE */
992void asymmetric_encryption_key_policy( int policy_usage,
993 int policy_alg,
994 int key_type,
995 data_t *key_data,
996 int exercise_alg )
997{
Ronald Cron5425a212020-08-04 14:58:35 +0200998 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001000 psa_status_t status;
1001 size_t key_bits;
1002 size_t buffer_length;
1003 unsigned char *buffer = NULL;
1004 size_t output_length;
1005
Gilles Peskine8817f612018-12-18 00:18:46 +01001006 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001008 psa_set_key_usage_flags( &attributes, policy_usage );
1009 psa_set_key_algorithm( &attributes, policy_alg );
1010 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001011
Gilles Peskine049c7532019-05-15 20:22:09 +02001012 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001013 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001014
Ronald Cron5425a212020-08-04 14:58:35 +02001015 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001017 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1018 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001019 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001020
Ronald Cron5425a212020-08-04 14:58:35 +02001021 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001022 NULL, 0,
1023 NULL, 0,
1024 buffer, buffer_length,
1025 &output_length );
1026 if( policy_alg == exercise_alg &&
1027 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001028 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001029 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001030 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001031
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001032 if( buffer_length != 0 )
1033 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001034 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001035 buffer, buffer_length,
1036 NULL, 0,
1037 buffer, buffer_length,
1038 &output_length );
1039 if( policy_alg == exercise_alg &&
1040 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001041 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001042 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001043 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001044
1045exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001046 /*
1047 * Key attributes may have been returned by psa_get_key_attributes()
1048 * thus reset them as required.
1049 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001050 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001051
1052 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001053 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001054 mbedtls_free( buffer );
1055}
1056/* END_CASE */
1057
1058/* BEGIN_CASE */
1059void asymmetric_signature_key_policy( int policy_usage,
1060 int policy_alg,
1061 int key_type,
1062 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001063 int exercise_alg,
1064 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001065{
Ronald Cron5425a212020-08-04 14:58:35 +02001066 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001069 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1070 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1071 * compatible with the policy and `payload_length_arg` is supposed to be
1072 * a valid input length to sign. If `payload_length_arg <= 0`,
1073 * `exercise_alg` is supposed to be forbidden by the policy. */
1074 int compatible_alg = payload_length_arg > 0;
1075 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001076 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077 size_t signature_length;
1078
Gilles Peskine8817f612018-12-18 00:18:46 +01001079 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001080
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001081 psa_set_key_usage_flags( &attributes, policy_usage );
1082 psa_set_key_algorithm( &attributes, policy_alg );
1083 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001084
Gilles Peskine049c7532019-05-15 20:22:09 +02001085 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001086 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087
Ronald Cron5425a212020-08-04 14:58:35 +02001088 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001089 payload, payload_length,
1090 signature, sizeof( signature ),
1091 &signature_length );
1092 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001093 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001094 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001095 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001096
1097 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001098 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001099 payload, payload_length,
1100 signature, sizeof( signature ) );
1101 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001102 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001103 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001104 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001105
1106exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001107 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001108 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001109}
1110/* END_CASE */
1111
Janos Follathba3fab92019-06-11 14:50:16 +01001112/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001113void derive_key_policy( int policy_usage,
1114 int policy_alg,
1115 int key_type,
1116 data_t *key_data,
1117 int exercise_alg )
1118{
Ronald Cron5425a212020-08-04 14:58:35 +02001119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001121 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001122 psa_status_t status;
1123
Gilles Peskine8817f612018-12-18 00:18:46 +01001124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001126 psa_set_key_usage_flags( &attributes, policy_usage );
1127 psa_set_key_algorithm( &attributes, policy_alg );
1128 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001129
Gilles Peskine049c7532019-05-15 20:22:09 +02001130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001131 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001132
Janos Follathba3fab92019-06-11 14:50:16 +01001133 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1134
1135 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1136 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001137 {
Janos Follathba3fab92019-06-11 14:50:16 +01001138 PSA_ASSERT( psa_key_derivation_input_bytes(
1139 &operation,
1140 PSA_KEY_DERIVATION_INPUT_SEED,
1141 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001142 }
Janos Follathba3fab92019-06-11 14:50:16 +01001143
1144 status = psa_key_derivation_input_key( &operation,
1145 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001146 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001147
Gilles Peskineea0fb492018-07-12 17:17:20 +02001148 if( policy_alg == exercise_alg &&
1149 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001150 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001151 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001152 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001153
1154exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001155 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001156 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001157 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001158}
1159/* END_CASE */
1160
1161/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001162void agreement_key_policy( int policy_usage,
1163 int policy_alg,
1164 int key_type_arg,
1165 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001166 int exercise_alg,
1167 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001168{
Ronald Cron5425a212020-08-04 14:58:35 +02001169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001171 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001172 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001173 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001174 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001175
Gilles Peskine8817f612018-12-18 00:18:46 +01001176 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001177
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001178 psa_set_key_usage_flags( &attributes, policy_usage );
1179 psa_set_key_algorithm( &attributes, policy_alg );
1180 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001181
Gilles Peskine049c7532019-05-15 20:22:09 +02001182 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001183 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001184
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001185 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001186 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001187
Steven Cooremance48e852020-10-05 16:02:45 +02001188 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001189
1190exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001191 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001192 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001193 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001194}
1195/* END_CASE */
1196
1197/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001198void key_policy_alg2( int key_type_arg, data_t *key_data,
1199 int usage_arg, int alg_arg, int alg2_arg )
1200{
Ronald Cron5425a212020-08-04 14:58:35 +02001201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001202 psa_key_type_t key_type = key_type_arg;
1203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1204 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1205 psa_key_usage_t usage = usage_arg;
1206 psa_algorithm_t alg = alg_arg;
1207 psa_algorithm_t alg2 = alg2_arg;
1208
1209 PSA_ASSERT( psa_crypto_init( ) );
1210
1211 psa_set_key_usage_flags( &attributes, usage );
1212 psa_set_key_algorithm( &attributes, alg );
1213 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1214 psa_set_key_type( &attributes, key_type );
1215 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001216 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001217
Ronald Cron5425a212020-08-04 14:58:35 +02001218 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001219 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1220 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1221 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1222
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001223 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001224 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001225 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001226 goto exit;
1227
1228exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001229 /*
1230 * Key attributes may have been returned by psa_get_key_attributes()
1231 * thus reset them as required.
1232 */
1233 psa_reset_key_attributes( &got_attributes );
1234
Ronald Cron5425a212020-08-04 14:58:35 +02001235 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001236 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001237}
1238/* END_CASE */
1239
1240/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001241void raw_agreement_key_policy( int policy_usage,
1242 int policy_alg,
1243 int key_type_arg,
1244 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001245 int exercise_alg,
1246 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001247{
Ronald Cron5425a212020-08-04 14:58:35 +02001248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001250 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001251 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001252 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001253 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001254
1255 PSA_ASSERT( psa_crypto_init( ) );
1256
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001257 psa_set_key_usage_flags( &attributes, policy_usage );
1258 psa_set_key_algorithm( &attributes, policy_alg );
1259 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001260
Gilles Peskine049c7532019-05-15 20:22:09 +02001261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001262 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001263
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001264 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001265
Steven Cooremance48e852020-10-05 16:02:45 +02001266 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001267
1268exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001269 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001270 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001271 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001272}
1273/* END_CASE */
1274
1275/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001276void copy_success( int source_usage_arg,
1277 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001278 int type_arg, data_t *material,
1279 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001280 int target_usage_arg,
1281 int target_alg_arg, int target_alg2_arg,
1282 int expected_usage_arg,
1283 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001284{
Gilles Peskineca25db92019-04-19 11:43:08 +02001285 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1286 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001287 psa_key_usage_t expected_usage = expected_usage_arg;
1288 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001289 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001290 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1291 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001292 uint8_t *export_buffer = NULL;
1293
Gilles Peskine57ab7212019-01-28 13:03:09 +01001294 PSA_ASSERT( psa_crypto_init( ) );
1295
Gilles Peskineca25db92019-04-19 11:43:08 +02001296 /* Prepare the source key. */
1297 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1298 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001299 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001300 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001301 PSA_ASSERT( psa_import_key( &source_attributes,
1302 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001303 &source_key ) );
1304 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001305
Gilles Peskineca25db92019-04-19 11:43:08 +02001306 /* Prepare the target attributes. */
1307 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001308 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001309 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001310 /* Set volatile lifetime to reset the key identifier to 0. */
1311 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1312 }
1313
Gilles Peskineca25db92019-04-19 11:43:08 +02001314 if( target_usage_arg != -1 )
1315 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1316 if( target_alg_arg != -1 )
1317 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001318 if( target_alg2_arg != -1 )
1319 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001320
1321 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001322 PSA_ASSERT( psa_copy_key( source_key,
1323 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001324
1325 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001326 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001327
1328 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001329 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001330 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1331 psa_get_key_type( &target_attributes ) );
1332 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1333 psa_get_key_bits( &target_attributes ) );
1334 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1335 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001336 TEST_EQUAL( expected_alg2,
1337 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001338 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1339 {
1340 size_t length;
1341 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001342 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001343 material->len, &length ) );
1344 ASSERT_COMPARE( material->x, material->len,
1345 export_buffer, length );
1346 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001347
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001348 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001349 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001350 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001351 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001352
Ronald Cron5425a212020-08-04 14:58:35 +02001353 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001354
1355exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001356 /*
1357 * Source and target key attributes may have been returned by
1358 * psa_get_key_attributes() thus reset them as required.
1359 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001360 psa_reset_key_attributes( &source_attributes );
1361 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001362
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001363 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001364 mbedtls_free( export_buffer );
1365}
1366/* END_CASE */
1367
1368/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001369void copy_fail( int source_usage_arg,
1370 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001371 int type_arg, data_t *material,
1372 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001373 int target_usage_arg,
1374 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001375 int expected_status_arg )
1376{
1377 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1378 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001379 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1380 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02001381
1382 PSA_ASSERT( psa_crypto_init( ) );
1383
1384 /* Prepare the source key. */
1385 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1386 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001387 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001388 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001389 PSA_ASSERT( psa_import_key( &source_attributes,
1390 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001391 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001392
1393 /* Prepare the target attributes. */
1394 psa_set_key_type( &target_attributes, target_type_arg );
1395 psa_set_key_bits( &target_attributes, target_bits_arg );
1396 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1397 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001398 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001399
1400 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001401 TEST_EQUAL( psa_copy_key( source_key,
1402 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001403 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001404
Ronald Cron5425a212020-08-04 14:58:35 +02001405 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001406
Gilles Peskine4a644642019-05-03 17:14:08 +02001407exit:
1408 psa_reset_key_attributes( &source_attributes );
1409 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001410 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001411}
1412/* END_CASE */
1413
1414/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001415void hash_operation_init( )
1416{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001417 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001418 /* Test each valid way of initializing the object, except for `= {0}`, as
1419 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1420 * though it's OK by the C standard. We could test for this, but we'd need
1421 * to supress the Clang warning for the test. */
1422 psa_hash_operation_t func = psa_hash_operation_init( );
1423 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1424 psa_hash_operation_t zero;
1425
1426 memset( &zero, 0, sizeof( zero ) );
1427
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001428 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001429 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1430 PSA_ERROR_BAD_STATE );
1431 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1432 PSA_ERROR_BAD_STATE );
1433 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1434 PSA_ERROR_BAD_STATE );
1435
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001436 /* A default hash operation should be abortable without error. */
1437 PSA_ASSERT( psa_hash_abort( &func ) );
1438 PSA_ASSERT( psa_hash_abort( &init ) );
1439 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001440}
1441/* END_CASE */
1442
1443/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001444void hash_setup( int alg_arg,
1445 int expected_status_arg )
1446{
1447 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001448 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001449 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001450 psa_status_t status;
1451
Gilles Peskine8817f612018-12-18 00:18:46 +01001452 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001453
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001454 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001455 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001456
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001457 /* Whether setup succeeded or failed, abort must succeed. */
1458 PSA_ASSERT( psa_hash_abort( &operation ) );
1459
1460 /* If setup failed, reproduce the failure, so as to
1461 * test the resulting state of the operation object. */
1462 if( status != PSA_SUCCESS )
1463 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1464
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001465 /* Now the operation object should be reusable. */
1466#if defined(KNOWN_SUPPORTED_HASH_ALG)
1467 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1468 PSA_ASSERT( psa_hash_abort( &operation ) );
1469#endif
1470
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001471exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001472 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001473}
1474/* END_CASE */
1475
1476/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001477void hash_compute_fail( int alg_arg, data_t *input,
1478 int output_size_arg, int expected_status_arg )
1479{
1480 psa_algorithm_t alg = alg_arg;
1481 uint8_t *output = NULL;
1482 size_t output_size = output_size_arg;
1483 size_t output_length = INVALID_EXPORT_LENGTH;
1484 psa_status_t expected_status = expected_status_arg;
1485 psa_status_t status;
1486
1487 ASSERT_ALLOC( output, output_size );
1488
1489 PSA_ASSERT( psa_crypto_init( ) );
1490
1491 status = psa_hash_compute( alg, input->x, input->len,
1492 output, output_size, &output_length );
1493 TEST_EQUAL( status, expected_status );
1494 TEST_ASSERT( output_length <= output_size );
1495
1496exit:
1497 mbedtls_free( output );
1498 PSA_DONE( );
1499}
1500/* END_CASE */
1501
1502/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001503void hash_compare_fail( int alg_arg, data_t *input,
1504 data_t *reference_hash,
1505 int expected_status_arg )
1506{
1507 psa_algorithm_t alg = alg_arg;
1508 psa_status_t expected_status = expected_status_arg;
1509 psa_status_t status;
1510
1511 PSA_ASSERT( psa_crypto_init( ) );
1512
1513 status = psa_hash_compare( alg, input->x, input->len,
1514 reference_hash->x, reference_hash->len );
1515 TEST_EQUAL( status, expected_status );
1516
1517exit:
1518 PSA_DONE( );
1519}
1520/* END_CASE */
1521
1522/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001523void hash_compute_compare( int alg_arg, data_t *input,
1524 data_t *expected_output )
1525{
1526 psa_algorithm_t alg = alg_arg;
1527 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1528 size_t output_length = INVALID_EXPORT_LENGTH;
1529 size_t i;
1530
1531 PSA_ASSERT( psa_crypto_init( ) );
1532
1533 /* Compute with tight buffer */
1534 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001535 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001536 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001537 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001538 ASSERT_COMPARE( output, output_length,
1539 expected_output->x, expected_output->len );
1540
1541 /* Compute with larger buffer */
1542 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1543 output, sizeof( output ),
1544 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001545 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001546 ASSERT_COMPARE( output, output_length,
1547 expected_output->x, expected_output->len );
1548
1549 /* Compare with correct hash */
1550 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1551 output, output_length ) );
1552
1553 /* Compare with trailing garbage */
1554 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1555 output, output_length + 1 ),
1556 PSA_ERROR_INVALID_SIGNATURE );
1557
1558 /* Compare with truncated hash */
1559 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1560 output, output_length - 1 ),
1561 PSA_ERROR_INVALID_SIGNATURE );
1562
1563 /* Compare with corrupted value */
1564 for( i = 0; i < output_length; i++ )
1565 {
Chris Jones9634bb12021-01-20 15:56:42 +00001566 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001567 output[i] ^= 1;
1568 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1569 output, output_length ),
1570 PSA_ERROR_INVALID_SIGNATURE );
1571 output[i] ^= 1;
1572 }
1573
1574exit:
1575 PSA_DONE( );
1576}
1577/* END_CASE */
1578
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001579/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001580void hash_bad_order( )
1581{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001582 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001583 unsigned char input[] = "";
1584 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001585 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001586 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1587 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1588 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001589 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001590 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001591 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001592
Gilles Peskine8817f612018-12-18 00:18:46 +01001593 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001594
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001595 /* Call setup twice in a row. */
1596 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1597 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1598 PSA_ERROR_BAD_STATE );
1599 PSA_ASSERT( psa_hash_abort( &operation ) );
1600
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001601 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001602 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001603 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001604 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001605
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001606 /* Call update after finish. */
1607 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1608 PSA_ASSERT( psa_hash_finish( &operation,
1609 hash, sizeof( hash ), &hash_len ) );
1610 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001611 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001612 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001613
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001614 /* Call verify without calling setup beforehand. */
1615 TEST_EQUAL( psa_hash_verify( &operation,
1616 valid_hash, sizeof( valid_hash ) ),
1617 PSA_ERROR_BAD_STATE );
1618 PSA_ASSERT( psa_hash_abort( &operation ) );
1619
1620 /* Call verify after finish. */
1621 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1622 PSA_ASSERT( psa_hash_finish( &operation,
1623 hash, sizeof( hash ), &hash_len ) );
1624 TEST_EQUAL( psa_hash_verify( &operation,
1625 valid_hash, sizeof( valid_hash ) ),
1626 PSA_ERROR_BAD_STATE );
1627 PSA_ASSERT( psa_hash_abort( &operation ) );
1628
1629 /* Call verify twice in a row. */
1630 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1631 PSA_ASSERT( psa_hash_verify( &operation,
1632 valid_hash, sizeof( valid_hash ) ) );
1633 TEST_EQUAL( psa_hash_verify( &operation,
1634 valid_hash, sizeof( valid_hash ) ),
1635 PSA_ERROR_BAD_STATE );
1636 PSA_ASSERT( psa_hash_abort( &operation ) );
1637
1638 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001639 TEST_EQUAL( psa_hash_finish( &operation,
1640 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001641 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001642 PSA_ASSERT( psa_hash_abort( &operation ) );
1643
1644 /* Call finish twice in a row. */
1645 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1646 PSA_ASSERT( psa_hash_finish( &operation,
1647 hash, sizeof( hash ), &hash_len ) );
1648 TEST_EQUAL( psa_hash_finish( &operation,
1649 hash, sizeof( hash ), &hash_len ),
1650 PSA_ERROR_BAD_STATE );
1651 PSA_ASSERT( psa_hash_abort( &operation ) );
1652
1653 /* Call finish after calling verify. */
1654 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1655 PSA_ASSERT( psa_hash_verify( &operation,
1656 valid_hash, sizeof( valid_hash ) ) );
1657 TEST_EQUAL( psa_hash_finish( &operation,
1658 hash, sizeof( hash ), &hash_len ),
1659 PSA_ERROR_BAD_STATE );
1660 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001661
1662exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001663 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001664}
1665/* END_CASE */
1666
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001667/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001668void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001669{
1670 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001671 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1672 * appended to it */
1673 unsigned char hash[] = {
1674 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1675 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1676 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001677 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001678 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001679
Gilles Peskine8817f612018-12-18 00:18:46 +01001680 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001681
itayzafrir27e69452018-11-01 14:26:34 +02001682 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001683 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001684 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001685 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001686
itayzafrir27e69452018-11-01 14:26:34 +02001687 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001688 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001689 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001690 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001691
itayzafrir27e69452018-11-01 14:26:34 +02001692 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001693 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001694 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001695 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001696
itayzafrirec93d302018-10-18 18:01:10 +03001697exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001698 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001699}
1700/* END_CASE */
1701
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001702/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1703void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001704{
1705 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001706 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001707 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001708 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001709 size_t hash_len;
1710
Gilles Peskine8817f612018-12-18 00:18:46 +01001711 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001712
itayzafrir58028322018-10-25 10:22:01 +03001713 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001714 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001715 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001716 hash, expected_size - 1, &hash_len ),
1717 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001718
1719exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001720 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001721}
1722/* END_CASE */
1723
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001724/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1725void hash_clone_source_state( )
1726{
1727 psa_algorithm_t alg = PSA_ALG_SHA_256;
1728 unsigned char hash[PSA_HASH_MAX_SIZE];
1729 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1730 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1731 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1732 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1733 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1734 size_t hash_len;
1735
1736 PSA_ASSERT( psa_crypto_init( ) );
1737 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1738
1739 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1740 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1741 PSA_ASSERT( psa_hash_finish( &op_finished,
1742 hash, sizeof( hash ), &hash_len ) );
1743 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1744 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1745
1746 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1747 PSA_ERROR_BAD_STATE );
1748
1749 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1750 PSA_ASSERT( psa_hash_finish( &op_init,
1751 hash, sizeof( hash ), &hash_len ) );
1752 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1753 PSA_ASSERT( psa_hash_finish( &op_finished,
1754 hash, sizeof( hash ), &hash_len ) );
1755 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1756 PSA_ASSERT( psa_hash_finish( &op_aborted,
1757 hash, sizeof( hash ), &hash_len ) );
1758
1759exit:
1760 psa_hash_abort( &op_source );
1761 psa_hash_abort( &op_init );
1762 psa_hash_abort( &op_setup );
1763 psa_hash_abort( &op_finished );
1764 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001765 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001766}
1767/* END_CASE */
1768
1769/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1770void hash_clone_target_state( )
1771{
1772 psa_algorithm_t alg = PSA_ALG_SHA_256;
1773 unsigned char hash[PSA_HASH_MAX_SIZE];
1774 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1775 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1776 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1777 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1778 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1779 size_t hash_len;
1780
1781 PSA_ASSERT( psa_crypto_init( ) );
1782
1783 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1784 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1785 PSA_ASSERT( psa_hash_finish( &op_finished,
1786 hash, sizeof( hash ), &hash_len ) );
1787 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1788 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1789
1790 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1791 PSA_ASSERT( psa_hash_finish( &op_target,
1792 hash, sizeof( hash ), &hash_len ) );
1793
1794 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1795 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1796 PSA_ERROR_BAD_STATE );
1797 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1798 PSA_ERROR_BAD_STATE );
1799
1800exit:
1801 psa_hash_abort( &op_target );
1802 psa_hash_abort( &op_init );
1803 psa_hash_abort( &op_setup );
1804 psa_hash_abort( &op_finished );
1805 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001806 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001807}
1808/* END_CASE */
1809
itayzafrir58028322018-10-25 10:22:01 +03001810/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001811void mac_operation_init( )
1812{
Jaeden Amero252ef282019-02-15 14:05:35 +00001813 const uint8_t input[1] = { 0 };
1814
Jaeden Amero769ce272019-01-04 11:48:03 +00001815 /* Test each valid way of initializing the object, except for `= {0}`, as
1816 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1817 * though it's OK by the C standard. We could test for this, but we'd need
1818 * to supress the Clang warning for the test. */
1819 psa_mac_operation_t func = psa_mac_operation_init( );
1820 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1821 psa_mac_operation_t zero;
1822
1823 memset( &zero, 0, sizeof( zero ) );
1824
Jaeden Amero252ef282019-02-15 14:05:35 +00001825 /* A freshly-initialized MAC operation should not be usable. */
1826 TEST_EQUAL( psa_mac_update( &func,
1827 input, sizeof( input ) ),
1828 PSA_ERROR_BAD_STATE );
1829 TEST_EQUAL( psa_mac_update( &init,
1830 input, sizeof( input ) ),
1831 PSA_ERROR_BAD_STATE );
1832 TEST_EQUAL( psa_mac_update( &zero,
1833 input, sizeof( input ) ),
1834 PSA_ERROR_BAD_STATE );
1835
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001836 /* A default MAC operation should be abortable without error. */
1837 PSA_ASSERT( psa_mac_abort( &func ) );
1838 PSA_ASSERT( psa_mac_abort( &init ) );
1839 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001840}
1841/* END_CASE */
1842
1843/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001844void mac_setup( int key_type_arg,
1845 data_t *key,
1846 int alg_arg,
1847 int expected_status_arg )
1848{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001849 psa_key_type_t key_type = key_type_arg;
1850 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001851 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001852 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001853 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1854#if defined(KNOWN_SUPPORTED_MAC_ALG)
1855 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1856#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001857
Gilles Peskine8817f612018-12-18 00:18:46 +01001858 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001859
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001860 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1861 &operation, &status ) )
1862 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001863 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001864
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001865 /* The operation object should be reusable. */
1866#if defined(KNOWN_SUPPORTED_MAC_ALG)
1867 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1868 smoke_test_key_data,
1869 sizeof( smoke_test_key_data ),
1870 KNOWN_SUPPORTED_MAC_ALG,
1871 &operation, &status ) )
1872 goto exit;
1873 TEST_EQUAL( status, PSA_SUCCESS );
1874#endif
1875
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001876exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001877 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001878}
1879/* END_CASE */
1880
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001881/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00001882void mac_bad_order( )
1883{
Ronald Cron5425a212020-08-04 14:58:35 +02001884 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001885 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1886 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001887 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001888 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1889 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1890 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001892 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1893 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1894 size_t sign_mac_length = 0;
1895 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1896 const uint8_t verify_mac[] = {
1897 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1898 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1899 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1900
1901 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001902 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001903 psa_set_key_algorithm( &attributes, alg );
1904 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001905
Ronald Cron5425a212020-08-04 14:58:35 +02001906 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1907 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001908
Jaeden Amero252ef282019-02-15 14:05:35 +00001909 /* Call update without calling setup beforehand. */
1910 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1911 PSA_ERROR_BAD_STATE );
1912 PSA_ASSERT( psa_mac_abort( &operation ) );
1913
1914 /* Call sign finish without calling setup beforehand. */
1915 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1916 &sign_mac_length),
1917 PSA_ERROR_BAD_STATE );
1918 PSA_ASSERT( psa_mac_abort( &operation ) );
1919
1920 /* Call verify finish without calling setup beforehand. */
1921 TEST_EQUAL( psa_mac_verify_finish( &operation,
1922 verify_mac, sizeof( verify_mac ) ),
1923 PSA_ERROR_BAD_STATE );
1924 PSA_ASSERT( psa_mac_abort( &operation ) );
1925
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001926 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001927 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
1928 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001929 PSA_ERROR_BAD_STATE );
1930 PSA_ASSERT( psa_mac_abort( &operation ) );
1931
Jaeden Amero252ef282019-02-15 14:05:35 +00001932 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001933 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001934 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1935 PSA_ASSERT( psa_mac_sign_finish( &operation,
1936 sign_mac, sizeof( sign_mac ),
1937 &sign_mac_length ) );
1938 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1939 PSA_ERROR_BAD_STATE );
1940 PSA_ASSERT( psa_mac_abort( &operation ) );
1941
1942 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001943 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001944 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1945 PSA_ASSERT( psa_mac_verify_finish( &operation,
1946 verify_mac, sizeof( verify_mac ) ) );
1947 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1948 PSA_ERROR_BAD_STATE );
1949 PSA_ASSERT( psa_mac_abort( &operation ) );
1950
1951 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001952 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001953 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1954 PSA_ASSERT( psa_mac_sign_finish( &operation,
1955 sign_mac, sizeof( sign_mac ),
1956 &sign_mac_length ) );
1957 TEST_EQUAL( psa_mac_sign_finish( &operation,
1958 sign_mac, sizeof( sign_mac ),
1959 &sign_mac_length ),
1960 PSA_ERROR_BAD_STATE );
1961 PSA_ASSERT( psa_mac_abort( &operation ) );
1962
1963 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001964 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001965 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1966 PSA_ASSERT( psa_mac_verify_finish( &operation,
1967 verify_mac, sizeof( verify_mac ) ) );
1968 TEST_EQUAL( psa_mac_verify_finish( &operation,
1969 verify_mac, sizeof( verify_mac ) ),
1970 PSA_ERROR_BAD_STATE );
1971 PSA_ASSERT( psa_mac_abort( &operation ) );
1972
1973 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001974 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001975 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1976 TEST_EQUAL( psa_mac_verify_finish( &operation,
1977 verify_mac, sizeof( verify_mac ) ),
1978 PSA_ERROR_BAD_STATE );
1979 PSA_ASSERT( psa_mac_abort( &operation ) );
1980
1981 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001982 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001983 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1984 TEST_EQUAL( psa_mac_sign_finish( &operation,
1985 sign_mac, sizeof( sign_mac ),
1986 &sign_mac_length ),
1987 PSA_ERROR_BAD_STATE );
1988 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001989
Ronald Cron5425a212020-08-04 14:58:35 +02001990 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001991
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001992exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001993 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001994}
1995/* END_CASE */
1996
1997/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001998void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02001999 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002000 int alg_arg,
2001 data_t *input,
2002 data_t *expected_mac )
2003{
Ronald Cron5425a212020-08-04 14:58:35 +02002004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005 psa_key_type_t key_type = key_type_arg;
2006 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002007 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002008 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002009 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002010 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002011 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002012 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002013 const size_t output_sizes_to_test[] = {
2014 0,
2015 1,
2016 expected_mac->len - 1,
2017 expected_mac->len,
2018 expected_mac->len + 1,
2019 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002020
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002021 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002022 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002023 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002024
Gilles Peskine8817f612018-12-18 00:18:46 +01002025 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002026
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002027 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002028 psa_set_key_algorithm( &attributes, alg );
2029 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002030
Ronald Cron5425a212020-08-04 14:58:35 +02002031 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2032 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002033
Gilles Peskine8b356b52020-08-25 23:44:59 +02002034 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2035 {
2036 const size_t output_size = output_sizes_to_test[i];
2037 psa_status_t expected_status =
2038 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2039 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002040
Chris Jones9634bb12021-01-20 15:56:42 +00002041 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002042 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002043
Gilles Peskine8b356b52020-08-25 23:44:59 +02002044 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002045 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002046 PSA_ASSERT( psa_mac_update( &operation,
2047 input->x, input->len ) );
2048 TEST_EQUAL( psa_mac_sign_finish( &operation,
2049 actual_mac, output_size,
2050 &mac_length ),
2051 expected_status );
2052 PSA_ASSERT( psa_mac_abort( &operation ) );
2053
2054 if( expected_status == PSA_SUCCESS )
2055 {
2056 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2057 actual_mac, mac_length );
2058 }
2059 mbedtls_free( actual_mac );
2060 actual_mac = NULL;
2061 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002062
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002063exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002064 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002065 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002066 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002067 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002068}
2069/* END_CASE */
2070
2071/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002072void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002073 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002074 int alg_arg,
2075 data_t *input,
2076 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002077{
Ronald Cron5425a212020-08-04 14:58:35 +02002078 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002079 psa_key_type_t key_type = key_type_arg;
2080 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002081 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002083 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002084
Gilles Peskine69c12672018-06-28 00:07:19 +02002085 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2086
Gilles Peskine8817f612018-12-18 00:18:46 +01002087 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002088
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002089 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002090 psa_set_key_algorithm( &attributes, alg );
2091 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002092
Ronald Cron5425a212020-08-04 14:58:35 +02002093 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2094 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002095
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002096 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02002097 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002098 PSA_ASSERT( psa_mac_update( &operation,
2099 input->x, input->len ) );
2100 PSA_ASSERT( psa_mac_verify_finish( &operation,
2101 expected_mac->x,
2102 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002103
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002104 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02002105 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002106 PSA_ASSERT( psa_mac_update( &operation,
2107 input->x, input->len ) );
2108 TEST_EQUAL( psa_mac_verify_finish( &operation,
2109 expected_mac->x,
2110 expected_mac->len - 1 ),
2111 PSA_ERROR_INVALID_SIGNATURE );
2112
2113 /* Test a MAC that's too long. */
2114 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2115 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002116 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002117 PSA_ASSERT( psa_mac_update( &operation,
2118 input->x, input->len ) );
2119 TEST_EQUAL( psa_mac_verify_finish( &operation,
2120 perturbed_mac,
2121 expected_mac->len + 1 ),
2122 PSA_ERROR_INVALID_SIGNATURE );
2123
2124 /* Test changing one byte. */
2125 for( size_t i = 0; i < expected_mac->len; i++ )
2126 {
Chris Jones9634bb12021-01-20 15:56:42 +00002127 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002128 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02002129 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002130 PSA_ASSERT( psa_mac_update( &operation,
2131 input->x, input->len ) );
2132 TEST_EQUAL( psa_mac_verify_finish( &operation,
2133 perturbed_mac,
2134 expected_mac->len ),
2135 PSA_ERROR_INVALID_SIGNATURE );
2136 perturbed_mac[i] ^= 1;
2137 }
2138
Gilles Peskine8c9def32018-02-08 10:02:12 +01002139exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002140 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002141 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002142 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002143 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002144}
2145/* END_CASE */
2146
2147/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002148void cipher_operation_init( )
2149{
Jaeden Ameroab439972019-02-15 14:12:05 +00002150 const uint8_t input[1] = { 0 };
2151 unsigned char output[1] = { 0 };
2152 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002153 /* Test each valid way of initializing the object, except for `= {0}`, as
2154 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2155 * though it's OK by the C standard. We could test for this, but we'd need
2156 * to supress the Clang warning for the test. */
2157 psa_cipher_operation_t func = psa_cipher_operation_init( );
2158 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2159 psa_cipher_operation_t zero;
2160
2161 memset( &zero, 0, sizeof( zero ) );
2162
Jaeden Ameroab439972019-02-15 14:12:05 +00002163 /* A freshly-initialized cipher operation should not be usable. */
2164 TEST_EQUAL( psa_cipher_update( &func,
2165 input, sizeof( input ),
2166 output, sizeof( output ),
2167 &output_length ),
2168 PSA_ERROR_BAD_STATE );
2169 TEST_EQUAL( psa_cipher_update( &init,
2170 input, sizeof( input ),
2171 output, sizeof( output ),
2172 &output_length ),
2173 PSA_ERROR_BAD_STATE );
2174 TEST_EQUAL( psa_cipher_update( &zero,
2175 input, sizeof( input ),
2176 output, sizeof( output ),
2177 &output_length ),
2178 PSA_ERROR_BAD_STATE );
2179
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002180 /* A default cipher operation should be abortable without error. */
2181 PSA_ASSERT( psa_cipher_abort( &func ) );
2182 PSA_ASSERT( psa_cipher_abort( &init ) );
2183 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002184}
2185/* END_CASE */
2186
2187/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002188void cipher_setup( int key_type_arg,
2189 data_t *key,
2190 int alg_arg,
2191 int expected_status_arg )
2192{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002193 psa_key_type_t key_type = key_type_arg;
2194 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002195 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002196 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002197 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002198#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002199 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2200#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002201
Gilles Peskine8817f612018-12-18 00:18:46 +01002202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002203
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002204 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2205 &operation, &status ) )
2206 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002207 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002208
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002209 /* The operation object should be reusable. */
2210#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2211 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2212 smoke_test_key_data,
2213 sizeof( smoke_test_key_data ),
2214 KNOWN_SUPPORTED_CIPHER_ALG,
2215 &operation, &status ) )
2216 goto exit;
2217 TEST_EQUAL( status, PSA_SUCCESS );
2218#endif
2219
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002220exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002221 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002222 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002223}
2224/* END_CASE */
2225
Steven Cooreman29eecbf2021-01-28 19:41:25 +01002226/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002227void cipher_bad_order( )
2228{
Ronald Cron5425a212020-08-04 14:58:35 +02002229 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002230 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2231 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002233 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002234 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002235 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002236 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2237 0xaa, 0xaa, 0xaa, 0xaa };
2238 const uint8_t text[] = {
2239 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2240 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002241 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002242 size_t length = 0;
2243
2244 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002245 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2246 psa_set_key_algorithm( &attributes, alg );
2247 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002248 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2249 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002250
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002251 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002252 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2253 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002254 PSA_ERROR_BAD_STATE );
2255 PSA_ASSERT( psa_cipher_abort( &operation ) );
2256
2257 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002258 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
2259 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002260 PSA_ERROR_BAD_STATE );
2261 PSA_ASSERT( psa_cipher_abort( &operation ) );
2262
Jaeden Ameroab439972019-02-15 14:12:05 +00002263 /* Generate an IV without calling setup beforehand. */
2264 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2265 buffer, sizeof( buffer ),
2266 &length ),
2267 PSA_ERROR_BAD_STATE );
2268 PSA_ASSERT( psa_cipher_abort( &operation ) );
2269
2270 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002271 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002272 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2273 buffer, sizeof( buffer ),
2274 &length ) );
2275 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2276 buffer, sizeof( buffer ),
2277 &length ),
2278 PSA_ERROR_BAD_STATE );
2279 PSA_ASSERT( psa_cipher_abort( &operation ) );
2280
2281 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002282 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002283 PSA_ASSERT( psa_cipher_set_iv( &operation,
2284 iv, sizeof( iv ) ) );
2285 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2286 buffer, sizeof( buffer ),
2287 &length ),
2288 PSA_ERROR_BAD_STATE );
2289 PSA_ASSERT( psa_cipher_abort( &operation ) );
2290
2291 /* Set an IV without calling setup beforehand. */
2292 TEST_EQUAL( psa_cipher_set_iv( &operation,
2293 iv, sizeof( iv ) ),
2294 PSA_ERROR_BAD_STATE );
2295 PSA_ASSERT( psa_cipher_abort( &operation ) );
2296
2297 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002298 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002299 PSA_ASSERT( psa_cipher_set_iv( &operation,
2300 iv, sizeof( iv ) ) );
2301 TEST_EQUAL( psa_cipher_set_iv( &operation,
2302 iv, sizeof( iv ) ),
2303 PSA_ERROR_BAD_STATE );
2304 PSA_ASSERT( psa_cipher_abort( &operation ) );
2305
2306 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002307 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002308 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2309 buffer, sizeof( buffer ),
2310 &length ) );
2311 TEST_EQUAL( psa_cipher_set_iv( &operation,
2312 iv, sizeof( iv ) ),
2313 PSA_ERROR_BAD_STATE );
2314 PSA_ASSERT( psa_cipher_abort( &operation ) );
2315
2316 /* Call update without calling setup beforehand. */
2317 TEST_EQUAL( psa_cipher_update( &operation,
2318 text, sizeof( text ),
2319 buffer, sizeof( buffer ),
2320 &length ),
2321 PSA_ERROR_BAD_STATE );
2322 PSA_ASSERT( psa_cipher_abort( &operation ) );
2323
2324 /* Call update without an IV where an IV is required. */
2325 TEST_EQUAL( psa_cipher_update( &operation,
2326 text, sizeof( text ),
2327 buffer, sizeof( buffer ),
2328 &length ),
2329 PSA_ERROR_BAD_STATE );
2330 PSA_ASSERT( psa_cipher_abort( &operation ) );
2331
2332 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002333 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002334 PSA_ASSERT( psa_cipher_set_iv( &operation,
2335 iv, sizeof( iv ) ) );
2336 PSA_ASSERT( psa_cipher_finish( &operation,
2337 buffer, sizeof( buffer ), &length ) );
2338 TEST_EQUAL( psa_cipher_update( &operation,
2339 text, sizeof( text ),
2340 buffer, sizeof( buffer ),
2341 &length ),
2342 PSA_ERROR_BAD_STATE );
2343 PSA_ASSERT( psa_cipher_abort( &operation ) );
2344
2345 /* Call finish without calling setup beforehand. */
2346 TEST_EQUAL( psa_cipher_finish( &operation,
2347 buffer, sizeof( buffer ), &length ),
2348 PSA_ERROR_BAD_STATE );
2349 PSA_ASSERT( psa_cipher_abort( &operation ) );
2350
2351 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002352 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002353 /* Not calling update means we are encrypting an empty buffer, which is OK
2354 * for cipher modes with padding. */
2355 TEST_EQUAL( psa_cipher_finish( &operation,
2356 buffer, sizeof( buffer ), &length ),
2357 PSA_ERROR_BAD_STATE );
2358 PSA_ASSERT( psa_cipher_abort( &operation ) );
2359
2360 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002361 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002362 PSA_ASSERT( psa_cipher_set_iv( &operation,
2363 iv, sizeof( iv ) ) );
2364 PSA_ASSERT( psa_cipher_finish( &operation,
2365 buffer, sizeof( buffer ), &length ) );
2366 TEST_EQUAL( psa_cipher_finish( &operation,
2367 buffer, sizeof( buffer ), &length ),
2368 PSA_ERROR_BAD_STATE );
2369 PSA_ASSERT( psa_cipher_abort( &operation ) );
2370
Ronald Cron5425a212020-08-04 14:58:35 +02002371 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002372
Jaeden Ameroab439972019-02-15 14:12:05 +00002373exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002374 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002375 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002376}
2377/* END_CASE */
2378
2379/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002380void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002381 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002382 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002383 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002384{
Ronald Cron5425a212020-08-04 14:58:35 +02002385 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002386 psa_status_t status;
2387 psa_key_type_t key_type = key_type_arg;
2388 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002389 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002390 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391 size_t output_buffer_size = 0;
2392 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002393 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002394 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002396
Gilles Peskine8817f612018-12-18 00:18:46 +01002397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002398
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002399 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2400 psa_set_key_algorithm( &attributes, alg );
2401 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002402
Ronald Cron5425a212020-08-04 14:58:35 +02002403 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2404 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405
Ronald Cron5425a212020-08-04 14:58:35 +02002406 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002407
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002408 if( iv->len > 0 )
2409 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002410 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002411 }
2412
gabor-mezei-armceface22021-01-21 12:26:17 +01002413 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2414 TEST_ASSERT( output_buffer_size <=
2415 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002416 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002417
Gilles Peskine8817f612018-12-18 00:18:46 +01002418 PSA_ASSERT( psa_cipher_update( &operation,
2419 input->x, input->len,
2420 output, output_buffer_size,
2421 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002422 TEST_ASSERT( function_output_length <=
2423 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2424 TEST_ASSERT( function_output_length <=
2425 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002426 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002427
Gilles Peskine50e586b2018-06-08 14:28:46 +02002428 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002429 output + total_output_length,
2430 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002431 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002432 TEST_ASSERT( function_output_length <=
2433 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2434 TEST_ASSERT( function_output_length <=
2435 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002436 total_output_length += function_output_length;
2437
Gilles Peskinefe11b722018-12-18 00:24:04 +01002438 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002439 if( expected_status == PSA_SUCCESS )
2440 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002441 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002442 ASSERT_COMPARE( expected_output->x, expected_output->len,
2443 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002444 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002445
Gilles Peskine50e586b2018-06-08 14:28:46 +02002446exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002447 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002448 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002449 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002450 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002451}
2452/* END_CASE */
2453
2454/* BEGIN_CASE */
2455void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002456 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002457 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002458 int first_part_size_arg,
2459 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002460 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002461{
Ronald Cron5425a212020-08-04 14:58:35 +02002462 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002463 psa_key_type_t key_type = key_type_arg;
2464 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002465 size_t first_part_size = first_part_size_arg;
2466 size_t output1_length = output1_length_arg;
2467 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002468 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002469 size_t output_buffer_size = 0;
2470 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002471 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002472 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002473 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002474
Gilles Peskine8817f612018-12-18 00:18:46 +01002475 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002476
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002477 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2478 psa_set_key_algorithm( &attributes, alg );
2479 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002480
Ronald Cron5425a212020-08-04 14:58:35 +02002481 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2482 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002483
Ronald Cron5425a212020-08-04 14:58:35 +02002484 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002485
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002486 if( iv->len > 0 )
2487 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002488 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002489 }
2490
gabor-mezei-armceface22021-01-21 12:26:17 +01002491 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2492 TEST_ASSERT( output_buffer_size <=
2493 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002494 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002495
Gilles Peskinee0866522019-02-19 19:44:00 +01002496 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002497 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2498 output, output_buffer_size,
2499 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002500 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002501 TEST_ASSERT( function_output_length <=
2502 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2503 TEST_ASSERT( function_output_length <=
2504 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002505 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002506
Gilles Peskine8817f612018-12-18 00:18:46 +01002507 PSA_ASSERT( psa_cipher_update( &operation,
2508 input->x + first_part_size,
2509 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002510 output + total_output_length,
2511 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002512 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002513 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002514 TEST_ASSERT( function_output_length <=
2515 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2516 alg,
2517 input->len - first_part_size ) );
2518 TEST_ASSERT( function_output_length <=
2519 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002520 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002521
Gilles Peskine8817f612018-12-18 00:18:46 +01002522 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002523 output + total_output_length,
2524 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002525 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002526 TEST_ASSERT( function_output_length <=
2527 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2528 TEST_ASSERT( function_output_length <=
2529 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002530 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002531 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002532
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002533 ASSERT_COMPARE( expected_output->x, expected_output->len,
2534 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002535
2536exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002537 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002538 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002539 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002540 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002541}
2542/* END_CASE */
2543
2544/* BEGIN_CASE */
2545void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002546 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002547 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002548 int first_part_size_arg,
2549 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002550 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002551{
Ronald Cron5425a212020-08-04 14:58:35 +02002552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002553 psa_key_type_t key_type = key_type_arg;
2554 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002555 size_t first_part_size = first_part_size_arg;
2556 size_t output1_length = output1_length_arg;
2557 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002558 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002559 size_t output_buffer_size = 0;
2560 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002561 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002562 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002564
Gilles Peskine8817f612018-12-18 00:18:46 +01002565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002566
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002567 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2568 psa_set_key_algorithm( &attributes, alg );
2569 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002570
Ronald Cron5425a212020-08-04 14:58:35 +02002571 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2572 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002573
Ronald Cron5425a212020-08-04 14:58:35 +02002574 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002575
Steven Cooreman177deba2020-09-07 17:14:14 +02002576 if( iv->len > 0 )
2577 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002578 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002579 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002580
gabor-mezei-armceface22021-01-21 12:26:17 +01002581 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2582 TEST_ASSERT( output_buffer_size <=
2583 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002584 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002585
Gilles Peskinee0866522019-02-19 19:44:00 +01002586 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002587 PSA_ASSERT( psa_cipher_update( &operation,
2588 input->x, first_part_size,
2589 output, output_buffer_size,
2590 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002591 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002592 TEST_ASSERT( function_output_length <=
2593 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2594 TEST_ASSERT( function_output_length <=
2595 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002596 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002597
Gilles Peskine8817f612018-12-18 00:18:46 +01002598 PSA_ASSERT( psa_cipher_update( &operation,
2599 input->x + first_part_size,
2600 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002601 output + total_output_length,
2602 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002603 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002604 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002605 TEST_ASSERT( function_output_length <=
2606 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2607 alg,
2608 input->len - first_part_size ) );
2609 TEST_ASSERT( function_output_length <=
2610 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002611 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002612
Gilles Peskine8817f612018-12-18 00:18:46 +01002613 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002614 output + total_output_length,
2615 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002616 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002617 TEST_ASSERT( function_output_length <=
2618 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2619 TEST_ASSERT( function_output_length <=
2620 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002621 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002622 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002623
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002624 ASSERT_COMPARE( expected_output->x, expected_output->len,
2625 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002626
2627exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002628 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002629 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002630 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002631 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002632}
2633/* END_CASE */
2634
Gilles Peskine50e586b2018-06-08 14:28:46 +02002635/* BEGIN_CASE */
2636void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002637 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002638 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002639 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002640{
Ronald Cron5425a212020-08-04 14:58:35 +02002641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002642 psa_status_t status;
2643 psa_key_type_t key_type = key_type_arg;
2644 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002645 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002646 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002647 size_t output_buffer_size = 0;
2648 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002649 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002650 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002652
Gilles Peskine8817f612018-12-18 00:18:46 +01002653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002654
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002655 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2656 psa_set_key_algorithm( &attributes, alg );
2657 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002658
Ronald Cron5425a212020-08-04 14:58:35 +02002659 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2660 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002661
Ronald Cron5425a212020-08-04 14:58:35 +02002662 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002663
Steven Cooreman177deba2020-09-07 17:14:14 +02002664 if( iv->len > 0 )
2665 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002666 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002667 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002668
gabor-mezei-armceface22021-01-21 12:26:17 +01002669 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2670 TEST_ASSERT( output_buffer_size <=
2671 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002672 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002673
Gilles Peskine8817f612018-12-18 00:18:46 +01002674 PSA_ASSERT( psa_cipher_update( &operation,
2675 input->x, input->len,
2676 output, output_buffer_size,
2677 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002678 TEST_ASSERT( function_output_length <=
2679 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2680 TEST_ASSERT( function_output_length <=
2681 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002682 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002683
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002685 output + total_output_length,
2686 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002687 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002688 TEST_ASSERT( function_output_length <=
2689 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2690 TEST_ASSERT( function_output_length <=
2691 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002692 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002693 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002694
2695 if( expected_status == PSA_SUCCESS )
2696 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002697 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002698 ASSERT_COMPARE( expected_output->x, expected_output->len,
2699 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002700 }
2701
Gilles Peskine50e586b2018-06-08 14:28:46 +02002702exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002703 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002704 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002705 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002706 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002707}
2708/* END_CASE */
2709
Gilles Peskine50e586b2018-06-08 14:28:46 +02002710/* BEGIN_CASE */
2711void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002712 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002713 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002714{
Ronald Cron5425a212020-08-04 14:58:35 +02002715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002716 psa_key_type_t key_type = key_type_arg;
2717 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002718 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002719 size_t iv_size = 16;
2720 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002721 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002722 size_t output1_size = 0;
2723 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002724 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002725 size_t output2_size = 0;
2726 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002727 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002728 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2729 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002730 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002731
Gilles Peskine8817f612018-12-18 00:18:46 +01002732 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002733
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002734 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2735 psa_set_key_algorithm( &attributes, alg );
2736 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002737
Ronald Cron5425a212020-08-04 14:58:35 +02002738 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2739 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002740
Ronald Cron5425a212020-08-04 14:58:35 +02002741 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2742 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002743
Steven Cooreman177deba2020-09-07 17:14:14 +02002744 if( alg != PSA_ALG_ECB_NO_PADDING )
2745 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002746 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2747 iv, iv_size,
2748 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002749 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002750 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2751 TEST_ASSERT( output1_size <=
2752 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002753 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002754
Gilles Peskine8817f612018-12-18 00:18:46 +01002755 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2756 output1, output1_size,
2757 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002758 TEST_ASSERT( output1_length <=
2759 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2760 TEST_ASSERT( output1_length <=
2761 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2762
Gilles Peskine8817f612018-12-18 00:18:46 +01002763 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002764 output1 + output1_length,
2765 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002766 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002767 TEST_ASSERT( function_output_length <=
2768 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2769 TEST_ASSERT( function_output_length <=
2770 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002771
Gilles Peskine048b7f02018-06-08 14:20:49 +02002772 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002773
Gilles Peskine8817f612018-12-18 00:18:46 +01002774 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002775
2776 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002777 TEST_ASSERT( output2_size <=
2778 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2779 TEST_ASSERT( output2_size <=
2780 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002781 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002782
Steven Cooreman177deba2020-09-07 17:14:14 +02002783 if( iv_length > 0 )
2784 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002785 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2786 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002787 }
2788
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2790 output2, output2_size,
2791 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002792 TEST_ASSERT( output2_length <=
2793 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2794 TEST_ASSERT( output2_length <=
2795 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2796
Gilles Peskine048b7f02018-06-08 14:20:49 +02002797 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 PSA_ASSERT( psa_cipher_finish( &operation2,
2799 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002800 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002801 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002802 TEST_ASSERT( function_output_length <=
2803 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2804 TEST_ASSERT( function_output_length <=
2805 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002806
Gilles Peskine048b7f02018-06-08 14:20:49 +02002807 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002808
Gilles Peskine8817f612018-12-18 00:18:46 +01002809 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002810
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002811 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002812
2813exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002814 psa_cipher_abort( &operation1 );
2815 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002816 mbedtls_free( output1 );
2817 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002818 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002819 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002820}
2821/* END_CASE */
2822
2823/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002824void cipher_verify_output_multipart( int alg_arg,
2825 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002826 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002827 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002828 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002829{
Ronald Cron5425a212020-08-04 14:58:35 +02002830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002831 psa_key_type_t key_type = key_type_arg;
2832 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002833 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002834 unsigned char iv[16] = {0};
2835 size_t iv_size = 16;
2836 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002837 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002838 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002839 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002840 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002841 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002842 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002843 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002844 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2845 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002846 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002847
Gilles Peskine8817f612018-12-18 00:18:46 +01002848 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002849
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002850 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2851 psa_set_key_algorithm( &attributes, alg );
2852 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002853
Ronald Cron5425a212020-08-04 14:58:35 +02002854 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2855 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002856
Ronald Cron5425a212020-08-04 14:58:35 +02002857 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2858 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002859
Steven Cooreman177deba2020-09-07 17:14:14 +02002860 if( alg != PSA_ALG_ECB_NO_PADDING )
2861 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002862 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2863 iv, iv_size,
2864 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002865 }
2866
gabor-mezei-armceface22021-01-21 12:26:17 +01002867 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2868 TEST_ASSERT( output1_buffer_size <=
2869 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002870 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002871
Gilles Peskinee0866522019-02-19 19:44:00 +01002872 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002873
Gilles Peskine8817f612018-12-18 00:18:46 +01002874 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2875 output1, output1_buffer_size,
2876 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002877 TEST_ASSERT( function_output_length <=
2878 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2879 TEST_ASSERT( function_output_length <=
2880 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002881 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002882
Gilles Peskine8817f612018-12-18 00:18:46 +01002883 PSA_ASSERT( psa_cipher_update( &operation1,
2884 input->x + first_part_size,
2885 input->len - first_part_size,
2886 output1, output1_buffer_size,
2887 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002888 TEST_ASSERT( function_output_length <=
2889 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2890 alg,
2891 input->len - first_part_size ) );
2892 TEST_ASSERT( function_output_length <=
2893 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002894 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002895
Gilles Peskine8817f612018-12-18 00:18:46 +01002896 PSA_ASSERT( psa_cipher_finish( &operation1,
2897 output1 + output1_length,
2898 output1_buffer_size - output1_length,
2899 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002900 TEST_ASSERT( function_output_length <=
2901 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2902 TEST_ASSERT( function_output_length <=
2903 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002904 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002905
Gilles Peskine8817f612018-12-18 00:18:46 +01002906 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002907
Gilles Peskine048b7f02018-06-08 14:20:49 +02002908 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002909 TEST_ASSERT( output2_buffer_size <=
2910 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2911 TEST_ASSERT( output2_buffer_size <=
2912 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002913 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002914
Steven Cooreman177deba2020-09-07 17:14:14 +02002915 if( iv_length > 0 )
2916 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002917 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2918 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002919 }
Moran Pekerded84402018-06-06 16:36:50 +03002920
Gilles Peskine8817f612018-12-18 00:18:46 +01002921 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2922 output2, output2_buffer_size,
2923 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002924 TEST_ASSERT( function_output_length <=
2925 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2926 TEST_ASSERT( function_output_length <=
2927 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002928 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002929
Gilles Peskine8817f612018-12-18 00:18:46 +01002930 PSA_ASSERT( psa_cipher_update( &operation2,
2931 output1 + first_part_size,
2932 output1_length - first_part_size,
2933 output2, output2_buffer_size,
2934 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002935 TEST_ASSERT( function_output_length <=
2936 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2937 alg,
2938 output1_length - first_part_size ) );
2939 TEST_ASSERT( function_output_length <=
2940 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002941 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002942
Gilles Peskine8817f612018-12-18 00:18:46 +01002943 PSA_ASSERT( psa_cipher_finish( &operation2,
2944 output2 + output2_length,
2945 output2_buffer_size - output2_length,
2946 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002947 TEST_ASSERT( function_output_length <=
2948 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2949 TEST_ASSERT( function_output_length <=
2950 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002951 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002952
Gilles Peskine8817f612018-12-18 00:18:46 +01002953 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002954
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002955 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002956
2957exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002958 psa_cipher_abort( &operation1 );
2959 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002960 mbedtls_free( output1 );
2961 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002962 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002963 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002964}
2965/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002966
Gilles Peskine20035e32018-02-03 22:44:14 +01002967/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002968void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002969 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002970 data_t *nonce,
2971 data_t *additional_data,
2972 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002973 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002974{
Ronald Cron5425a212020-08-04 14:58:35 +02002975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002976 psa_key_type_t key_type = key_type_arg;
2977 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002978 unsigned char *output_data = NULL;
2979 size_t output_size = 0;
2980 size_t output_length = 0;
2981 unsigned char *output_data2 = NULL;
2982 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002983 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Steven Cooremanf49478b2021-02-15 15:19:25 +01002984 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002985 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002987
Gilles Peskine4abf7412018-06-18 16:35:34 +02002988 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02002989 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
2990 * should be exact. */
2991 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
2992 TEST_EQUAL( output_size,
2993 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002994 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002995
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002997
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002998 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2999 psa_set_key_algorithm( &attributes, alg );
3000 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003001
Gilles Peskine049c7532019-05-15 20:22:09 +02003002 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003003 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003004
Steven Cooremanf49478b2021-02-15 15:19:25 +01003005 status = psa_aead_encrypt( key, alg,
3006 nonce->x, nonce->len,
3007 additional_data->x,
3008 additional_data->len,
3009 input_data->x, input_data->len,
3010 output_data, output_size,
3011 &output_length );
3012
3013 /* If the operation is not supported, just skip and not fail in case the
3014 * encryption involves a common limitation of cryptography hardwares and
3015 * an alternative implementation. */
3016 if( status == PSA_ERROR_NOT_SUPPORTED )
3017 {
3018 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3019 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3020 }
3021
3022 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003023
3024 if( PSA_SUCCESS == expected_result )
3025 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003026 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003027
Gilles Peskine003a4a92019-05-14 16:09:40 +02003028 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3029 * should be exact. */
3030 TEST_EQUAL( input_data->len,
3031 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3032
gabor-mezei-armceface22021-01-21 12:26:17 +01003033 TEST_ASSERT( input_data->len <=
3034 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3035
Ronald Cron5425a212020-08-04 14:58:35 +02003036 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003037 nonce->x, nonce->len,
3038 additional_data->x,
3039 additional_data->len,
3040 output_data, output_length,
3041 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003042 &output_length2 ),
3043 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003044
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003045 ASSERT_COMPARE( input_data->x, input_data->len,
3046 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003047 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003048
Gilles Peskinea1cac842018-06-11 19:33:02 +02003049exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003050 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003051 mbedtls_free( output_data );
3052 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003053 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003054}
3055/* END_CASE */
3056
3057/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003058void aead_encrypt( int key_type_arg, data_t *key_data,
3059 int alg_arg,
3060 data_t *nonce,
3061 data_t *additional_data,
3062 data_t *input_data,
3063 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003064{
Ronald Cron5425a212020-08-04 14:58:35 +02003065 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003066 psa_key_type_t key_type = key_type_arg;
3067 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003068 unsigned char *output_data = NULL;
3069 size_t output_size = 0;
3070 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003071 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003072 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003073 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003074
Gilles Peskine4abf7412018-06-18 16:35:34 +02003075 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003076 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3077 * should be exact. */
3078 TEST_EQUAL( output_size,
3079 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003080 TEST_ASSERT( output_size <=
3081 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003082 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003083
Gilles Peskine8817f612018-12-18 00:18:46 +01003084 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003085
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003086 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3087 psa_set_key_algorithm( &attributes, alg );
3088 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003089
Gilles Peskine049c7532019-05-15 20:22:09 +02003090 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003091 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003092
Steven Cooremand588ea12021-01-11 19:36:04 +01003093 status = psa_aead_encrypt( key, alg,
3094 nonce->x, nonce->len,
3095 additional_data->x, additional_data->len,
3096 input_data->x, input_data->len,
3097 output_data, output_size,
3098 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003099
Ronald Cron28a45ed2021-02-09 20:35:42 +01003100 /* If the operation is not supported, just skip and not fail in case the
3101 * encryption involves a common limitation of cryptography hardwares and
3102 * an alternative implementation. */
3103 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003104 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003105 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3106 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003107 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003108
3109 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003110 ASSERT_COMPARE( expected_result->x, expected_result->len,
3111 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003112
Gilles Peskinea1cac842018-06-11 19:33:02 +02003113exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003114 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003115 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003116 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003117}
3118/* END_CASE */
3119
3120/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003121void aead_decrypt( int key_type_arg, data_t *key_data,
3122 int alg_arg,
3123 data_t *nonce,
3124 data_t *additional_data,
3125 data_t *input_data,
3126 data_t *expected_data,
3127 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003128{
Ronald Cron5425a212020-08-04 14:58:35 +02003129 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003130 psa_key_type_t key_type = key_type_arg;
3131 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003132 unsigned char *output_data = NULL;
3133 size_t output_size = 0;
3134 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003135 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003137 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003138 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003139
Gilles Peskine003a4a92019-05-14 16:09:40 +02003140 output_size = input_data->len - tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003141 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
gabor-mezei-armceface22021-01-21 12:26:17 +01003142 {
3143 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3144 * should be exact. */
Gilles Peskine003a4a92019-05-14 16:09:40 +02003145 TEST_EQUAL( output_size,
3146 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003147 TEST_ASSERT( output_size <=
3148 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3149 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003150 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003151
Gilles Peskine8817f612018-12-18 00:18:46 +01003152 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003153
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003154 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3155 psa_set_key_algorithm( &attributes, alg );
3156 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003157
Gilles Peskine049c7532019-05-15 20:22:09 +02003158 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003159 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003160
Steven Cooremand588ea12021-01-11 19:36:04 +01003161 status = psa_aead_decrypt( key, alg,
3162 nonce->x, nonce->len,
3163 additional_data->x,
3164 additional_data->len,
3165 input_data->x, input_data->len,
3166 output_data, output_size,
3167 &output_length );
3168
Ronald Cron28a45ed2021-02-09 20:35:42 +01003169 /* If the operation is not supported, just skip and not fail in case the
3170 * decryption involves a common limitation of cryptography hardwares and
3171 * an alternative implementation. */
3172 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003173 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003174 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3175 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003176 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003177
3178 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003179
Gilles Peskine2d277862018-06-18 15:41:12 +02003180 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003181 ASSERT_COMPARE( expected_data->x, expected_data->len,
3182 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003183
Gilles Peskinea1cac842018-06-11 19:33:02 +02003184exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003185 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003186 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003187 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003188}
3189/* END_CASE */
3190
3191/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003192void signature_size( int type_arg,
3193 int bits,
3194 int alg_arg,
3195 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003196{
3197 psa_key_type_t type = type_arg;
3198 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003199 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003200
Gilles Peskinefe11b722018-12-18 00:24:04 +01003201 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003202#if defined(MBEDTLS_TEST_DEPRECATED)
3203 TEST_EQUAL( actual_size,
3204 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3205#endif /* MBEDTLS_TEST_DEPRECATED */
3206
Gilles Peskinee59236f2018-01-27 23:32:46 +01003207exit:
3208 ;
3209}
3210/* END_CASE */
3211
3212/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003213void sign_deterministic( int key_type_arg, data_t *key_data,
3214 int alg_arg, data_t *input_data,
3215 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003216{
Ronald Cron5425a212020-08-04 14:58:35 +02003217 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003218 psa_key_type_t key_type = key_type_arg;
3219 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003220 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003221 unsigned char *signature = NULL;
3222 size_t signature_size;
3223 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003224 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003225
Gilles Peskine8817f612018-12-18 00:18:46 +01003226 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003227
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003228 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003229 psa_set_key_algorithm( &attributes, alg );
3230 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003231
Gilles Peskine049c7532019-05-15 20:22:09 +02003232 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003233 &key ) );
3234 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003235 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003236
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003237 /* Allocate a buffer which has the size advertized by the
3238 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003239 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003240 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003241 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003242 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003243 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003244
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003245 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003246 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003247 input_data->x, input_data->len,
3248 signature, signature_size,
3249 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003250 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003251 ASSERT_COMPARE( output_data->x, output_data->len,
3252 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003253
Gilles Peskine0627f982019-11-26 19:12:16 +01003254#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003255 memset( signature, 0, signature_size );
3256 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003257 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003258 input_data->x, input_data->len,
3259 signature, signature_size,
3260 &signature_length ) );
3261 ASSERT_COMPARE( output_data->x, output_data->len,
3262 signature, signature_length );
3263#endif /* MBEDTLS_TEST_DEPRECATED */
3264
Gilles Peskine20035e32018-02-03 22:44:14 +01003265exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003266 /*
3267 * Key attributes may have been returned by psa_get_key_attributes()
3268 * thus reset them as required.
3269 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003270 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003271
Ronald Cron5425a212020-08-04 14:58:35 +02003272 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003273 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003274 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003275}
3276/* END_CASE */
3277
3278/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003279void sign_fail( int key_type_arg, data_t *key_data,
3280 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003281 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003282{
Ronald Cron5425a212020-08-04 14:58:35 +02003283 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003284 psa_key_type_t key_type = key_type_arg;
3285 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003286 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003287 psa_status_t actual_status;
3288 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003289 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003290 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003291 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003292
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003293 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003294
Gilles Peskine8817f612018-12-18 00:18:46 +01003295 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003296
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003297 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003298 psa_set_key_algorithm( &attributes, alg );
3299 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003300
Gilles Peskine049c7532019-05-15 20:22:09 +02003301 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003302 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003303
Ronald Cron5425a212020-08-04 14:58:35 +02003304 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003305 input_data->x, input_data->len,
3306 signature, signature_size,
3307 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003308 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003309 /* The value of *signature_length is unspecified on error, but
3310 * whatever it is, it should be less than signature_size, so that
3311 * if the caller tries to read *signature_length bytes without
3312 * checking the error code then they don't overflow a buffer. */
3313 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003314
Gilles Peskine895242b2019-11-29 12:15:40 +01003315#if defined(MBEDTLS_TEST_DEPRECATED)
3316 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003317 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003318 input_data->x, input_data->len,
3319 signature, signature_size,
3320 &signature_length ),
3321 expected_status );
3322 TEST_ASSERT( signature_length <= signature_size );
3323#endif /* MBEDTLS_TEST_DEPRECATED */
3324
Gilles Peskine20035e32018-02-03 22:44:14 +01003325exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003326 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003327 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003328 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003329 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003330}
3331/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003332
3333/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003334void sign_verify( int key_type_arg, data_t *key_data,
3335 int alg_arg, data_t *input_data )
3336{
Ronald Cron5425a212020-08-04 14:58:35 +02003337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003338 psa_key_type_t key_type = key_type_arg;
3339 psa_algorithm_t alg = alg_arg;
3340 size_t key_bits;
3341 unsigned char *signature = NULL;
3342 size_t signature_size;
3343 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003345
Gilles Peskine8817f612018-12-18 00:18:46 +01003346 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003347
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003349 psa_set_key_algorithm( &attributes, alg );
3350 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003351
Gilles Peskine049c7532019-05-15 20:22:09 +02003352 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003353 &key ) );
3354 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003355 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003356
3357 /* Allocate a buffer which has the size advertized by the
3358 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003359 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003360 key_bits, alg );
3361 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003362 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003363 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003364
3365 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003366 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003367 input_data->x, input_data->len,
3368 signature, signature_size,
3369 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003370 /* Check that the signature length looks sensible. */
3371 TEST_ASSERT( signature_length <= signature_size );
3372 TEST_ASSERT( signature_length > 0 );
3373
3374 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003375 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003376 input_data->x, input_data->len,
3377 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003378
3379 if( input_data->len != 0 )
3380 {
3381 /* Flip a bit in the input and verify that the signature is now
3382 * detected as invalid. Flip a bit at the beginning, not at the end,
3383 * because ECDSA may ignore the last few bits of the input. */
3384 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003385 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003386 input_data->x, input_data->len,
3387 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003388 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003389 }
3390
3391exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003392 /*
3393 * Key attributes may have been returned by psa_get_key_attributes()
3394 * thus reset them as required.
3395 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003396 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003397
Ronald Cron5425a212020-08-04 14:58:35 +02003398 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003399 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003400 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003401}
3402/* END_CASE */
3403
3404/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003405void asymmetric_verify( int key_type_arg, data_t *key_data,
3406 int alg_arg, data_t *hash_data,
3407 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003408{
Ronald Cron5425a212020-08-04 14:58:35 +02003409 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003410 psa_key_type_t key_type = key_type_arg;
3411 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003413
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003414 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003415
Gilles Peskine8817f612018-12-18 00:18:46 +01003416 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003417
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003418 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003419 psa_set_key_algorithm( &attributes, alg );
3420 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003421
Gilles Peskine049c7532019-05-15 20:22:09 +02003422 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003423 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003424
Ronald Cron5425a212020-08-04 14:58:35 +02003425 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003426 hash_data->x, hash_data->len,
3427 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003428
3429#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003430 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003431 hash_data->x, hash_data->len,
3432 signature_data->x,
3433 signature_data->len ) );
3434
3435#endif /* MBEDTLS_TEST_DEPRECATED */
3436
itayzafrir5c753392018-05-08 11:18:38 +03003437exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003438 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003439 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003440 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003441}
3442/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003443
3444/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003445void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3446 int alg_arg, data_t *hash_data,
3447 data_t *signature_data,
3448 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003449{
Ronald Cron5425a212020-08-04 14:58:35 +02003450 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003451 psa_key_type_t key_type = key_type_arg;
3452 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003453 psa_status_t actual_status;
3454 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003456
Gilles Peskine8817f612018-12-18 00:18:46 +01003457 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003458
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003459 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003460 psa_set_key_algorithm( &attributes, alg );
3461 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003462
Gilles Peskine049c7532019-05-15 20:22:09 +02003463 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003464 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003465
Ronald Cron5425a212020-08-04 14:58:35 +02003466 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003467 hash_data->x, hash_data->len,
3468 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003469 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003470
Gilles Peskine895242b2019-11-29 12:15:40 +01003471#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003472 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003473 hash_data->x, hash_data->len,
3474 signature_data->x, signature_data->len ),
3475 expected_status );
3476#endif /* MBEDTLS_TEST_DEPRECATED */
3477
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003478exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003479 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003480 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003481 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003482}
3483/* END_CASE */
3484
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003485/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003486void asymmetric_encrypt( int key_type_arg,
3487 data_t *key_data,
3488 int alg_arg,
3489 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003490 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003491 int expected_output_length_arg,
3492 int expected_status_arg )
3493{
Ronald Cron5425a212020-08-04 14:58:35 +02003494 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003495 psa_key_type_t key_type = key_type_arg;
3496 psa_algorithm_t alg = alg_arg;
3497 size_t expected_output_length = expected_output_length_arg;
3498 size_t key_bits;
3499 unsigned char *output = NULL;
3500 size_t output_size;
3501 size_t output_length = ~0;
3502 psa_status_t actual_status;
3503 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003505
Gilles Peskine8817f612018-12-18 00:18:46 +01003506 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003507
Gilles Peskine656896e2018-06-29 19:12:28 +02003508 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003509 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3510 psa_set_key_algorithm( &attributes, alg );
3511 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003512 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003513 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003514
3515 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003516 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003517 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003518
Gilles Peskine656896e2018-06-29 19:12:28 +02003519 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003520 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003521 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003522
3523 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003524 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003525 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003526 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003527 output, output_size,
3528 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003529 TEST_EQUAL( actual_status, expected_status );
3530 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003531
Gilles Peskine68428122018-06-30 18:42:41 +02003532 /* If the label is empty, the test framework puts a non-null pointer
3533 * in label->x. Test that a null pointer works as well. */
3534 if( label->len == 0 )
3535 {
3536 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003537 if( output_size != 0 )
3538 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003539 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003540 input_data->x, input_data->len,
3541 NULL, label->len,
3542 output, output_size,
3543 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003544 TEST_EQUAL( actual_status, expected_status );
3545 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003546 }
3547
Gilles Peskine656896e2018-06-29 19:12:28 +02003548exit:
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 Peskine656896e2018-06-29 19:12:28 +02003556 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003557 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003558}
3559/* END_CASE */
3560
3561/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003562void asymmetric_encrypt_decrypt( int key_type_arg,
3563 data_t *key_data,
3564 int alg_arg,
3565 data_t *input_data,
3566 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567{
Ronald Cron5425a212020-08-04 14:58:35 +02003568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003569 psa_key_type_t key_type = key_type_arg;
3570 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003571 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003572 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003573 size_t output_size;
3574 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003575 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003576 size_t output2_size;
3577 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003578 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579
Gilles Peskine8817f612018-12-18 00:18:46 +01003580 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003581
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003582 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3583 psa_set_key_algorithm( &attributes, alg );
3584 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003585
Gilles Peskine049c7532019-05-15 20:22:09 +02003586 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003587 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003588
3589 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003590 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003591 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003592
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003593 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003594 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003595 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003596
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003597 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003598 TEST_ASSERT( output2_size <=
3599 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3600 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003601 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003602
Gilles Peskineeebd7382018-06-08 18:11:54 +02003603 /* We test encryption by checking that encrypt-then-decrypt gives back
3604 * the original plaintext because of the non-optional random
3605 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003606 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003607 input_data->x, input_data->len,
3608 label->x, label->len,
3609 output, output_size,
3610 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003611 /* We don't know what ciphertext length to expect, but check that
3612 * it looks sensible. */
3613 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003614
Ronald Cron5425a212020-08-04 14:58:35 +02003615 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003616 output, output_length,
3617 label->x, label->len,
3618 output2, output2_size,
3619 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003620 ASSERT_COMPARE( input_data->x, input_data->len,
3621 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003622
3623exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003624 /*
3625 * Key attributes may have been returned by psa_get_key_attributes()
3626 * thus reset them as required.
3627 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003628 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003629
Ronald Cron5425a212020-08-04 14:58:35 +02003630 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003631 mbedtls_free( output );
3632 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003633 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634}
3635/* END_CASE */
3636
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003637/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003638void asymmetric_decrypt( int key_type_arg,
3639 data_t *key_data,
3640 int alg_arg,
3641 data_t *input_data,
3642 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003643 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644{
Ronald Cron5425a212020-08-04 14:58:35 +02003645 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003646 psa_key_type_t key_type = key_type_arg;
3647 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003648 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003649 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003650 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003651 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003652 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003653
Gilles Peskine8817f612018-12-18 00:18:46 +01003654 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003655
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003656 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3657 psa_set_key_algorithm( &attributes, alg );
3658 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003659
Gilles Peskine049c7532019-05-15 20:22:09 +02003660 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003661 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662
gabor-mezei-armceface22021-01-21 12:26:17 +01003663 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3664 key_bits = psa_get_key_bits( &attributes );
3665
3666 /* Determine the maximum ciphertext length */
3667 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3668 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3669 ASSERT_ALLOC( output, output_size );
3670
Ronald Cron5425a212020-08-04 14:58:35 +02003671 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003672 input_data->x, input_data->len,
3673 label->x, label->len,
3674 output,
3675 output_size,
3676 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003677 ASSERT_COMPARE( expected_data->x, expected_data->len,
3678 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003679
Gilles Peskine68428122018-06-30 18:42:41 +02003680 /* If the label is empty, the test framework puts a non-null pointer
3681 * in label->x. Test that a null pointer works as well. */
3682 if( label->len == 0 )
3683 {
3684 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003685 if( output_size != 0 )
3686 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003687 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003688 input_data->x, input_data->len,
3689 NULL, label->len,
3690 output,
3691 output_size,
3692 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003693 ASSERT_COMPARE( expected_data->x, expected_data->len,
3694 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003695 }
3696
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003697exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003698 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003699 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003700 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003701 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003702}
3703/* END_CASE */
3704
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003705/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003706void asymmetric_decrypt_fail( int key_type_arg,
3707 data_t *key_data,
3708 int alg_arg,
3709 data_t *input_data,
3710 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003711 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003712 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003713{
Ronald Cron5425a212020-08-04 14:58:35 +02003714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003715 psa_key_type_t key_type = key_type_arg;
3716 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003717 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003718 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003719 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003720 psa_status_t actual_status;
3721 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003723
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003724 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003725
Gilles Peskine8817f612018-12-18 00:18:46 +01003726 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3729 psa_set_key_algorithm( &attributes, alg );
3730 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003731
Gilles Peskine049c7532019-05-15 20:22:09 +02003732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003733 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003734
Ronald Cron5425a212020-08-04 14:58:35 +02003735 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003736 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003737 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003738 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003739 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003740 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003741 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003742
Gilles Peskine68428122018-06-30 18:42:41 +02003743 /* If the label is empty, the test framework puts a non-null pointer
3744 * in label->x. Test that a null pointer works as well. */
3745 if( label->len == 0 )
3746 {
3747 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003748 if( output_size != 0 )
3749 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003750 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003751 input_data->x, input_data->len,
3752 NULL, label->len,
3753 output, output_size,
3754 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003755 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003756 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003757 }
3758
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003759exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003760 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003761 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02003762 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003763 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003764}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003765/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003766
3767/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003768void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00003769{
3770 /* Test each valid way of initializing the object, except for `= {0}`, as
3771 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3772 * though it's OK by the C standard. We could test for this, but we'd need
3773 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003774 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003775 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
3776 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
3777 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003778
3779 memset( &zero, 0, sizeof( zero ) );
3780
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003781 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003782 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003783 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003784 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003785 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003786 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003787 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003788
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003789 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003790 PSA_ASSERT( psa_key_derivation_abort(&func) );
3791 PSA_ASSERT( psa_key_derivation_abort(&init) );
3792 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003793}
3794/* END_CASE */
3795
Janos Follath16de4a42019-06-13 16:32:24 +01003796/* BEGIN_CASE */
3797void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02003798{
Gilles Peskineea0fb492018-07-12 17:17:20 +02003799 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003800 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003801 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003802
Gilles Peskine8817f612018-12-18 00:18:46 +01003803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003804
Janos Follath16de4a42019-06-13 16:32:24 +01003805 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003806 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003807
3808exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003809 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003810 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003811}
3812/* END_CASE */
3813
Janos Follathaf3c2a02019-06-12 12:34:34 +01003814/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01003815void derive_set_capacity( int alg_arg, int capacity_arg,
3816 int expected_status_arg )
3817{
3818 psa_algorithm_t alg = alg_arg;
3819 size_t capacity = capacity_arg;
3820 psa_status_t expected_status = expected_status_arg;
3821 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3822
3823 PSA_ASSERT( psa_crypto_init( ) );
3824
3825 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3826
3827 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
3828 expected_status );
3829
3830exit:
3831 psa_key_derivation_abort( &operation );
3832 PSA_DONE( );
3833}
3834/* END_CASE */
3835
3836/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01003837void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02003838 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003839 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02003840 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01003841 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02003842 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003843 int expected_status_arg3,
3844 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003845{
3846 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02003847 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
3848 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01003849 psa_status_t expected_statuses[] = {expected_status_arg1,
3850 expected_status_arg2,
3851 expected_status_arg3};
3852 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02003853 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
3854 MBEDTLS_SVC_KEY_ID_INIT,
3855 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01003856 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3857 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3858 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003859 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003860 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003861 psa_status_t expected_output_status = expected_output_status_arg;
3862 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01003863
3864 PSA_ASSERT( psa_crypto_init( ) );
3865
3866 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3867 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003868
3869 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
3870
3871 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
3872 {
Gilles Peskineb8965192019-09-24 16:21:10 +02003873 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01003874 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02003875 psa_set_key_type( &attributes, key_types[i] );
3876 PSA_ASSERT( psa_import_key( &attributes,
3877 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003878 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003879 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
3880 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
3881 {
3882 // When taking a private key as secret input, use key agreement
3883 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003884 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
3885 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003886 expected_statuses[i] );
3887 }
3888 else
3889 {
3890 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02003891 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02003892 expected_statuses[i] );
3893 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02003894 }
3895 else
3896 {
3897 TEST_EQUAL( psa_key_derivation_input_bytes(
3898 &operation, steps[i],
3899 inputs[i]->x, inputs[i]->len ),
3900 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003901 }
3902 }
3903
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003904 if( output_key_type != PSA_KEY_TYPE_NONE )
3905 {
3906 psa_reset_key_attributes( &attributes );
3907 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
3908 psa_set_key_bits( &attributes, 8 );
3909 actual_output_status =
3910 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02003911 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02003912 }
3913 else
3914 {
3915 uint8_t buffer[1];
3916 actual_output_status =
3917 psa_key_derivation_output_bytes( &operation,
3918 buffer, sizeof( buffer ) );
3919 }
3920 TEST_EQUAL( actual_output_status, expected_output_status );
3921
Janos Follathaf3c2a02019-06-12 12:34:34 +01003922exit:
3923 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003924 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
3925 psa_destroy_key( keys[i] );
3926 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01003927 PSA_DONE( );
3928}
3929/* END_CASE */
3930
Janos Follathd958bb72019-07-03 15:02:16 +01003931/* BEGIN_CASE */
3932void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003933{
Janos Follathd958bb72019-07-03 15:02:16 +01003934 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02003935 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003936 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003937 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01003938 unsigned char input1[] = "Input 1";
3939 size_t input1_length = sizeof( input1 );
3940 unsigned char input2[] = "Input 2";
3941 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003942 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003943 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003944 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3945 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3946 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003947 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003948
Gilles Peskine8817f612018-12-18 00:18:46 +01003949 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003950
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003951 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
3952 psa_set_key_algorithm( &attributes, alg );
3953 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003954
Gilles Peskine73676cb2019-05-15 20:15:10 +02003955 PSA_ASSERT( psa_import_key( &attributes,
3956 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02003957 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003958
3959 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01003960 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
3961 input1, input1_length,
3962 input2, input2_length,
3963 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01003964 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003965
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003966 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01003967 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003968 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003969
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003970 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003971
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003972 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02003973 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003974
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003975exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003976 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003977 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003978 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003979}
3980/* END_CASE */
3981
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003982/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003983void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003984{
3985 uint8_t output_buffer[16];
3986 size_t buffer_size = 16;
3987 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003988 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003989
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003990 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
3991 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003992 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003993
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003994 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003995 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003996
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003997 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003998
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003999 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4000 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004001 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004002
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004003 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004004 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004005
4006exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004007 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004008}
4009/* END_CASE */
4010
4011/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004012void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004013 int step1_arg, data_t *input1,
4014 int step2_arg, data_t *input2,
4015 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004016 int requested_capacity_arg,
4017 data_t *expected_output1,
4018 data_t *expected_output2 )
4019{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004020 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004021 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4022 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004023 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4024 MBEDTLS_SVC_KEY_ID_INIT,
4025 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004026 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004027 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004028 uint8_t *expected_outputs[2] =
4029 {expected_output1->x, expected_output2->x};
4030 size_t output_sizes[2] =
4031 {expected_output1->len, expected_output2->len};
4032 size_t output_buffer_size = 0;
4033 uint8_t *output_buffer = NULL;
4034 size_t expected_capacity;
4035 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004036 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004037 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004038 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004039
4040 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4041 {
4042 if( output_sizes[i] > output_buffer_size )
4043 output_buffer_size = output_sizes[i];
4044 if( output_sizes[i] == 0 )
4045 expected_outputs[i] = NULL;
4046 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004047 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004048 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004049
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004050 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4051 psa_set_key_algorithm( &attributes, alg );
4052 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004053
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004054 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004055 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4056 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4057 requested_capacity ) );
4058 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004059 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004060 switch( steps[i] )
4061 {
4062 case 0:
4063 break;
4064 case PSA_KEY_DERIVATION_INPUT_SECRET:
4065 PSA_ASSERT( psa_import_key( &attributes,
4066 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004067 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004068
4069 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4070 {
4071 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4072 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4073 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4074 }
4075
Gilles Peskine1468da72019-05-29 17:35:49 +02004076 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004077 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004078 break;
4079 default:
4080 PSA_ASSERT( psa_key_derivation_input_bytes(
4081 &operation, steps[i],
4082 inputs[i]->x, inputs[i]->len ) );
4083 break;
4084 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004085 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004086
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004087 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004088 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004089 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004090 expected_capacity = requested_capacity;
4091
4092 /* Expansion phase. */
4093 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4094 {
4095 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004096 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004097 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004098 if( expected_capacity == 0 && output_sizes[i] == 0 )
4099 {
4100 /* Reading 0 bytes when 0 bytes are available can go either way. */
4101 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004102 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004103 continue;
4104 }
4105 else if( expected_capacity == 0 ||
4106 output_sizes[i] > expected_capacity )
4107 {
4108 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004109 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004110 expected_capacity = 0;
4111 continue;
4112 }
4113 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004114 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004115 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004116 ASSERT_COMPARE( output_buffer, output_sizes[i],
4117 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004118 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004119 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004120 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004121 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004122 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004123 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004124 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004125
4126exit:
4127 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004128 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004129 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4130 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004131 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004132}
4133/* END_CASE */
4134
4135/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004136void derive_full( int alg_arg,
4137 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004138 data_t *input1,
4139 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004140 int requested_capacity_arg )
4141{
Ronald Cron5425a212020-08-04 14:58:35 +02004142 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004143 psa_algorithm_t alg = alg_arg;
4144 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004145 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004146 unsigned char output_buffer[16];
4147 size_t expected_capacity = requested_capacity;
4148 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004150
Gilles Peskine8817f612018-12-18 00:18:46 +01004151 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004152
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4154 psa_set_key_algorithm( &attributes, alg );
4155 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004156
Gilles Peskine049c7532019-05-15 20:22:09 +02004157 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004158 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004159
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004160 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4161 input1->x, input1->len,
4162 input2->x, input2->len,
4163 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004164 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004165
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004166 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004167 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004168 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004169
4170 /* Expansion phase. */
4171 while( current_capacity > 0 )
4172 {
4173 size_t read_size = sizeof( output_buffer );
4174 if( read_size > current_capacity )
4175 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004176 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004177 output_buffer,
4178 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004179 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004180 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004181 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004182 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004183 }
4184
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004185 /* Check that the operation refuses to go over capacity. */
4186 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004187 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004188
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004189 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004190
4191exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004192 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004193 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004194 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004195}
4196/* END_CASE */
4197
Janos Follathe60c9052019-07-03 13:51:30 +01004198/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004199void derive_key_exercise( int alg_arg,
4200 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004201 data_t *input1,
4202 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004203 int derived_type_arg,
4204 int derived_bits_arg,
4205 int derived_usage_arg,
4206 int derived_alg_arg )
4207{
Ronald Cron5425a212020-08-04 14:58:35 +02004208 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4209 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004210 psa_algorithm_t alg = alg_arg;
4211 psa_key_type_t derived_type = derived_type_arg;
4212 size_t derived_bits = derived_bits_arg;
4213 psa_key_usage_t derived_usage = derived_usage_arg;
4214 psa_algorithm_t derived_alg = derived_alg_arg;
4215 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004216 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004218 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004219
Gilles Peskine8817f612018-12-18 00:18:46 +01004220 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004221
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004222 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4223 psa_set_key_algorithm( &attributes, alg );
4224 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004225 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004226 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004227
4228 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004229 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4230 input1->x, input1->len,
4231 input2->x, input2->len,
4232 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004233 goto exit;
4234
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004235 psa_set_key_usage_flags( &attributes, derived_usage );
4236 psa_set_key_algorithm( &attributes, derived_alg );
4237 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004238 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004239 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004240 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004241
4242 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004243 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004244 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4245 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004246
4247 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004248 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004249 goto exit;
4250
4251exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004252 /*
4253 * Key attributes may have been returned by psa_get_key_attributes()
4254 * thus reset them as required.
4255 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004256 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004257
4258 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004259 psa_destroy_key( base_key );
4260 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004261 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004262}
4263/* END_CASE */
4264
Janos Follath42fd8882019-07-03 14:17:09 +01004265/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004266void derive_key_export( int alg_arg,
4267 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004268 data_t *input1,
4269 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004270 int bytes1_arg,
4271 int bytes2_arg )
4272{
Ronald Cron5425a212020-08-04 14:58:35 +02004273 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4274 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004275 psa_algorithm_t alg = alg_arg;
4276 size_t bytes1 = bytes1_arg;
4277 size_t bytes2 = bytes2_arg;
4278 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004279 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004280 uint8_t *output_buffer = NULL;
4281 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004282 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4283 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004284 size_t length;
4285
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004286 ASSERT_ALLOC( output_buffer, capacity );
4287 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004288 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004289
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004290 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4291 psa_set_key_algorithm( &base_attributes, alg );
4292 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004293 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004294 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004295
4296 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004297 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4298 input1->x, input1->len,
4299 input2->x, input2->len,
4300 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004301 goto exit;
4302
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004303 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004304 output_buffer,
4305 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004306 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004307
4308 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004309 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4310 input1->x, input1->len,
4311 input2->x, input2->len,
4312 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004313 goto exit;
4314
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004315 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4316 psa_set_key_algorithm( &derived_attributes, 0 );
4317 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004318 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004319 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004320 &derived_key ) );
4321 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004322 export_buffer, bytes1,
4323 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004324 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004325 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004326 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004327 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004328 &derived_key ) );
4329 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004330 export_buffer + bytes1, bytes2,
4331 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004332 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004333
4334 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004335 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4336 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004337
4338exit:
4339 mbedtls_free( output_buffer );
4340 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004341 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004342 psa_destroy_key( base_key );
4343 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004344 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004345}
4346/* END_CASE */
4347
4348/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004349void derive_key( int alg_arg,
4350 data_t *key_data, data_t *input1, data_t *input2,
4351 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004352 int expected_status_arg,
4353 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004354{
Ronald Cron5425a212020-08-04 14:58:35 +02004355 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4356 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004357 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004358 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004359 size_t bits = bits_arg;
4360 psa_status_t expected_status = expected_status_arg;
4361 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4362 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4363 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4364
4365 PSA_ASSERT( psa_crypto_init( ) );
4366
4367 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4368 psa_set_key_algorithm( &base_attributes, alg );
4369 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4370 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004371 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004372
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004373 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4374 input1->x, input1->len,
4375 input2->x, input2->len,
4376 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004377 goto exit;
4378
4379 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4380 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004381 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004382 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004383
4384 psa_status_t status =
4385 psa_key_derivation_output_key( &derived_attributes,
4386 &operation,
4387 &derived_key );
4388 if( is_large_output > 0 )
4389 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4390 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004391
4392exit:
4393 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004394 psa_destroy_key( base_key );
4395 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004396 PSA_DONE( );
4397}
4398/* END_CASE */
4399
4400/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004401void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004402 int our_key_type_arg, int our_key_alg_arg,
4403 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004404 int expected_status_arg )
4405{
Ronald Cron5425a212020-08-04 14:58:35 +02004406 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004407 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004408 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004409 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004410 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004412 psa_status_t expected_status = expected_status_arg;
4413 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004414
Gilles Peskine8817f612018-12-18 00:18:46 +01004415 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004416
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004417 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004418 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004419 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004420 PSA_ASSERT( psa_import_key( &attributes,
4421 our_key_data->x, our_key_data->len,
4422 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004423
Gilles Peskine77f40d82019-04-11 21:27:06 +02004424 /* The tests currently include inputs that should fail at either step.
4425 * Test cases that fail at the setup step should be changed to call
4426 * key_derivation_setup instead, and this function should be renamed
4427 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004429 if( status == PSA_SUCCESS )
4430 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004431 TEST_EQUAL( psa_key_derivation_key_agreement(
4432 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4433 our_key,
4434 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004435 expected_status );
4436 }
4437 else
4438 {
4439 TEST_ASSERT( status == expected_status );
4440 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004441
4442exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004444 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004445 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004446}
4447/* END_CASE */
4448
4449/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004450void raw_key_agreement( int alg_arg,
4451 int our_key_type_arg, data_t *our_key_data,
4452 data_t *peer_key_data,
4453 data_t *expected_output )
4454{
Ronald Cron5425a212020-08-04 14:58:35 +02004455 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004456 psa_algorithm_t alg = alg_arg;
4457 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004459 unsigned char *output = NULL;
4460 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004461 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004462
4463 ASSERT_ALLOC( output, expected_output->len );
4464 PSA_ASSERT( psa_crypto_init( ) );
4465
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004466 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4467 psa_set_key_algorithm( &attributes, alg );
4468 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004469 PSA_ASSERT( psa_import_key( &attributes,
4470 our_key_data->x, our_key_data->len,
4471 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004472
gabor-mezei-armceface22021-01-21 12:26:17 +01004473 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4474 key_bits = psa_get_key_bits( &attributes );
4475
Gilles Peskinebe697d82019-05-16 18:00:41 +02004476 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4477 peer_key_data->x, peer_key_data->len,
4478 output, expected_output->len,
4479 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004480 ASSERT_COMPARE( output, output_length,
4481 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004482 TEST_ASSERT( output_length <=
4483 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4484 TEST_ASSERT( output_length <=
4485 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004486
4487exit:
4488 mbedtls_free( output );
4489 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004490 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004491}
4492/* END_CASE */
4493
4494/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004495void key_agreement_capacity( int alg_arg,
4496 int our_key_type_arg, data_t *our_key_data,
4497 data_t *peer_key_data,
4498 int expected_capacity_arg )
4499{
Ronald Cron5425a212020-08-04 14:58:35 +02004500 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004501 psa_algorithm_t alg = alg_arg;
4502 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004503 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004505 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004506 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004507
Gilles Peskine8817f612018-12-18 00:18:46 +01004508 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004509
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004510 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4511 psa_set_key_algorithm( &attributes, alg );
4512 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004513 PSA_ASSERT( psa_import_key( &attributes,
4514 our_key_data->x, our_key_data->len,
4515 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004516
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004517 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004518 PSA_ASSERT( psa_key_derivation_key_agreement(
4519 &operation,
4520 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4521 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004522 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4523 {
4524 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004525 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004526 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004527 NULL, 0 ) );
4528 }
Gilles Peskine59685592018-09-18 12:11:34 +02004529
Gilles Peskinebf491972018-10-25 22:36:12 +02004530 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004531 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004533 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004534
Gilles Peskinebf491972018-10-25 22:36:12 +02004535 /* Test the actual capacity by reading the output. */
4536 while( actual_capacity > sizeof( output ) )
4537 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004538 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004539 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004540 actual_capacity -= sizeof( output );
4541 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004542 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004543 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004544 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004545 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004546
Gilles Peskine59685592018-09-18 12:11:34 +02004547exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004548 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004549 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004550 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004551}
4552/* END_CASE */
4553
4554/* BEGIN_CASE */
4555void key_agreement_output( int alg_arg,
4556 int our_key_type_arg, data_t *our_key_data,
4557 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004558 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004559{
Ronald Cron5425a212020-08-04 14:58:35 +02004560 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004561 psa_algorithm_t alg = alg_arg;
4562 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004563 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004565 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004566
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004567 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4568 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004569
Gilles Peskine8817f612018-12-18 00:18:46 +01004570 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004571
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004572 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4573 psa_set_key_algorithm( &attributes, alg );
4574 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004575 PSA_ASSERT( psa_import_key( &attributes,
4576 our_key_data->x, our_key_data->len,
4577 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004578
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004579 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004580 PSA_ASSERT( psa_key_derivation_key_agreement(
4581 &operation,
4582 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4583 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004584 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4585 {
4586 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004587 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004588 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004589 NULL, 0 ) );
4590 }
Gilles Peskine59685592018-09-18 12:11:34 +02004591
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004592 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004593 actual_output,
4594 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004595 ASSERT_COMPARE( actual_output, expected_output1->len,
4596 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004597 if( expected_output2->len != 0 )
4598 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004599 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004600 actual_output,
4601 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004602 ASSERT_COMPARE( actual_output, expected_output2->len,
4603 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004604 }
Gilles Peskine59685592018-09-18 12:11:34 +02004605
4606exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004607 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004608 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004609 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004610 mbedtls_free( actual_output );
4611}
4612/* END_CASE */
4613
4614/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004615void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004616{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004617 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004618 unsigned char *output = NULL;
4619 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004620 size_t i;
4621 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004622
Simon Butcher49f8e312020-03-03 15:51:50 +00004623 TEST_ASSERT( bytes_arg >= 0 );
4624
Gilles Peskine91892022021-02-08 19:50:26 +01004625 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004626 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004627
Gilles Peskine8817f612018-12-18 00:18:46 +01004628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004629
Gilles Peskinea50d7392018-06-21 10:22:13 +02004630 /* Run several times, to ensure that every output byte will be
4631 * nonzero at least once with overwhelming probability
4632 * (2^(-8*number_of_runs)). */
4633 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004634 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004635 if( bytes != 0 )
4636 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004637 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004638
Gilles Peskinea50d7392018-06-21 10:22:13 +02004639 for( i = 0; i < bytes; i++ )
4640 {
4641 if( output[i] != 0 )
4642 ++changed[i];
4643 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004644 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004645
4646 /* Check that every byte was changed to nonzero at least once. This
4647 * validates that psa_generate_random is overwriting every byte of
4648 * the output buffer. */
4649 for( i = 0; i < bytes; i++ )
4650 {
4651 TEST_ASSERT( changed[i] != 0 );
4652 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004653
4654exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004655 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004656 mbedtls_free( output );
4657 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004658}
4659/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004660
4661/* BEGIN_CASE */
4662void generate_key( int type_arg,
4663 int bits_arg,
4664 int usage_arg,
4665 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004666 int expected_status_arg,
4667 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004668{
Ronald Cron5425a212020-08-04 14:58:35 +02004669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004670 psa_key_type_t type = type_arg;
4671 psa_key_usage_t usage = usage_arg;
4672 size_t bits = bits_arg;
4673 psa_algorithm_t alg = alg_arg;
4674 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004676 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004677
Gilles Peskine8817f612018-12-18 00:18:46 +01004678 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004679
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004680 psa_set_key_usage_flags( &attributes, usage );
4681 psa_set_key_algorithm( &attributes, alg );
4682 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004683 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004684
4685 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004686 psa_status_t status = psa_generate_key( &attributes, &key );
4687
4688 if( is_large_key > 0 )
4689 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4690 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004691 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004692 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004693
4694 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004695 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004696 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4697 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004698
Gilles Peskine818ca122018-06-20 18:16:48 +02004699 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004700 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004701 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004702
4703exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004704 /*
4705 * Key attributes may have been returned by psa_get_key_attributes()
4706 * thus reset them as required.
4707 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004708 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004709
Ronald Cron5425a212020-08-04 14:58:35 +02004710 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004711 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004712}
4713/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004714
Gilles Peskinee56e8782019-04-26 17:34:02 +02004715/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4716void generate_key_rsa( int bits_arg,
4717 data_t *e_arg,
4718 int expected_status_arg )
4719{
Ronald Cron5425a212020-08-04 14:58:35 +02004720 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004721 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004722 size_t bits = bits_arg;
4723 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4724 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4725 psa_status_t expected_status = expected_status_arg;
4726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4727 uint8_t *exported = NULL;
4728 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004729 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004730 size_t exported_length = SIZE_MAX;
4731 uint8_t *e_read_buffer = NULL;
4732 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004733 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004734 size_t e_read_length = SIZE_MAX;
4735
4736 if( e_arg->len == 0 ||
4737 ( e_arg->len == 3 &&
4738 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4739 {
4740 is_default_public_exponent = 1;
4741 e_read_size = 0;
4742 }
4743 ASSERT_ALLOC( e_read_buffer, e_read_size );
4744 ASSERT_ALLOC( exported, exported_size );
4745
4746 PSA_ASSERT( psa_crypto_init( ) );
4747
4748 psa_set_key_usage_flags( &attributes, usage );
4749 psa_set_key_algorithm( &attributes, alg );
4750 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4751 e_arg->x, e_arg->len ) );
4752 psa_set_key_bits( &attributes, bits );
4753
4754 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004755 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004756 if( expected_status != PSA_SUCCESS )
4757 goto exit;
4758
4759 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004760 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004761 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4762 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4763 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4764 e_read_buffer, e_read_size,
4765 &e_read_length ) );
4766 if( is_default_public_exponent )
4767 TEST_EQUAL( e_read_length, 0 );
4768 else
4769 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4770
4771 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004772 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02004773 goto exit;
4774
4775 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02004776 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02004777 exported, exported_size,
4778 &exported_length ) );
4779 {
4780 uint8_t *p = exported;
4781 uint8_t *end = exported + exported_length;
4782 size_t len;
4783 /* RSAPublicKey ::= SEQUENCE {
4784 * modulus INTEGER, -- n
4785 * publicExponent INTEGER } -- e
4786 */
4787 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004788 MBEDTLS_ASN1_SEQUENCE |
4789 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01004790 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004791 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4792 MBEDTLS_ASN1_INTEGER ) );
4793 if( len >= 1 && p[0] == 0 )
4794 {
4795 ++p;
4796 --len;
4797 }
4798 if( e_arg->len == 0 )
4799 {
4800 TEST_EQUAL( len, 3 );
4801 TEST_EQUAL( p[0], 1 );
4802 TEST_EQUAL( p[1], 0 );
4803 TEST_EQUAL( p[2], 1 );
4804 }
4805 else
4806 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4807 }
4808
4809exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004810 /*
4811 * Key attributes may have been returned by psa_get_key_attributes() or
4812 * set by psa_set_key_domain_parameters() thus reset them as required.
4813 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004814 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004815
Ronald Cron5425a212020-08-04 14:58:35 +02004816 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004817 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004818 mbedtls_free( e_read_buffer );
4819 mbedtls_free( exported );
4820}
4821/* END_CASE */
4822
Darryl Greend49a4992018-06-18 17:27:26 +01004823/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004824void persistent_key_load_key_from_storage( data_t *data,
4825 int type_arg, int bits_arg,
4826 int usage_flags_arg, int alg_arg,
4827 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004828{
Ronald Cron71016a92020-08-28 19:01:50 +02004829 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004830 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02004831 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4832 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004833 psa_key_type_t type = type_arg;
4834 size_t bits = bits_arg;
4835 psa_key_usage_t usage_flags = usage_flags_arg;
4836 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004838 unsigned char *first_export = NULL;
4839 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004840 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004841 size_t first_exported_length;
4842 size_t second_exported_length;
4843
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004844 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4845 {
4846 ASSERT_ALLOC( first_export, export_size );
4847 ASSERT_ALLOC( second_export, export_size );
4848 }
Darryl Greend49a4992018-06-18 17:27:26 +01004849
Gilles Peskine8817f612018-12-18 00:18:46 +01004850 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004851
Gilles Peskinec87af662019-05-15 16:12:22 +02004852 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004853 psa_set_key_usage_flags( &attributes, usage_flags );
4854 psa_set_key_algorithm( &attributes, alg );
4855 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004856 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004857
Darryl Green0c6575a2018-11-07 16:05:30 +00004858 switch( generation_method )
4859 {
4860 case IMPORT_KEY:
4861 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004862 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004863 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004864 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004865
Darryl Green0c6575a2018-11-07 16:05:30 +00004866 case GENERATE_KEY:
4867 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02004868 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004869 break;
4870
4871 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01004872#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004873 {
4874 /* Create base key */
4875 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4876 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4877 psa_set_key_usage_flags( &base_attributes,
4878 PSA_KEY_USAGE_DERIVE );
4879 psa_set_key_algorithm( &base_attributes, derive_alg );
4880 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004881 PSA_ASSERT( psa_import_key( &base_attributes,
4882 data->x, data->len,
4883 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004884 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004885 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004886 PSA_ASSERT( psa_key_derivation_input_key(
4887 &operation,
4888 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004889 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004891 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004892 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
4893 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004894 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004896 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02004897 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004898 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01004899#else
4900 TEST_ASSUME( ! "KDF not supported in this configuration" );
4901#endif
4902 break;
4903
4904 default:
4905 TEST_ASSERT( ! "generation_method not implemented in test" );
4906 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00004907 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004908 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004909
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004910 /* Export the key if permitted by the key policy. */
4911 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4912 {
Ronald Cron5425a212020-08-04 14:58:35 +02004913 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004914 first_export, export_size,
4915 &first_exported_length ) );
4916 if( generation_method == IMPORT_KEY )
4917 ASSERT_COMPARE( data->x, data->len,
4918 first_export, first_exported_length );
4919 }
Darryl Greend49a4992018-06-18 17:27:26 +01004920
4921 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02004922 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004923 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01004924 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004925
Darryl Greend49a4992018-06-18 17:27:26 +01004926 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02004927 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02004928 TEST_ASSERT( mbedtls_svc_key_id_equal(
4929 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004930 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4931 PSA_KEY_LIFETIME_PERSISTENT );
4932 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4933 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4934 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4935 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004936
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004937 /* Export the key again if permitted by the key policy. */
4938 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004939 {
Ronald Cron5425a212020-08-04 14:58:35 +02004940 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004941 second_export, export_size,
4942 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004943 ASSERT_COMPARE( first_export, first_exported_length,
4944 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004945 }
4946
4947 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004948 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004949 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004950
4951exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004952 /*
4953 * Key attributes may have been returned by psa_get_key_attributes()
4954 * thus reset them as required.
4955 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004956 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004957
Darryl Greend49a4992018-06-18 17:27:26 +01004958 mbedtls_free( first_export );
4959 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004961 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02004962 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004963 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01004964}
4965/* END_CASE */