blob: c2c94c10e39c111af11c8cb28ce65169b37bcca8 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskinef6279312021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Dave Rodgman34b147d2021-06-23 12:49:59 +010022/* Assert that an operation is (not) active.
23 * This serves as a proxy for checking if the operation is aborted. */
24#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
25#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
26
Jaeden Amerof24c7f82018-06-27 17:20:43 +010027/** An invalid export length that will never be set by psa_export_key(). */
28static const size_t INVALID_EXPORT_LENGTH = ~0U;
29
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030/** Test if a buffer contains a constant byte value.
31 *
32 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 *
34 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020035 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020036 * \param size Size of the buffer in bytes.
37 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020038 * \return 1 if the buffer is all-bits-zero.
39 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042{
43 size_t i;
44 for( i = 0; i < size; i++ )
45 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020046 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020047 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020049 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020050}
Gilles Peskine818ca122018-06-20 18:16:48 +020051
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
53static int asn1_write_10x( unsigned char **p,
54 unsigned char *start,
55 size_t bits,
56 unsigned char x )
57{
58 int ret;
59 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020060 if( bits == 0 )
61 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
62 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020063 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030064 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020065 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
66 *p -= len;
67 ( *p )[len-1] = x;
68 if( bits % 8 == 0 )
69 ( *p )[1] |= 1;
70 else
71 ( *p )[0] |= 1 << ( bits % 8 );
72 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
73 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
74 MBEDTLS_ASN1_INTEGER ) );
75 return( len );
76}
77
78static int construct_fake_rsa_key( unsigned char *buffer,
79 size_t buffer_size,
80 unsigned char **p,
81 size_t bits,
82 int keypair )
83{
84 size_t half_bits = ( bits + 1 ) / 2;
85 int ret;
86 int len = 0;
87 /* Construct something that looks like a DER encoding of
88 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
89 * RSAPrivateKey ::= SEQUENCE {
90 * version Version,
91 * modulus INTEGER, -- n
92 * publicExponent INTEGER, -- e
93 * privateExponent INTEGER, -- d
94 * prime1 INTEGER, -- p
95 * prime2 INTEGER, -- q
96 * exponent1 INTEGER, -- d mod (p-1)
97 * exponent2 INTEGER, -- d mod (q-1)
98 * coefficient INTEGER, -- (inverse of q) mod p
99 * otherPrimeInfos OtherPrimeInfos OPTIONAL
100 * }
101 * Or, for a public key, the same structure with only
102 * version, modulus and publicExponent.
103 */
104 *p = buffer + buffer_size;
105 if( keypair )
106 {
107 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
110 asn1_write_10x( p, buffer, half_bits, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
112 asn1_write_10x( p, buffer, half_bits, 1 ) );
113 MBEDTLS_ASN1_CHK_ADD( len, /* q */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
116 asn1_write_10x( p, buffer, half_bits, 3 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* d */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 }
120 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
121 asn1_write_10x( p, buffer, 17, 1 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, /* n */
123 asn1_write_10x( p, buffer, bits, 1 ) );
124 if( keypair )
125 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
126 mbedtls_asn1_write_int( p, buffer, 0 ) );
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
128 {
129 const unsigned char tag =
130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
131 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
132 }
133 return( len );
134}
135
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100136int exercise_mac_setup( psa_key_type_t key_type,
137 const unsigned char *key_bytes,
138 size_t key_length,
139 psa_algorithm_t alg,
140 psa_mac_operation_t *operation,
141 psa_status_t *status )
142{
Ronald Cron5425a212020-08-04 14:58:35 +0200143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200147 psa_set_key_algorithm( &attributes, alg );
148 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200149 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100150
Ronald Cron5425a212020-08-04 14:58:35 +0200151 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100152 /* Whether setup succeeded or failed, abort must succeed. */
153 PSA_ASSERT( psa_mac_abort( operation ) );
154 /* If setup failed, reproduce the failure, so that the caller can
155 * test the resulting state of the operation object. */
156 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 {
Ronald Cron5425a212020-08-04 14:58:35 +0200158 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100159 }
160
Ronald Cron5425a212020-08-04 14:58:35 +0200161 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100162 return( 1 );
163
164exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200165 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 return( 0 );
167}
168
169int exercise_cipher_setup( psa_key_type_t key_type,
170 const unsigned char *key_bytes,
171 size_t key_length,
172 psa_algorithm_t alg,
173 psa_cipher_operation_t *operation,
174 psa_status_t *status )
175{
Ronald Cron5425a212020-08-04 14:58:35 +0200176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
180 psa_set_key_algorithm( &attributes, alg );
181 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200182 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Ronald Cron5425a212020-08-04 14:58:35 +0200184 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100185 /* Whether setup succeeded or failed, abort must succeed. */
186 PSA_ASSERT( psa_cipher_abort( operation ) );
187 /* If setup failed, reproduce the failure, so that the caller can
188 * test the resulting state of the operation object. */
189 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190 {
Ronald Cron5425a212020-08-04 14:58:35 +0200191 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 return( 1 );
197
198exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200199 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 return( 0 );
201}
202
Ronald Cron5425a212020-08-04 14:58:35 +0200203static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204{
205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 uint8_t buffer[1];
208 size_t length;
209 int ok = 0;
210
Ronald Cronecfb2372020-07-23 17:13:42 +0200211 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
213 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
214 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200215 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000216 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200217 TEST_EQUAL(
218 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
219 TEST_EQUAL(
220 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200221 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
223 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
224 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
225 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
226
Ronald Cron5425a212020-08-04 14:58:35 +0200227 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000228 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200229 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000231 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233 ok = 1;
234
235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236 /*
237 * Key attributes may have been returned by psa_get_key_attributes()
238 * thus reset them as required.
239 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100241
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 return( ok );
243}
244
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200245/* Assert that a key isn't reported as having a slot number. */
246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
247#define ASSERT_NO_SLOT_NUMBER( attributes ) \
248 do \
249 { \
250 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
251 TEST_EQUAL( psa_get_key_slot_number( \
252 attributes, \
253 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
254 PSA_ERROR_INVALID_ARGUMENT ); \
255 } \
256 while( 0 )
257#else /* MBEDTLS_PSA_CRYPTO_SE_C */
258#define ASSERT_NO_SLOT_NUMBER( attributes ) \
259 ( (void) 0 )
260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
261
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100262/* An overapproximation of the amount of storage needed for a key of the
263 * given type and with the given content. The API doesn't make it easy
264 * to find a good value for the size. The current implementation doesn't
265 * care about the value anyway. */
266#define KEY_BITS_FROM_DATA( type, data ) \
267 ( data )->len
268
Darryl Green0c6575a2018-11-07 16:05:30 +0000269typedef enum {
270 IMPORT_KEY = 0,
271 GENERATE_KEY = 1,
272 DERIVE_KEY = 2
273} generate_method;
274
Gilles Peskinee59236f2018-01-27 23:32:46 +0100275/* END_HEADER */
276
277/* BEGIN_DEPENDENCIES
278 * depends_on:MBEDTLS_PSA_CRYPTO_C
279 * END_DEPENDENCIES
280 */
281
282/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200283void static_checks( )
284{
285 size_t max_truncated_mac_size =
286 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
287
288 /* Check that the length for a truncated MAC always fits in the algorithm
289 * encoding. The shifted mask is the maximum truncated value. The
290 * untruncated algorithm may be one byte larger. */
291 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +0100292
293#if defined(MBEDTLS_TEST_DEPRECATED)
294 /* Check deprecated constants. */
295 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
296 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
297 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
298 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
299 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
300 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
301 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
302 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100303
Paul Elliott8ff510a2020-06-02 17:19:28 +0100304 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
305 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
306 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
307 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
308 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
309 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
310 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
311 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
312 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
313 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
314 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
315 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
316 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
317 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
318 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
319 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
320 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
321 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
322 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
323 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
324 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
325 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
326 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
327 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
328 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
329 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
330 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
331 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
332 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
333 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
334
335 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
336 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
337 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
338 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
339 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
340 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
341 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
342 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100343
Paul Elliott75e27032020-06-03 15:17:39 +0100344 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
345 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
346 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
347 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
348 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
349
350 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
351 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +0100352#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200353}
354/* END_CASE */
355
356/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200357void import_with_policy( int type_arg,
358 int usage_arg, int alg_arg,
359 int expected_status_arg )
360{
361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
362 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200363 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200364 psa_key_type_t type = type_arg;
365 psa_key_usage_t usage = usage_arg;
366 psa_algorithm_t alg = alg_arg;
367 psa_status_t expected_status = expected_status_arg;
368 const uint8_t key_material[16] = {0};
369 psa_status_t status;
370
371 PSA_ASSERT( psa_crypto_init( ) );
372
373 psa_set_key_type( &attributes, type );
374 psa_set_key_usage_flags( &attributes, usage );
375 psa_set_key_algorithm( &attributes, alg );
376
377 status = psa_import_key( &attributes,
378 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200379 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200380 TEST_EQUAL( status, expected_status );
381 if( status != PSA_SUCCESS )
382 goto exit;
383
Ronald Cron5425a212020-08-04 14:58:35 +0200384 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200385 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +0200386 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200387 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200388 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200389 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200390
Ronald Cron5425a212020-08-04 14:58:35 +0200391 PSA_ASSERT( psa_destroy_key( key ) );
392 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200393
394exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100395 /*
396 * Key attributes may have been returned by psa_get_key_attributes()
397 * thus reset them as required.
398 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200399 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100400
401 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200402 PSA_DONE( );
403}
404/* END_CASE */
405
406/* BEGIN_CASE */
407void import_with_data( data_t *data, int type_arg,
408 int attr_bits_arg,
409 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200410{
411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
412 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200413 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200414 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200415 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200416 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100417 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100418
Gilles Peskine8817f612018-12-18 00:18:46 +0100419 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100420
Gilles Peskine4747d192019-04-17 15:05:45 +0200421 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200422 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200423
Ronald Cron5425a212020-08-04 14:58:35 +0200424 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100425 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200426 if( status != PSA_SUCCESS )
427 goto exit;
428
Ronald Cron5425a212020-08-04 14:58:35 +0200429 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200430 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200431 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200432 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200433 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200434
Ronald Cron5425a212020-08-04 14:58:35 +0200435 PSA_ASSERT( psa_destroy_key( key ) );
436 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100437
438exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100439 /*
440 * Key attributes may have been returned by psa_get_key_attributes()
441 * thus reset them as required.
442 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200443 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100444
445 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200446 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100447}
448/* END_CASE */
449
450/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200451void import_large_key( int type_arg, int byte_size_arg,
452 int expected_status_arg )
453{
454 psa_key_type_t type = type_arg;
455 size_t byte_size = byte_size_arg;
456 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
457 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200459 psa_status_t status;
460 uint8_t *buffer = NULL;
461 size_t buffer_size = byte_size + 1;
462 size_t n;
463
Steven Cooreman69967ce2021-01-18 18:01:08 +0100464 /* Skip the test case if the target running the test cannot
465 * accomodate large keys due to heap size constraints */
466 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200467 memset( buffer, 'K', byte_size );
468
469 PSA_ASSERT( psa_crypto_init( ) );
470
471 /* Try importing the key */
472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
473 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200474 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100475 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200476 TEST_EQUAL( status, expected_status );
477
478 if( status == PSA_SUCCESS )
479 {
Ronald Cron5425a212020-08-04 14:58:35 +0200480 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200481 TEST_EQUAL( psa_get_key_type( &attributes ), type );
482 TEST_EQUAL( psa_get_key_bits( &attributes ),
483 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200484 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200485 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200486 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200487 for( n = 0; n < byte_size; n++ )
488 TEST_EQUAL( buffer[n], 'K' );
489 for( n = byte_size; n < buffer_size; n++ )
490 TEST_EQUAL( buffer[n], 0 );
491 }
492
493exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100494 /*
495 * Key attributes may have been returned by psa_get_key_attributes()
496 * thus reset them as required.
497 */
498 psa_reset_key_attributes( &attributes );
499
Ronald Cron5425a212020-08-04 14:58:35 +0200500 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200501 PSA_DONE( );
502 mbedtls_free( buffer );
503}
504/* END_CASE */
505
506/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200507void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
508{
Ronald Cron5425a212020-08-04 14:58:35 +0200509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200510 size_t bits = bits_arg;
511 psa_status_t expected_status = expected_status_arg;
512 psa_status_t status;
513 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200514 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200515 size_t buffer_size = /* Slight overapproximations */
516 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200517 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200518 unsigned char *p;
519 int ret;
520 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200522
Gilles Peskine8817f612018-12-18 00:18:46 +0100523 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200524 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200525
526 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
527 bits, keypair ) ) >= 0 );
528 length = ret;
529
530 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200531 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200532 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100533 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200534
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200535 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200536 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200537
538exit:
539 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200540 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200541}
542/* END_CASE */
543
544/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300545void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300546 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200547 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 int expected_bits,
549 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200550 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 int canonical_input )
552{
Ronald Cron5425a212020-08-04 14:58:35 +0200553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200555 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200556 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100557 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558 unsigned char *exported = NULL;
559 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100560 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100561 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200564 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565
Moran Pekercb088e72018-07-17 17:36:59 +0300566 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200567 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200569 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100570 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571
Gilles Peskine4747d192019-04-17 15:05:45 +0200572 psa_set_key_usage_flags( &attributes, usage_arg );
573 psa_set_key_algorithm( &attributes, alg );
574 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700575
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100576 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200577 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578
579 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200580 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200581 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
582 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200583 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584
585 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200586 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100587 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100588
589 /* The exported length must be set by psa_export_key() to a value between 0
590 * and export_size. On errors, the exported length must be 0. */
591 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
592 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
593 TEST_ASSERT( exported_length <= export_size );
594
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200595 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200596 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200598 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100599 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200601 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100602
Gilles Peskineea38a922021-02-13 00:05:16 +0100603 /* Run sanity checks on the exported key. For non-canonical inputs,
604 * this validates the canonical representations. For canonical inputs,
605 * this doesn't directly validate the implementation, but it still helps
606 * by cross-validating the test data with the sanity check code. */
607 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200608 goto exit;
609
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200611 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100612 else
613 {
Ronald Cron5425a212020-08-04 14:58:35 +0200614 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200615 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200616 &key2 ) );
617 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100618 reexported,
619 export_size,
620 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200621 ASSERT_COMPARE( exported, exported_length,
622 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200623 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100625 TEST_ASSERT( exported_length <=
626 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
627 psa_get_key_bits( &got_attributes ) ) );
628 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629
630destroy:
631 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200632 PSA_ASSERT( psa_destroy_key( key ) );
633 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634
635exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100636 /*
637 * Key attributes may have been returned by psa_get_key_attributes()
638 * thus reset them as required.
639 */
640 psa_reset_key_attributes( &got_attributes );
641
itayzafrir3e02b3b2018-06-12 17:06:52 +0300642 mbedtls_free( exported );
643 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200644 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645}
646/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100647
Moran Pekerf709f4a2018-06-06 17:26:04 +0300648/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300649void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200650 int type_arg,
651 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100652 int export_size_delta,
653 int expected_export_status_arg,
654 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300655{
Ronald Cron5425a212020-08-04 14:58:35 +0200656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300657 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200658 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200659 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300660 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300661 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100662 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100663 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300665
Gilles Peskine8817f612018-12-18 00:18:46 +0100666 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300667
Gilles Peskine4747d192019-04-17 15:05:45 +0200668 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
669 psa_set_key_algorithm( &attributes, alg );
670 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300671
672 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300674
Gilles Peskine49c25912018-10-29 15:15:31 +0100675 /* Export the public key */
676 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200677 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200678 exported, export_size,
679 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100680 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100681 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100682 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200683 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100684 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200685 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200686 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100687 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100688 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100689 TEST_ASSERT( expected_public_key->len <=
690 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
691 TEST_ASSERT( expected_public_key->len <=
692 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100693 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
694 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100695 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300696
697exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100698 /*
699 * Key attributes may have been returned by psa_get_key_attributes()
700 * thus reset them as required.
701 */
702 psa_reset_key_attributes( &attributes );
703
itayzafrir3e02b3b2018-06-12 17:06:52 +0300704 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200705 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200706 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300707}
708/* END_CASE */
709
Gilles Peskine20035e32018-02-03 22:44:14 +0100710/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200711void import_and_exercise_key( data_t *data,
712 int type_arg,
713 int bits_arg,
714 int alg_arg )
715{
Ronald Cron5425a212020-08-04 14:58:35 +0200716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200717 psa_key_type_t type = type_arg;
718 size_t bits = bits_arg;
719 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100720 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200722 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200723
Gilles Peskine8817f612018-12-18 00:18:46 +0100724 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200725
Gilles Peskine4747d192019-04-17 15:05:45 +0200726 psa_set_key_usage_flags( &attributes, usage );
727 psa_set_key_algorithm( &attributes, alg );
728 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200729
730 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200731 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200732
733 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200734 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200735 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
736 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200737
738 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100739 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200740 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200741
Ronald Cron5425a212020-08-04 14:58:35 +0200742 PSA_ASSERT( psa_destroy_key( key ) );
743 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200744
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200745exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100746 /*
747 * Key attributes may have been returned by psa_get_key_attributes()
748 * thus reset them as required.
749 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200750 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100751
752 psa_reset_key_attributes( &attributes );
753 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200754 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200755}
756/* END_CASE */
757
758/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100759void effective_key_attributes( int type_arg, int expected_type_arg,
760 int bits_arg, int expected_bits_arg,
761 int usage_arg, int expected_usage_arg,
762 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200763{
Ronald Cron5425a212020-08-04 14:58:35 +0200764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100765 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100766 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100767 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100768 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200769 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100770 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200771 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100772 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200774
Gilles Peskine8817f612018-12-18 00:18:46 +0100775 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200776
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200777 psa_set_key_usage_flags( &attributes, usage );
778 psa_set_key_algorithm( &attributes, alg );
779 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100780 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200781
Ronald Cron5425a212020-08-04 14:58:35 +0200782 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100783 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200784
Ronald Cron5425a212020-08-04 14:58:35 +0200785 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100786 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
787 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
788 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
789 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200790
791exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100792 /*
793 * Key attributes may have been returned by psa_get_key_attributes()
794 * thus reset them as required.
795 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200796 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100797
798 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200799 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200800}
801/* END_CASE */
802
803/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100804void check_key_policy( int type_arg, int bits_arg,
805 int usage_arg, int alg_arg )
806{
807 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-arm58e510f2021-06-28 14:36:03 +0200808 usage_arg,
809 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200810 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +0100811 goto exit;
812}
813/* END_CASE */
814
815/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200816void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000817{
818 /* Test each valid way of initializing the object, except for `= {0}`, as
819 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
820 * though it's OK by the C standard. We could test for this, but we'd need
821 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200822 psa_key_attributes_t func = psa_key_attributes_init( );
823 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
824 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000825
826 memset( &zero, 0, sizeof( zero ) );
827
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200828 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
829 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
830 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000831
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200832 TEST_EQUAL( psa_get_key_type( &func ), 0 );
833 TEST_EQUAL( psa_get_key_type( &init ), 0 );
834 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
835
836 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
837 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
838 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
839
840 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
841 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
842 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
843
844 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
845 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
846 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000847}
848/* END_CASE */
849
850/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200851void mac_key_policy( int policy_usage_arg,
852 int policy_alg_arg,
853 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200854 data_t *key_data,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200855 int exercise_alg_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100856 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200857{
Ronald Cron5425a212020-08-04 14:58:35 +0200858 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000860 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200861 psa_key_type_t key_type = key_type_arg;
862 psa_algorithm_t policy_alg = policy_alg_arg;
863 psa_algorithm_t exercise_alg = exercise_alg_arg;
864 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200865 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100866 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200867 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200868
Gilles Peskine8817f612018-12-18 00:18:46 +0100869 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200870
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200871 psa_set_key_usage_flags( &attributes, policy_usage );
872 psa_set_key_algorithm( &attributes, policy_alg );
873 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200874
Gilles Peskine049c7532019-05-15 20:22:09 +0200875 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200876 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200877
gabor-mezei-arm659af9e2021-06-29 11:06:16 +0200878 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
879 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200880
Ronald Cron5425a212020-08-04 14:58:35 +0200881 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100882 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100883 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100884 else
885 TEST_EQUAL( status, expected_status );
886
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200887 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200888
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200889 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200890 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100891 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100892 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100893 else
894 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200895
896exit:
897 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200898 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200899 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200900}
901/* END_CASE */
902
903/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200904void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200905 int policy_alg,
906 int key_type,
907 data_t *key_data,
908 int exercise_alg )
909{
Ronald Cron5425a212020-08-04 14:58:35 +0200910 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000912 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200913 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200914 psa_status_t status;
915
Gilles Peskine8817f612018-12-18 00:18:46 +0100916 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200917
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200918 psa_set_key_usage_flags( &attributes, policy_usage );
919 psa_set_key_algorithm( &attributes, policy_alg );
920 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200921
Gilles Peskine049c7532019-05-15 20:22:09 +0200922 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200923 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200924
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200925 /* Check if no key usage flag implication is done */
926 TEST_EQUAL( policy_usage,
927 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200928
Ronald Cron5425a212020-08-04 14:58:35 +0200929 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200930 if( policy_alg == exercise_alg &&
931 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100932 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200933 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100934 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200935 psa_cipher_abort( &operation );
936
Ronald Cron5425a212020-08-04 14:58:35 +0200937 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200938 if( policy_alg == exercise_alg &&
939 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100940 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200941 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100942 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200943
944exit:
945 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200946 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200947 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200948}
949/* END_CASE */
950
951/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200952void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200953 int policy_alg,
954 int key_type,
955 data_t *key_data,
956 int nonce_length_arg,
957 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100958 int exercise_alg,
959 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200960{
Ronald Cron5425a212020-08-04 14:58:35 +0200961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200963 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200964 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100965 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200966 unsigned char nonce[16] = {0};
967 size_t nonce_length = nonce_length_arg;
968 unsigned char tag[16];
969 size_t tag_length = tag_length_arg;
970 size_t output_length;
971
972 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
973 TEST_ASSERT( tag_length <= sizeof( tag ) );
974
Gilles Peskine8817f612018-12-18 00:18:46 +0100975 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200976
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200977 psa_set_key_usage_flags( &attributes, policy_usage );
978 psa_set_key_algorithm( &attributes, policy_alg );
979 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200980
Gilles Peskine049c7532019-05-15 20:22:09 +0200981 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200982 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200983
gabor-mezei-armff03fd62021-06-28 14:05:00 +0200984 /* Check if no key usage implication is done */
985 TEST_EQUAL( policy_usage,
986 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +0200987
Ronald Cron5425a212020-08-04 14:58:35 +0200988 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200989 nonce, nonce_length,
990 NULL, 0,
991 NULL, 0,
992 tag, tag_length,
993 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100994 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
995 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200996 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100997 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200998
999 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001000 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001001 nonce, nonce_length,
1002 NULL, 0,
1003 tag, tag_length,
1004 NULL, 0,
1005 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001006 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1007 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1008 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001009 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001010 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001011 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001012
1013exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001014 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001015 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016}
1017/* END_CASE */
1018
1019/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001020void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001021 int policy_alg,
1022 int key_type,
1023 data_t *key_data,
1024 int exercise_alg )
1025{
Ronald Cron5425a212020-08-04 14:58:35 +02001026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001028 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001029 psa_status_t status;
1030 size_t key_bits;
1031 size_t buffer_length;
1032 unsigned char *buffer = NULL;
1033 size_t output_length;
1034
Gilles Peskine8817f612018-12-18 00:18:46 +01001035 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001036
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001037 psa_set_key_usage_flags( &attributes, policy_usage );
1038 psa_set_key_algorithm( &attributes, policy_alg );
1039 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001040
Gilles Peskine049c7532019-05-15 20:22:09 +02001041 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001042 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001043
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001044 /* Check if no key usage implication is done */
1045 TEST_EQUAL( policy_usage,
1046 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001047
Ronald Cron5425a212020-08-04 14:58:35 +02001048 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001049 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001050 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1051 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001052 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001053
Ronald Cron5425a212020-08-04 14:58:35 +02001054 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001055 NULL, 0,
1056 NULL, 0,
1057 buffer, buffer_length,
1058 &output_length );
1059 if( policy_alg == exercise_alg &&
1060 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001061 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001062 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001063 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001064
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001065 if( buffer_length != 0 )
1066 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001067 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001068 buffer, buffer_length,
1069 NULL, 0,
1070 buffer, buffer_length,
1071 &output_length );
1072 if( policy_alg == exercise_alg &&
1073 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001074 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001075 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001076 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077
1078exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001079 /*
1080 * Key attributes may have been returned by psa_get_key_attributes()
1081 * thus reset them as required.
1082 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001083 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001084
1085 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001086 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001087 mbedtls_free( buffer );
1088}
1089/* END_CASE */
1090
1091/* BEGIN_CASE */
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001092void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001093 int policy_alg,
1094 int key_type,
1095 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001096 int exercise_alg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001097 int payload_length_arg,
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001098 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001099{
Ronald Cron5425a212020-08-04 14:58:35 +02001100 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001102 psa_key_usage_t policy_usage = policy_usage_arg;
1103 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001104 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001105 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1106 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1107 * compatible with the policy and `payload_length_arg` is supposed to be
1108 * a valid input length to sign. If `payload_length_arg <= 0`,
1109 * `exercise_alg` is supposed to be forbidden by the policy. */
1110 int compatible_alg = payload_length_arg > 0;
1111 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001112 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001113 size_t signature_length;
1114
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001115 /* Check if all implicit usage flags are deployed
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001116 in the expected usage flags. */
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001117 TEST_EQUAL( expected_usage,
1118 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001119
Gilles Peskine8817f612018-12-18 00:18:46 +01001120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001122 psa_set_key_usage_flags( &attributes, policy_usage );
1123 psa_set_key_algorithm( &attributes, policy_alg );
1124 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001125
Gilles Peskine049c7532019-05-15 20:22:09 +02001126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001127 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001128
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001129 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1130
Ronald Cron5425a212020-08-04 14:58:35 +02001131 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001132 payload, payload_length,
1133 signature, sizeof( signature ),
1134 &signature_length );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001135 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001136 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001137 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001138 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001139
1140 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001141 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001142 payload, payload_length,
1143 signature, sizeof( signature ) );
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001144 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001145 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001146 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001147 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001148
gabor-mezei-arm79df41d2021-06-28 14:53:49 +02001149 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1150 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-arm3e5f6cd2021-05-13 16:17:16 +02001151 {
1152 status = psa_sign_message( key, exercise_alg,
1153 payload, payload_length,
1154 signature, sizeof( signature ),
1155 &signature_length );
1156 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1157 PSA_ASSERT( status );
1158 else
1159 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1160
1161 memset( signature, 0, sizeof( signature ) );
1162 status = psa_verify_message( key, exercise_alg,
1163 payload, payload_length,
1164 signature, sizeof( signature ) );
1165 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1166 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1167 else
1168 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1169 }
1170
Gilles Peskined5b33222018-06-18 22:20:03 +02001171exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001172 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001173 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001174}
1175/* END_CASE */
1176
Janos Follathba3fab92019-06-11 14:50:16 +01001177/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001178void derive_key_policy( int policy_usage,
1179 int policy_alg,
1180 int key_type,
1181 data_t *key_data,
1182 int exercise_alg )
1183{
Ronald Cron5425a212020-08-04 14:58:35 +02001184 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001186 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001187 psa_status_t status;
1188
Gilles Peskine8817f612018-12-18 00:18:46 +01001189 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001190
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001191 psa_set_key_usage_flags( &attributes, policy_usage );
1192 psa_set_key_algorithm( &attributes, policy_alg );
1193 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001194
Gilles Peskine049c7532019-05-15 20:22:09 +02001195 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001196 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001197
Janos Follathba3fab92019-06-11 14:50:16 +01001198 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1199
1200 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1201 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001202 {
Janos Follathba3fab92019-06-11 14:50:16 +01001203 PSA_ASSERT( psa_key_derivation_input_bytes(
1204 &operation,
1205 PSA_KEY_DERIVATION_INPUT_SEED,
1206 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001207 }
Janos Follathba3fab92019-06-11 14:50:16 +01001208
1209 status = psa_key_derivation_input_key( &operation,
1210 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001211 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001212
Gilles Peskineea0fb492018-07-12 17:17:20 +02001213 if( policy_alg == exercise_alg &&
1214 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001215 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001216 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001217 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001218
1219exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001220 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001221 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001222 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001223}
1224/* END_CASE */
1225
1226/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001227void agreement_key_policy( int policy_usage,
1228 int policy_alg,
1229 int key_type_arg,
1230 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001231 int exercise_alg,
1232 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001233{
Ronald Cron5425a212020-08-04 14:58:35 +02001234 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001236 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001237 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001238 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001239 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001240
Gilles Peskine8817f612018-12-18 00:18:46 +01001241 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001242
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001243 psa_set_key_usage_flags( &attributes, policy_usage );
1244 psa_set_key_algorithm( &attributes, policy_alg );
1245 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001246
Gilles Peskine049c7532019-05-15 20:22:09 +02001247 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001248 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001249
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001250 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001251 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001252
Steven Cooremance48e852020-10-05 16:02:45 +02001253 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001254
1255exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001256 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001257 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001258 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001259}
1260/* END_CASE */
1261
1262/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001263void key_policy_alg2( int key_type_arg, data_t *key_data,
1264 int usage_arg, int alg_arg, int alg2_arg )
1265{
Ronald Cron5425a212020-08-04 14:58:35 +02001266 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001267 psa_key_type_t key_type = key_type_arg;
1268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1269 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1270 psa_key_usage_t usage = usage_arg;
1271 psa_algorithm_t alg = alg_arg;
1272 psa_algorithm_t alg2 = alg2_arg;
1273
1274 PSA_ASSERT( psa_crypto_init( ) );
1275
1276 psa_set_key_usage_flags( &attributes, usage );
1277 psa_set_key_algorithm( &attributes, alg );
1278 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1279 psa_set_key_type( &attributes, key_type );
1280 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001281 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001282
gabor-mezei-armff03fd62021-06-28 14:05:00 +02001283 /* Update the usage flags to obtain implicit usage flags */
1284 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001285 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001286 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1287 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1288 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1289
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001290 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001291 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001292 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001293 goto exit;
1294
1295exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001296 /*
1297 * Key attributes may have been returned by psa_get_key_attributes()
1298 * thus reset them as required.
1299 */
1300 psa_reset_key_attributes( &got_attributes );
1301
Ronald Cron5425a212020-08-04 14:58:35 +02001302 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001303 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001304}
1305/* END_CASE */
1306
1307/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001308void raw_agreement_key_policy( int policy_usage,
1309 int policy_alg,
1310 int key_type_arg,
1311 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001312 int exercise_alg,
1313 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001314{
Ronald Cron5425a212020-08-04 14:58:35 +02001315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001317 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001318 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001319 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001320 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001321
1322 PSA_ASSERT( psa_crypto_init( ) );
1323
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001324 psa_set_key_usage_flags( &attributes, policy_usage );
1325 psa_set_key_algorithm( &attributes, policy_alg );
1326 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001327
Gilles Peskine049c7532019-05-15 20:22:09 +02001328 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001329 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001330
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001331 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001332
Steven Cooremance48e852020-10-05 16:02:45 +02001333 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001334
1335exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001336 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001337 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001338 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001339}
1340/* END_CASE */
1341
1342/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001343void copy_success( int source_usage_arg,
1344 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001345 int type_arg, data_t *material,
1346 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001347 int target_usage_arg,
1348 int target_alg_arg, int target_alg2_arg,
1349 int expected_usage_arg,
1350 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001351{
Gilles Peskineca25db92019-04-19 11:43:08 +02001352 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1353 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001354 psa_key_usage_t expected_usage = expected_usage_arg;
1355 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001356 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001357 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1358 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001359 uint8_t *export_buffer = NULL;
1360
Gilles Peskine57ab7212019-01-28 13:03:09 +01001361 PSA_ASSERT( psa_crypto_init( ) );
1362
Gilles Peskineca25db92019-04-19 11:43:08 +02001363 /* Prepare the source key. */
1364 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1365 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001366 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001367 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001368 PSA_ASSERT( psa_import_key( &source_attributes,
1369 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001370 &source_key ) );
1371 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001372
Gilles Peskineca25db92019-04-19 11:43:08 +02001373 /* Prepare the target attributes. */
1374 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001375 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001376 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001377 /* Set volatile lifetime to reset the key identifier to 0. */
1378 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1379 }
1380
Gilles Peskineca25db92019-04-19 11:43:08 +02001381 if( target_usage_arg != -1 )
1382 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1383 if( target_alg_arg != -1 )
1384 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001385 if( target_alg2_arg != -1 )
1386 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001387
1388 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001389 PSA_ASSERT( psa_copy_key( source_key,
1390 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001391
1392 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001393 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001394
1395 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001396 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001397 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1398 psa_get_key_type( &target_attributes ) );
1399 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1400 psa_get_key_bits( &target_attributes ) );
1401 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1402 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001403 TEST_EQUAL( expected_alg2,
1404 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001405 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1406 {
1407 size_t length;
1408 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001409 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001410 material->len, &length ) );
1411 ASSERT_COMPARE( material->x, material->len,
1412 export_buffer, length );
1413 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001414
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001415 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001416 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001417 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001418 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001419
Ronald Cron5425a212020-08-04 14:58:35 +02001420 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001421
1422exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001423 /*
1424 * Source and target key attributes may have been returned by
1425 * psa_get_key_attributes() thus reset them as required.
1426 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001427 psa_reset_key_attributes( &source_attributes );
1428 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001429
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001430 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001431 mbedtls_free( export_buffer );
1432}
1433/* END_CASE */
1434
1435/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001436void copy_fail( int source_usage_arg,
1437 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001438 int type_arg, data_t *material,
1439 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001440 int target_usage_arg,
1441 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001442 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001443 int expected_status_arg )
1444{
1445 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1446 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001447 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1448 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001449 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001450
1451 PSA_ASSERT( psa_crypto_init( ) );
1452
1453 /* Prepare the source key. */
1454 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1455 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001456 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001457 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001458 PSA_ASSERT( psa_import_key( &source_attributes,
1459 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001460 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001461
1462 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001463 psa_set_key_id( &target_attributes, key_id );
1464 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001465 psa_set_key_type( &target_attributes, target_type_arg );
1466 psa_set_key_bits( &target_attributes, target_bits_arg );
1467 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1468 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001469 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001470
1471 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001472 TEST_EQUAL( psa_copy_key( source_key,
1473 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001474 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001475
Ronald Cron5425a212020-08-04 14:58:35 +02001476 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001477
Gilles Peskine4a644642019-05-03 17:14:08 +02001478exit:
1479 psa_reset_key_attributes( &source_attributes );
1480 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001481 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001482}
1483/* END_CASE */
1484
1485/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001486void hash_operation_init( )
1487{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001488 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001489 /* Test each valid way of initializing the object, except for `= {0}`, as
1490 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1491 * though it's OK by the C standard. We could test for this, but we'd need
1492 * to supress the Clang warning for the test. */
1493 psa_hash_operation_t func = psa_hash_operation_init( );
1494 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1495 psa_hash_operation_t zero;
1496
1497 memset( &zero, 0, sizeof( zero ) );
1498
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001499 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001500 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1501 PSA_ERROR_BAD_STATE );
1502 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1503 PSA_ERROR_BAD_STATE );
1504 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1505 PSA_ERROR_BAD_STATE );
1506
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001507 /* A default hash operation should be abortable without error. */
1508 PSA_ASSERT( psa_hash_abort( &func ) );
1509 PSA_ASSERT( psa_hash_abort( &init ) );
1510 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001511}
1512/* END_CASE */
1513
1514/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001515void hash_setup( int alg_arg,
1516 int expected_status_arg )
1517{
1518 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001519 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001520 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001521 psa_status_t status;
1522
Gilles Peskine8817f612018-12-18 00:18:46 +01001523 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001524
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001525 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001526 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001527
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001528 /* Whether setup succeeded or failed, abort must succeed. */
1529 PSA_ASSERT( psa_hash_abort( &operation ) );
1530
1531 /* If setup failed, reproduce the failure, so as to
1532 * test the resulting state of the operation object. */
1533 if( status != PSA_SUCCESS )
1534 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1535
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001536 /* Now the operation object should be reusable. */
1537#if defined(KNOWN_SUPPORTED_HASH_ALG)
1538 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1539 PSA_ASSERT( psa_hash_abort( &operation ) );
1540#endif
1541
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001542exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001543 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001544}
1545/* END_CASE */
1546
1547/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001548void hash_compute_fail( int alg_arg, data_t *input,
1549 int output_size_arg, int expected_status_arg )
1550{
1551 psa_algorithm_t alg = alg_arg;
1552 uint8_t *output = NULL;
1553 size_t output_size = output_size_arg;
1554 size_t output_length = INVALID_EXPORT_LENGTH;
1555 psa_status_t expected_status = expected_status_arg;
1556 psa_status_t status;
1557
1558 ASSERT_ALLOC( output, output_size );
1559
1560 PSA_ASSERT( psa_crypto_init( ) );
1561
1562 status = psa_hash_compute( alg, input->x, input->len,
1563 output, output_size, &output_length );
1564 TEST_EQUAL( status, expected_status );
1565 TEST_ASSERT( output_length <= output_size );
1566
1567exit:
1568 mbedtls_free( output );
1569 PSA_DONE( );
1570}
1571/* END_CASE */
1572
1573/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001574void hash_compare_fail( int alg_arg, data_t *input,
1575 data_t *reference_hash,
1576 int expected_status_arg )
1577{
1578 psa_algorithm_t alg = alg_arg;
1579 psa_status_t expected_status = expected_status_arg;
1580 psa_status_t status;
1581
1582 PSA_ASSERT( psa_crypto_init( ) );
1583
1584 status = psa_hash_compare( alg, input->x, input->len,
1585 reference_hash->x, reference_hash->len );
1586 TEST_EQUAL( status, expected_status );
1587
1588exit:
1589 PSA_DONE( );
1590}
1591/* END_CASE */
1592
1593/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001594void hash_compute_compare( int alg_arg, data_t *input,
1595 data_t *expected_output )
1596{
1597 psa_algorithm_t alg = alg_arg;
1598 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1599 size_t output_length = INVALID_EXPORT_LENGTH;
1600 size_t i;
1601
1602 PSA_ASSERT( psa_crypto_init( ) );
1603
1604 /* Compute with tight buffer */
1605 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001606 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001607 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001608 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001609 ASSERT_COMPARE( output, output_length,
1610 expected_output->x, expected_output->len );
1611
1612 /* Compute with larger buffer */
1613 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1614 output, sizeof( output ),
1615 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001616 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001617 ASSERT_COMPARE( output, output_length,
1618 expected_output->x, expected_output->len );
1619
1620 /* Compare with correct hash */
1621 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1622 output, output_length ) );
1623
1624 /* Compare with trailing garbage */
1625 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1626 output, output_length + 1 ),
1627 PSA_ERROR_INVALID_SIGNATURE );
1628
1629 /* Compare with truncated hash */
1630 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1631 output, output_length - 1 ),
1632 PSA_ERROR_INVALID_SIGNATURE );
1633
1634 /* Compare with corrupted value */
1635 for( i = 0; i < output_length; i++ )
1636 {
Chris Jones9634bb12021-01-20 15:56:42 +00001637 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001638 output[i] ^= 1;
1639 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1640 output, output_length ),
1641 PSA_ERROR_INVALID_SIGNATURE );
1642 output[i] ^= 1;
1643 }
1644
1645exit:
1646 PSA_DONE( );
1647}
1648/* END_CASE */
1649
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001650/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001651void hash_bad_order( )
1652{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001653 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001654 unsigned char input[] = "";
1655 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001656 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001657 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1658 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1659 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001660 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001661 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001662 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001663
Gilles Peskine8817f612018-12-18 00:18:46 +01001664 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001665
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001666 /* Call setup twice in a row. */
1667 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001668 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001669 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1670 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001671 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001672 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001673 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001674
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001675 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001676 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001677 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001678 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001679
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001680 /* Check that update calls abort on error. */
1681 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman54f73512021-06-24 18:14:52 +01001682 operation.id = UINT_MAX;
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001683 ASSERT_OPERATION_IS_ACTIVE( operation );
1684 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1685 PSA_ERROR_BAD_STATE );
1686 ASSERT_OPERATION_IS_INACTIVE( operation );
1687 PSA_ASSERT( psa_hash_abort( &operation ) );
1688 ASSERT_OPERATION_IS_INACTIVE( operation );
1689
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001690 /* Call update after finish. */
1691 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1692 PSA_ASSERT( psa_hash_finish( &operation,
1693 hash, sizeof( hash ), &hash_len ) );
1694 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001695 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001696 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001697
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001698 /* Call verify without calling setup beforehand. */
1699 TEST_EQUAL( psa_hash_verify( &operation,
1700 valid_hash, sizeof( valid_hash ) ),
1701 PSA_ERROR_BAD_STATE );
1702 PSA_ASSERT( psa_hash_abort( &operation ) );
1703
1704 /* Call verify after finish. */
1705 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1706 PSA_ASSERT( psa_hash_finish( &operation,
1707 hash, sizeof( hash ), &hash_len ) );
1708 TEST_EQUAL( psa_hash_verify( &operation,
1709 valid_hash, sizeof( valid_hash ) ),
1710 PSA_ERROR_BAD_STATE );
1711 PSA_ASSERT( psa_hash_abort( &operation ) );
1712
1713 /* Call verify twice in a row. */
1714 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001715 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001716 PSA_ASSERT( psa_hash_verify( &operation,
1717 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001718 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001719 TEST_EQUAL( psa_hash_verify( &operation,
1720 valid_hash, sizeof( valid_hash ) ),
1721 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001722 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001723 PSA_ASSERT( psa_hash_abort( &operation ) );
1724
1725 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001726 TEST_EQUAL( psa_hash_finish( &operation,
1727 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001728 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001729 PSA_ASSERT( psa_hash_abort( &operation ) );
1730
1731 /* Call finish twice in a row. */
1732 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1733 PSA_ASSERT( psa_hash_finish( &operation,
1734 hash, sizeof( hash ), &hash_len ) );
1735 TEST_EQUAL( psa_hash_finish( &operation,
1736 hash, sizeof( hash ), &hash_len ),
1737 PSA_ERROR_BAD_STATE );
1738 PSA_ASSERT( psa_hash_abort( &operation ) );
1739
1740 /* Call finish after calling verify. */
1741 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1742 PSA_ASSERT( psa_hash_verify( &operation,
1743 valid_hash, sizeof( valid_hash ) ) );
1744 TEST_EQUAL( psa_hash_finish( &operation,
1745 hash, sizeof( hash ), &hash_len ),
1746 PSA_ERROR_BAD_STATE );
1747 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001748
1749exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001750 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001751}
1752/* END_CASE */
1753
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001754/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001755void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001756{
1757 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001758 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1759 * appended to it */
1760 unsigned char hash[] = {
1761 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1762 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1763 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001764 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001765 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001766
Gilles Peskine8817f612018-12-18 00:18:46 +01001767 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001768
itayzafrir27e69452018-11-01 14:26:34 +02001769 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001770 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001771 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001772 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001773 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01001774 ASSERT_OPERATION_IS_INACTIVE( operation );
1775 PSA_ASSERT( psa_hash_abort( &operation ) );
1776 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001777
itayzafrir27e69452018-11-01 14:26:34 +02001778 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001779 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001780 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001781 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001782
itayzafrir27e69452018-11-01 14:26:34 +02001783 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001784 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001785 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001786 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001787
itayzafrirec93d302018-10-18 18:01:10 +03001788exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001789 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001790}
1791/* END_CASE */
1792
Ronald Cronee414c72021-03-18 18:50:08 +01001793/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001794void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001795{
1796 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001797 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001798 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001799 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001800 size_t hash_len;
1801
Gilles Peskine8817f612018-12-18 00:18:46 +01001802 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001803
itayzafrir58028322018-10-25 10:22:01 +03001804 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001805 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001806 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001807 hash, expected_size - 1, &hash_len ),
1808 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001809
1810exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001811 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001812}
1813/* END_CASE */
1814
Ronald Cronee414c72021-03-18 18:50:08 +01001815/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001816void hash_clone_source_state( )
1817{
1818 psa_algorithm_t alg = PSA_ALG_SHA_256;
1819 unsigned char hash[PSA_HASH_MAX_SIZE];
1820 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1821 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1822 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1823 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1824 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1825 size_t hash_len;
1826
1827 PSA_ASSERT( psa_crypto_init( ) );
1828 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1829
1830 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1831 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1832 PSA_ASSERT( psa_hash_finish( &op_finished,
1833 hash, sizeof( hash ), &hash_len ) );
1834 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1835 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1836
1837 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1838 PSA_ERROR_BAD_STATE );
1839
1840 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1841 PSA_ASSERT( psa_hash_finish( &op_init,
1842 hash, sizeof( hash ), &hash_len ) );
1843 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1844 PSA_ASSERT( psa_hash_finish( &op_finished,
1845 hash, sizeof( hash ), &hash_len ) );
1846 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1847 PSA_ASSERT( psa_hash_finish( &op_aborted,
1848 hash, sizeof( hash ), &hash_len ) );
1849
1850exit:
1851 psa_hash_abort( &op_source );
1852 psa_hash_abort( &op_init );
1853 psa_hash_abort( &op_setup );
1854 psa_hash_abort( &op_finished );
1855 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001856 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001857}
1858/* END_CASE */
1859
Ronald Cronee414c72021-03-18 18:50:08 +01001860/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001861void hash_clone_target_state( )
1862{
1863 psa_algorithm_t alg = PSA_ALG_SHA_256;
1864 unsigned char hash[PSA_HASH_MAX_SIZE];
1865 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1866 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1867 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1868 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1869 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1870 size_t hash_len;
1871
1872 PSA_ASSERT( psa_crypto_init( ) );
1873
1874 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1875 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1876 PSA_ASSERT( psa_hash_finish( &op_finished,
1877 hash, sizeof( hash ), &hash_len ) );
1878 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1879 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1880
1881 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1882 PSA_ASSERT( psa_hash_finish( &op_target,
1883 hash, sizeof( hash ), &hash_len ) );
1884
1885 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1886 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1887 PSA_ERROR_BAD_STATE );
1888 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1889 PSA_ERROR_BAD_STATE );
1890
1891exit:
1892 psa_hash_abort( &op_target );
1893 psa_hash_abort( &op_init );
1894 psa_hash_abort( &op_setup );
1895 psa_hash_abort( &op_finished );
1896 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001897 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001898}
1899/* END_CASE */
1900
itayzafrir58028322018-10-25 10:22:01 +03001901/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001902void mac_operation_init( )
1903{
Jaeden Amero252ef282019-02-15 14:05:35 +00001904 const uint8_t input[1] = { 0 };
1905
Jaeden Amero769ce272019-01-04 11:48:03 +00001906 /* Test each valid way of initializing the object, except for `= {0}`, as
1907 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1908 * though it's OK by the C standard. We could test for this, but we'd need
1909 * to supress the Clang warning for the test. */
1910 psa_mac_operation_t func = psa_mac_operation_init( );
1911 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1912 psa_mac_operation_t zero;
1913
1914 memset( &zero, 0, sizeof( zero ) );
1915
Jaeden Amero252ef282019-02-15 14:05:35 +00001916 /* A freshly-initialized MAC operation should not be usable. */
1917 TEST_EQUAL( psa_mac_update( &func,
1918 input, sizeof( input ) ),
1919 PSA_ERROR_BAD_STATE );
1920 TEST_EQUAL( psa_mac_update( &init,
1921 input, sizeof( input ) ),
1922 PSA_ERROR_BAD_STATE );
1923 TEST_EQUAL( psa_mac_update( &zero,
1924 input, sizeof( input ) ),
1925 PSA_ERROR_BAD_STATE );
1926
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001927 /* A default MAC operation should be abortable without error. */
1928 PSA_ASSERT( psa_mac_abort( &func ) );
1929 PSA_ASSERT( psa_mac_abort( &init ) );
1930 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001931}
1932/* END_CASE */
1933
1934/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001935void mac_setup( int key_type_arg,
1936 data_t *key,
1937 int alg_arg,
1938 int expected_status_arg )
1939{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001940 psa_key_type_t key_type = key_type_arg;
1941 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001942 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001943 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001944 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1945#if defined(KNOWN_SUPPORTED_MAC_ALG)
1946 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1947#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001948
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001950
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001951 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1952 &operation, &status ) )
1953 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001954 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001955
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001956 /* The operation object should be reusable. */
1957#if defined(KNOWN_SUPPORTED_MAC_ALG)
1958 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1959 smoke_test_key_data,
1960 sizeof( smoke_test_key_data ),
1961 KNOWN_SUPPORTED_MAC_ALG,
1962 &operation, &status ) )
1963 goto exit;
1964 TEST_EQUAL( status, PSA_SUCCESS );
1965#endif
1966
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001967exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001968 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001969}
1970/* END_CASE */
1971
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001972/* 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 +00001973void mac_bad_order( )
1974{
Ronald Cron5425a212020-08-04 14:58:35 +02001975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001976 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1977 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001978 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001979 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1980 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1981 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001983 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1984 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1985 size_t sign_mac_length = 0;
1986 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1987 const uint8_t verify_mac[] = {
1988 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1989 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1990 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1991
1992 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001993 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001994 psa_set_key_algorithm( &attributes, alg );
1995 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001996
Ronald Cron5425a212020-08-04 14:58:35 +02001997 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1998 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001999
Jaeden Amero252ef282019-02-15 14:05:35 +00002000 /* Call update without calling setup beforehand. */
2001 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2002 PSA_ERROR_BAD_STATE );
2003 PSA_ASSERT( psa_mac_abort( &operation ) );
2004
2005 /* Call sign finish without calling setup beforehand. */
2006 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2007 &sign_mac_length),
2008 PSA_ERROR_BAD_STATE );
2009 PSA_ASSERT( psa_mac_abort( &operation ) );
2010
2011 /* Call verify finish without calling setup beforehand. */
2012 TEST_EQUAL( psa_mac_verify_finish( &operation,
2013 verify_mac, sizeof( verify_mac ) ),
2014 PSA_ERROR_BAD_STATE );
2015 PSA_ASSERT( psa_mac_abort( &operation ) );
2016
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002017 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002018 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002019 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002020 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002021 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002022 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002023 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002024 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002025
Jaeden Amero252ef282019-02-15 14:05:35 +00002026 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002027 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002028 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2029 PSA_ASSERT( psa_mac_sign_finish( &operation,
2030 sign_mac, sizeof( sign_mac ),
2031 &sign_mac_length ) );
2032 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2033 PSA_ERROR_BAD_STATE );
2034 PSA_ASSERT( psa_mac_abort( &operation ) );
2035
2036 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002037 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002038 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2039 PSA_ASSERT( psa_mac_verify_finish( &operation,
2040 verify_mac, sizeof( verify_mac ) ) );
2041 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2042 PSA_ERROR_BAD_STATE );
2043 PSA_ASSERT( psa_mac_abort( &operation ) );
2044
2045 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002046 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002047 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2048 PSA_ASSERT( psa_mac_sign_finish( &operation,
2049 sign_mac, sizeof( sign_mac ),
2050 &sign_mac_length ) );
2051 TEST_EQUAL( psa_mac_sign_finish( &operation,
2052 sign_mac, sizeof( sign_mac ),
2053 &sign_mac_length ),
2054 PSA_ERROR_BAD_STATE );
2055 PSA_ASSERT( psa_mac_abort( &operation ) );
2056
2057 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002058 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002059 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2060 PSA_ASSERT( psa_mac_verify_finish( &operation,
2061 verify_mac, sizeof( verify_mac ) ) );
2062 TEST_EQUAL( psa_mac_verify_finish( &operation,
2063 verify_mac, sizeof( verify_mac ) ),
2064 PSA_ERROR_BAD_STATE );
2065 PSA_ASSERT( psa_mac_abort( &operation ) );
2066
2067 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002068 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002069 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002070 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002071 TEST_EQUAL( psa_mac_verify_finish( &operation,
2072 verify_mac, sizeof( verify_mac ) ),
2073 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002074 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002075 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002076 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002077
2078 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002079 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002080 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002081 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002082 TEST_EQUAL( psa_mac_sign_finish( &operation,
2083 sign_mac, sizeof( sign_mac ),
2084 &sign_mac_length ),
2085 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002086 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002087 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002088 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002089
Ronald Cron5425a212020-08-04 14:58:35 +02002090 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002091
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002092exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002093 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002094}
2095/* END_CASE */
2096
2097/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002098void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002099 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002100 int alg_arg,
2101 data_t *input,
2102 data_t *expected_mac )
2103{
Ronald Cron5425a212020-08-04 14:58:35 +02002104 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002105 psa_key_type_t key_type = key_type_arg;
2106 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002107 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002109 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002110 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002111 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002112 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002113 const size_t output_sizes_to_test[] = {
2114 0,
2115 1,
2116 expected_mac->len - 1,
2117 expected_mac->len,
2118 expected_mac->len + 1,
2119 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002120
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002121 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002122 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002123 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002124
Gilles Peskine8817f612018-12-18 00:18:46 +01002125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002126
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002127 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002128 psa_set_key_algorithm( &attributes, alg );
2129 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002130
Ronald Cron5425a212020-08-04 14:58:35 +02002131 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2132 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002133
Gilles Peskine8b356b52020-08-25 23:44:59 +02002134 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2135 {
2136 const size_t output_size = output_sizes_to_test[i];
2137 psa_status_t expected_status =
2138 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2139 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002140
Chris Jones9634bb12021-01-20 15:56:42 +00002141 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002142 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002143
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002144 /* Calculate the MAC, one-shot case. */
2145 TEST_EQUAL( psa_mac_compute( key, alg,
2146 input->x, input->len,
2147 actual_mac, output_size, &mac_length ),
2148 expected_status );
2149 if( expected_status == PSA_SUCCESS )
2150 {
2151 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2152 actual_mac, mac_length );
2153 }
2154
2155 if( output_size > 0 )
2156 memset( actual_mac, 0, output_size );
2157
2158 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002159 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002160 PSA_ASSERT( psa_mac_update( &operation,
2161 input->x, input->len ) );
2162 TEST_EQUAL( psa_mac_sign_finish( &operation,
2163 actual_mac, output_size,
2164 &mac_length ),
2165 expected_status );
2166 PSA_ASSERT( psa_mac_abort( &operation ) );
2167
2168 if( expected_status == PSA_SUCCESS )
2169 {
2170 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2171 actual_mac, mac_length );
2172 }
2173 mbedtls_free( actual_mac );
2174 actual_mac = NULL;
2175 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002176
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002177exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002178 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002179 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002180 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002181 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002182}
2183/* END_CASE */
2184
2185/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002186void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002187 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002188 int alg_arg,
2189 data_t *input,
2190 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002191{
Ronald Cron5425a212020-08-04 14:58:35 +02002192 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002193 psa_key_type_t key_type = key_type_arg;
2194 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002195 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002197 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002198
Gilles Peskine69c12672018-06-28 00:07:19 +02002199 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2200
Gilles Peskine8817f612018-12-18 00:18:46 +01002201 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002202
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002203 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002204 psa_set_key_algorithm( &attributes, alg );
2205 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002206
Ronald Cron5425a212020-08-04 14:58:35 +02002207 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2208 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002209
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002210 /* Verify correct MAC, one-shot case. */
2211 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2212 expected_mac->x, expected_mac->len ) );
2213
2214 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002215 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002216 PSA_ASSERT( psa_mac_update( &operation,
2217 input->x, input->len ) );
2218 PSA_ASSERT( psa_mac_verify_finish( &operation,
2219 expected_mac->x,
2220 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002221
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002222 /* Test a MAC that's too short, one-shot case. */
2223 TEST_EQUAL( psa_mac_verify( key, alg,
2224 input->x, input->len,
2225 expected_mac->x,
2226 expected_mac->len - 1 ),
2227 PSA_ERROR_INVALID_SIGNATURE );
2228
2229 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002230 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002231 PSA_ASSERT( psa_mac_update( &operation,
2232 input->x, input->len ) );
2233 TEST_EQUAL( psa_mac_verify_finish( &operation,
2234 expected_mac->x,
2235 expected_mac->len - 1 ),
2236 PSA_ERROR_INVALID_SIGNATURE );
2237
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002238 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002239 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2240 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002241 TEST_EQUAL( psa_mac_verify( key, alg,
2242 input->x, input->len,
2243 perturbed_mac, expected_mac->len + 1 ),
2244 PSA_ERROR_INVALID_SIGNATURE );
2245
2246 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002247 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002248 PSA_ASSERT( psa_mac_update( &operation,
2249 input->x, input->len ) );
2250 TEST_EQUAL( psa_mac_verify_finish( &operation,
2251 perturbed_mac,
2252 expected_mac->len + 1 ),
2253 PSA_ERROR_INVALID_SIGNATURE );
2254
2255 /* Test changing one byte. */
2256 for( size_t i = 0; i < expected_mac->len; i++ )
2257 {
Chris Jones9634bb12021-01-20 15:56:42 +00002258 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002259 perturbed_mac[i] ^= 1;
gabor-mezei-arma93e4232021-03-01 15:35:48 +01002260
2261 TEST_EQUAL( psa_mac_verify( key, alg,
2262 input->x, input->len,
2263 perturbed_mac, expected_mac->len ),
2264 PSA_ERROR_INVALID_SIGNATURE );
2265
Ronald Cron5425a212020-08-04 14:58:35 +02002266 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002267 PSA_ASSERT( psa_mac_update( &operation,
2268 input->x, input->len ) );
2269 TEST_EQUAL( psa_mac_verify_finish( &operation,
2270 perturbed_mac,
2271 expected_mac->len ),
2272 PSA_ERROR_INVALID_SIGNATURE );
2273 perturbed_mac[i] ^= 1;
2274 }
2275
Gilles Peskine8c9def32018-02-08 10:02:12 +01002276exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002277 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002278 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002279 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002280 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002281}
2282/* END_CASE */
2283
2284/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002285void cipher_operation_init( )
2286{
Jaeden Ameroab439972019-02-15 14:12:05 +00002287 const uint8_t input[1] = { 0 };
2288 unsigned char output[1] = { 0 };
2289 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002290 /* Test each valid way of initializing the object, except for `= {0}`, as
2291 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2292 * though it's OK by the C standard. We could test for this, but we'd need
2293 * to supress the Clang warning for the test. */
2294 psa_cipher_operation_t func = psa_cipher_operation_init( );
2295 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2296 psa_cipher_operation_t zero;
2297
2298 memset( &zero, 0, sizeof( zero ) );
2299
Jaeden Ameroab439972019-02-15 14:12:05 +00002300 /* A freshly-initialized cipher operation should not be usable. */
2301 TEST_EQUAL( psa_cipher_update( &func,
2302 input, sizeof( input ),
2303 output, sizeof( output ),
2304 &output_length ),
2305 PSA_ERROR_BAD_STATE );
2306 TEST_EQUAL( psa_cipher_update( &init,
2307 input, sizeof( input ),
2308 output, sizeof( output ),
2309 &output_length ),
2310 PSA_ERROR_BAD_STATE );
2311 TEST_EQUAL( psa_cipher_update( &zero,
2312 input, sizeof( input ),
2313 output, sizeof( output ),
2314 &output_length ),
2315 PSA_ERROR_BAD_STATE );
2316
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002317 /* A default cipher operation should be abortable without error. */
2318 PSA_ASSERT( psa_cipher_abort( &func ) );
2319 PSA_ASSERT( psa_cipher_abort( &init ) );
2320 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002321}
2322/* END_CASE */
2323
2324/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002325void cipher_setup( int key_type_arg,
2326 data_t *key,
2327 int alg_arg,
2328 int expected_status_arg )
2329{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002330 psa_key_type_t key_type = key_type_arg;
2331 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002332 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002333 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002334 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002335#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002336 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2337#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002338
Gilles Peskine8817f612018-12-18 00:18:46 +01002339 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002341 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2342 &operation, &status ) )
2343 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002344 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002346 /* The operation object should be reusable. */
2347#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2348 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2349 smoke_test_key_data,
2350 sizeof( smoke_test_key_data ),
2351 KNOWN_SUPPORTED_CIPHER_ALG,
2352 &operation, &status ) )
2353 goto exit;
2354 TEST_EQUAL( status, PSA_SUCCESS );
2355#endif
2356
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002357exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002358 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002359 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002360}
2361/* END_CASE */
2362
Ronald Cronee414c72021-03-18 18:50:08 +01002363/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002364void cipher_bad_order( )
2365{
Ronald Cron5425a212020-08-04 14:58:35 +02002366 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002367 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2368 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002370 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002371 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002372 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002373 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2374 0xaa, 0xaa, 0xaa, 0xaa };
2375 const uint8_t text[] = {
2376 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2377 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002378 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002379 size_t length = 0;
2380
2381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002382 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2383 psa_set_key_algorithm( &attributes, alg );
2384 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002385 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2386 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002387
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002388 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002389 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002390 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002391 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002392 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002393 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002394 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002395 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002396
2397 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002398 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002399 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002400 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002401 PSA_ERROR_BAD_STATE );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002402 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002403 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgmanff8d52b2021-06-24 11:36:14 +01002404 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002405
Jaeden Ameroab439972019-02-15 14:12:05 +00002406 /* Generate an IV without calling setup beforehand. */
2407 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2408 buffer, sizeof( buffer ),
2409 &length ),
2410 PSA_ERROR_BAD_STATE );
2411 PSA_ASSERT( psa_cipher_abort( &operation ) );
2412
2413 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002414 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002415 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2416 buffer, sizeof( buffer ),
2417 &length ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002418 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002419 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2420 buffer, sizeof( buffer ),
2421 &length ),
2422 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002423 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002424 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002425 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002426
2427 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002428 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002429 PSA_ASSERT( psa_cipher_set_iv( &operation,
2430 iv, sizeof( iv ) ) );
2431 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2432 buffer, sizeof( buffer ),
2433 &length ),
2434 PSA_ERROR_BAD_STATE );
2435 PSA_ASSERT( psa_cipher_abort( &operation ) );
2436
2437 /* Set an IV without calling setup beforehand. */
2438 TEST_EQUAL( psa_cipher_set_iv( &operation,
2439 iv, sizeof( iv ) ),
2440 PSA_ERROR_BAD_STATE );
2441 PSA_ASSERT( psa_cipher_abort( &operation ) );
2442
2443 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002444 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002445 PSA_ASSERT( psa_cipher_set_iv( &operation,
2446 iv, sizeof( iv ) ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002447 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002448 TEST_EQUAL( psa_cipher_set_iv( &operation,
2449 iv, sizeof( iv ) ),
2450 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002451 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002452 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002453 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002454
2455 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002456 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002457 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2458 buffer, sizeof( buffer ),
2459 &length ) );
2460 TEST_EQUAL( psa_cipher_set_iv( &operation,
2461 iv, sizeof( iv ) ),
2462 PSA_ERROR_BAD_STATE );
2463 PSA_ASSERT( psa_cipher_abort( &operation ) );
2464
2465 /* Call update without calling setup beforehand. */
2466 TEST_EQUAL( psa_cipher_update( &operation,
2467 text, sizeof( text ),
2468 buffer, sizeof( buffer ),
2469 &length ),
2470 PSA_ERROR_BAD_STATE );
2471 PSA_ASSERT( psa_cipher_abort( &operation ) );
2472
2473 /* Call update without an IV where an IV is required. */
Dave Rodgman33b58ee2021-06-23 12:48:52 +01002474 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002475 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002476 TEST_EQUAL( psa_cipher_update( &operation,
2477 text, sizeof( text ),
2478 buffer, sizeof( buffer ),
2479 &length ),
2480 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002481 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002482 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002483 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002484
2485 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002486 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002487 PSA_ASSERT( psa_cipher_set_iv( &operation,
2488 iv, sizeof( iv ) ) );
2489 PSA_ASSERT( psa_cipher_finish( &operation,
2490 buffer, sizeof( buffer ), &length ) );
2491 TEST_EQUAL( psa_cipher_update( &operation,
2492 text, sizeof( text ),
2493 buffer, sizeof( buffer ),
2494 &length ),
2495 PSA_ERROR_BAD_STATE );
2496 PSA_ASSERT( psa_cipher_abort( &operation ) );
2497
2498 /* Call finish without calling setup beforehand. */
2499 TEST_EQUAL( psa_cipher_finish( &operation,
2500 buffer, sizeof( buffer ), &length ),
2501 PSA_ERROR_BAD_STATE );
2502 PSA_ASSERT( psa_cipher_abort( &operation ) );
2503
2504 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002505 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002506 /* Not calling update means we are encrypting an empty buffer, which is OK
2507 * for cipher modes with padding. */
Dave Rodgman34b147d2021-06-23 12:49:59 +01002508 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002509 TEST_EQUAL( psa_cipher_finish( &operation,
2510 buffer, sizeof( buffer ), &length ),
2511 PSA_ERROR_BAD_STATE );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002512 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002513 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman34b147d2021-06-23 12:49:59 +01002514 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002515
2516 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002517 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002518 PSA_ASSERT( psa_cipher_set_iv( &operation,
2519 iv, sizeof( iv ) ) );
2520 PSA_ASSERT( psa_cipher_finish( &operation,
2521 buffer, sizeof( buffer ), &length ) );
2522 TEST_EQUAL( psa_cipher_finish( &operation,
2523 buffer, sizeof( buffer ), &length ),
2524 PSA_ERROR_BAD_STATE );
2525 PSA_ASSERT( psa_cipher_abort( &operation ) );
2526
Ronald Cron5425a212020-08-04 14:58:35 +02002527 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002528
Jaeden Ameroab439972019-02-15 14:12:05 +00002529exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002530 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002531 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002532}
2533/* END_CASE */
2534
2535/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002536void cipher_encrypt_fail( int alg_arg,
2537 int key_type_arg,
2538 data_t *key_data,
2539 data_t *input,
2540 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002541{
Ronald Cron5425a212020-08-04 14:58:35 +02002542 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002543 psa_status_t status;
2544 psa_key_type_t key_type = key_type_arg;
2545 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002546 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002547 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002548 size_t output_buffer_size = 0;
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002549 size_t output_length = 0;
2550 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2551
2552 if ( PSA_ERROR_BAD_STATE != expected_status )
2553 {
2554 PSA_ASSERT( psa_crypto_init( ) );
2555
2556 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2557 psa_set_key_algorithm( &attributes, alg );
2558 psa_set_key_type( &attributes, key_type );
2559
2560 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2561 input->len );
2562 ASSERT_ALLOC( output, output_buffer_size );
2563
2564 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2565 &key ) );
2566 }
2567
2568 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2569 output_buffer_size, &output_length );
2570
2571 TEST_EQUAL( status, expected_status );
2572
2573exit:
2574 mbedtls_free( output );
2575 psa_destroy_key( key );
2576 PSA_DONE( );
2577}
2578/* END_CASE */
2579
2580/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002581void cipher_encrypt_alg_without_iv( int alg_arg,
2582 int key_type_arg,
2583 data_t *key_data,
2584 data_t *input,
2585 data_t *expected_output )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002586{
2587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2588 psa_key_type_t key_type = key_type_arg;
2589 psa_algorithm_t alg = alg_arg;
2590 unsigned char *output = NULL;
2591 size_t output_buffer_size = 0;
2592 size_t output_length = 0;
2593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2594
2595 PSA_ASSERT( psa_crypto_init( ) );
2596
2597 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2598 psa_set_key_algorithm( &attributes, alg );
2599 psa_set_key_type( &attributes, key_type );
2600
2601 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2602 ASSERT_ALLOC( output, output_buffer_size );
2603
2604 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2605 &key ) );
2606
2607 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2608 output_buffer_size, &output_length ) );
2609 TEST_ASSERT( output_length <=
2610 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2611 TEST_ASSERT( output_length <=
2612 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2613
2614 ASSERT_COMPARE( expected_output->x, expected_output->len,
2615 output, output_length );
2616exit:
2617 mbedtls_free( output );
2618 psa_destroy_key( key );
2619 PSA_DONE( );
2620}
2621/* END_CASE */
2622
2623/* BEGIN_CASE */
Paul Elliotted33ef12021-07-14 12:31:21 +01002624void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2625{
2626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2627 psa_algorithm_t alg = alg_arg;
2628 psa_key_type_t key_type = key_type_arg;
2629 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2630 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2631 psa_status_t status;
2632
2633 PSA_ASSERT( psa_crypto_init( ) );
2634
2635 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2636 psa_set_key_algorithm( &attributes, alg );
2637 psa_set_key_type( &attributes, key_type );
2638
2639 /* Usage of either of these two size macros would cause divide by zero
2640 * with incorrect key types previously. Input length should be irrelevant
2641 * here. */
2642 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2643 0 );
2644 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2645
2646
2647 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2648 &key ) );
2649
2650 /* Should fail due to invalid alg type (to support invalid key type).
2651 * Encrypt or decrypt will end up in the same place. */
2652 status = psa_cipher_encrypt_setup( &operation, key, alg );
2653
2654 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2655
2656exit:
2657 psa_cipher_abort( &operation );
2658 psa_destroy_key( key );
2659 PSA_DONE( );
2660}
2661/* END_CASE */
2662
2663/* BEGIN_CASE */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002664void cipher_encrypt_validation( int alg_arg,
2665 int key_type_arg,
2666 data_t *key_data,
2667 data_t *input )
2668{
2669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2670 psa_key_type_t key_type = key_type_arg;
2671 psa_algorithm_t alg = alg_arg;
2672 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2673 unsigned char *output1 = NULL;
2674 size_t output1_buffer_size = 0;
2675 size_t output1_length = 0;
2676 unsigned char *output2 = NULL;
2677 size_t output2_buffer_size = 0;
2678 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002679 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002680 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002682
Gilles Peskine8817f612018-12-18 00:18:46 +01002683 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002685 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2686 psa_set_key_algorithm( &attributes, alg );
2687 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002688
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002689 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2690 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2691 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2692 ASSERT_ALLOC( output1, output1_buffer_size );
2693 ASSERT_ALLOC( output2, output2_buffer_size );
2694
Ronald Cron5425a212020-08-04 14:58:35 +02002695 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2696 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002697
gabor-mezei-arm7aa1efd2021-06-25 15:47:50 +02002698 /* The one-shot cipher encryption uses generated iv so validating
2699 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002700 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2701 output1_buffer_size, &output1_length ) );
2702 TEST_ASSERT( output1_length <=
2703 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2704 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002705 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002706
2707 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2708 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002709
Gilles Peskine8817f612018-12-18 00:18:46 +01002710 PSA_ASSERT( psa_cipher_update( &operation,
2711 input->x, input->len,
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002712 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002713 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002714 TEST_ASSERT( function_output_length <=
2715 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2716 TEST_ASSERT( function_output_length <=
2717 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002718 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002719
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002720 PSA_ASSERT( psa_cipher_finish( &operation,
2721 output2 + output2_length,
2722 output2_buffer_size - output2_length,
2723 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002724 TEST_ASSERT( function_output_length <=
2725 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2726 TEST_ASSERT( function_output_length <=
2727 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002728 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002729
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002730 PSA_ASSERT( psa_cipher_abort( &operation ) );
2731 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
2732 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002733
Gilles Peskine50e586b2018-06-08 14:28:46 +02002734exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002735 psa_cipher_abort( &operation );
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002736 mbedtls_free( output1 );
2737 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002738 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002739 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002740}
2741/* END_CASE */
2742
2743/* BEGIN_CASE */
2744void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002745 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002746 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002747 int first_part_size_arg,
2748 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002749 data_t *expected_output,
2750 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002751{
Ronald Cron5425a212020-08-04 14:58:35 +02002752 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002753 psa_key_type_t key_type = key_type_arg;
2754 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002755 psa_status_t status;
2756 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002757 size_t first_part_size = first_part_size_arg;
2758 size_t output1_length = output1_length_arg;
2759 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002760 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002761 size_t output_buffer_size = 0;
2762 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002763 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002764 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002765 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002766
Gilles Peskine8817f612018-12-18 00:18:46 +01002767 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002768
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002769 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2770 psa_set_key_algorithm( &attributes, alg );
2771 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002772
Ronald Cron5425a212020-08-04 14:58:35 +02002773 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2774 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002775
Ronald Cron5425a212020-08-04 14:58:35 +02002776 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002777
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002778 if( iv->len > 0 )
2779 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002780 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002781 }
2782
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002783 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2784 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002785 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002786
Gilles Peskinee0866522019-02-19 19:44:00 +01002787 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002788 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2789 output, output_buffer_size,
2790 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002791 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002792 TEST_ASSERT( function_output_length <=
2793 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2794 TEST_ASSERT( function_output_length <=
2795 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002796 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002797
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002798 if( first_part_size < input->len )
2799 {
2800 PSA_ASSERT( psa_cipher_update( &operation,
2801 input->x + first_part_size,
2802 input->len - first_part_size,
2803 ( output_buffer_size == 0 ? NULL :
2804 output + total_output_length ),
2805 output_buffer_size - total_output_length,
2806 &function_output_length ) );
2807 TEST_ASSERT( function_output_length == output2_length );
2808 TEST_ASSERT( function_output_length <=
2809 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2810 alg,
2811 input->len - first_part_size ) );
2812 TEST_ASSERT( function_output_length <=
2813 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2814 total_output_length += function_output_length;
2815 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002816
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002817 status = psa_cipher_finish( &operation,
2818 ( output_buffer_size == 0 ? NULL :
2819 output + total_output_length ),
2820 output_buffer_size - total_output_length,
2821 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002822 TEST_ASSERT( function_output_length <=
2823 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2824 TEST_ASSERT( function_output_length <=
2825 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002826 total_output_length += function_output_length;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002827 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002829 if( expected_status == PSA_SUCCESS )
2830 {
2831 PSA_ASSERT( psa_cipher_abort( &operation ) );
2832
2833 ASSERT_COMPARE( expected_output->x, expected_output->len,
2834 output, total_output_length );
2835 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002836
2837exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002838 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002839 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002840 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002841 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002842}
2843/* END_CASE */
2844
2845/* BEGIN_CASE */
2846void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002847 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002848 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002849 int first_part_size_arg,
2850 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002851 data_t *expected_output,
2852 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002853{
Ronald Cron5425a212020-08-04 14:58:35 +02002854 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002855 psa_key_type_t key_type = key_type_arg;
2856 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002857 psa_status_t status;
2858 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002859 size_t first_part_size = first_part_size_arg;
2860 size_t output1_length = output1_length_arg;
2861 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002862 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002863 size_t output_buffer_size = 0;
2864 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002865 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002866 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002868
Gilles Peskine8817f612018-12-18 00:18:46 +01002869 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002870
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002871 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2872 psa_set_key_algorithm( &attributes, alg );
2873 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002874
Ronald Cron5425a212020-08-04 14:58:35 +02002875 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2876 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002877
Ronald Cron5425a212020-08-04 14:58:35 +02002878 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002879
Steven Cooreman177deba2020-09-07 17:14:14 +02002880 if( iv->len > 0 )
2881 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002882 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002883 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002884
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002885 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2886 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002887 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002888
Gilles Peskinee0866522019-02-19 19:44:00 +01002889 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002890 PSA_ASSERT( psa_cipher_update( &operation,
2891 input->x, first_part_size,
2892 output, output_buffer_size,
2893 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002894 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002895 TEST_ASSERT( function_output_length <=
2896 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2897 TEST_ASSERT( function_output_length <=
2898 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002899 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002900
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002901 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02002902 {
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002903 PSA_ASSERT( psa_cipher_update( &operation,
2904 input->x + first_part_size,
2905 input->len - first_part_size,
2906 ( output_buffer_size == 0 ? NULL :
2907 output + total_output_length ),
2908 output_buffer_size - total_output_length,
2909 &function_output_length ) );
2910 TEST_ASSERT( function_output_length == output2_length );
2911 TEST_ASSERT( function_output_length <=
2912 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2913 alg,
2914 input->len - first_part_size ) );
2915 TEST_ASSERT( function_output_length <=
2916 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2917 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002918 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002919
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002921 ( output_buffer_size == 0 ? NULL :
2922 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002923 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002925 TEST_ASSERT( function_output_length <=
2926 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2927 TEST_ASSERT( function_output_length <=
2928 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002929 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002930 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002931
2932 if( expected_status == PSA_SUCCESS )
2933 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002934 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm9ac48472021-06-25 18:21:33 +02002935
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002936 ASSERT_COMPARE( expected_output->x, expected_output->len,
2937 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002938 }
2939
Gilles Peskine50e586b2018-06-08 14:28:46 +02002940exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002941 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002942 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002943 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002944 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002945}
2946/* END_CASE */
2947
Gilles Peskine50e586b2018-06-08 14:28:46 +02002948/* BEGIN_CASE */
gabor-mezei-arm43611b02021-06-25 15:49:14 +02002949void cipher_decrypt_fail( int alg_arg,
2950 int key_type_arg,
2951 data_t *key_data,
2952 data_t *iv,
2953 data_t *input_arg,
2954 int expected_status_arg )
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01002955{
2956 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2957 psa_status_t status;
2958 psa_key_type_t key_type = key_type_arg;
2959 psa_algorithm_t alg = alg_arg;
2960 psa_status_t expected_status = expected_status_arg;
2961 unsigned char *input = NULL;
2962 size_t input_buffer_size = 0;
2963 unsigned char *output = NULL;
2964 size_t output_buffer_size = 0;
2965 size_t output_length = 0;
2966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2967
2968 if ( PSA_ERROR_BAD_STATE != expected_status )
2969 {
2970 PSA_ASSERT( psa_crypto_init( ) );
2971
2972 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2973 psa_set_key_algorithm( &attributes, alg );
2974 psa_set_key_type( &attributes, key_type );
2975
2976 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2977 &key ) );
2978 }
2979
2980 /* Allocate input buffer and copy the iv and the plaintext */
2981 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
2982 if ( input_buffer_size > 0 )
2983 {
2984 ASSERT_ALLOC( input, input_buffer_size );
2985 memcpy( input, iv->x, iv->len );
2986 memcpy( input + iv->len, input_arg->x, input_arg->len );
2987 }
2988
2989 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
2990 ASSERT_ALLOC( output, output_buffer_size );
2991
2992 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
2993 output_buffer_size, &output_length );
2994 TEST_EQUAL( status, expected_status );
2995
2996exit:
2997 mbedtls_free( input );
2998 mbedtls_free( output );
2999 psa_destroy_key( key );
3000 PSA_DONE( );
3001}
3002/* END_CASE */
3003
3004/* BEGIN_CASE */
3005void cipher_decrypt( int alg_arg,
3006 int key_type_arg,
3007 data_t *key_data,
3008 data_t *iv,
3009 data_t *input_arg,
3010 data_t *expected_output )
3011{
3012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3013 psa_key_type_t key_type = key_type_arg;
3014 psa_algorithm_t alg = alg_arg;
3015 unsigned char *input = NULL;
3016 size_t input_buffer_size = 0;
3017 unsigned char *output = NULL;
3018 size_t output_buffer_size = 0;
3019 size_t output_length = 0;
3020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3021
3022 PSA_ASSERT( psa_crypto_init( ) );
3023
3024 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3025 psa_set_key_algorithm( &attributes, alg );
3026 psa_set_key_type( &attributes, key_type );
3027
3028 /* Allocate input buffer and copy the iv and the plaintext */
3029 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3030 if ( input_buffer_size > 0 )
3031 {
3032 ASSERT_ALLOC( input, input_buffer_size );
3033 memcpy( input, iv->x, iv->len );
3034 memcpy( input + iv->len, input_arg->x, input_arg->len );
3035 }
3036
3037 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3038 ASSERT_ALLOC( output, output_buffer_size );
3039
3040 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3041 &key ) );
3042
3043 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3044 output_buffer_size, &output_length ) );
3045 TEST_ASSERT( output_length <=
3046 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3047 TEST_ASSERT( output_length <=
3048 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3049
3050 ASSERT_COMPARE( expected_output->x, expected_output->len,
3051 output, output_length );
3052exit:
3053 mbedtls_free( input );
3054 mbedtls_free( output );
3055 psa_destroy_key( key );
3056 PSA_DONE( );
3057}
3058/* END_CASE */
3059
3060/* BEGIN_CASE */
3061void cipher_verify_output( int alg_arg,
3062 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003063 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003064 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003065{
Ronald Cron5425a212020-08-04 14:58:35 +02003066 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003067 psa_key_type_t key_type = key_type_arg;
3068 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003069 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003070 size_t output1_size = 0;
3071 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003072 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003073 size_t output2_size = 0;
3074 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003076
Gilles Peskine8817f612018-12-18 00:18:46 +01003077 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003078
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3080 psa_set_key_algorithm( &attributes, alg );
3081 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003082
Ronald Cron5425a212020-08-04 14:58:35 +02003083 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3084 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003085 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003086 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003087
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003088 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3089 output1, output1_size,
3090 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003091 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003092 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003093 TEST_ASSERT( output1_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003094 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003095
3096 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003097 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003098
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003099 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3100 output2, output2_size,
3101 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003102 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003103 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003104 TEST_ASSERT( output2_length <=
gabor-mezei-armd086e6e2021-03-01 15:11:46 +01003105 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003106
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003107 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003108
3109exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003110 mbedtls_free( output1 );
3111 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003112 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003113 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003114}
3115/* END_CASE */
3116
3117/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003118void cipher_verify_output_multipart( int alg_arg,
3119 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003120 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003121 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003122 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003123{
Ronald Cron5425a212020-08-04 14:58:35 +02003124 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003125 psa_key_type_t key_type = key_type_arg;
3126 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003127 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003128 unsigned char iv[16] = {0};
3129 size_t iv_size = 16;
3130 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003131 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003132 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003133 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003134 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003135 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003136 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003137 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003138 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3139 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003140 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003141
Gilles Peskine8817f612018-12-18 00:18:46 +01003142 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003143
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003144 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3145 psa_set_key_algorithm( &attributes, alg );
3146 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003147
Ronald Cron5425a212020-08-04 14:58:35 +02003148 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3149 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003150
Ronald Cron5425a212020-08-04 14:58:35 +02003151 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3152 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003153
Steven Cooreman177deba2020-09-07 17:14:14 +02003154 if( alg != PSA_ALG_ECB_NO_PADDING )
3155 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003156 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3157 iv, iv_size,
3158 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003159 }
3160
gabor-mezei-armceface22021-01-21 12:26:17 +01003161 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3162 TEST_ASSERT( output1_buffer_size <=
3163 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003164 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003165
Gilles Peskinee0866522019-02-19 19:44:00 +01003166 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003167
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3169 output1, output1_buffer_size,
3170 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003171 TEST_ASSERT( function_output_length <=
3172 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3173 TEST_ASSERT( function_output_length <=
3174 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003175 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003176
Gilles Peskine8817f612018-12-18 00:18:46 +01003177 PSA_ASSERT( psa_cipher_update( &operation1,
3178 input->x + first_part_size,
3179 input->len - first_part_size,
3180 output1, output1_buffer_size,
3181 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003182 TEST_ASSERT( function_output_length <=
3183 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3184 alg,
3185 input->len - first_part_size ) );
3186 TEST_ASSERT( function_output_length <=
3187 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003188 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003189
Gilles Peskine8817f612018-12-18 00:18:46 +01003190 PSA_ASSERT( psa_cipher_finish( &operation1,
3191 output1 + output1_length,
3192 output1_buffer_size - output1_length,
3193 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003194 TEST_ASSERT( function_output_length <=
3195 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3196 TEST_ASSERT( function_output_length <=
3197 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003198 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003199
Gilles Peskine8817f612018-12-18 00:18:46 +01003200 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003201
Gilles Peskine048b7f02018-06-08 14:20:49 +02003202 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003203 TEST_ASSERT( output2_buffer_size <=
3204 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3205 TEST_ASSERT( output2_buffer_size <=
3206 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003207 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003208
Steven Cooreman177deba2020-09-07 17:14:14 +02003209 if( iv_length > 0 )
3210 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003211 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3212 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003213 }
Moran Pekerded84402018-06-06 16:36:50 +03003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3216 output2, output2_buffer_size,
3217 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003218 TEST_ASSERT( function_output_length <=
3219 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3220 TEST_ASSERT( function_output_length <=
3221 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003222 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003223
Gilles Peskine8817f612018-12-18 00:18:46 +01003224 PSA_ASSERT( psa_cipher_update( &operation2,
3225 output1 + first_part_size,
3226 output1_length - first_part_size,
3227 output2, output2_buffer_size,
3228 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003229 TEST_ASSERT( function_output_length <=
3230 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3231 alg,
3232 output1_length - first_part_size ) );
3233 TEST_ASSERT( function_output_length <=
3234 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003235 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003236
Gilles Peskine8817f612018-12-18 00:18:46 +01003237 PSA_ASSERT( psa_cipher_finish( &operation2,
3238 output2 + output2_length,
3239 output2_buffer_size - output2_length,
3240 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003241 TEST_ASSERT( function_output_length <=
3242 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3243 TEST_ASSERT( function_output_length <=
3244 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003245 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003246
Gilles Peskine8817f612018-12-18 00:18:46 +01003247 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003248
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003249 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003250
3251exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003252 psa_cipher_abort( &operation1 );
3253 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003254 mbedtls_free( output1 );
3255 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003256 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003257 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003258}
3259/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003260
Gilles Peskine20035e32018-02-03 22:44:14 +01003261/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003262void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003263 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003264 data_t *nonce,
3265 data_t *additional_data,
3266 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003267 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003268{
Ronald Cron5425a212020-08-04 14:58:35 +02003269 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003270 psa_key_type_t key_type = key_type_arg;
3271 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003272 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003273 unsigned char *output_data = NULL;
3274 size_t output_size = 0;
3275 size_t output_length = 0;
3276 unsigned char *output_data2 = NULL;
3277 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003278 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003279 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003281
Gilles Peskine8817f612018-12-18 00:18:46 +01003282 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003283
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003284 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3285 psa_set_key_algorithm( &attributes, alg );
3286 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003287
Gilles Peskine049c7532019-05-15 20:22:09 +02003288 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003289 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003290 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3291 key_bits = psa_get_key_bits( &attributes );
3292
3293 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3294 alg );
3295 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3296 * should be exact. */
3297 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3298 expected_result != PSA_ERROR_NOT_SUPPORTED )
3299 {
3300 TEST_EQUAL( output_size,
3301 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3302 TEST_ASSERT( output_size <=
3303 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3304 }
3305 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003306
Steven Cooremanf49478b2021-02-15 15:19:25 +01003307 status = psa_aead_encrypt( key, alg,
3308 nonce->x, nonce->len,
3309 additional_data->x,
3310 additional_data->len,
3311 input_data->x, input_data->len,
3312 output_data, output_size,
3313 &output_length );
3314
3315 /* If the operation is not supported, just skip and not fail in case the
3316 * encryption involves a common limitation of cryptography hardwares and
3317 * an alternative implementation. */
3318 if( status == PSA_ERROR_NOT_SUPPORTED )
3319 {
3320 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3321 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3322 }
3323
3324 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003325
3326 if( PSA_SUCCESS == expected_result )
3327 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003328 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329
Gilles Peskine003a4a92019-05-14 16:09:40 +02003330 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3331 * should be exact. */
3332 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003333 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003334
gabor-mezei-armceface22021-01-21 12:26:17 +01003335 TEST_ASSERT( input_data->len <=
3336 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3337
Ronald Cron5425a212020-08-04 14:58:35 +02003338 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003339 nonce->x, nonce->len,
3340 additional_data->x,
3341 additional_data->len,
3342 output_data, output_length,
3343 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003344 &output_length2 ),
3345 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003346
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003347 ASSERT_COMPARE( input_data->x, input_data->len,
3348 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003349 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003350
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003352 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003353 mbedtls_free( output_data );
3354 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003355 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003356}
3357/* END_CASE */
3358
3359/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003360void aead_encrypt( int key_type_arg, data_t *key_data,
3361 int alg_arg,
3362 data_t *nonce,
3363 data_t *additional_data,
3364 data_t *input_data,
3365 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003366{
Ronald Cron5425a212020-08-04 14:58:35 +02003367 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003368 psa_key_type_t key_type = key_type_arg;
3369 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003370 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003371 unsigned char *output_data = NULL;
3372 size_t output_size = 0;
3373 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003375 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003376
Gilles Peskine8817f612018-12-18 00:18:46 +01003377 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003378
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003379 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3380 psa_set_key_algorithm( &attributes, alg );
3381 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003382
Gilles Peskine049c7532019-05-15 20:22:09 +02003383 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003384 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003385 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3386 key_bits = psa_get_key_bits( &attributes );
3387
3388 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3389 alg );
3390 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3391 * should be exact. */
3392 TEST_EQUAL( output_size,
3393 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3394 TEST_ASSERT( output_size <=
3395 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3396 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003397
Steven Cooremand588ea12021-01-11 19:36:04 +01003398 status = psa_aead_encrypt( key, alg,
3399 nonce->x, nonce->len,
3400 additional_data->x, additional_data->len,
3401 input_data->x, input_data->len,
3402 output_data, output_size,
3403 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404
Ronald Cron28a45ed2021-02-09 20:35:42 +01003405 /* If the operation is not supported, just skip and not fail in case the
3406 * encryption involves a common limitation of cryptography hardwares and
3407 * an alternative implementation. */
3408 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003409 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003410 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3411 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003412 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003413
3414 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003415 ASSERT_COMPARE( expected_result->x, expected_result->len,
3416 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003417
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003419 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003421 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003422}
3423/* END_CASE */
3424
3425/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003426void aead_decrypt( int key_type_arg, data_t *key_data,
3427 int alg_arg,
3428 data_t *nonce,
3429 data_t *additional_data,
3430 data_t *input_data,
3431 data_t *expected_data,
3432 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003433{
Ronald Cron5425a212020-08-04 14:58:35 +02003434 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003435 psa_key_type_t key_type = key_type_arg;
3436 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003437 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003438 unsigned char *output_data = NULL;
3439 size_t output_size = 0;
3440 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003442 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003443 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444
Gilles Peskine8817f612018-12-18 00:18:46 +01003445 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003446
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003447 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3448 psa_set_key_algorithm( &attributes, alg );
3449 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003450
Gilles Peskine049c7532019-05-15 20:22:09 +02003451 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003452 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003453 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3454 key_bits = psa_get_key_bits( &attributes );
3455
3456 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3457 alg );
3458 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3459 expected_result != PSA_ERROR_NOT_SUPPORTED )
3460 {
3461 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3462 * should be exact. */
3463 TEST_EQUAL( output_size,
3464 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3465 TEST_ASSERT( output_size <=
3466 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3467 }
3468 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003469
Steven Cooremand588ea12021-01-11 19:36:04 +01003470 status = psa_aead_decrypt( key, alg,
3471 nonce->x, nonce->len,
3472 additional_data->x,
3473 additional_data->len,
3474 input_data->x, input_data->len,
3475 output_data, output_size,
3476 &output_length );
3477
Ronald Cron28a45ed2021-02-09 20:35:42 +01003478 /* If the operation is not supported, just skip and not fail in case the
3479 * decryption involves a common limitation of cryptography hardwares and
3480 * an alternative implementation. */
3481 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003482 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003483 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3484 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003485 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003486
3487 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003488
Gilles Peskine2d277862018-06-18 15:41:12 +02003489 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003490 ASSERT_COMPARE( expected_data->x, expected_data->len,
3491 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003492
Gilles Peskinea1cac842018-06-11 19:33:02 +02003493exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003494 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003495 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003496 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003497}
3498/* END_CASE */
3499
3500/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003501void signature_size( int type_arg,
3502 int bits,
3503 int alg_arg,
3504 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003505{
3506 psa_key_type_t type = type_arg;
3507 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003508 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003509
Gilles Peskinefe11b722018-12-18 00:24:04 +01003510 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003511#if defined(MBEDTLS_TEST_DEPRECATED)
3512 TEST_EQUAL( actual_size,
3513 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3514#endif /* MBEDTLS_TEST_DEPRECATED */
3515
Gilles Peskinee59236f2018-01-27 23:32:46 +01003516exit:
3517 ;
3518}
3519/* END_CASE */
3520
3521/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003522void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3523 int alg_arg, data_t *input_data,
3524 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003525{
Ronald Cron5425a212020-08-04 14:58:35 +02003526 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003527 psa_key_type_t key_type = key_type_arg;
3528 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003529 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003530 unsigned char *signature = NULL;
3531 size_t signature_size;
3532 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003534
Gilles Peskine8817f612018-12-18 00:18:46 +01003535 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003536
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003537 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003538 psa_set_key_algorithm( &attributes, alg );
3539 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003540
Gilles Peskine049c7532019-05-15 20:22:09 +02003541 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003542 &key ) );
3543 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003544 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003545
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003546 /* Allocate a buffer which has the size advertized by the
3547 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003548 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003549 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003550 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003551 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003552 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003553
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003554 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003555 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003556 input_data->x, input_data->len,
3557 signature, signature_size,
3558 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003559 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003560 ASSERT_COMPARE( output_data->x, output_data->len,
3561 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003562
Gilles Peskine0627f982019-11-26 19:12:16 +01003563#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003564 memset( signature, 0, signature_size );
3565 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003566 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003567 input_data->x, input_data->len,
3568 signature, signature_size,
3569 &signature_length ) );
3570 ASSERT_COMPARE( output_data->x, output_data->len,
3571 signature, signature_length );
3572#endif /* MBEDTLS_TEST_DEPRECATED */
3573
Gilles Peskine20035e32018-02-03 22:44:14 +01003574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003575 /*
3576 * Key attributes may have been returned by psa_get_key_attributes()
3577 * thus reset them as required.
3578 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003579 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003580
Ronald Cron5425a212020-08-04 14:58:35 +02003581 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003582 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003583 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003584}
3585/* END_CASE */
3586
3587/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003588void sign_hash_fail( int key_type_arg, data_t *key_data,
3589 int alg_arg, data_t *input_data,
3590 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003591{
Ronald Cron5425a212020-08-04 14:58:35 +02003592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003593 psa_key_type_t key_type = key_type_arg;
3594 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003595 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003596 psa_status_t actual_status;
3597 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003598 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003599 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003600 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003601
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003602 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003603
Gilles Peskine8817f612018-12-18 00:18:46 +01003604 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003605
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003606 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003607 psa_set_key_algorithm( &attributes, alg );
3608 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003609
Gilles Peskine049c7532019-05-15 20:22:09 +02003610 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003611 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003612
Ronald Cron5425a212020-08-04 14:58:35 +02003613 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003614 input_data->x, input_data->len,
3615 signature, signature_size,
3616 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003617 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003618 /* The value of *signature_length is unspecified on error, but
3619 * whatever it is, it should be less than signature_size, so that
3620 * if the caller tries to read *signature_length bytes without
3621 * checking the error code then they don't overflow a buffer. */
3622 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003623
Gilles Peskine895242b2019-11-29 12:15:40 +01003624#if defined(MBEDTLS_TEST_DEPRECATED)
3625 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02003626 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003627 input_data->x, input_data->len,
3628 signature, signature_size,
3629 &signature_length ),
3630 expected_status );
3631 TEST_ASSERT( signature_length <= signature_size );
3632#endif /* MBEDTLS_TEST_DEPRECATED */
3633
Gilles Peskine20035e32018-02-03 22:44:14 +01003634exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003635 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003636 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003637 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003638 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003639}
3640/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003641
3642/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003643void sign_verify_hash( int key_type_arg, data_t *key_data,
3644 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003645{
Ronald Cron5425a212020-08-04 14:58:35 +02003646 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003647 psa_key_type_t key_type = key_type_arg;
3648 psa_algorithm_t alg = alg_arg;
3649 size_t key_bits;
3650 unsigned char *signature = NULL;
3651 size_t signature_size;
3652 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003653 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003654
Gilles Peskine8817f612018-12-18 00:18:46 +01003655 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003656
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003657 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003658 psa_set_key_algorithm( &attributes, alg );
3659 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003660
Gilles Peskine049c7532019-05-15 20:22:09 +02003661 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003662 &key ) );
3663 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003664 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003665
3666 /* Allocate a buffer which has the size advertized by the
3667 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003668 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003669 key_bits, alg );
3670 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003671 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003672 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003673
3674 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003675 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003676 input_data->x, input_data->len,
3677 signature, signature_size,
3678 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003679 /* Check that the signature length looks sensible. */
3680 TEST_ASSERT( signature_length <= signature_size );
3681 TEST_ASSERT( signature_length > 0 );
3682
3683 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003684 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003685 input_data->x, input_data->len,
3686 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003687
3688 if( input_data->len != 0 )
3689 {
3690 /* Flip a bit in the input and verify that the signature is now
3691 * detected as invalid. Flip a bit at the beginning, not at the end,
3692 * because ECDSA may ignore the last few bits of the input. */
3693 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003694 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003695 input_data->x, input_data->len,
3696 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003697 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003698 }
3699
3700exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003701 /*
3702 * Key attributes may have been returned by psa_get_key_attributes()
3703 * thus reset them as required.
3704 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003705 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003706
Ronald Cron5425a212020-08-04 14:58:35 +02003707 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003708 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003709 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003710}
3711/* END_CASE */
3712
3713/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003714void verify_hash( int key_type_arg, data_t *key_data,
3715 int alg_arg, data_t *hash_data,
3716 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003717{
Ronald Cron5425a212020-08-04 14:58:35 +02003718 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003719 psa_key_type_t key_type = key_type_arg;
3720 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003722
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003723 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003724
Gilles Peskine8817f612018-12-18 00:18:46 +01003725 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003726
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003727 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003728 psa_set_key_algorithm( &attributes, alg );
3729 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003730
Gilles Peskine049c7532019-05-15 20:22:09 +02003731 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003732 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003733
Ronald Cron5425a212020-08-04 14:58:35 +02003734 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003735 hash_data->x, hash_data->len,
3736 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003737
3738#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003739 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01003740 hash_data->x, hash_data->len,
3741 signature_data->x,
3742 signature_data->len ) );
3743
3744#endif /* MBEDTLS_TEST_DEPRECATED */
3745
itayzafrir5c753392018-05-08 11:18:38 +03003746exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003747 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003748 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003749 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003750}
3751/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003752
3753/* BEGIN_CASE */
gabor-mezei-armdc76df42021-04-16 14:21:21 +02003754void verify_hash_fail( int key_type_arg, data_t *key_data,
3755 int alg_arg, data_t *hash_data,
3756 data_t *signature_data,
3757 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003758{
Ronald Cron5425a212020-08-04 14:58:35 +02003759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003760 psa_key_type_t key_type = key_type_arg;
3761 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003762 psa_status_t actual_status;
3763 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003765
Gilles Peskine8817f612018-12-18 00:18:46 +01003766 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003767
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003768 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003769 psa_set_key_algorithm( &attributes, alg );
3770 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003771
Gilles Peskine049c7532019-05-15 20:22:09 +02003772 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003773 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003774
Ronald Cron5425a212020-08-04 14:58:35 +02003775 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003776 hash_data->x, hash_data->len,
3777 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003778 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003779
Gilles Peskine895242b2019-11-29 12:15:40 +01003780#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02003781 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01003782 hash_data->x, hash_data->len,
3783 signature_data->x, signature_data->len ),
3784 expected_status );
3785#endif /* MBEDTLS_TEST_DEPRECATED */
3786
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003787exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003788 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003789 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003790 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003791}
3792/* END_CASE */
3793
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003794/* BEGIN_CASE */
gabor-mezei-armabd72582021-04-15 18:19:50 +02003795void sign_message_deterministic( int key_type_arg,
3796 data_t *key_data,
3797 int alg_arg,
3798 data_t *input_data,
3799 data_t *output_data )
3800{
3801 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3802 psa_key_type_t key_type = key_type_arg;
3803 psa_algorithm_t alg = alg_arg;
3804 size_t key_bits;
3805 unsigned char *signature = NULL;
3806 size_t signature_size;
3807 size_t signature_length = 0xdeadbeef;
3808 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3809
3810 PSA_ASSERT( psa_crypto_init( ) );
3811
3812 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3813 psa_set_key_algorithm( &attributes, alg );
3814 psa_set_key_type( &attributes, key_type );
3815
3816 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3817 &key ) );
3818 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3819 key_bits = psa_get_key_bits( &attributes );
3820
3821 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3822 TEST_ASSERT( signature_size != 0 );
3823 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3824 ASSERT_ALLOC( signature, signature_size );
3825
3826 PSA_ASSERT( psa_sign_message( key, alg,
3827 input_data->x, input_data->len,
3828 signature, signature_size,
3829 &signature_length ) );
3830
3831 ASSERT_COMPARE( output_data->x, output_data->len,
3832 signature, signature_length );
3833
3834exit:
3835 psa_reset_key_attributes( &attributes );
3836
3837 psa_destroy_key( key );
3838 mbedtls_free( signature );
3839 PSA_DONE( );
3840
3841}
3842/* END_CASE */
3843
3844/* BEGIN_CASE */
3845void sign_message_fail( int key_type_arg,
3846 data_t *key_data,
3847 int alg_arg,
3848 data_t *input_data,
3849 int signature_size_arg,
3850 int expected_status_arg )
3851{
3852 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3853 psa_key_type_t key_type = key_type_arg;
3854 psa_algorithm_t alg = alg_arg;
3855 size_t signature_size = signature_size_arg;
3856 psa_status_t actual_status;
3857 psa_status_t expected_status = expected_status_arg;
3858 unsigned char *signature = NULL;
3859 size_t signature_length = 0xdeadbeef;
3860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3861
3862 ASSERT_ALLOC( signature, signature_size );
3863
3864 PSA_ASSERT( psa_crypto_init( ) );
3865
3866 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3867 psa_set_key_algorithm( &attributes, alg );
3868 psa_set_key_type( &attributes, key_type );
3869
3870 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3871 &key ) );
3872
3873 actual_status = psa_sign_message( key, alg,
3874 input_data->x, input_data->len,
3875 signature, signature_size,
3876 &signature_length );
3877 TEST_EQUAL( actual_status, expected_status );
3878 /* The value of *signature_length is unspecified on error, but
3879 * whatever it is, it should be less than signature_size, so that
3880 * if the caller tries to read *signature_length bytes without
3881 * checking the error code then they don't overflow a buffer. */
3882 TEST_ASSERT( signature_length <= signature_size );
3883
3884exit:
3885 psa_reset_key_attributes( &attributes );
3886 psa_destroy_key( key );
3887 mbedtls_free( signature );
3888 PSA_DONE( );
3889}
3890/* END_CASE */
3891
3892/* BEGIN_CASE */
3893void sign_verify_message( int key_type_arg,
3894 data_t *key_data,
3895 int alg_arg,
3896 data_t *input_data )
3897{
3898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3899 psa_key_type_t key_type = key_type_arg;
3900 psa_algorithm_t alg = alg_arg;
3901 size_t key_bits;
3902 unsigned char *signature = NULL;
3903 size_t signature_size;
3904 size_t signature_length = 0xdeadbeef;
3905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3906
3907 PSA_ASSERT( psa_crypto_init( ) );
3908
3909 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3910 PSA_KEY_USAGE_VERIFY_MESSAGE );
3911 psa_set_key_algorithm( &attributes, alg );
3912 psa_set_key_type( &attributes, key_type );
3913
3914 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3915 &key ) );
3916 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3917 key_bits = psa_get_key_bits( &attributes );
3918
3919 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3920 TEST_ASSERT( signature_size != 0 );
3921 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3922 ASSERT_ALLOC( signature, signature_size );
3923
3924 PSA_ASSERT( psa_sign_message( key, alg,
3925 input_data->x, input_data->len,
3926 signature, signature_size,
3927 &signature_length ) );
3928 TEST_ASSERT( signature_length <= signature_size );
3929 TEST_ASSERT( signature_length > 0 );
3930
3931 PSA_ASSERT( psa_verify_message( key, alg,
3932 input_data->x, input_data->len,
3933 signature, signature_length ) );
3934
3935 if( input_data->len != 0 )
3936 {
3937 /* Flip a bit in the input and verify that the signature is now
3938 * detected as invalid. Flip a bit at the beginning, not at the end,
3939 * because ECDSA may ignore the last few bits of the input. */
3940 input_data->x[0] ^= 1;
3941 TEST_EQUAL( psa_verify_message( key, alg,
3942 input_data->x, input_data->len,
3943 signature, signature_length ),
3944 PSA_ERROR_INVALID_SIGNATURE );
3945 }
3946
3947exit:
3948 psa_reset_key_attributes( &attributes );
3949
3950 psa_destroy_key( key );
3951 mbedtls_free( signature );
3952 PSA_DONE( );
3953}
3954/* END_CASE */
3955
3956/* BEGIN_CASE */
3957void verify_message( int key_type_arg,
3958 data_t *key_data,
3959 int alg_arg,
3960 data_t *input_data,
3961 data_t *signature_data )
3962{
3963 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3964 psa_key_type_t key_type = key_type_arg;
3965 psa_algorithm_t alg = alg_arg;
3966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3967
3968 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3969
3970 PSA_ASSERT( psa_crypto_init( ) );
3971
3972 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3973 psa_set_key_algorithm( &attributes, alg );
3974 psa_set_key_type( &attributes, key_type );
3975
3976 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3977 &key ) );
3978
3979 PSA_ASSERT( psa_verify_message( key, alg,
3980 input_data->x, input_data->len,
3981 signature_data->x, signature_data->len ) );
3982
3983exit:
3984 psa_reset_key_attributes( &attributes );
3985 psa_destroy_key( key );
3986 PSA_DONE( );
3987}
3988/* END_CASE */
3989
3990/* BEGIN_CASE */
3991void verify_message_fail( int key_type_arg,
3992 data_t *key_data,
3993 int alg_arg,
3994 data_t *hash_data,
3995 data_t *signature_data,
3996 int expected_status_arg )
3997{
3998 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3999 psa_key_type_t key_type = key_type_arg;
4000 psa_algorithm_t alg = alg_arg;
4001 psa_status_t actual_status;
4002 psa_status_t expected_status = expected_status_arg;
4003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4004
4005 PSA_ASSERT( psa_crypto_init( ) );
4006
4007 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
4008 psa_set_key_algorithm( &attributes, alg );
4009 psa_set_key_type( &attributes, key_type );
4010
4011 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4012 &key ) );
4013
4014 actual_status = psa_verify_message( key, alg,
4015 hash_data->x, hash_data->len,
4016 signature_data->x,
4017 signature_data->len );
4018 TEST_EQUAL( actual_status, expected_status );
4019
4020exit:
4021 psa_reset_key_attributes( &attributes );
4022 psa_destroy_key( key );
4023 PSA_DONE( );
4024}
4025/* END_CASE */
4026
4027/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004028void asymmetric_encrypt( int key_type_arg,
4029 data_t *key_data,
4030 int alg_arg,
4031 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004032 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004033 int expected_output_length_arg,
4034 int expected_status_arg )
4035{
Ronald Cron5425a212020-08-04 14:58:35 +02004036 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004037 psa_key_type_t key_type = key_type_arg;
4038 psa_algorithm_t alg = alg_arg;
4039 size_t expected_output_length = expected_output_length_arg;
4040 size_t key_bits;
4041 unsigned char *output = NULL;
4042 size_t output_size;
4043 size_t output_length = ~0;
4044 psa_status_t actual_status;
4045 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004047
Gilles Peskine8817f612018-12-18 00:18:46 +01004048 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004049
Gilles Peskine656896e2018-06-29 19:12:28 +02004050 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004051 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4052 psa_set_key_algorithm( &attributes, alg );
4053 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004054 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004055 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004056
4057 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004058 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004059 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004060
Gilles Peskine656896e2018-06-29 19:12:28 +02004061 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004062 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004063 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004064
4065 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004066 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004067 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004068 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004069 output, output_size,
4070 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004071 TEST_EQUAL( actual_status, expected_status );
4072 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004073
Gilles Peskine68428122018-06-30 18:42:41 +02004074 /* If the label is empty, the test framework puts a non-null pointer
4075 * in label->x. Test that a null pointer works as well. */
4076 if( label->len == 0 )
4077 {
4078 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004079 if( output_size != 0 )
4080 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004081 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004082 input_data->x, input_data->len,
4083 NULL, label->len,
4084 output, output_size,
4085 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004086 TEST_EQUAL( actual_status, expected_status );
4087 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004088 }
4089
Gilles Peskine656896e2018-06-29 19:12:28 +02004090exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004091 /*
4092 * Key attributes may have been returned by psa_get_key_attributes()
4093 * thus reset them as required.
4094 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004095 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004096
Ronald Cron5425a212020-08-04 14:58:35 +02004097 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004098 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004099 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004100}
4101/* END_CASE */
4102
4103/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004104void asymmetric_encrypt_decrypt( int key_type_arg,
4105 data_t *key_data,
4106 int alg_arg,
4107 data_t *input_data,
4108 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004109{
Ronald Cron5425a212020-08-04 14:58:35 +02004110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004111 psa_key_type_t key_type = key_type_arg;
4112 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004113 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004114 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004115 size_t output_size;
4116 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004117 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004118 size_t output2_size;
4119 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004121
Gilles Peskine8817f612018-12-18 00:18:46 +01004122 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004123
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004124 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4125 psa_set_key_algorithm( &attributes, alg );
4126 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004127
Gilles Peskine049c7532019-05-15 20:22:09 +02004128 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004129 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004130
4131 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004132 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004133 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01004134
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004135 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01004136 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004137 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01004138
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004139 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01004140 TEST_ASSERT( output2_size <=
4141 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
4142 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004143 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004144
Gilles Peskineeebd7382018-06-08 18:11:54 +02004145 /* We test encryption by checking that encrypt-then-decrypt gives back
4146 * the original plaintext because of the non-optional random
4147 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004148 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004149 input_data->x, input_data->len,
4150 label->x, label->len,
4151 output, output_size,
4152 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004153 /* We don't know what ciphertext length to expect, but check that
4154 * it looks sensible. */
4155 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004156
Ronald Cron5425a212020-08-04 14:58:35 +02004157 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004158 output, output_length,
4159 label->x, label->len,
4160 output2, output2_size,
4161 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004162 ASSERT_COMPARE( input_data->x, input_data->len,
4163 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004164
4165exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004166 /*
4167 * Key attributes may have been returned by psa_get_key_attributes()
4168 * thus reset them as required.
4169 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004170 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004171
Ronald Cron5425a212020-08-04 14:58:35 +02004172 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004173 mbedtls_free( output );
4174 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004175 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004176}
4177/* END_CASE */
4178
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004179/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004180void asymmetric_decrypt( int key_type_arg,
4181 data_t *key_data,
4182 int alg_arg,
4183 data_t *input_data,
4184 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004185 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004186{
Ronald Cron5425a212020-08-04 14:58:35 +02004187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004188 psa_key_type_t key_type = key_type_arg;
4189 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01004190 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004191 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004192 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004193 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004194 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004195
Gilles Peskine8817f612018-12-18 00:18:46 +01004196 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004197
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004198 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4199 psa_set_key_algorithm( &attributes, alg );
4200 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004201
Gilles Peskine049c7532019-05-15 20:22:09 +02004202 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004203 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004204
gabor-mezei-armceface22021-01-21 12:26:17 +01004205 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4206 key_bits = psa_get_key_bits( &attributes );
4207
4208 /* Determine the maximum ciphertext length */
4209 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
4210 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
4211 ASSERT_ALLOC( output, output_size );
4212
Ronald Cron5425a212020-08-04 14:58:35 +02004213 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004214 input_data->x, input_data->len,
4215 label->x, label->len,
4216 output,
4217 output_size,
4218 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004219 ASSERT_COMPARE( expected_data->x, expected_data->len,
4220 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004221
Gilles Peskine68428122018-06-30 18:42:41 +02004222 /* If the label is empty, the test framework puts a non-null pointer
4223 * in label->x. Test that a null pointer works as well. */
4224 if( label->len == 0 )
4225 {
4226 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004227 if( output_size != 0 )
4228 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004229 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004230 input_data->x, input_data->len,
4231 NULL, label->len,
4232 output,
4233 output_size,
4234 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004235 ASSERT_COMPARE( expected_data->x, expected_data->len,
4236 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004237 }
4238
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004239exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004240 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004241 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004242 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004243 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004244}
4245/* END_CASE */
4246
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004247/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004248void asymmetric_decrypt_fail( int key_type_arg,
4249 data_t *key_data,
4250 int alg_arg,
4251 data_t *input_data,
4252 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004253 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004254 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004255{
Ronald Cron5425a212020-08-04 14:58:35 +02004256 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004257 psa_key_type_t key_type = key_type_arg;
4258 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004259 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004260 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004261 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004262 psa_status_t actual_status;
4263 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004265
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004266 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004267
Gilles Peskine8817f612018-12-18 00:18:46 +01004268 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004269
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004270 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4271 psa_set_key_algorithm( &attributes, alg );
4272 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004273
Gilles Peskine049c7532019-05-15 20:22:09 +02004274 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004275 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004276
Ronald Cron5425a212020-08-04 14:58:35 +02004277 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004278 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004279 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004280 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004281 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004282 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004283 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004284
Gilles Peskine68428122018-06-30 18:42:41 +02004285 /* If the label is empty, the test framework puts a non-null pointer
4286 * in label->x. Test that a null pointer works as well. */
4287 if( label->len == 0 )
4288 {
4289 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004290 if( output_size != 0 )
4291 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004292 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004293 input_data->x, input_data->len,
4294 NULL, label->len,
4295 output, output_size,
4296 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004297 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004298 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004299 }
4300
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004301exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004302 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004303 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004304 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004305 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004306}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004307/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004308
4309/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004310void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004311{
4312 /* Test each valid way of initializing the object, except for `= {0}`, as
4313 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4314 * though it's OK by the C standard. We could test for this, but we'd need
4315 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004316 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004317 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4318 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4319 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004320
4321 memset( &zero, 0, sizeof( zero ) );
4322
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004323 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004324 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004325 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004326 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004327 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004328 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004329 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004330
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004331 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004332 PSA_ASSERT( psa_key_derivation_abort(&func) );
4333 PSA_ASSERT( psa_key_derivation_abort(&init) );
4334 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004335}
4336/* END_CASE */
4337
Janos Follath16de4a42019-06-13 16:32:24 +01004338/* BEGIN_CASE */
4339void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004340{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004341 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004342 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004343 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004344
Gilles Peskine8817f612018-12-18 00:18:46 +01004345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004346
Janos Follath16de4a42019-06-13 16:32:24 +01004347 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004348 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004349
4350exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004351 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004352 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004353}
4354/* END_CASE */
4355
Janos Follathaf3c2a02019-06-12 12:34:34 +01004356/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004357void derive_set_capacity( int alg_arg, int capacity_arg,
4358 int expected_status_arg )
4359{
4360 psa_algorithm_t alg = alg_arg;
4361 size_t capacity = capacity_arg;
4362 psa_status_t expected_status = expected_status_arg;
4363 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4364
4365 PSA_ASSERT( psa_crypto_init( ) );
4366
4367 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4368
4369 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4370 expected_status );
4371
4372exit:
4373 psa_key_derivation_abort( &operation );
4374 PSA_DONE( );
4375}
4376/* END_CASE */
4377
4378/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004379void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004380 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004381 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004382 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004383 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004384 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004385 int expected_status_arg3,
4386 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004387{
4388 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004389 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4390 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004391 psa_status_t expected_statuses[] = {expected_status_arg1,
4392 expected_status_arg2,
4393 expected_status_arg3};
4394 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004395 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4396 MBEDTLS_SVC_KEY_ID_INIT,
4397 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004398 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4399 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4400 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004401 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004402 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004403 psa_status_t expected_output_status = expected_output_status_arg;
4404 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004405
4406 PSA_ASSERT( psa_crypto_init( ) );
4407
4408 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4409 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004410
4411 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4412
4413 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4414 {
Gilles Peskinef6279312021-05-27 13:21:20 +02004415 mbedtls_test_set_step( i );
4416 if( steps[i] == 0 )
4417 {
4418 /* Skip this step */
4419 }
4420 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004421 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004422 psa_set_key_type( &attributes, key_types[i] );
4423 PSA_ASSERT( psa_import_key( &attributes,
4424 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004425 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004426 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4427 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4428 {
4429 // When taking a private key as secret input, use key agreement
4430 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004431 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4432 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004433 expected_statuses[i] );
4434 }
4435 else
4436 {
4437 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004438 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004439 expected_statuses[i] );
4440 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004441 }
4442 else
4443 {
4444 TEST_EQUAL( psa_key_derivation_input_bytes(
4445 &operation, steps[i],
4446 inputs[i]->x, inputs[i]->len ),
4447 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004448 }
4449 }
4450
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004451 if( output_key_type != PSA_KEY_TYPE_NONE )
4452 {
4453 psa_reset_key_attributes( &attributes );
4454 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4455 psa_set_key_bits( &attributes, 8 );
4456 actual_output_status =
4457 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004458 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004459 }
4460 else
4461 {
4462 uint8_t buffer[1];
4463 actual_output_status =
4464 psa_key_derivation_output_bytes( &operation,
4465 buffer, sizeof( buffer ) );
4466 }
4467 TEST_EQUAL( actual_output_status, expected_output_status );
4468
Janos Follathaf3c2a02019-06-12 12:34:34 +01004469exit:
4470 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004471 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4472 psa_destroy_key( keys[i] );
4473 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004474 PSA_DONE( );
4475}
4476/* END_CASE */
4477
Janos Follathd958bb72019-07-03 15:02:16 +01004478/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004479void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004480{
Janos Follathd958bb72019-07-03 15:02:16 +01004481 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004482 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004483 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004484 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004485 unsigned char input1[] = "Input 1";
4486 size_t input1_length = sizeof( input1 );
4487 unsigned char input2[] = "Input 2";
4488 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004489 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004490 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004491 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4492 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4493 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004494 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004495
Gilles Peskine8817f612018-12-18 00:18:46 +01004496 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004497
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004498 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4499 psa_set_key_algorithm( &attributes, alg );
4500 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004501
Gilles Peskine73676cb2019-05-15 20:15:10 +02004502 PSA_ASSERT( psa_import_key( &attributes,
4503 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004504 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004505
4506 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004507 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4508 input1, input1_length,
4509 input2, input2_length,
4510 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004511 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004512
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004513 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004514 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004515 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004516
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004517 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004518
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004519 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004520 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004521
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004522exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004523 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004524 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004525 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004526}
4527/* END_CASE */
4528
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004529/* BEGIN_CASE */
Gilles Peskine0faba4e2021-05-27 11:55:02 +02004530void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004531{
4532 uint8_t output_buffer[16];
4533 size_t buffer_size = 16;
4534 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004535 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004536
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004537 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4538 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004539 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004540
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004541 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004542 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004543
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004544 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004545
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004546 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4547 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004548 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004549
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004550 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004551 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004552
4553exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004554 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004555}
4556/* END_CASE */
4557
4558/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004559void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004560 int step1_arg, data_t *input1,
4561 int step2_arg, data_t *input2,
4562 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004563 int requested_capacity_arg,
4564 data_t *expected_output1,
4565 data_t *expected_output2 )
4566{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004567 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004568 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4569 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004570 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4571 MBEDTLS_SVC_KEY_ID_INIT,
4572 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004573 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004574 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004575 uint8_t *expected_outputs[2] =
4576 {expected_output1->x, expected_output2->x};
4577 size_t output_sizes[2] =
4578 {expected_output1->len, expected_output2->len};
4579 size_t output_buffer_size = 0;
4580 uint8_t *output_buffer = NULL;
4581 size_t expected_capacity;
4582 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004584 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004585 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004586
4587 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4588 {
4589 if( output_sizes[i] > output_buffer_size )
4590 output_buffer_size = output_sizes[i];
4591 if( output_sizes[i] == 0 )
4592 expected_outputs[i] = NULL;
4593 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004594 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004595 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004596
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004597 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4598 psa_set_key_algorithm( &attributes, alg );
4599 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004600
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004601 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004602 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4603 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4604 requested_capacity ) );
4605 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004606 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004607 switch( steps[i] )
4608 {
4609 case 0:
4610 break;
4611 case PSA_KEY_DERIVATION_INPUT_SECRET:
4612 PSA_ASSERT( psa_import_key( &attributes,
4613 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004614 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004615
4616 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4617 {
4618 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4619 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4620 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4621 }
4622
Gilles Peskine1468da72019-05-29 17:35:49 +02004623 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004624 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004625 break;
4626 default:
4627 PSA_ASSERT( psa_key_derivation_input_bytes(
4628 &operation, steps[i],
4629 inputs[i]->x, inputs[i]->len ) );
4630 break;
4631 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004632 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004633
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004634 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004635 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004636 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004637 expected_capacity = requested_capacity;
4638
4639 /* Expansion phase. */
4640 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4641 {
4642 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004643 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004644 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004645 if( expected_capacity == 0 && output_sizes[i] == 0 )
4646 {
4647 /* Reading 0 bytes when 0 bytes are available can go either way. */
4648 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004649 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004650 continue;
4651 }
4652 else if( expected_capacity == 0 ||
4653 output_sizes[i] > expected_capacity )
4654 {
4655 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004656 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004657 expected_capacity = 0;
4658 continue;
4659 }
4660 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004661 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004662 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004663 ASSERT_COMPARE( output_buffer, output_sizes[i],
4664 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004665 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004666 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004668 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004669 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004670 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004671 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004672
4673exit:
4674 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004676 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4677 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004678 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004679}
4680/* END_CASE */
4681
4682/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004683void derive_full( int alg_arg,
4684 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004685 data_t *input1,
4686 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004687 int requested_capacity_arg )
4688{
Ronald Cron5425a212020-08-04 14:58:35 +02004689 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004690 psa_algorithm_t alg = alg_arg;
4691 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004692 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004693 unsigned char output_buffer[16];
4694 size_t expected_capacity = requested_capacity;
4695 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004696 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004697
Gilles Peskine8817f612018-12-18 00:18:46 +01004698 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004699
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004700 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4701 psa_set_key_algorithm( &attributes, alg );
4702 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004703
Gilles Peskine049c7532019-05-15 20:22:09 +02004704 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004705 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004706
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004707 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4708 input1->x, input1->len,
4709 input2->x, input2->len,
4710 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004711 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004712
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004713 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004714 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004715 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004716
4717 /* Expansion phase. */
4718 while( current_capacity > 0 )
4719 {
4720 size_t read_size = sizeof( output_buffer );
4721 if( read_size > current_capacity )
4722 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004723 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004724 output_buffer,
4725 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004726 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004727 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004728 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004729 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004730 }
4731
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 /* Check that the operation refuses to go over capacity. */
4733 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004734 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004735
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004737
4738exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004740 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004741 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004742}
4743/* END_CASE */
4744
Janos Follathe60c9052019-07-03 13:51:30 +01004745/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004746void derive_key_exercise( int alg_arg,
4747 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004748 data_t *input1,
4749 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004750 int derived_type_arg,
4751 int derived_bits_arg,
4752 int derived_usage_arg,
4753 int derived_alg_arg )
4754{
Ronald Cron5425a212020-08-04 14:58:35 +02004755 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4756 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004757 psa_algorithm_t alg = alg_arg;
4758 psa_key_type_t derived_type = derived_type_arg;
4759 size_t derived_bits = derived_bits_arg;
4760 psa_key_usage_t derived_usage = derived_usage_arg;
4761 psa_algorithm_t derived_alg = derived_alg_arg;
4762 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004765 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004766
Gilles Peskine8817f612018-12-18 00:18:46 +01004767 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004768
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004769 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4770 psa_set_key_algorithm( &attributes, alg );
4771 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004772 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004773 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004774
4775 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004776 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4777 input1->x, input1->len,
4778 input2->x, input2->len,
4779 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004780 goto exit;
4781
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004782 psa_set_key_usage_flags( &attributes, derived_usage );
4783 psa_set_key_algorithm( &attributes, derived_alg );
4784 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004785 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004786 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004787 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004788
4789 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004790 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004791 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4792 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004793
4794 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004795 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004796 goto exit;
4797
4798exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004799 /*
4800 * Key attributes may have been returned by psa_get_key_attributes()
4801 * thus reset them as required.
4802 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004803 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004804
4805 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004806 psa_destroy_key( base_key );
4807 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004808 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004809}
4810/* END_CASE */
4811
Janos Follath42fd8882019-07-03 14:17:09 +01004812/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004813void derive_key_export( int alg_arg,
4814 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004815 data_t *input1,
4816 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004817 int bytes1_arg,
4818 int bytes2_arg )
4819{
Ronald Cron5425a212020-08-04 14:58:35 +02004820 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4821 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004822 psa_algorithm_t alg = alg_arg;
4823 size_t bytes1 = bytes1_arg;
4824 size_t bytes2 = bytes2_arg;
4825 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004827 uint8_t *output_buffer = NULL;
4828 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004829 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4830 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004831 size_t length;
4832
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004833 ASSERT_ALLOC( output_buffer, capacity );
4834 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004835 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004836
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004837 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4838 psa_set_key_algorithm( &base_attributes, alg );
4839 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004840 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004841 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004842
4843 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004844 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4845 input1->x, input1->len,
4846 input2->x, input2->len,
4847 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004848 goto exit;
4849
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004850 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004851 output_buffer,
4852 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004854
4855 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004856 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4857 input1->x, input1->len,
4858 input2->x, input2->len,
4859 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004860 goto exit;
4861
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004862 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4863 psa_set_key_algorithm( &derived_attributes, 0 );
4864 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004865 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004867 &derived_key ) );
4868 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004869 export_buffer, bytes1,
4870 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004871 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004872 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004873 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004874 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004875 &derived_key ) );
4876 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004877 export_buffer + bytes1, bytes2,
4878 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004879 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004880
4881 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004882 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4883 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004884
4885exit:
4886 mbedtls_free( output_buffer );
4887 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004889 psa_destroy_key( base_key );
4890 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004891 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004892}
4893/* END_CASE */
4894
4895/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004896void derive_key( int alg_arg,
4897 data_t *key_data, data_t *input1, data_t *input2,
4898 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004899 int expected_status_arg,
4900 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004901{
Ronald Cron5425a212020-08-04 14:58:35 +02004902 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4903 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004904 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004905 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004906 size_t bits = bits_arg;
4907 psa_status_t expected_status = expected_status_arg;
4908 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4909 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4910 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4911
4912 PSA_ASSERT( psa_crypto_init( ) );
4913
4914 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4915 psa_set_key_algorithm( &base_attributes, alg );
4916 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4917 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004918 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004919
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004920 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4921 input1->x, input1->len,
4922 input2->x, input2->len,
4923 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004924 goto exit;
4925
4926 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4927 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004928 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004929 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004930
4931 psa_status_t status =
4932 psa_key_derivation_output_key( &derived_attributes,
4933 &operation,
4934 &derived_key );
4935 if( is_large_output > 0 )
4936 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4937 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004938
4939exit:
4940 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004941 psa_destroy_key( base_key );
4942 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004943 PSA_DONE( );
4944}
4945/* END_CASE */
4946
4947/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004948void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004949 int our_key_type_arg, int our_key_alg_arg,
4950 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004951 int expected_status_arg )
4952{
Ronald Cron5425a212020-08-04 14:58:35 +02004953 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004954 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004955 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004956 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004957 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004958 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004959 psa_status_t expected_status = expected_status_arg;
4960 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004961
Gilles Peskine8817f612018-12-18 00:18:46 +01004962 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004963
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004964 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004965 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004966 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004967 PSA_ASSERT( psa_import_key( &attributes,
4968 our_key_data->x, our_key_data->len,
4969 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004970
Gilles Peskine77f40d82019-04-11 21:27:06 +02004971 /* The tests currently include inputs that should fail at either step.
4972 * Test cases that fail at the setup step should be changed to call
4973 * key_derivation_setup instead, and this function should be renamed
4974 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004976 if( status == PSA_SUCCESS )
4977 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004978 TEST_EQUAL( psa_key_derivation_key_agreement(
4979 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4980 our_key,
4981 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004982 expected_status );
4983 }
4984 else
4985 {
4986 TEST_ASSERT( status == expected_status );
4987 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004988
4989exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004990 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004991 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004992 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004993}
4994/* END_CASE */
4995
4996/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004997void raw_key_agreement( int alg_arg,
4998 int our_key_type_arg, data_t *our_key_data,
4999 data_t *peer_key_data,
5000 data_t *expected_output )
5001{
Ronald Cron5425a212020-08-04 14:58:35 +02005002 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005003 psa_algorithm_t alg = alg_arg;
5004 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005005 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005006 unsigned char *output = NULL;
5007 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01005008 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005009
5010 ASSERT_ALLOC( output, expected_output->len );
5011 PSA_ASSERT( psa_crypto_init( ) );
5012
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005013 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5014 psa_set_key_algorithm( &attributes, alg );
5015 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005016 PSA_ASSERT( psa_import_key( &attributes,
5017 our_key_data->x, our_key_data->len,
5018 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005019
gabor-mezei-armceface22021-01-21 12:26:17 +01005020 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
5021 key_bits = psa_get_key_bits( &attributes );
5022
Gilles Peskinebe697d82019-05-16 18:00:41 +02005023 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5024 peer_key_data->x, peer_key_data->len,
5025 output, expected_output->len,
5026 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005027 ASSERT_COMPARE( output, output_length,
5028 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01005029 TEST_ASSERT( output_length <=
5030 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
5031 TEST_ASSERT( output_length <=
5032 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005033
5034exit:
5035 mbedtls_free( output );
5036 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005037 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005038}
5039/* END_CASE */
5040
5041/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005042void key_agreement_capacity( int alg_arg,
5043 int our_key_type_arg, data_t *our_key_data,
5044 data_t *peer_key_data,
5045 int expected_capacity_arg )
5046{
Ronald Cron5425a212020-08-04 14:58:35 +02005047 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005048 psa_algorithm_t alg = alg_arg;
5049 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005050 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005051 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005052 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005053 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005054
Gilles Peskine8817f612018-12-18 00:18:46 +01005055 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005056
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005057 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5058 psa_set_key_algorithm( &attributes, alg );
5059 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005060 PSA_ASSERT( psa_import_key( &attributes,
5061 our_key_data->x, our_key_data->len,
5062 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005063
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005065 PSA_ASSERT( psa_key_derivation_key_agreement(
5066 &operation,
5067 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5068 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005069 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5070 {
5071 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005073 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005074 NULL, 0 ) );
5075 }
Gilles Peskine59685592018-09-18 12:11:34 +02005076
Gilles Peskinebf491972018-10-25 22:36:12 +02005077 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005078 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005079 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005080 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005081
Gilles Peskinebf491972018-10-25 22:36:12 +02005082 /* Test the actual capacity by reading the output. */
5083 while( actual_capacity > sizeof( output ) )
5084 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005085 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005086 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005087 actual_capacity -= sizeof( output );
5088 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005089 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005090 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005091 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005092 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005093
Gilles Peskine59685592018-09-18 12:11:34 +02005094exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005095 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005096 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005097 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005098}
5099/* END_CASE */
5100
5101/* BEGIN_CASE */
5102void key_agreement_output( int alg_arg,
5103 int our_key_type_arg, data_t *our_key_data,
5104 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005105 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005106{
Ronald Cron5425a212020-08-04 14:58:35 +02005107 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005108 psa_algorithm_t alg = alg_arg;
5109 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005112 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005113
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005114 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5115 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005116
Gilles Peskine8817f612018-12-18 00:18:46 +01005117 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005118
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005119 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5120 psa_set_key_algorithm( &attributes, alg );
5121 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005122 PSA_ASSERT( psa_import_key( &attributes,
5123 our_key_data->x, our_key_data->len,
5124 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005125
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005126 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005127 PSA_ASSERT( psa_key_derivation_key_agreement(
5128 &operation,
5129 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5130 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005131 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5132 {
5133 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005135 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005136 NULL, 0 ) );
5137 }
Gilles Peskine59685592018-09-18 12:11:34 +02005138
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005140 actual_output,
5141 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005142 ASSERT_COMPARE( actual_output, expected_output1->len,
5143 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005144 if( expected_output2->len != 0 )
5145 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005146 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005147 actual_output,
5148 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005149 ASSERT_COMPARE( actual_output, expected_output2->len,
5150 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005151 }
Gilles Peskine59685592018-09-18 12:11:34 +02005152
5153exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005154 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005155 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005156 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005157 mbedtls_free( actual_output );
5158}
5159/* END_CASE */
5160
5161/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005162void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005163{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005164 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005165 unsigned char *output = NULL;
5166 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005167 size_t i;
5168 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005169
Simon Butcher49f8e312020-03-03 15:51:50 +00005170 TEST_ASSERT( bytes_arg >= 0 );
5171
Gilles Peskine91892022021-02-08 19:50:26 +01005172 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005173 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005174
Gilles Peskine8817f612018-12-18 00:18:46 +01005175 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005176
Gilles Peskinea50d7392018-06-21 10:22:13 +02005177 /* Run several times, to ensure that every output byte will be
5178 * nonzero at least once with overwhelming probability
5179 * (2^(-8*number_of_runs)). */
5180 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005181 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005182 if( bytes != 0 )
5183 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005184 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005185
Gilles Peskinea50d7392018-06-21 10:22:13 +02005186 for( i = 0; i < bytes; i++ )
5187 {
5188 if( output[i] != 0 )
5189 ++changed[i];
5190 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005191 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005192
5193 /* Check that every byte was changed to nonzero at least once. This
5194 * validates that psa_generate_random is overwriting every byte of
5195 * the output buffer. */
5196 for( i = 0; i < bytes; i++ )
5197 {
5198 TEST_ASSERT( changed[i] != 0 );
5199 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005200
5201exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005202 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005203 mbedtls_free( output );
5204 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005205}
5206/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005207
5208/* BEGIN_CASE */
5209void generate_key( int type_arg,
5210 int bits_arg,
5211 int usage_arg,
5212 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005213 int expected_status_arg,
5214 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005215{
Ronald Cron5425a212020-08-04 14:58:35 +02005216 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005217 psa_key_type_t type = type_arg;
5218 psa_key_usage_t usage = usage_arg;
5219 size_t bits = bits_arg;
5220 psa_algorithm_t alg = alg_arg;
5221 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005222 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005223 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005224
Gilles Peskine8817f612018-12-18 00:18:46 +01005225 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005226
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005227 psa_set_key_usage_flags( &attributes, usage );
5228 psa_set_key_algorithm( &attributes, alg );
5229 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005230 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005231
5232 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005233 psa_status_t status = psa_generate_key( &attributes, &key );
5234
5235 if( is_large_key > 0 )
5236 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5237 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005238 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005239 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005240
5241 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005242 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005243 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5244 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005245
Gilles Peskine818ca122018-06-20 18:16:48 +02005246 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005247 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005248 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005249
5250exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005251 /*
5252 * Key attributes may have been returned by psa_get_key_attributes()
5253 * thus reset them as required.
5254 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005255 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005256
Ronald Cron5425a212020-08-04 14:58:35 +02005257 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005258 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005259}
5260/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005261
Ronald Cronee414c72021-03-18 18:50:08 +01005262/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005263void generate_key_rsa( int bits_arg,
5264 data_t *e_arg,
5265 int expected_status_arg )
5266{
Ronald Cron5425a212020-08-04 14:58:35 +02005267 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005268 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005269 size_t bits = bits_arg;
5270 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5271 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5272 psa_status_t expected_status = expected_status_arg;
5273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5274 uint8_t *exported = NULL;
5275 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005276 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005277 size_t exported_length = SIZE_MAX;
5278 uint8_t *e_read_buffer = NULL;
5279 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005280 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005281 size_t e_read_length = SIZE_MAX;
5282
5283 if( e_arg->len == 0 ||
5284 ( e_arg->len == 3 &&
5285 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5286 {
5287 is_default_public_exponent = 1;
5288 e_read_size = 0;
5289 }
5290 ASSERT_ALLOC( e_read_buffer, e_read_size );
5291 ASSERT_ALLOC( exported, exported_size );
5292
5293 PSA_ASSERT( psa_crypto_init( ) );
5294
5295 psa_set_key_usage_flags( &attributes, usage );
5296 psa_set_key_algorithm( &attributes, alg );
5297 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5298 e_arg->x, e_arg->len ) );
5299 psa_set_key_bits( &attributes, bits );
5300
5301 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005302 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005303 if( expected_status != PSA_SUCCESS )
5304 goto exit;
5305
5306 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005307 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005308 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5309 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5310 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5311 e_read_buffer, e_read_size,
5312 &e_read_length ) );
5313 if( is_default_public_exponent )
5314 TEST_EQUAL( e_read_length, 0 );
5315 else
5316 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5317
5318 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005319 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005320 goto exit;
5321
5322 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005323 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005324 exported, exported_size,
5325 &exported_length ) );
5326 {
5327 uint8_t *p = exported;
5328 uint8_t *end = exported + exported_length;
5329 size_t len;
5330 /* RSAPublicKey ::= SEQUENCE {
5331 * modulus INTEGER, -- n
5332 * publicExponent INTEGER } -- e
5333 */
5334 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005335 MBEDTLS_ASN1_SEQUENCE |
5336 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005337 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005338 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5339 MBEDTLS_ASN1_INTEGER ) );
5340 if( len >= 1 && p[0] == 0 )
5341 {
5342 ++p;
5343 --len;
5344 }
5345 if( e_arg->len == 0 )
5346 {
5347 TEST_EQUAL( len, 3 );
5348 TEST_EQUAL( p[0], 1 );
5349 TEST_EQUAL( p[1], 0 );
5350 TEST_EQUAL( p[2], 1 );
5351 }
5352 else
5353 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5354 }
5355
5356exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005357 /*
5358 * Key attributes may have been returned by psa_get_key_attributes() or
5359 * set by psa_set_key_domain_parameters() thus reset them as required.
5360 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005361 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005362
Ronald Cron5425a212020-08-04 14:58:35 +02005363 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005364 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005365 mbedtls_free( e_read_buffer );
5366 mbedtls_free( exported );
5367}
5368/* END_CASE */
5369
Darryl Greend49a4992018-06-18 17:27:26 +01005370/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005371void persistent_key_load_key_from_storage( data_t *data,
5372 int type_arg, int bits_arg,
5373 int usage_flags_arg, int alg_arg,
5374 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005375{
Ronald Cron71016a92020-08-28 19:01:50 +02005376 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005378 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5379 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005380 psa_key_type_t type = type_arg;
5381 size_t bits = bits_arg;
5382 psa_key_usage_t usage_flags = usage_flags_arg;
5383 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005384 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005385 unsigned char *first_export = NULL;
5386 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005387 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005388 size_t first_exported_length;
5389 size_t second_exported_length;
5390
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005391 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5392 {
5393 ASSERT_ALLOC( first_export, export_size );
5394 ASSERT_ALLOC( second_export, export_size );
5395 }
Darryl Greend49a4992018-06-18 17:27:26 +01005396
Gilles Peskine8817f612018-12-18 00:18:46 +01005397 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005398
Gilles Peskinec87af662019-05-15 16:12:22 +02005399 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005400 psa_set_key_usage_flags( &attributes, usage_flags );
5401 psa_set_key_algorithm( &attributes, alg );
5402 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005403 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005404
Darryl Green0c6575a2018-11-07 16:05:30 +00005405 switch( generation_method )
5406 {
5407 case IMPORT_KEY:
5408 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005409 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005410 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005411 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005412
Darryl Green0c6575a2018-11-07 16:05:30 +00005413 case GENERATE_KEY:
5414 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005415 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005416 break;
5417
5418 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005419#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005420 {
5421 /* Create base key */
5422 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5423 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5424 psa_set_key_usage_flags( &base_attributes,
5425 PSA_KEY_USAGE_DERIVE );
5426 psa_set_key_algorithm( &base_attributes, derive_alg );
5427 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005428 PSA_ASSERT( psa_import_key( &base_attributes,
5429 data->x, data->len,
5430 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005431 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005432 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005433 PSA_ASSERT( psa_key_derivation_input_key(
5434 &operation,
5435 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005436 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005438 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005439 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5440 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005441 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005443 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005444 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005445 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005446#else
5447 TEST_ASSUME( ! "KDF not supported in this configuration" );
5448#endif
5449 break;
5450
5451 default:
5452 TEST_ASSERT( ! "generation_method not implemented in test" );
5453 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005454 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005455 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005456
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005457 /* Export the key if permitted by the key policy. */
5458 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5459 {
Ronald Cron5425a212020-08-04 14:58:35 +02005460 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005461 first_export, export_size,
5462 &first_exported_length ) );
5463 if( generation_method == IMPORT_KEY )
5464 ASSERT_COMPARE( data->x, data->len,
5465 first_export, first_exported_length );
5466 }
Darryl Greend49a4992018-06-18 17:27:26 +01005467
5468 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005469 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005470 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005471 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005472
Darryl Greend49a4992018-06-18 17:27:26 +01005473 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005474 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005475 TEST_ASSERT( mbedtls_svc_key_id_equal(
5476 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005477 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5478 PSA_KEY_LIFETIME_PERSISTENT );
5479 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5480 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4d9009e2021-05-13 12:05:01 +02005481 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-armff03fd62021-06-28 14:05:00 +02005482 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005483 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005484
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005485 /* Export the key again if permitted by the key policy. */
5486 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005487 {
Ronald Cron5425a212020-08-04 14:58:35 +02005488 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005489 second_export, export_size,
5490 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005491 ASSERT_COMPARE( first_export, first_exported_length,
5492 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005493 }
5494
5495 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005496 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005497 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005498
5499exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005500 /*
5501 * Key attributes may have been returned by psa_get_key_attributes()
5502 * thus reset them as required.
5503 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005504 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005505
Darryl Greend49a4992018-06-18 17:27:26 +01005506 mbedtls_free( first_export );
5507 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005508 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005509 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005510 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005511 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005512}
5513/* END_CASE */