blob: f0b86476f7de08f2f6f2dd7be226cb4907e12c91 [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"
17
Jaeden Amerof24c7f82018-06-27 17:20:43 +010018/** An invalid export length that will never be set by psa_export_key(). */
19static const size_t INVALID_EXPORT_LENGTH = ~0U;
20
Gilles Peskinef426e0f2019-02-25 17:42:03 +010021/* A hash algorithm that is known to be supported.
22 *
23 * This is used in some smoke tests.
24 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010025#if defined(PSA_WANT_ALG_MD2)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010026#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
Gilles Peskined6dc40c2021-01-12 12:55:31 +010027#elif defined(PSA_WANT_ALG_MD4)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010028#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
Gilles Peskined6dc40c2021-01-12 12:55:31 +010029#elif defined(PSA_WANT_ALG_MD5)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010030#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
31/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
32 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
33 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
34 * implausible anyway. */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010035#elif defined(PSA_WANT_ALG_SHA_1)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010036#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
Gilles Peskined6dc40c2021-01-12 12:55:31 +010037#elif defined(PSA_WANT_ALG_SHA_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010038#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
Gilles Peskined6dc40c2021-01-12 12:55:31 +010039#elif defined(PSA_WANT_ALG_SHA_384)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010040#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
Gilles Peskined6dc40c2021-01-12 12:55:31 +010041#elif defined(PSA_WANT_ALG_SHA_512)
42#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512
43#elif defined(PSA_WANT_ALG_SHA3_256)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010044#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
45#else
46#undef KNOWN_SUPPORTED_HASH_ALG
47#endif
48
49/* A block cipher that is known to be supported.
50 *
51 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
52 */
53#if defined(MBEDTLS_AES_C)
54#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
55#elif defined(MBEDTLS_ARIA_C)
56#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
57#elif defined(MBEDTLS_CAMELLIA_C)
58#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
59#undef KNOWN_SUPPORTED_BLOCK_CIPHER
60#endif
61
62/* A MAC mode that is known to be supported.
63 *
64 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
65 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
66 *
67 * This is used in some smoke tests.
68 */
Gilles Peskined6dc40c2021-01-12 12:55:31 +010069#if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010070#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
71#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
72#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
73#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
74#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
75#else
76#undef KNOWN_SUPPORTED_MAC_ALG
77#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
78#endif
79
80/* A cipher algorithm and key type that are known to be supported.
81 *
82 * This is used in some smoke tests.
83 */
84#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
85#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
86#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
87#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
88#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
89#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
90#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
91#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
92#else
93#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
94#endif
95#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
96#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
97#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
98#elif defined(MBEDTLS_RC4_C)
99#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
100#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
101#else
102#undef KNOWN_SUPPORTED_CIPHER_ALG
103#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
104#endif
105
Gilles Peskine667c1112019-12-03 19:03:20 +0100106#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100107static int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
Gilles Peskine667c1112019-12-03 19:03:20 +0100108{
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100109 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
110 PSA_KEY_LOCATION_LOCAL_STORAGE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100111}
Gilles Peskine667c1112019-12-03 19:03:20 +0100112#endif
113
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200114/** Test if a buffer contains a constant byte value.
115 *
116 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200117 *
118 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200119 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200120 * \param size Size of the buffer in bytes.
121 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200122 * \return 1 if the buffer is all-bits-zero.
123 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200124 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200125static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200126{
127 size_t i;
128 for( i = 0; i < size; i++ )
129 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200130 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200131 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200132 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200133 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200134}
Gilles Peskine818ca122018-06-20 18:16:48 +0200135
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200136/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
137static int asn1_write_10x( unsigned char **p,
138 unsigned char *start,
139 size_t bits,
140 unsigned char x )
141{
142 int ret;
143 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200144 if( bits == 0 )
145 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
146 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200147 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300148 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200149 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
150 *p -= len;
151 ( *p )[len-1] = x;
152 if( bits % 8 == 0 )
153 ( *p )[1] |= 1;
154 else
155 ( *p )[0] |= 1 << ( bits % 8 );
156 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
157 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
158 MBEDTLS_ASN1_INTEGER ) );
159 return( len );
160}
161
162static int construct_fake_rsa_key( unsigned char *buffer,
163 size_t buffer_size,
164 unsigned char **p,
165 size_t bits,
166 int keypair )
167{
168 size_t half_bits = ( bits + 1 ) / 2;
169 int ret;
170 int len = 0;
171 /* Construct something that looks like a DER encoding of
172 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
173 * RSAPrivateKey ::= SEQUENCE {
174 * version Version,
175 * modulus INTEGER, -- n
176 * publicExponent INTEGER, -- e
177 * privateExponent INTEGER, -- d
178 * prime1 INTEGER, -- p
179 * prime2 INTEGER, -- q
180 * exponent1 INTEGER, -- d mod (p-1)
181 * exponent2 INTEGER, -- d mod (q-1)
182 * coefficient INTEGER, -- (inverse of q) mod p
183 * otherPrimeInfos OtherPrimeInfos OPTIONAL
184 * }
185 * Or, for a public key, the same structure with only
186 * version, modulus and publicExponent.
187 */
188 *p = buffer + buffer_size;
189 if( keypair )
190 {
191 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
192 asn1_write_10x( p, buffer, half_bits, 1 ) );
193 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
194 asn1_write_10x( p, buffer, half_bits, 1 ) );
195 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
196 asn1_write_10x( p, buffer, half_bits, 1 ) );
197 MBEDTLS_ASN1_CHK_ADD( len, /* q */
198 asn1_write_10x( p, buffer, half_bits, 1 ) );
199 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
200 asn1_write_10x( p, buffer, half_bits, 3 ) );
201 MBEDTLS_ASN1_CHK_ADD( len, /* d */
202 asn1_write_10x( p, buffer, bits, 1 ) );
203 }
204 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
205 asn1_write_10x( p, buffer, 17, 1 ) );
206 MBEDTLS_ASN1_CHK_ADD( len, /* n */
207 asn1_write_10x( p, buffer, bits, 1 ) );
208 if( keypair )
209 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
210 mbedtls_asn1_write_int( p, buffer, 0 ) );
211 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
212 {
213 const unsigned char tag =
214 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
215 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
216 }
217 return( len );
218}
219
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100220static int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
Gilles Peskine667c1112019-12-03 19:03:20 +0100221{
222 int ok = 0;
223 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
224 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200225 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100226 psa_key_type_t type;
227 psa_key_type_t bits;
228
229 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
230 lifetime = psa_get_key_lifetime( &attributes );
231 id = psa_get_key_id( &attributes );
232 type = psa_get_key_type( &attributes );
233 bits = psa_get_key_bits( &attributes );
234
235 /* Persistence */
Ronald Cronf1ff9a82020-10-19 08:44:19 +0200236 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
Ronald Cron41841072020-09-17 15:28:26 +0200237 {
238 TEST_ASSERT(
239 ( PSA_KEY_ID_VOLATILE_MIN <=
240 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
241 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
242 PSA_KEY_ID_VOLATILE_MAX ) );
243 }
Gilles Peskine667c1112019-12-03 19:03:20 +0100244 else
245 {
246 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200247 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
248 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100249 }
250#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
251 /* randomly-generated 64-bit constant, should never appear in test data */
252 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
253 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
Ronald Cron9e12f8f2020-11-13 09:46:44 +0100254 if( lifetime_is_dynamic_secure_element( lifetime ) )
Gilles Peskine667c1112019-12-03 19:03:20 +0100255 {
256 /* Mbed Crypto currently always exposes the slot number to
257 * applications. This is not mandated by the PSA specification
258 * and may change in future versions. */
259 TEST_EQUAL( status, 0 );
260 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
261 }
262 else
263 {
264 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
265 }
266#endif
267
268 /* Type and size */
269 TEST_ASSERT( type != 0 );
270 TEST_ASSERT( bits != 0 );
271 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
272 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
273 TEST_ASSERT( bits % 8 == 0 );
274
275 /* MAX macros concerning specific key types */
276 if( PSA_KEY_TYPE_IS_ECC( type ) )
277 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
278 else if( PSA_KEY_TYPE_IS_RSA( type ) )
279 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100280 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
Gilles Peskine667c1112019-12-03 19:03:20 +0100281
282 ok = 1;
283
284exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100285 /*
286 * Key attributes may have been returned by psa_get_key_attributes()
287 * thus reset them as required.
288 */
Gilles Peskine667c1112019-12-03 19:03:20 +0100289 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100290
Gilles Peskine667c1112019-12-03 19:03:20 +0100291 return( ok );
292}
293
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100294int exercise_mac_setup( psa_key_type_t key_type,
295 const unsigned char *key_bytes,
296 size_t key_length,
297 psa_algorithm_t alg,
298 psa_mac_operation_t *operation,
299 psa_status_t *status )
300{
Ronald Cron5425a212020-08-04 14:58:35 +0200301 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200302 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100303
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100304 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200305 psa_set_key_algorithm( &attributes, alg );
306 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200307 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100308
Ronald Cron5425a212020-08-04 14:58:35 +0200309 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100310 /* Whether setup succeeded or failed, abort must succeed. */
311 PSA_ASSERT( psa_mac_abort( operation ) );
312 /* If setup failed, reproduce the failure, so that the caller can
313 * test the resulting state of the operation object. */
314 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100315 {
Ronald Cron5425a212020-08-04 14:58:35 +0200316 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100317 }
318
Ronald Cron5425a212020-08-04 14:58:35 +0200319 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100320 return( 1 );
321
322exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200323 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100324 return( 0 );
325}
326
327int exercise_cipher_setup( psa_key_type_t key_type,
328 const unsigned char *key_bytes,
329 size_t key_length,
330 psa_algorithm_t alg,
331 psa_cipher_operation_t *operation,
332 psa_status_t *status )
333{
Ronald Cron5425a212020-08-04 14:58:35 +0200334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100336
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200337 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
338 psa_set_key_algorithm( &attributes, alg );
339 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200340 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100341
Ronald Cron5425a212020-08-04 14:58:35 +0200342 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100343 /* Whether setup succeeded or failed, abort must succeed. */
344 PSA_ASSERT( psa_cipher_abort( operation ) );
345 /* If setup failed, reproduce the failure, so that the caller can
346 * test the resulting state of the operation object. */
347 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100348 {
Ronald Cron5425a212020-08-04 14:58:35 +0200349 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100350 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100351 }
352
Ronald Cron5425a212020-08-04 14:58:35 +0200353 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100354 return( 1 );
355
356exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200357 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100358 return( 0 );
359}
360
Ronald Cron5425a212020-08-04 14:58:35 +0200361static int exercise_mac_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200362 psa_key_usage_t usage,
363 psa_algorithm_t alg )
364{
Jaeden Amero769ce272019-01-04 11:48:03 +0000365 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200366 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200367 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200368 size_t mac_length = sizeof( mac );
369
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100370 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200371 {
Ronald Cron5425a212020-08-04 14:58:35 +0200372 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100373 PSA_ASSERT( psa_mac_update( &operation,
374 input, sizeof( input ) ) );
375 PSA_ASSERT( psa_mac_sign_finish( &operation,
376 mac, sizeof( mac ),
377 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200378 }
379
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100380 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200381 {
382 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100383 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200384 PSA_SUCCESS :
385 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200386 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100387 PSA_ASSERT( psa_mac_update( &operation,
388 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100389 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
390 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200391 }
392
393 return( 1 );
394
395exit:
396 psa_mac_abort( &operation );
397 return( 0 );
398}
399
Ronald Cron5425a212020-08-04 14:58:35 +0200400static int exercise_cipher_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200401 psa_key_usage_t usage,
402 psa_algorithm_t alg )
403{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000404 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200405 unsigned char iv[16] = {0};
406 size_t iv_length = sizeof( iv );
407 const unsigned char plaintext[16] = "Hello, world...";
408 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
409 size_t ciphertext_length = sizeof( ciphertext );
410 unsigned char decrypted[sizeof( ciphertext )];
411 size_t part_length;
412
413 if( usage & PSA_KEY_USAGE_ENCRYPT )
414 {
Ronald Cron5425a212020-08-04 14:58:35 +0200415 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100416 PSA_ASSERT( psa_cipher_generate_iv( &operation,
417 iv, sizeof( iv ),
418 &iv_length ) );
419 PSA_ASSERT( psa_cipher_update( &operation,
420 plaintext, sizeof( plaintext ),
421 ciphertext, sizeof( ciphertext ),
422 &ciphertext_length ) );
423 PSA_ASSERT( psa_cipher_finish( &operation,
424 ciphertext + ciphertext_length,
425 sizeof( ciphertext ) - ciphertext_length,
426 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200427 ciphertext_length += part_length;
428 }
429
430 if( usage & PSA_KEY_USAGE_DECRYPT )
431 {
432 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200433 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200434 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
435 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200437 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200438 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
439 * have this macro yet. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100440 iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200441 psa_get_key_type( &attributes ) );
442 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100443 psa_reset_key_attributes( &attributes );
Gilles Peskine818ca122018-06-20 18:16:48 +0200444 }
Ronald Cron5425a212020-08-04 14:58:35 +0200445 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100446 PSA_ASSERT( psa_cipher_set_iv( &operation,
447 iv, iv_length ) );
448 PSA_ASSERT( psa_cipher_update( &operation,
449 ciphertext, ciphertext_length,
450 decrypted, sizeof( decrypted ),
451 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200452 status = psa_cipher_finish( &operation,
453 decrypted + part_length,
454 sizeof( decrypted ) - part_length,
455 &part_length );
456 /* For a stream cipher, all inputs are valid. For a block cipher,
457 * if the input is some aribtrary data rather than an actual
458 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200459 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200460 TEST_ASSERT( status == PSA_SUCCESS ||
461 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200462 else
463 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200464 }
465
466 return( 1 );
467
468exit:
469 psa_cipher_abort( &operation );
470 return( 0 );
471}
472
Ronald Cron5425a212020-08-04 14:58:35 +0200473static int exercise_aead_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200474 psa_key_usage_t usage,
475 psa_algorithm_t alg )
476{
477 unsigned char nonce[16] = {0};
478 size_t nonce_length = sizeof( nonce );
479 unsigned char plaintext[16] = "Hello, world...";
480 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
481 size_t ciphertext_length = sizeof( ciphertext );
482 size_t plaintext_length = sizeof( ciphertext );
483
Steven Cooreman2f099132021-01-11 20:33:45 +0100484 /* Default IV length for AES-GCM is 12 bytes */
Bence Szépkútia63b20d2020-12-16 11:36:46 +0100485 if( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ==
486 PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ) )
Steven Cooreman50f1f5e2021-01-25 10:26:49 +0100487 {
Steven Cooreman2f099132021-01-11 20:33:45 +0100488 nonce_length = 12;
Steven Cooreman50f1f5e2021-01-25 10:26:49 +0100489 }
Steven Cooreman2f099132021-01-11 20:33:45 +0100490
Gilles Peskine818ca122018-06-20 18:16:48 +0200491 if( usage & PSA_KEY_USAGE_ENCRYPT )
492 {
Ronald Cron5425a212020-08-04 14:58:35 +0200493 PSA_ASSERT( psa_aead_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +0100494 nonce, nonce_length,
495 NULL, 0,
496 plaintext, sizeof( plaintext ),
497 ciphertext, sizeof( ciphertext ),
498 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200499 }
500
501 if( usage & PSA_KEY_USAGE_DECRYPT )
502 {
503 psa_status_t verify_status =
504 ( usage & PSA_KEY_USAGE_ENCRYPT ?
505 PSA_SUCCESS :
506 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200507 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +0100508 nonce, nonce_length,
509 NULL, 0,
510 ciphertext, ciphertext_length,
511 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100512 &plaintext_length ),
513 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200514 }
515
516 return( 1 );
517
518exit:
519 return( 0 );
520}
521
Ronald Cron5425a212020-08-04 14:58:35 +0200522static int exercise_signature_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200523 psa_key_usage_t usage,
524 psa_algorithm_t alg )
525{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200526 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
527 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100528 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200529 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100530 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
531
532 /* If the policy allows signing with any hash, just pick one. */
533 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
534 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100535#if defined(KNOWN_SUPPORTED_HASH_ALG)
536 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
537 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100538#else
Chris Jones9634bb12021-01-20 15:56:42 +0000539 mbedtls_test_fail( "No hash algorithm for hash-and-sign testing",
540 __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100541 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100542#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100543 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200544
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100545 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200546 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200547 /* Some algorithms require the payload to have the size of
548 * the hash encoded in the algorithm. Use this input size
549 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200550 if( hash_alg != 0 )
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100551 payload_length = PSA_HASH_LENGTH( hash_alg );
Ronald Cron5425a212020-08-04 14:58:35 +0200552 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100553 payload, payload_length,
554 signature, sizeof( signature ),
555 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200556 }
557
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100558 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200559 {
560 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100561 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200562 PSA_SUCCESS :
563 PSA_ERROR_INVALID_SIGNATURE );
Ronald Cron5425a212020-08-04 14:58:35 +0200564 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100565 payload, payload_length,
566 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100567 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200568 }
569
570 return( 1 );
571
572exit:
573 return( 0 );
574}
575
Ronald Cron5425a212020-08-04 14:58:35 +0200576static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
Gilles Peskine818ca122018-06-20 18:16:48 +0200577 psa_key_usage_t usage,
578 psa_algorithm_t alg )
579{
580 unsigned char plaintext[256] = "Hello, world...";
581 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
582 size_t ciphertext_length = sizeof( ciphertext );
583 size_t plaintext_length = 16;
584
585 if( usage & PSA_KEY_USAGE_ENCRYPT )
586 {
Ronald Cron5425a212020-08-04 14:58:35 +0200587 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100588 plaintext, plaintext_length,
589 NULL, 0,
590 ciphertext, sizeof( ciphertext ),
591 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200592 }
593
594 if( usage & PSA_KEY_USAGE_DECRYPT )
595 {
596 psa_status_t status =
Ronald Cron5425a212020-08-04 14:58:35 +0200597 psa_asymmetric_decrypt( key, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200598 ciphertext, ciphertext_length,
599 NULL, 0,
600 plaintext, sizeof( plaintext ),
601 &plaintext_length );
602 TEST_ASSERT( status == PSA_SUCCESS ||
603 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
604 ( status == PSA_ERROR_INVALID_ARGUMENT ||
605 status == PSA_ERROR_INVALID_PADDING ) ) );
606 }
607
608 return( 1 );
609
610exit:
611 return( 0 );
612}
Gilles Peskine02b75072018-07-01 22:31:34 +0200613
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100614int mbedtls_test_psa_setup_key_derivation_wrap(
615 psa_key_derivation_operation_t* operation,
616 mbedtls_svc_key_id_t key,
617 psa_algorithm_t alg,
618 unsigned char* input1, size_t input1_length,
619 unsigned char* input2, size_t input2_length,
620 size_t capacity )
Janos Follathf2815ea2019-07-03 12:41:36 +0100621{
622 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
623 if( PSA_ALG_IS_HKDF( alg ) )
624 {
625 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
626 PSA_KEY_DERIVATION_INPUT_SALT,
627 input1, input1_length ) );
628 PSA_ASSERT( psa_key_derivation_input_key( operation,
629 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200630 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100631 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
632 PSA_KEY_DERIVATION_INPUT_INFO,
633 input2,
634 input2_length ) );
635 }
636 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
637 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
638 {
639 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
640 PSA_KEY_DERIVATION_INPUT_SEED,
641 input1, input1_length ) );
642 PSA_ASSERT( psa_key_derivation_input_key( operation,
643 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +0200644 key ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100645 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
646 PSA_KEY_DERIVATION_INPUT_LABEL,
647 input2, input2_length ) );
648 }
649 else
650 {
651 TEST_ASSERT( ! "Key derivation algorithm not supported" );
652 }
653
Gilles Peskinec744d992019-07-30 17:26:54 +0200654 if( capacity != SIZE_MAX )
655 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100656
657 return( 1 );
658
659exit:
660 return( 0 );
661}
662
663
Ronald Cron5425a212020-08-04 14:58:35 +0200664static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200665 psa_key_usage_t usage,
666 psa_algorithm_t alg )
667{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200668 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100669 unsigned char input1[] = "Input 1";
670 size_t input1_length = sizeof( input1 );
671 unsigned char input2[] = "Input 2";
672 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200673 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100674 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200675
676 if( usage & PSA_KEY_USAGE_DERIVE )
677 {
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100678 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
679 input1, input1_length,
680 input2, input2_length,
681 capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +0100682 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200683
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200684 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200685 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100686 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200687 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200688 }
689
690 return( 1 );
691
692exit:
693 return( 0 );
694}
695
Gilles Peskinec7998b72018-11-07 18:45:02 +0100696/* We need two keys to exercise key agreement. Exercise the
697 * private key against its own public key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100698psa_status_t mbedtls_test_psa_key_agreement_with_self(
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200699 psa_key_derivation_operation_t *operation,
Ronald Cron5425a212020-08-04 14:58:35 +0200700 mbedtls_svc_key_id_t key )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100701{
702 psa_key_type_t private_key_type;
703 psa_key_type_t public_key_type;
704 size_t key_bits;
705 uint8_t *public_key = NULL;
706 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200707 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200708 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
709 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200710 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200711 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100712
Ronald Cron5425a212020-08-04 14:58:35 +0200713 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200714 private_key_type = psa_get_key_type( &attributes );
715 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200716 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100717 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100718 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200719 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
Gilles Peskine8817f612018-12-18 00:18:46 +0100720 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100721
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200722 status = psa_key_derivation_key_agreement(
Ronald Cron5425a212020-08-04 14:58:35 +0200723 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200724 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100725exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100726 /*
727 * Key attributes may have been returned by psa_get_key_attributes()
728 * thus reset them as required.
729 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200730 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100731
732 mbedtls_free( public_key );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100733 return( status );
734}
735
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200736/* We need two keys to exercise key agreement. Exercise the
737 * private key against its own public key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100738psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
739 psa_algorithm_t alg,
740 mbedtls_svc_key_id_t key )
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200741{
742 psa_key_type_t private_key_type;
743 psa_key_type_t public_key_type;
744 size_t key_bits;
745 uint8_t *public_key = NULL;
746 size_t public_key_length;
747 uint8_t output[1024];
748 size_t output_length;
749 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200750 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
751 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200752 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200754
Ronald Cron5425a212020-08-04 14:58:35 +0200755 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200756 private_key_type = psa_get_key_type( &attributes );
757 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200758 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100759 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200760 ASSERT_ALLOC( public_key, public_key_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200761 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200762 public_key, public_key_length,
763 &public_key_length ) );
764
Ronald Cron5425a212020-08-04 14:58:35 +0200765 status = psa_raw_key_agreement( alg, key,
Gilles Peskinebe697d82019-05-16 18:00:41 +0200766 public_key, public_key_length,
767 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200768exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100769 /*
770 * Key attributes may have been returned by psa_get_key_attributes()
771 * thus reset them as required.
772 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200773 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100774
775 mbedtls_free( public_key );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200776 return( status );
777}
778
Ronald Cron5425a212020-08-04 14:58:35 +0200779static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200780 psa_key_usage_t usage,
781 psa_algorithm_t alg )
782{
783 int ok = 0;
784
785 if( usage & PSA_KEY_USAGE_DERIVE )
786 {
787 /* We need two keys to exercise key agreement. Exercise the
788 * private key against its own public key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100789 PSA_ASSERT( mbedtls_test_psa_raw_key_agreement_with_self( alg, key ) );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200790 }
791 ok = 1;
792
793exit:
794 return( ok );
795}
796
Ronald Cron5425a212020-08-04 14:58:35 +0200797static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200798 psa_key_usage_t usage,
799 psa_algorithm_t alg )
800{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200801 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200802 unsigned char output[1];
803 int ok = 0;
804
805 if( usage & PSA_KEY_USAGE_DERIVE )
806 {
807 /* We need two keys to exercise key agreement. Exercise the
808 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200809 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100810 PSA_ASSERT( mbedtls_test_psa_key_agreement_with_self( &operation, key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200811 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200812 output,
813 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200814 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200815 }
816 ok = 1;
817
818exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200819 return( ok );
820}
821
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100822int mbedtls_test_psa_exported_key_sanity_check(
823 psa_key_type_t type, size_t bits,
824 uint8_t *exported, size_t exported_length )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200825{
826 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100827 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200828 else
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100829 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200830
831#if defined(MBEDTLS_DES_C)
832 if( type == PSA_KEY_TYPE_DES )
833 {
834 /* Check the parity bits. */
835 unsigned i;
836 for( i = 0; i < bits / 8; i++ )
837 {
838 unsigned bit_count = 0;
839 unsigned m;
840 for( m = 1; m <= 0x100; m <<= 1 )
841 {
842 if( exported[i] & m )
843 ++bit_count;
844 }
845 TEST_ASSERT( bit_count % 2 != 0 );
846 }
847 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200848 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200849#endif
850
851#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200852 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200853 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200854 uint8_t *p = exported;
855 uint8_t *end = exported + exported_length;
856 size_t len;
857 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200858 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200859 * modulus INTEGER, -- n
860 * publicExponent INTEGER, -- e
861 * privateExponent INTEGER, -- d
862 * prime1 INTEGER, -- p
863 * prime2 INTEGER, -- q
864 * exponent1 INTEGER, -- d mod (p-1)
865 * exponent2 INTEGER, -- d mod (q-1)
866 * coefficient INTEGER, -- (inverse of q) mod p
867 * }
868 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100869 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
870 MBEDTLS_ASN1_SEQUENCE |
871 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
872 TEST_EQUAL( p + len, end );
Gilles Peskine8e94efe2021-02-13 00:25:53 +0100873 if( ! mbedtls_test_asn1_skip_integer( &p, end, 0, 0, 0 ) )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200874 goto exit;
Gilles Peskine8e94efe2021-02-13 00:25:53 +0100875 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200876 goto exit;
Gilles Peskine8e94efe2021-02-13 00:25:53 +0100877 if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200878 goto exit;
879 /* Require d to be at least half the size of n. */
Gilles Peskine8e94efe2021-02-13 00:25:53 +0100880 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200881 goto exit;
882 /* Require p and q to be at most half the size of n, rounded up. */
Gilles Peskine8e94efe2021-02-13 00:25:53 +0100883 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200884 goto exit;
Gilles Peskine8e94efe2021-02-13 00:25:53 +0100885 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200886 goto exit;
Gilles Peskine8e94efe2021-02-13 00:25:53 +0100887 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200888 goto exit;
Gilles Peskine8e94efe2021-02-13 00:25:53 +0100889 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200890 goto exit;
Gilles Peskine8e94efe2021-02-13 00:25:53 +0100891 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200892 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100893 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100894 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200895 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200896#endif /* MBEDTLS_RSA_C */
897
898#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200899 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200900 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100901 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100902 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100903 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200904 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200905#endif /* MBEDTLS_ECP_C */
906
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200907 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
908 {
909 uint8_t *p = exported;
910 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200911#if defined(MBEDTLS_RSA_C)
912 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
913 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100914 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200915 /* RSAPublicKey ::= SEQUENCE {
916 * modulus INTEGER, -- n
917 * publicExponent INTEGER } -- e
918 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100919 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
920 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100921 MBEDTLS_ASN1_CONSTRUCTED ),
922 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100923 TEST_EQUAL( p + len, end );
Gilles Peskine8e94efe2021-02-13 00:25:53 +0100924 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200925 goto exit;
Gilles Peskine8e94efe2021-02-13 00:25:53 +0100926 if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200927 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100928 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200929 }
930 else
931#endif /* MBEDTLS_RSA_C */
932#if defined(MBEDTLS_ECP_C)
933 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
934 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200935 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
936 {
937 /* The representation of an ECC Montgomery public key is
938 * the raw compressed point */
939 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
940 }
941 else
942 {
943 /* The representation of an ECC Weierstrass public key is:
944 * - The byte 0x04;
945 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
946 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
947 * - where m is the bit size associated with the curve.
948 */
949 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
950 TEST_EQUAL( p[0], 4 );
951 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200952 }
953 else
954#endif /* MBEDTLS_ECP_C */
955 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100956 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200957 mbedtls_snprintf( message, sizeof( message ),
958 "No sanity check for public key type=0x%08lx",
959 (unsigned long) type );
Chris Jones9634bb12021-01-20 15:56:42 +0000960 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200961 (void) p;
962 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200963 return( 0 );
964 }
965 }
966 else
967
968 {
969 /* No sanity checks for other types */
970 }
971
972 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200973
974exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200975 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200976}
977
Ronald Cron5425a212020-08-04 14:58:35 +0200978static int exercise_export_key( mbedtls_svc_key_id_t key,
Gilles Peskined14664a2018-08-10 19:07:32 +0200979 psa_key_usage_t usage )
980{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200981 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200982 uint8_t *exported = NULL;
983 size_t exported_size = 0;
984 size_t exported_length = 0;
985 int ok = 0;
986
Ronald Cron5425a212020-08-04 14:58:35 +0200987 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200988
Ronald Cron1e87d5b2021-01-18 13:32:28 +0100989 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
990 psa_get_key_type( &attributes ),
991 psa_get_key_bits( &attributes ) );
992 ASSERT_ALLOC( exported, exported_size );
993
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200994 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200995 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200996 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +0100997 TEST_EQUAL( psa_export_key( key, exported,
998 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100999 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001000 ok = 1;
1001 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001002 }
1003
Ronald Cron5425a212020-08-04 14:58:35 +02001004 PSA_ASSERT( psa_export_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001005 exported, exported_size,
1006 &exported_length ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001007 ok = mbedtls_test_psa_exported_key_sanity_check(
1008 psa_get_key_type( &attributes ), psa_get_key_bits( &attributes ),
1009 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001010
1011exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001012 /*
1013 * Key attributes may have been returned by psa_get_key_attributes()
1014 * thus reset them as required.
1015 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001016 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001017
1018 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001019 return( ok );
1020}
1021
Ronald Cron5425a212020-08-04 14:58:35 +02001022static int exercise_export_public_key( mbedtls_svc_key_id_t key )
Gilles Peskined14664a2018-08-10 19:07:32 +02001023{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001025 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001026 uint8_t *exported = NULL;
1027 size_t exported_size = 0;
1028 size_t exported_length = 0;
1029 int ok = 0;
1030
Ronald Cron5425a212020-08-04 14:58:35 +02001031 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001032 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001033 {
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001034 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
1035 psa_get_key_type( &attributes ),
1036 psa_get_key_bits( &attributes ) );
1037 ASSERT_ALLOC( exported, exported_size );
1038
1039 TEST_EQUAL( psa_export_public_key( key, exported,
1040 exported_size, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001041 PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron1e87d5b2021-01-18 13:32:28 +01001042 ok = 1;
1043 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001044 }
1045
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001046 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001047 psa_get_key_type( &attributes ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001048 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
1049 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001050 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001051
Ronald Cron5425a212020-08-04 14:58:35 +02001052 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskine8817f612018-12-18 00:18:46 +01001053 exported, exported_size,
1054 &exported_length ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001055 ok = mbedtls_test_psa_exported_key_sanity_check(
1056 public_type, psa_get_key_bits( &attributes ),
1057 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001058
1059exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001060 /*
1061 * Key attributes may have been returned by psa_get_key_attributes()
1062 * thus reset them as required.
1063 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001064 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001065
1066 mbedtls_free( exported );
Gilles Peskined14664a2018-08-10 19:07:32 +02001067 return( ok );
1068}
1069
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001070/** Do smoke tests on a key.
1071 *
1072 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1073 * sign/verify, or derivation) that is permitted according to \p usage.
1074 * \p usage and \p alg should correspond to the expected policy on the
1075 * key.
1076 *
1077 * Export the key if permitted by \p usage, and check that the output
1078 * looks sensible. If \p usage forbids export, check that
1079 * \p psa_export_key correctly rejects the attempt. If the key is
1080 * asymmetric, also check \p psa_export_public_key.
1081 *
1082 * If the key fails the tests, this function calls the test framework's
Chris Jones9634bb12021-01-20 15:56:42 +00001083 * `mbedtls_test_fail` function and returns false. Otherwise this function
1084 * returns true. Therefore it should be used as follows:
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001085 * ```
1086 * if( ! exercise_key( ... ) ) goto exit;
1087 * ```
1088 *
Ronald Cron5425a212020-08-04 14:58:35 +02001089 * \param key The key to exercise. It should be capable of performing
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001090 * \p alg.
1091 * \param usage The usage flags to assume.
1092 * \param alg The algorithm to exercise.
1093 *
1094 * \retval 0 The key failed the smoke tests.
1095 * \retval 1 The key passed the smoke tests.
1096 */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001097int mbedtls_test_psa_exercise_key( mbedtls_svc_key_id_t key,
1098 psa_key_usage_t usage,
1099 psa_algorithm_t alg )
Gilles Peskine02b75072018-07-01 22:31:34 +02001100{
1101 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001102
Ronald Cron5425a212020-08-04 14:58:35 +02001103 if( ! check_key_attributes_sanity( key ) )
Gilles Peskine667c1112019-12-03 19:03:20 +01001104 return( 0 );
1105
Gilles Peskine02b75072018-07-01 22:31:34 +02001106 if( alg == 0 )
1107 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1108 else if( PSA_ALG_IS_MAC( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001109 ok = exercise_mac_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001110 else if( PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001111 ok = exercise_cipher_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001112 else if( PSA_ALG_IS_AEAD( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001113 ok = exercise_aead_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001114 else if( PSA_ALG_IS_SIGN( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001115 ok = exercise_signature_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001116 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001117 ok = exercise_asymmetric_encryption_key( key, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001118 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001119 ok = exercise_key_derivation_key( key, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001120 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001121 ok = exercise_raw_key_agreement_key( key, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001122 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Ronald Cron5425a212020-08-04 14:58:35 +02001123 ok = exercise_key_agreement_key( key, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001124 else
1125 {
1126 char message[40];
1127 mbedtls_snprintf( message, sizeof( message ),
1128 "No code to exercise alg=0x%08lx",
1129 (unsigned long) alg );
Chris Jones9634bb12021-01-20 15:56:42 +00001130 mbedtls_test_fail( message, __LINE__, __FILE__ );
Gilles Peskine02b75072018-07-01 22:31:34 +02001131 ok = 0;
1132 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001133
Ronald Cron5425a212020-08-04 14:58:35 +02001134 ok = ok && exercise_export_key( key, usage );
1135 ok = ok && exercise_export_public_key( key );
Gilles Peskined14664a2018-08-10 19:07:32 +02001136
Gilles Peskine02b75072018-07-01 22:31:34 +02001137 return( ok );
1138}
1139
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001140psa_key_usage_t mbedtls_test_psa_usage_to_exercise( psa_key_type_t type,
1141 psa_algorithm_t alg )
Gilles Peskine10df3412018-10-25 22:35:43 +02001142{
1143 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1144 {
1145 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001146 PSA_KEY_USAGE_VERIFY_HASH :
1147 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001148 }
1149 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1150 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1151 {
1152 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1153 PSA_KEY_USAGE_ENCRYPT :
1154 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1155 }
1156 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1157 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1158 {
1159 return( PSA_KEY_USAGE_DERIVE );
1160 }
1161 else
1162 {
1163 return( 0 );
1164 }
1165
1166}
Darryl Green0c6575a2018-11-07 16:05:30 +00001167
Ronald Cron5425a212020-08-04 14:58:35 +02001168static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001169{
1170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001171 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001172 uint8_t buffer[1];
1173 size_t length;
1174 int ok = 0;
1175
Ronald Cronecfb2372020-07-23 17:13:42 +02001176 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001177 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1178 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1179 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +02001180 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001181 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cronecfb2372020-07-23 17:13:42 +02001182 TEST_EQUAL(
1183 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1184 TEST_EQUAL(
1185 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001186 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001187 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1188 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1189 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1190 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1191
Ronald Cron5425a212020-08-04 14:58:35 +02001192 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001193 PSA_ERROR_DOES_NOT_EXIST );
Ronald Cron5425a212020-08-04 14:58:35 +02001194 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001195 buffer, sizeof( buffer ), &length ),
Ronald Cron432e19c2020-09-17 14:12:30 +02001196 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001197
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001198 ok = 1;
1199
1200exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001201 /*
1202 * Key attributes may have been returned by psa_get_key_attributes()
1203 * thus reset them as required.
1204 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001205 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001206
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001207 return( ok );
1208}
1209
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001210/* Assert that a key isn't reported as having a slot number. */
1211#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1212#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1213 do \
1214 { \
1215 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1216 TEST_EQUAL( psa_get_key_slot_number( \
1217 attributes, \
1218 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1219 PSA_ERROR_INVALID_ARGUMENT ); \
1220 } \
1221 while( 0 )
1222#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1223#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1224 ( (void) 0 )
1225#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1226
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001227/* An overapproximation of the amount of storage needed for a key of the
1228 * given type and with the given content. The API doesn't make it easy
1229 * to find a good value for the size. The current implementation doesn't
1230 * care about the value anyway. */
1231#define KEY_BITS_FROM_DATA( type, data ) \
1232 ( data )->len
1233
Darryl Green0c6575a2018-11-07 16:05:30 +00001234typedef enum {
1235 IMPORT_KEY = 0,
1236 GENERATE_KEY = 1,
1237 DERIVE_KEY = 2
1238} generate_method;
1239
Gilles Peskinee59236f2018-01-27 23:32:46 +01001240/* END_HEADER */
1241
1242/* BEGIN_DEPENDENCIES
1243 * depends_on:MBEDTLS_PSA_CRYPTO_C
1244 * END_DEPENDENCIES
1245 */
1246
1247/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001248void static_checks( )
1249{
1250 size_t max_truncated_mac_size =
1251 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1252
1253 /* Check that the length for a truncated MAC always fits in the algorithm
1254 * encoding. The shifted mask is the maximum truncated value. The
1255 * untruncated algorithm may be one byte larger. */
1256 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001257
1258#if defined(MBEDTLS_TEST_DEPRECATED)
1259 /* Check deprecated constants. */
1260 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1261 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1262 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1263 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1264 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1265 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1266 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1267 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001268
Paul Elliott8ff510a2020-06-02 17:19:28 +01001269 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1270 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1271 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1272 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1273 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1274 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1275 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1276 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1277 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1278 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1279 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1280 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1281 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1282 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1283 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1284 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1285 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1286 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1287 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1288 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1289 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1290 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1291 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1292 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1293 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1294 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1295 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1296 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1297 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1298 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1299
1300 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1301 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1302 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1303 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1304 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1305 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1306 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001308
Paul Elliott75e27032020-06-03 15:17:39 +01001309 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1310 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1311 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1312 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1313 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1314
1315 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1316 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001317#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001318}
1319/* END_CASE */
1320
1321/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001322void import_with_policy( int type_arg,
1323 int usage_arg, int alg_arg,
1324 int expected_status_arg )
1325{
1326 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1327 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001328 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001329 psa_key_type_t type = type_arg;
1330 psa_key_usage_t usage = usage_arg;
1331 psa_algorithm_t alg = alg_arg;
1332 psa_status_t expected_status = expected_status_arg;
1333 const uint8_t key_material[16] = {0};
1334 psa_status_t status;
1335
1336 PSA_ASSERT( psa_crypto_init( ) );
1337
1338 psa_set_key_type( &attributes, type );
1339 psa_set_key_usage_flags( &attributes, usage );
1340 psa_set_key_algorithm( &attributes, alg );
1341
1342 status = psa_import_key( &attributes,
1343 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001344 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001345 TEST_EQUAL( status, expected_status );
1346 if( status != PSA_SUCCESS )
1347 goto exit;
1348
Ronald Cron5425a212020-08-04 14:58:35 +02001349 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001350 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1351 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1352 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001353 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001354
Ronald Cron5425a212020-08-04 14:58:35 +02001355 PSA_ASSERT( psa_destroy_key( key ) );
1356 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001357
1358exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001359 /*
1360 * Key attributes may have been returned by psa_get_key_attributes()
1361 * thus reset them as required.
1362 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001363 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001364
1365 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001366 PSA_DONE( );
1367}
1368/* END_CASE */
1369
1370/* BEGIN_CASE */
1371void import_with_data( data_t *data, int type_arg,
1372 int attr_bits_arg,
1373 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001374{
1375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1376 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001377 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001378 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001379 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001380 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001381 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001382
Gilles Peskine8817f612018-12-18 00:18:46 +01001383 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001384
Gilles Peskine4747d192019-04-17 15:05:45 +02001385 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001386 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001387
Ronald Cron5425a212020-08-04 14:58:35 +02001388 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001389 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001390 if( status != PSA_SUCCESS )
1391 goto exit;
1392
Ronald Cron5425a212020-08-04 14:58:35 +02001393 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001394 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001395 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001396 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001397 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001398
Ronald Cron5425a212020-08-04 14:58:35 +02001399 PSA_ASSERT( psa_destroy_key( key ) );
1400 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001401
1402exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001403 /*
1404 * Key attributes may have been returned by psa_get_key_attributes()
1405 * thus reset them as required.
1406 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001407 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001408
1409 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001410 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001411}
1412/* END_CASE */
1413
1414/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001415void import_large_key( int type_arg, int byte_size_arg,
1416 int expected_status_arg )
1417{
1418 psa_key_type_t type = type_arg;
1419 size_t byte_size = byte_size_arg;
1420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1421 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001422 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001423 psa_status_t status;
1424 uint8_t *buffer = NULL;
1425 size_t buffer_size = byte_size + 1;
1426 size_t n;
1427
Steven Cooreman69967ce2021-01-18 18:01:08 +01001428 /* Skip the test case if the target running the test cannot
1429 * accomodate large keys due to heap size constraints */
1430 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001431 memset( buffer, 'K', byte_size );
1432
1433 PSA_ASSERT( psa_crypto_init( ) );
1434
1435 /* Try importing the key */
1436 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1437 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001438 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001439 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001440 TEST_EQUAL( status, expected_status );
1441
1442 if( status == PSA_SUCCESS )
1443 {
Ronald Cron5425a212020-08-04 14:58:35 +02001444 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001445 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1446 TEST_EQUAL( psa_get_key_bits( &attributes ),
1447 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001448 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001449 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001450 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001451 for( n = 0; n < byte_size; n++ )
1452 TEST_EQUAL( buffer[n], 'K' );
1453 for( n = byte_size; n < buffer_size; n++ )
1454 TEST_EQUAL( buffer[n], 0 );
1455 }
1456
1457exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001458 /*
1459 * Key attributes may have been returned by psa_get_key_attributes()
1460 * thus reset them as required.
1461 */
1462 psa_reset_key_attributes( &attributes );
1463
Ronald Cron5425a212020-08-04 14:58:35 +02001464 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001465 PSA_DONE( );
1466 mbedtls_free( buffer );
1467}
1468/* END_CASE */
1469
1470/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001471void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1472{
Ronald Cron5425a212020-08-04 14:58:35 +02001473 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001474 size_t bits = bits_arg;
1475 psa_status_t expected_status = expected_status_arg;
1476 psa_status_t status;
1477 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001478 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001479 size_t buffer_size = /* Slight overapproximations */
1480 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001481 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001482 unsigned char *p;
1483 int ret;
1484 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001486
Gilles Peskine8817f612018-12-18 00:18:46 +01001487 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001488 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001489
1490 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1491 bits, keypair ) ) >= 0 );
1492 length = ret;
1493
1494 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001495 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001496 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001497 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001498
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001499 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001500 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001501
1502exit:
1503 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001504 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001505}
1506/* END_CASE */
1507
1508/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001509void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001510 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001511 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001512 int expected_bits,
1513 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001514 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001515 int canonical_input )
1516{
Ronald Cron5425a212020-08-04 14:58:35 +02001517 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001518 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001519 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001520 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001521 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001522 unsigned char *exported = NULL;
1523 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001525 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001526 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001528 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001529
Moran Pekercb088e72018-07-17 17:36:59 +03001530 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001531 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001532 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001533 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001534 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001535
Gilles Peskine4747d192019-04-17 15:05:45 +02001536 psa_set_key_usage_flags( &attributes, usage_arg );
1537 psa_set_key_algorithm( &attributes, alg );
1538 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001539
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001541 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001542
1543 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001544 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001545 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1546 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001547 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001548
1549 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001550 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001551 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001552
1553 /* The exported length must be set by psa_export_key() to a value between 0
1554 * and export_size. On errors, the exported length must be 0. */
1555 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1556 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1557 TEST_ASSERT( exported_length <= export_size );
1558
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001559 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001560 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001561 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001562 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001563 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001565 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001566
Gilles Peskineea38a922021-02-13 00:05:16 +01001567 /* Run sanity checks on the exported key. For non-canonical inputs,
1568 * this validates the canonical representations. For canonical inputs,
1569 * this doesn't directly validate the implementation, but it still helps
1570 * by cross-validating the test data with the sanity check code. */
1571 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001572 goto exit;
1573
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001574 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001575 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001576 else
1577 {
Ronald Cron5425a212020-08-04 14:58:35 +02001578 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001579 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001580 &key2 ) );
1581 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001582 reexported,
1583 export_size,
1584 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001585 ASSERT_COMPARE( exported, exported_length,
1586 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001587 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001588 }
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001589 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001590
1591destroy:
1592 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001593 PSA_ASSERT( psa_destroy_key( key ) );
1594 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001595
1596exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001597 /*
1598 * Key attributes may have been returned by psa_get_key_attributes()
1599 * thus reset them as required.
1600 */
1601 psa_reset_key_attributes( &got_attributes );
1602
itayzafrir3e02b3b2018-06-12 17:06:52 +03001603 mbedtls_free( exported );
1604 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001605 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001606}
1607/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001608
Moran Pekerf709f4a2018-06-06 17:26:04 +03001609/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001610void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001611 int type_arg,
1612 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001613 int export_size_delta,
1614 int expected_export_status_arg,
1615 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001616{
Ronald Cron5425a212020-08-04 14:58:35 +02001617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001618 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001619 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001620 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001621 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001622 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001623 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001624 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001626
Gilles Peskine8817f612018-12-18 00:18:46 +01001627 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001628
Gilles Peskine4747d192019-04-17 15:05:45 +02001629 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1630 psa_set_key_algorithm( &attributes, alg );
1631 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001632
1633 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001634 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001635
Gilles Peskine49c25912018-10-29 15:15:31 +01001636 /* Export the public key */
1637 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001638 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001639 exported, export_size,
1640 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001641 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001642 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001643 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001644 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001645 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001646 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001647 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001648 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001649 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001650 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1651 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001652 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001653
1654exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001655 /*
1656 * Key attributes may have been returned by psa_get_key_attributes()
1657 * thus reset them as required.
1658 */
1659 psa_reset_key_attributes( &attributes );
1660
itayzafrir3e02b3b2018-06-12 17:06:52 +03001661 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001662 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001663 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001664}
1665/* END_CASE */
1666
Gilles Peskine20035e32018-02-03 22:44:14 +01001667/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001668void import_and_exercise_key( data_t *data,
1669 int type_arg,
1670 int bits_arg,
1671 int alg_arg )
1672{
Ronald Cron5425a212020-08-04 14:58:35 +02001673 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001674 psa_key_type_t type = type_arg;
1675 size_t bits = bits_arg;
1676 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001677 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001679 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001680
Gilles Peskine8817f612018-12-18 00:18:46 +01001681 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001682
Gilles Peskine4747d192019-04-17 15:05:45 +02001683 psa_set_key_usage_flags( &attributes, usage );
1684 psa_set_key_algorithm( &attributes, alg );
1685 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001686
1687 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001688 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001689
1690 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001691 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001692 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1693 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001694
1695 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001696 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001697 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001698
Ronald Cron5425a212020-08-04 14:58:35 +02001699 PSA_ASSERT( psa_destroy_key( key ) );
1700 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001701
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001702exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001703 /*
1704 * Key attributes may have been returned by psa_get_key_attributes()
1705 * thus reset them as required.
1706 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001707 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001708
1709 psa_reset_key_attributes( &attributes );
1710 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001711 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001712}
1713/* END_CASE */
1714
1715/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001716void effective_key_attributes( int type_arg, int expected_type_arg,
1717 int bits_arg, int expected_bits_arg,
1718 int usage_arg, int expected_usage_arg,
1719 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001720{
Ronald Cron5425a212020-08-04 14:58:35 +02001721 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001722 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001723 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001724 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001725 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001726 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001727 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001728 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001729 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001730 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001731
Gilles Peskine8817f612018-12-18 00:18:46 +01001732 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001733
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001734 psa_set_key_usage_flags( &attributes, usage );
1735 psa_set_key_algorithm( &attributes, alg );
1736 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001737 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001738
Ronald Cron5425a212020-08-04 14:58:35 +02001739 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001740 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001741
Ronald Cron5425a212020-08-04 14:58:35 +02001742 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001743 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1744 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1745 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1746 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001747
1748exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001749 /*
1750 * Key attributes may have been returned by psa_get_key_attributes()
1751 * thus reset them as required.
1752 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001753 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001754
1755 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001756 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001757}
1758/* END_CASE */
1759
1760/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001761void check_key_policy( int type_arg, int bits_arg,
1762 int usage_arg, int alg_arg )
1763{
1764 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1765 usage_arg, usage_arg, alg_arg, alg_arg );
1766 goto exit;
1767}
1768/* END_CASE */
1769
1770/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001771void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001772{
1773 /* Test each valid way of initializing the object, except for `= {0}`, as
1774 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1775 * though it's OK by the C standard. We could test for this, but we'd need
1776 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001777 psa_key_attributes_t func = psa_key_attributes_init( );
1778 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1779 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001780
1781 memset( &zero, 0, sizeof( zero ) );
1782
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001783 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1784 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1785 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001786
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001787 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1788 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1789 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1790
1791 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1792 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1793 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1794
1795 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1796 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1797 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1798
1799 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1800 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1801 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001802}
1803/* END_CASE */
1804
1805/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001806void mac_key_policy( int policy_usage,
1807 int policy_alg,
1808 int key_type,
1809 data_t *key_data,
1810 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001811{
Ronald Cron5425a212020-08-04 14:58:35 +02001812 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001813 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001814 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001815 psa_status_t status;
1816 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001817
Gilles Peskine8817f612018-12-18 00:18:46 +01001818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001819
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001820 psa_set_key_usage_flags( &attributes, policy_usage );
1821 psa_set_key_algorithm( &attributes, policy_alg );
1822 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001823
Gilles Peskine049c7532019-05-15 20:22:09 +02001824 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001825 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001826
Ronald Cron5425a212020-08-04 14:58:35 +02001827 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001828 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001829 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001830 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001831 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001832 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001834
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001835 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001836 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001837 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001838 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001839 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001840 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001841 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001842
1843exit:
1844 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001845 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001846 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001847}
1848/* END_CASE */
1849
1850/* BEGIN_CASE */
1851void cipher_key_policy( int policy_usage,
1852 int policy_alg,
1853 int key_type,
1854 data_t *key_data,
1855 int exercise_alg )
1856{
Ronald Cron5425a212020-08-04 14:58:35 +02001857 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001859 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001860 psa_status_t status;
1861
Gilles Peskine8817f612018-12-18 00:18:46 +01001862 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001863
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001864 psa_set_key_usage_flags( &attributes, policy_usage );
1865 psa_set_key_algorithm( &attributes, policy_alg );
1866 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001867
Gilles Peskine049c7532019-05-15 20:22:09 +02001868 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001869 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001870
Ronald Cron5425a212020-08-04 14:58:35 +02001871 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001872 if( policy_alg == exercise_alg &&
1873 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001874 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001875 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001876 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001877 psa_cipher_abort( &operation );
1878
Ronald Cron5425a212020-08-04 14:58:35 +02001879 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001880 if( policy_alg == exercise_alg &&
1881 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001882 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001883 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001884 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001885
1886exit:
1887 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001888 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001889 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001890}
1891/* END_CASE */
1892
1893/* BEGIN_CASE */
1894void aead_key_policy( int policy_usage,
1895 int policy_alg,
1896 int key_type,
1897 data_t *key_data,
1898 int nonce_length_arg,
1899 int tag_length_arg,
1900 int exercise_alg )
1901{
Ronald Cron5425a212020-08-04 14:58:35 +02001902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001904 psa_status_t status;
1905 unsigned char nonce[16] = {0};
1906 size_t nonce_length = nonce_length_arg;
1907 unsigned char tag[16];
1908 size_t tag_length = tag_length_arg;
1909 size_t output_length;
1910
1911 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1912 TEST_ASSERT( tag_length <= sizeof( tag ) );
1913
Gilles Peskine8817f612018-12-18 00:18:46 +01001914 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001915
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001916 psa_set_key_usage_flags( &attributes, policy_usage );
1917 psa_set_key_algorithm( &attributes, policy_alg );
1918 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001919
Gilles Peskine049c7532019-05-15 20:22:09 +02001920 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001921 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922
Ronald Cron5425a212020-08-04 14:58:35 +02001923 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924 nonce, nonce_length,
1925 NULL, 0,
1926 NULL, 0,
1927 tag, tag_length,
1928 &output_length );
1929 if( policy_alg == exercise_alg &&
1930 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001931 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001932 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001933 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934
1935 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001936 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001937 nonce, nonce_length,
1938 NULL, 0,
1939 tag, tag_length,
1940 NULL, 0,
1941 &output_length );
1942 if( policy_alg == exercise_alg &&
1943 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001944 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001945 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001946 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001947
1948exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001949 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001950 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001951}
1952/* END_CASE */
1953
1954/* BEGIN_CASE */
1955void asymmetric_encryption_key_policy( int policy_usage,
1956 int policy_alg,
1957 int key_type,
1958 data_t *key_data,
1959 int exercise_alg )
1960{
Ronald Cron5425a212020-08-04 14:58:35 +02001961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001963 psa_status_t status;
1964 size_t key_bits;
1965 size_t buffer_length;
1966 unsigned char *buffer = NULL;
1967 size_t output_length;
1968
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001970
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001971 psa_set_key_usage_flags( &attributes, policy_usage );
1972 psa_set_key_algorithm( &attributes, policy_alg );
1973 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001974
Gilles Peskine049c7532019-05-15 20:22:09 +02001975 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001976 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977
Ronald Cron5425a212020-08-04 14:58:35 +02001978 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001979 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001980 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1981 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001982 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983
Ronald Cron5425a212020-08-04 14:58:35 +02001984 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985 NULL, 0,
1986 NULL, 0,
1987 buffer, buffer_length,
1988 &output_length );
1989 if( policy_alg == exercise_alg &&
1990 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001991 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001992 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001993 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001995 if( buffer_length != 0 )
1996 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001997 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001998 buffer, buffer_length,
1999 NULL, 0,
2000 buffer, buffer_length,
2001 &output_length );
2002 if( policy_alg == exercise_alg &&
2003 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002004 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002005 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002006 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002007
2008exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002009 /*
2010 * Key attributes may have been returned by psa_get_key_attributes()
2011 * thus reset them as required.
2012 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002013 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002014
2015 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002016 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017 mbedtls_free( buffer );
2018}
2019/* END_CASE */
2020
2021/* BEGIN_CASE */
2022void asymmetric_signature_key_policy( int policy_usage,
2023 int policy_alg,
2024 int key_type,
2025 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002026 int exercise_alg,
2027 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028{
Ronald Cron5425a212020-08-04 14:58:35 +02002029 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002030 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002031 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002032 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2033 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2034 * compatible with the policy and `payload_length_arg` is supposed to be
2035 * a valid input length to sign. If `payload_length_arg <= 0`,
2036 * `exercise_alg` is supposed to be forbidden by the policy. */
2037 int compatible_alg = payload_length_arg > 0;
2038 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002039 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002040 size_t signature_length;
2041
Gilles Peskine8817f612018-12-18 00:18:46 +01002042 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002044 psa_set_key_usage_flags( &attributes, policy_usage );
2045 psa_set_key_algorithm( &attributes, policy_alg );
2046 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002047
Gilles Peskine049c7532019-05-15 20:22:09 +02002048 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002049 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050
Ronald Cron5425a212020-08-04 14:58:35 +02002051 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002052 payload, payload_length,
2053 signature, sizeof( signature ),
2054 &signature_length );
2055 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002056 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002057 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002058 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002059
2060 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002061 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002062 payload, payload_length,
2063 signature, sizeof( signature ) );
2064 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002065 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002067 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002068
2069exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002070 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002071 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002072}
2073/* END_CASE */
2074
Janos Follathba3fab92019-06-11 14:50:16 +01002075/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002076void derive_key_policy( int policy_usage,
2077 int policy_alg,
2078 int key_type,
2079 data_t *key_data,
2080 int exercise_alg )
2081{
Ronald Cron5425a212020-08-04 14:58:35 +02002082 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002084 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002085 psa_status_t status;
2086
Gilles Peskine8817f612018-12-18 00:18:46 +01002087 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002088
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002089 psa_set_key_usage_flags( &attributes, policy_usage );
2090 psa_set_key_algorithm( &attributes, policy_alg );
2091 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002092
Gilles Peskine049c7532019-05-15 20:22:09 +02002093 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002094 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002095
Janos Follathba3fab92019-06-11 14:50:16 +01002096 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2097
2098 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2099 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002100 {
Janos Follathba3fab92019-06-11 14:50:16 +01002101 PSA_ASSERT( psa_key_derivation_input_bytes(
2102 &operation,
2103 PSA_KEY_DERIVATION_INPUT_SEED,
2104 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002105 }
Janos Follathba3fab92019-06-11 14:50:16 +01002106
2107 status = psa_key_derivation_input_key( &operation,
2108 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002109 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002110
Gilles Peskineea0fb492018-07-12 17:17:20 +02002111 if( policy_alg == exercise_alg &&
2112 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002113 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002114 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002115 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002116
2117exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002118 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002119 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002120 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002121}
2122/* END_CASE */
2123
2124/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002125void agreement_key_policy( int policy_usage,
2126 int policy_alg,
2127 int key_type_arg,
2128 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002129 int exercise_alg,
2130 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002131{
Ronald Cron5425a212020-08-04 14:58:35 +02002132 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002134 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002135 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002136 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002137 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002138
Gilles Peskine8817f612018-12-18 00:18:46 +01002139 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002140
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002141 psa_set_key_usage_flags( &attributes, policy_usage );
2142 psa_set_key_algorithm( &attributes, policy_alg );
2143 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002144
Gilles Peskine049c7532019-05-15 20:22:09 +02002145 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002146 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002147
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002148 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002149 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002150
Steven Cooremance48e852020-10-05 16:02:45 +02002151 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002152
2153exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002154 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002155 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002156 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002157}
2158/* END_CASE */
2159
2160/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002161void key_policy_alg2( int key_type_arg, data_t *key_data,
2162 int usage_arg, int alg_arg, int alg2_arg )
2163{
Ronald Cron5425a212020-08-04 14:58:35 +02002164 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002165 psa_key_type_t key_type = key_type_arg;
2166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2167 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2168 psa_key_usage_t usage = usage_arg;
2169 psa_algorithm_t alg = alg_arg;
2170 psa_algorithm_t alg2 = alg2_arg;
2171
2172 PSA_ASSERT( psa_crypto_init( ) );
2173
2174 psa_set_key_usage_flags( &attributes, usage );
2175 psa_set_key_algorithm( &attributes, alg );
2176 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2177 psa_set_key_type( &attributes, key_type );
2178 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002179 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002180
Ronald Cron5425a212020-08-04 14:58:35 +02002181 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002182 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2183 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2184 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2185
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002186 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002187 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002188 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002189 goto exit;
2190
2191exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002192 /*
2193 * Key attributes may have been returned by psa_get_key_attributes()
2194 * thus reset them as required.
2195 */
2196 psa_reset_key_attributes( &got_attributes );
2197
Ronald Cron5425a212020-08-04 14:58:35 +02002198 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002199 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002200}
2201/* END_CASE */
2202
2203/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002204void raw_agreement_key_policy( int policy_usage,
2205 int policy_alg,
2206 int key_type_arg,
2207 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002208 int exercise_alg,
2209 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002210{
Ronald Cron5425a212020-08-04 14:58:35 +02002211 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002213 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002214 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002215 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002216 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002217
2218 PSA_ASSERT( psa_crypto_init( ) );
2219
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002220 psa_set_key_usage_flags( &attributes, policy_usage );
2221 psa_set_key_algorithm( &attributes, policy_alg );
2222 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002223
Gilles Peskine049c7532019-05-15 20:22:09 +02002224 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002225 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002226
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002227 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002228
Steven Cooremance48e852020-10-05 16:02:45 +02002229 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002230
2231exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002232 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002233 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002234 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002235}
2236/* END_CASE */
2237
2238/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002239void copy_success( int source_usage_arg,
2240 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002241 int type_arg, data_t *material,
2242 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002243 int target_usage_arg,
2244 int target_alg_arg, int target_alg2_arg,
2245 int expected_usage_arg,
2246 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002247{
Gilles Peskineca25db92019-04-19 11:43:08 +02002248 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2249 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002250 psa_key_usage_t expected_usage = expected_usage_arg;
2251 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002252 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002253 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2254 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002255 uint8_t *export_buffer = NULL;
2256
Gilles Peskine57ab7212019-01-28 13:03:09 +01002257 PSA_ASSERT( psa_crypto_init( ) );
2258
Gilles Peskineca25db92019-04-19 11:43:08 +02002259 /* Prepare the source key. */
2260 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2261 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002262 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002263 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002264 PSA_ASSERT( psa_import_key( &source_attributes,
2265 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002266 &source_key ) );
2267 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002268
Gilles Peskineca25db92019-04-19 11:43:08 +02002269 /* Prepare the target attributes. */
2270 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002271 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002272 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002273 /* Set volatile lifetime to reset the key identifier to 0. */
2274 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
2275 }
2276
Gilles Peskineca25db92019-04-19 11:43:08 +02002277 if( target_usage_arg != -1 )
2278 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2279 if( target_alg_arg != -1 )
2280 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002281 if( target_alg2_arg != -1 )
2282 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002283
2284 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002285 PSA_ASSERT( psa_copy_key( source_key,
2286 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002287
2288 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002289 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002290
2291 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002292 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002293 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2294 psa_get_key_type( &target_attributes ) );
2295 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2296 psa_get_key_bits( &target_attributes ) );
2297 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2298 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002299 TEST_EQUAL( expected_alg2,
2300 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002301 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2302 {
2303 size_t length;
2304 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002305 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002306 material->len, &length ) );
2307 ASSERT_COMPARE( material->x, material->len,
2308 export_buffer, length );
2309 }
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002310 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002311 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002312 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002313 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002314
Ronald Cron5425a212020-08-04 14:58:35 +02002315 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002316
2317exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002318 /*
2319 * Source and target key attributes may have been returned by
2320 * psa_get_key_attributes() thus reset them as required.
2321 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002322 psa_reset_key_attributes( &source_attributes );
2323 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002324
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002325 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002326 mbedtls_free( export_buffer );
2327}
2328/* END_CASE */
2329
2330/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002331void copy_fail( int source_usage_arg,
2332 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002333 int type_arg, data_t *material,
2334 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002335 int target_usage_arg,
2336 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002337 int expected_status_arg )
2338{
2339 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2340 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002341 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2342 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine4a644642019-05-03 17:14:08 +02002343
2344 PSA_ASSERT( psa_crypto_init( ) );
2345
2346 /* Prepare the source key. */
2347 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2348 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002349 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002350 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002351 PSA_ASSERT( psa_import_key( &source_attributes,
2352 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002353 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002354
2355 /* Prepare the target attributes. */
2356 psa_set_key_type( &target_attributes, target_type_arg );
2357 psa_set_key_bits( &target_attributes, target_bits_arg );
2358 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2359 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002360 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002361
2362 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002363 TEST_EQUAL( psa_copy_key( source_key,
2364 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002365 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002366
Ronald Cron5425a212020-08-04 14:58:35 +02002367 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002368
Gilles Peskine4a644642019-05-03 17:14:08 +02002369exit:
2370 psa_reset_key_attributes( &source_attributes );
2371 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002372 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002373}
2374/* END_CASE */
2375
2376/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002377void hash_operation_init( )
2378{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002379 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002380 /* Test each valid way of initializing the object, except for `= {0}`, as
2381 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2382 * though it's OK by the C standard. We could test for this, but we'd need
2383 * to supress the Clang warning for the test. */
2384 psa_hash_operation_t func = psa_hash_operation_init( );
2385 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2386 psa_hash_operation_t zero;
2387
2388 memset( &zero, 0, sizeof( zero ) );
2389
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002390 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002391 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2392 PSA_ERROR_BAD_STATE );
2393 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2394 PSA_ERROR_BAD_STATE );
2395 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2396 PSA_ERROR_BAD_STATE );
2397
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002398 /* A default hash operation should be abortable without error. */
2399 PSA_ASSERT( psa_hash_abort( &func ) );
2400 PSA_ASSERT( psa_hash_abort( &init ) );
2401 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002402}
2403/* END_CASE */
2404
2405/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002406void hash_setup( int alg_arg,
2407 int expected_status_arg )
2408{
2409 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002410 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002411 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002412 psa_status_t status;
2413
Gilles Peskine8817f612018-12-18 00:18:46 +01002414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002415
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002416 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002417 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002418
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002419 /* Whether setup succeeded or failed, abort must succeed. */
2420 PSA_ASSERT( psa_hash_abort( &operation ) );
2421
2422 /* If setup failed, reproduce the failure, so as to
2423 * test the resulting state of the operation object. */
2424 if( status != PSA_SUCCESS )
2425 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2426
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002427 /* Now the operation object should be reusable. */
2428#if defined(KNOWN_SUPPORTED_HASH_ALG)
2429 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2430 PSA_ASSERT( psa_hash_abort( &operation ) );
2431#endif
2432
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002433exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002434 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002435}
2436/* END_CASE */
2437
2438/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002439void hash_compute_fail( int alg_arg, data_t *input,
2440 int output_size_arg, int expected_status_arg )
2441{
2442 psa_algorithm_t alg = alg_arg;
2443 uint8_t *output = NULL;
2444 size_t output_size = output_size_arg;
2445 size_t output_length = INVALID_EXPORT_LENGTH;
2446 psa_status_t expected_status = expected_status_arg;
2447 psa_status_t status;
2448
2449 ASSERT_ALLOC( output, output_size );
2450
2451 PSA_ASSERT( psa_crypto_init( ) );
2452
2453 status = psa_hash_compute( alg, input->x, input->len,
2454 output, output_size, &output_length );
2455 TEST_EQUAL( status, expected_status );
2456 TEST_ASSERT( output_length <= output_size );
2457
2458exit:
2459 mbedtls_free( output );
2460 PSA_DONE( );
2461}
2462/* END_CASE */
2463
2464/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002465void hash_compare_fail( int alg_arg, data_t *input,
2466 data_t *reference_hash,
2467 int expected_status_arg )
2468{
2469 psa_algorithm_t alg = alg_arg;
2470 psa_status_t expected_status = expected_status_arg;
2471 psa_status_t status;
2472
2473 PSA_ASSERT( psa_crypto_init( ) );
2474
2475 status = psa_hash_compare( alg, input->x, input->len,
2476 reference_hash->x, reference_hash->len );
2477 TEST_EQUAL( status, expected_status );
2478
2479exit:
2480 PSA_DONE( );
2481}
2482/* END_CASE */
2483
2484/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002485void hash_compute_compare( int alg_arg, data_t *input,
2486 data_t *expected_output )
2487{
2488 psa_algorithm_t alg = alg_arg;
2489 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2490 size_t output_length = INVALID_EXPORT_LENGTH;
2491 size_t i;
2492
2493 PSA_ASSERT( psa_crypto_init( ) );
2494
2495 /* Compute with tight buffer */
2496 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002497 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002498 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002499 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002500 ASSERT_COMPARE( output, output_length,
2501 expected_output->x, expected_output->len );
2502
2503 /* Compute with larger buffer */
2504 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2505 output, sizeof( output ),
2506 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002507 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002508 ASSERT_COMPARE( output, output_length,
2509 expected_output->x, expected_output->len );
2510
2511 /* Compare with correct hash */
2512 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2513 output, output_length ) );
2514
2515 /* Compare with trailing garbage */
2516 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2517 output, output_length + 1 ),
2518 PSA_ERROR_INVALID_SIGNATURE );
2519
2520 /* Compare with truncated hash */
2521 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2522 output, output_length - 1 ),
2523 PSA_ERROR_INVALID_SIGNATURE );
2524
2525 /* Compare with corrupted value */
2526 for( i = 0; i < output_length; i++ )
2527 {
Chris Jones9634bb12021-01-20 15:56:42 +00002528 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002529 output[i] ^= 1;
2530 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2531 output, output_length ),
2532 PSA_ERROR_INVALID_SIGNATURE );
2533 output[i] ^= 1;
2534 }
2535
2536exit:
2537 PSA_DONE( );
2538}
2539/* END_CASE */
2540
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002541/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002542void hash_bad_order( )
2543{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002544 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002545 unsigned char input[] = "";
2546 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002547 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002548 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2549 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2550 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002551 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002552 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002553 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002554
Gilles Peskine8817f612018-12-18 00:18:46 +01002555 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002556
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002557 /* Call setup twice in a row. */
2558 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2559 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2560 PSA_ERROR_BAD_STATE );
2561 PSA_ASSERT( psa_hash_abort( &operation ) );
2562
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002563 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002564 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002565 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002566 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002567
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002568 /* Call update after finish. */
2569 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2570 PSA_ASSERT( psa_hash_finish( &operation,
2571 hash, sizeof( hash ), &hash_len ) );
2572 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002573 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002574 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002575
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002576 /* Call verify without calling setup beforehand. */
2577 TEST_EQUAL( psa_hash_verify( &operation,
2578 valid_hash, sizeof( valid_hash ) ),
2579 PSA_ERROR_BAD_STATE );
2580 PSA_ASSERT( psa_hash_abort( &operation ) );
2581
2582 /* Call verify after finish. */
2583 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2584 PSA_ASSERT( psa_hash_finish( &operation,
2585 hash, sizeof( hash ), &hash_len ) );
2586 TEST_EQUAL( psa_hash_verify( &operation,
2587 valid_hash, sizeof( valid_hash ) ),
2588 PSA_ERROR_BAD_STATE );
2589 PSA_ASSERT( psa_hash_abort( &operation ) );
2590
2591 /* Call verify twice in a row. */
2592 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2593 PSA_ASSERT( psa_hash_verify( &operation,
2594 valid_hash, sizeof( valid_hash ) ) );
2595 TEST_EQUAL( psa_hash_verify( &operation,
2596 valid_hash, sizeof( valid_hash ) ),
2597 PSA_ERROR_BAD_STATE );
2598 PSA_ASSERT( psa_hash_abort( &operation ) );
2599
2600 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002601 TEST_EQUAL( psa_hash_finish( &operation,
2602 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002603 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002604 PSA_ASSERT( psa_hash_abort( &operation ) );
2605
2606 /* Call finish twice in a row. */
2607 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2608 PSA_ASSERT( psa_hash_finish( &operation,
2609 hash, sizeof( hash ), &hash_len ) );
2610 TEST_EQUAL( psa_hash_finish( &operation,
2611 hash, sizeof( hash ), &hash_len ),
2612 PSA_ERROR_BAD_STATE );
2613 PSA_ASSERT( psa_hash_abort( &operation ) );
2614
2615 /* Call finish after calling verify. */
2616 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2617 PSA_ASSERT( psa_hash_verify( &operation,
2618 valid_hash, sizeof( valid_hash ) ) );
2619 TEST_EQUAL( psa_hash_finish( &operation,
2620 hash, sizeof( hash ), &hash_len ),
2621 PSA_ERROR_BAD_STATE );
2622 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002623
2624exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002625 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002626}
2627/* END_CASE */
2628
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002629/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002630void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002631{
2632 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002633 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2634 * appended to it */
2635 unsigned char hash[] = {
2636 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2637 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2638 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002639 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002640 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002641
Gilles Peskine8817f612018-12-18 00:18:46 +01002642 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002643
itayzafrir27e69452018-11-01 14:26:34 +02002644 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002645 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002646 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002647 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002648
itayzafrir27e69452018-11-01 14:26:34 +02002649 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002650 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002651 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002652 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002653
itayzafrir27e69452018-11-01 14:26:34 +02002654 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002655 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002656 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002657 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002658
itayzafrirec93d302018-10-18 18:01:10 +03002659exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002660 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002661}
2662/* END_CASE */
2663
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002664/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2665void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002666{
2667 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002668 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002669 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002670 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002671 size_t hash_len;
2672
Gilles Peskine8817f612018-12-18 00:18:46 +01002673 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002674
itayzafrir58028322018-10-25 10:22:01 +03002675 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002676 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002677 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002678 hash, expected_size - 1, &hash_len ),
2679 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002680
2681exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002682 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002683}
2684/* END_CASE */
2685
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002686/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2687void hash_clone_source_state( )
2688{
2689 psa_algorithm_t alg = PSA_ALG_SHA_256;
2690 unsigned char hash[PSA_HASH_MAX_SIZE];
2691 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2692 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2693 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2694 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2695 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2696 size_t hash_len;
2697
2698 PSA_ASSERT( psa_crypto_init( ) );
2699 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2700
2701 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2702 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2703 PSA_ASSERT( psa_hash_finish( &op_finished,
2704 hash, sizeof( hash ), &hash_len ) );
2705 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2706 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2707
2708 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2709 PSA_ERROR_BAD_STATE );
2710
2711 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2712 PSA_ASSERT( psa_hash_finish( &op_init,
2713 hash, sizeof( hash ), &hash_len ) );
2714 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2715 PSA_ASSERT( psa_hash_finish( &op_finished,
2716 hash, sizeof( hash ), &hash_len ) );
2717 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2718 PSA_ASSERT( psa_hash_finish( &op_aborted,
2719 hash, sizeof( hash ), &hash_len ) );
2720
2721exit:
2722 psa_hash_abort( &op_source );
2723 psa_hash_abort( &op_init );
2724 psa_hash_abort( &op_setup );
2725 psa_hash_abort( &op_finished );
2726 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002727 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002728}
2729/* END_CASE */
2730
2731/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2732void hash_clone_target_state( )
2733{
2734 psa_algorithm_t alg = PSA_ALG_SHA_256;
2735 unsigned char hash[PSA_HASH_MAX_SIZE];
2736 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2737 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2738 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2739 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2740 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2741 size_t hash_len;
2742
2743 PSA_ASSERT( psa_crypto_init( ) );
2744
2745 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2746 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2747 PSA_ASSERT( psa_hash_finish( &op_finished,
2748 hash, sizeof( hash ), &hash_len ) );
2749 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2750 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2751
2752 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2753 PSA_ASSERT( psa_hash_finish( &op_target,
2754 hash, sizeof( hash ), &hash_len ) );
2755
2756 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2757 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2758 PSA_ERROR_BAD_STATE );
2759 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2760 PSA_ERROR_BAD_STATE );
2761
2762exit:
2763 psa_hash_abort( &op_target );
2764 psa_hash_abort( &op_init );
2765 psa_hash_abort( &op_setup );
2766 psa_hash_abort( &op_finished );
2767 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002768 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002769}
2770/* END_CASE */
2771
itayzafrir58028322018-10-25 10:22:01 +03002772/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002773void mac_operation_init( )
2774{
Jaeden Amero252ef282019-02-15 14:05:35 +00002775 const uint8_t input[1] = { 0 };
2776
Jaeden Amero769ce272019-01-04 11:48:03 +00002777 /* Test each valid way of initializing the object, except for `= {0}`, as
2778 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2779 * though it's OK by the C standard. We could test for this, but we'd need
2780 * to supress the Clang warning for the test. */
2781 psa_mac_operation_t func = psa_mac_operation_init( );
2782 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2783 psa_mac_operation_t zero;
2784
2785 memset( &zero, 0, sizeof( zero ) );
2786
Jaeden Amero252ef282019-02-15 14:05:35 +00002787 /* A freshly-initialized MAC operation should not be usable. */
2788 TEST_EQUAL( psa_mac_update( &func,
2789 input, sizeof( input ) ),
2790 PSA_ERROR_BAD_STATE );
2791 TEST_EQUAL( psa_mac_update( &init,
2792 input, sizeof( input ) ),
2793 PSA_ERROR_BAD_STATE );
2794 TEST_EQUAL( psa_mac_update( &zero,
2795 input, sizeof( input ) ),
2796 PSA_ERROR_BAD_STATE );
2797
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002798 /* A default MAC operation should be abortable without error. */
2799 PSA_ASSERT( psa_mac_abort( &func ) );
2800 PSA_ASSERT( psa_mac_abort( &init ) );
2801 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002802}
2803/* END_CASE */
2804
2805/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002806void mac_setup( int key_type_arg,
2807 data_t *key,
2808 int alg_arg,
2809 int expected_status_arg )
2810{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002811 psa_key_type_t key_type = key_type_arg;
2812 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002813 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002814 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002815 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2816#if defined(KNOWN_SUPPORTED_MAC_ALG)
2817 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2818#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002819
Gilles Peskine8817f612018-12-18 00:18:46 +01002820 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002821
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002822 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2823 &operation, &status ) )
2824 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002825 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002826
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002827 /* The operation object should be reusable. */
2828#if defined(KNOWN_SUPPORTED_MAC_ALG)
2829 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2830 smoke_test_key_data,
2831 sizeof( smoke_test_key_data ),
2832 KNOWN_SUPPORTED_MAC_ALG,
2833 &operation, &status ) )
2834 goto exit;
2835 TEST_EQUAL( status, PSA_SUCCESS );
2836#endif
2837
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002838exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002839 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002840}
2841/* END_CASE */
2842
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002843/* 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 +00002844void mac_bad_order( )
2845{
Ronald Cron5425a212020-08-04 14:58:35 +02002846 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002847 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2848 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002849 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002850 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2851 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2852 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002854 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2855 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2856 size_t sign_mac_length = 0;
2857 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2858 const uint8_t verify_mac[] = {
2859 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2860 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2861 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2862
2863 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002864 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002865 psa_set_key_algorithm( &attributes, alg );
2866 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002867
Ronald Cron5425a212020-08-04 14:58:35 +02002868 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2869 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002870
Jaeden Amero252ef282019-02-15 14:05:35 +00002871 /* Call update without calling setup beforehand. */
2872 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2873 PSA_ERROR_BAD_STATE );
2874 PSA_ASSERT( psa_mac_abort( &operation ) );
2875
2876 /* Call sign finish without calling setup beforehand. */
2877 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2878 &sign_mac_length),
2879 PSA_ERROR_BAD_STATE );
2880 PSA_ASSERT( psa_mac_abort( &operation ) );
2881
2882 /* Call verify finish without calling setup beforehand. */
2883 TEST_EQUAL( psa_mac_verify_finish( &operation,
2884 verify_mac, sizeof( verify_mac ) ),
2885 PSA_ERROR_BAD_STATE );
2886 PSA_ASSERT( psa_mac_abort( &operation ) );
2887
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002888 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002889 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
2890 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002891 PSA_ERROR_BAD_STATE );
2892 PSA_ASSERT( psa_mac_abort( &operation ) );
2893
Jaeden Amero252ef282019-02-15 14:05:35 +00002894 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002895 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002896 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2897 PSA_ASSERT( psa_mac_sign_finish( &operation,
2898 sign_mac, sizeof( sign_mac ),
2899 &sign_mac_length ) );
2900 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2901 PSA_ERROR_BAD_STATE );
2902 PSA_ASSERT( psa_mac_abort( &operation ) );
2903
2904 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002905 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002906 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2907 PSA_ASSERT( psa_mac_verify_finish( &operation,
2908 verify_mac, sizeof( verify_mac ) ) );
2909 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2910 PSA_ERROR_BAD_STATE );
2911 PSA_ASSERT( psa_mac_abort( &operation ) );
2912
2913 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002914 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002915 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2916 PSA_ASSERT( psa_mac_sign_finish( &operation,
2917 sign_mac, sizeof( sign_mac ),
2918 &sign_mac_length ) );
2919 TEST_EQUAL( psa_mac_sign_finish( &operation,
2920 sign_mac, sizeof( sign_mac ),
2921 &sign_mac_length ),
2922 PSA_ERROR_BAD_STATE );
2923 PSA_ASSERT( psa_mac_abort( &operation ) );
2924
2925 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002926 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002927 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2928 PSA_ASSERT( psa_mac_verify_finish( &operation,
2929 verify_mac, sizeof( verify_mac ) ) );
2930 TEST_EQUAL( psa_mac_verify_finish( &operation,
2931 verify_mac, sizeof( verify_mac ) ),
2932 PSA_ERROR_BAD_STATE );
2933 PSA_ASSERT( psa_mac_abort( &operation ) );
2934
2935 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002936 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002937 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2938 TEST_EQUAL( psa_mac_verify_finish( &operation,
2939 verify_mac, sizeof( verify_mac ) ),
2940 PSA_ERROR_BAD_STATE );
2941 PSA_ASSERT( psa_mac_abort( &operation ) );
2942
2943 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002944 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002945 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2946 TEST_EQUAL( psa_mac_sign_finish( &operation,
2947 sign_mac, sizeof( sign_mac ),
2948 &sign_mac_length ),
2949 PSA_ERROR_BAD_STATE );
2950 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002951
Ronald Cron5425a212020-08-04 14:58:35 +02002952 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002953
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002954exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002955 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002956}
2957/* END_CASE */
2958
2959/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002960void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002961 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002962 int alg_arg,
2963 data_t *input,
2964 data_t *expected_mac )
2965{
Ronald Cron5425a212020-08-04 14:58:35 +02002966 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002967 psa_key_type_t key_type = key_type_arg;
2968 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002969 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002970 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002971 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002972 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002973 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002974 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002975 const size_t output_sizes_to_test[] = {
2976 0,
2977 1,
2978 expected_mac->len - 1,
2979 expected_mac->len,
2980 expected_mac->len + 1,
2981 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002982
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002983 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002984 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002985 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002986
Gilles Peskine8817f612018-12-18 00:18:46 +01002987 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002988
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002989 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002990 psa_set_key_algorithm( &attributes, alg );
2991 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002992
Ronald Cron5425a212020-08-04 14:58:35 +02002993 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2994 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002995
Gilles Peskine8b356b52020-08-25 23:44:59 +02002996 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2997 {
2998 const size_t output_size = output_sizes_to_test[i];
2999 psa_status_t expected_status =
3000 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3001 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003002
Chris Jones9634bb12021-01-20 15:56:42 +00003003 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003004 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003005
Gilles Peskine8b356b52020-08-25 23:44:59 +02003006 /* Calculate the MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003007 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003008 PSA_ASSERT( psa_mac_update( &operation,
3009 input->x, input->len ) );
3010 TEST_EQUAL( psa_mac_sign_finish( &operation,
3011 actual_mac, output_size,
3012 &mac_length ),
3013 expected_status );
3014 PSA_ASSERT( psa_mac_abort( &operation ) );
3015
3016 if( expected_status == PSA_SUCCESS )
3017 {
3018 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3019 actual_mac, mac_length );
3020 }
3021 mbedtls_free( actual_mac );
3022 actual_mac = NULL;
3023 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003024
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003025exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003026 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003027 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003028 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003029 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003030}
3031/* END_CASE */
3032
3033/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003034void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003035 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003036 int alg_arg,
3037 data_t *input,
3038 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003039{
Ronald Cron5425a212020-08-04 14:58:35 +02003040 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003041 psa_key_type_t key_type = key_type_arg;
3042 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003043 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003044 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003045 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003046
Gilles Peskine69c12672018-06-28 00:07:19 +02003047 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3048
Gilles Peskine8817f612018-12-18 00:18:46 +01003049 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003050
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003051 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003052 psa_set_key_algorithm( &attributes, alg );
3053 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003054
Ronald Cron5425a212020-08-04 14:58:35 +02003055 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3056 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003057
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003058 /* Test the correct MAC. */
Ronald Cron5425a212020-08-04 14:58:35 +02003059 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 PSA_ASSERT( psa_mac_update( &operation,
3061 input->x, input->len ) );
3062 PSA_ASSERT( psa_mac_verify_finish( &operation,
3063 expected_mac->x,
3064 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003065
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003066 /* Test a MAC that's too short. */
Ronald Cron5425a212020-08-04 14:58:35 +02003067 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003068 PSA_ASSERT( psa_mac_update( &operation,
3069 input->x, input->len ) );
3070 TEST_EQUAL( psa_mac_verify_finish( &operation,
3071 expected_mac->x,
3072 expected_mac->len - 1 ),
3073 PSA_ERROR_INVALID_SIGNATURE );
3074
3075 /* Test a MAC that's too long. */
3076 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3077 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
Ronald Cron5425a212020-08-04 14:58:35 +02003078 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003079 PSA_ASSERT( psa_mac_update( &operation,
3080 input->x, input->len ) );
3081 TEST_EQUAL( psa_mac_verify_finish( &operation,
3082 perturbed_mac,
3083 expected_mac->len + 1 ),
3084 PSA_ERROR_INVALID_SIGNATURE );
3085
3086 /* Test changing one byte. */
3087 for( size_t i = 0; i < expected_mac->len; i++ )
3088 {
Chris Jones9634bb12021-01-20 15:56:42 +00003089 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003090 perturbed_mac[i] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003091 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003092 PSA_ASSERT( psa_mac_update( &operation,
3093 input->x, input->len ) );
3094 TEST_EQUAL( psa_mac_verify_finish( &operation,
3095 perturbed_mac,
3096 expected_mac->len ),
3097 PSA_ERROR_INVALID_SIGNATURE );
3098 perturbed_mac[i] ^= 1;
3099 }
3100
Gilles Peskine8c9def32018-02-08 10:02:12 +01003101exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003102 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003103 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003104 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003105 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003106}
3107/* END_CASE */
3108
3109/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003110void cipher_operation_init( )
3111{
Jaeden Ameroab439972019-02-15 14:12:05 +00003112 const uint8_t input[1] = { 0 };
3113 unsigned char output[1] = { 0 };
3114 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003115 /* Test each valid way of initializing the object, except for `= {0}`, as
3116 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3117 * though it's OK by the C standard. We could test for this, but we'd need
3118 * to supress the Clang warning for the test. */
3119 psa_cipher_operation_t func = psa_cipher_operation_init( );
3120 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3121 psa_cipher_operation_t zero;
3122
3123 memset( &zero, 0, sizeof( zero ) );
3124
Jaeden Ameroab439972019-02-15 14:12:05 +00003125 /* A freshly-initialized cipher operation should not be usable. */
3126 TEST_EQUAL( psa_cipher_update( &func,
3127 input, sizeof( input ),
3128 output, sizeof( output ),
3129 &output_length ),
3130 PSA_ERROR_BAD_STATE );
3131 TEST_EQUAL( psa_cipher_update( &init,
3132 input, sizeof( input ),
3133 output, sizeof( output ),
3134 &output_length ),
3135 PSA_ERROR_BAD_STATE );
3136 TEST_EQUAL( psa_cipher_update( &zero,
3137 input, sizeof( input ),
3138 output, sizeof( output ),
3139 &output_length ),
3140 PSA_ERROR_BAD_STATE );
3141
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003142 /* A default cipher operation should be abortable without error. */
3143 PSA_ASSERT( psa_cipher_abort( &func ) );
3144 PSA_ASSERT( psa_cipher_abort( &init ) );
3145 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003146}
3147/* END_CASE */
3148
3149/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003150void cipher_setup( int key_type_arg,
3151 data_t *key,
3152 int alg_arg,
3153 int expected_status_arg )
3154{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003155 psa_key_type_t key_type = key_type_arg;
3156 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003157 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003158 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003159 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003160#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003161 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3162#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003163
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003165
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003166 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3167 &operation, &status ) )
3168 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003169 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003170
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003171 /* The operation object should be reusable. */
3172#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3173 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3174 smoke_test_key_data,
3175 sizeof( smoke_test_key_data ),
3176 KNOWN_SUPPORTED_CIPHER_ALG,
3177 &operation, &status ) )
3178 goto exit;
3179 TEST_EQUAL( status, PSA_SUCCESS );
3180#endif
3181
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003182exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003183 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003184 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003185}
3186/* END_CASE */
3187
Steven Cooreman29eecbf2021-01-28 19:41:25 +01003188/* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003189void cipher_bad_order( )
3190{
Ronald Cron5425a212020-08-04 14:58:35 +02003191 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003192 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3193 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003194 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003195 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003196 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003197 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003198 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3199 0xaa, 0xaa, 0xaa, 0xaa };
3200 const uint8_t text[] = {
3201 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3202 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003203 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003204 size_t length = 0;
3205
3206 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003207 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3208 psa_set_key_algorithm( &attributes, alg );
3209 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003210 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3211 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003212
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003213 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003214 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3215 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003216 PSA_ERROR_BAD_STATE );
3217 PSA_ASSERT( psa_cipher_abort( &operation ) );
3218
3219 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003220 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3221 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003222 PSA_ERROR_BAD_STATE );
3223 PSA_ASSERT( psa_cipher_abort( &operation ) );
3224
Jaeden Ameroab439972019-02-15 14:12:05 +00003225 /* Generate an IV without calling setup beforehand. */
3226 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3227 buffer, sizeof( buffer ),
3228 &length ),
3229 PSA_ERROR_BAD_STATE );
3230 PSA_ASSERT( psa_cipher_abort( &operation ) );
3231
3232 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003233 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003234 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3235 buffer, sizeof( buffer ),
3236 &length ) );
3237 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3238 buffer, sizeof( buffer ),
3239 &length ),
3240 PSA_ERROR_BAD_STATE );
3241 PSA_ASSERT( psa_cipher_abort( &operation ) );
3242
3243 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003244 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003245 PSA_ASSERT( psa_cipher_set_iv( &operation,
3246 iv, sizeof( iv ) ) );
3247 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3248 buffer, sizeof( buffer ),
3249 &length ),
3250 PSA_ERROR_BAD_STATE );
3251 PSA_ASSERT( psa_cipher_abort( &operation ) );
3252
3253 /* Set an IV without calling setup beforehand. */
3254 TEST_EQUAL( psa_cipher_set_iv( &operation,
3255 iv, sizeof( iv ) ),
3256 PSA_ERROR_BAD_STATE );
3257 PSA_ASSERT( psa_cipher_abort( &operation ) );
3258
3259 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003260 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003261 PSA_ASSERT( psa_cipher_set_iv( &operation,
3262 iv, sizeof( iv ) ) );
3263 TEST_EQUAL( psa_cipher_set_iv( &operation,
3264 iv, sizeof( iv ) ),
3265 PSA_ERROR_BAD_STATE );
3266 PSA_ASSERT( psa_cipher_abort( &operation ) );
3267
3268 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003269 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003270 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3271 buffer, sizeof( buffer ),
3272 &length ) );
3273 TEST_EQUAL( psa_cipher_set_iv( &operation,
3274 iv, sizeof( iv ) ),
3275 PSA_ERROR_BAD_STATE );
3276 PSA_ASSERT( psa_cipher_abort( &operation ) );
3277
3278 /* Call update without calling setup beforehand. */
3279 TEST_EQUAL( psa_cipher_update( &operation,
3280 text, sizeof( text ),
3281 buffer, sizeof( buffer ),
3282 &length ),
3283 PSA_ERROR_BAD_STATE );
3284 PSA_ASSERT( psa_cipher_abort( &operation ) );
3285
3286 /* Call update without an IV where an IV is required. */
3287 TEST_EQUAL( psa_cipher_update( &operation,
3288 text, sizeof( text ),
3289 buffer, sizeof( buffer ),
3290 &length ),
3291 PSA_ERROR_BAD_STATE );
3292 PSA_ASSERT( psa_cipher_abort( &operation ) );
3293
3294 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003295 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003296 PSA_ASSERT( psa_cipher_set_iv( &operation,
3297 iv, sizeof( iv ) ) );
3298 PSA_ASSERT( psa_cipher_finish( &operation,
3299 buffer, sizeof( buffer ), &length ) );
3300 TEST_EQUAL( psa_cipher_update( &operation,
3301 text, sizeof( text ),
3302 buffer, sizeof( buffer ),
3303 &length ),
3304 PSA_ERROR_BAD_STATE );
3305 PSA_ASSERT( psa_cipher_abort( &operation ) );
3306
3307 /* Call finish without calling setup beforehand. */
3308 TEST_EQUAL( psa_cipher_finish( &operation,
3309 buffer, sizeof( buffer ), &length ),
3310 PSA_ERROR_BAD_STATE );
3311 PSA_ASSERT( psa_cipher_abort( &operation ) );
3312
3313 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003314 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003315 /* Not calling update means we are encrypting an empty buffer, which is OK
3316 * for cipher modes with padding. */
3317 TEST_EQUAL( psa_cipher_finish( &operation,
3318 buffer, sizeof( buffer ), &length ),
3319 PSA_ERROR_BAD_STATE );
3320 PSA_ASSERT( psa_cipher_abort( &operation ) );
3321
3322 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003323 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003324 PSA_ASSERT( psa_cipher_set_iv( &operation,
3325 iv, sizeof( iv ) ) );
3326 PSA_ASSERT( psa_cipher_finish( &operation,
3327 buffer, sizeof( buffer ), &length ) );
3328 TEST_EQUAL( psa_cipher_finish( &operation,
3329 buffer, sizeof( buffer ), &length ),
3330 PSA_ERROR_BAD_STATE );
3331 PSA_ASSERT( psa_cipher_abort( &operation ) );
3332
Ronald Cron5425a212020-08-04 14:58:35 +02003333 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003334
Jaeden Ameroab439972019-02-15 14:12:05 +00003335exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003336 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003337 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003338}
3339/* END_CASE */
3340
3341/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003342void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003343 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003344 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003345 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003346{
Ronald Cron5425a212020-08-04 14:58:35 +02003347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003348 psa_status_t status;
3349 psa_key_type_t key_type = key_type_arg;
3350 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003351 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003352 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003353 size_t output_buffer_size = 0;
3354 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003355 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003356 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003358
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003360
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3362 psa_set_key_algorithm( &attributes, alg );
3363 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003364
Ronald Cron5425a212020-08-04 14:58:35 +02003365 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3366 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003367
Ronald Cron5425a212020-08-04 14:58:35 +02003368 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003369
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003370 if( iv->len > 0 )
3371 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003372 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003373 }
3374
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003375 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003376 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003377 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003378
Gilles Peskine8817f612018-12-18 00:18:46 +01003379 PSA_ASSERT( psa_cipher_update( &operation,
3380 input->x, input->len,
3381 output, output_buffer_size,
3382 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003383 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003384 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003385 output + total_output_length,
3386 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003387 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003388 total_output_length += function_output_length;
3389
Gilles Peskinefe11b722018-12-18 00:24:04 +01003390 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003391 if( expected_status == PSA_SUCCESS )
3392 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003393 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003394 ASSERT_COMPARE( expected_output->x, expected_output->len,
3395 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003396 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003397
Gilles Peskine50e586b2018-06-08 14:28:46 +02003398exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003399 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003400 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003401 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003402 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003403}
3404/* END_CASE */
3405
3406/* BEGIN_CASE */
3407void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003408 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003409 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003410 int first_part_size_arg,
3411 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003412 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003413{
Ronald Cron5425a212020-08-04 14:58:35 +02003414 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003415 psa_key_type_t key_type = key_type_arg;
3416 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003417 size_t first_part_size = first_part_size_arg;
3418 size_t output1_length = output1_length_arg;
3419 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003420 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003421 size_t output_buffer_size = 0;
3422 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003423 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003424 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003425 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003426
Gilles Peskine8817f612018-12-18 00:18:46 +01003427 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003428
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003429 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3430 psa_set_key_algorithm( &attributes, alg );
3431 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003432
Ronald Cron5425a212020-08-04 14:58:35 +02003433 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3434 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003435
Ronald Cron5425a212020-08-04 14:58:35 +02003436 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003437
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003438 if( iv->len > 0 )
3439 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003440 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003441 }
3442
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003443 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003444 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003445 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003446
Gilles Peskinee0866522019-02-19 19:44:00 +01003447 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003448 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3449 output, output_buffer_size,
3450 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003451 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003452 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003453 PSA_ASSERT( psa_cipher_update( &operation,
3454 input->x + first_part_size,
3455 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003456 output + total_output_length,
3457 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003458 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003459 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003460 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003461 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003462 output + total_output_length,
3463 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003464 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003465 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003466 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003467
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003468 ASSERT_COMPARE( expected_output->x, expected_output->len,
3469 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003470
3471exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003472 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003473 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003474 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003475 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003476}
3477/* END_CASE */
3478
3479/* BEGIN_CASE */
3480void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003481 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003482 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003483 int first_part_size_arg,
3484 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003485 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003486{
Ronald Cron5425a212020-08-04 14:58:35 +02003487 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003488 psa_key_type_t key_type = key_type_arg;
3489 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003490 size_t first_part_size = first_part_size_arg;
3491 size_t output1_length = output1_length_arg;
3492 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003493 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003494 size_t output_buffer_size = 0;
3495 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003496 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003497 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003499
Gilles Peskine8817f612018-12-18 00:18:46 +01003500 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003501
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003502 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3503 psa_set_key_algorithm( &attributes, alg );
3504 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003505
Ronald Cron5425a212020-08-04 14:58:35 +02003506 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3507 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003508
Ronald Cron5425a212020-08-04 14:58:35 +02003509 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003510
Steven Cooreman177deba2020-09-07 17:14:14 +02003511 if( iv->len > 0 )
3512 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003513 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003514 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003515
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003516 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003517 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003518 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003519
Gilles Peskinee0866522019-02-19 19:44:00 +01003520 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003521 PSA_ASSERT( psa_cipher_update( &operation,
3522 input->x, first_part_size,
3523 output, output_buffer_size,
3524 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003525 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003526 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003527 PSA_ASSERT( psa_cipher_update( &operation,
3528 input->x + first_part_size,
3529 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003530 output + total_output_length,
3531 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003532 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003533 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003534 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003535 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003536 output + total_output_length,
3537 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003538 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003539 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003541
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003542 ASSERT_COMPARE( expected_output->x, expected_output->len,
3543 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003544
3545exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003546 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003547 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003548 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003549 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003550}
3551/* END_CASE */
3552
Gilles Peskine50e586b2018-06-08 14:28:46 +02003553/* BEGIN_CASE */
3554void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003555 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003556 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003557 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003558{
Ronald Cron5425a212020-08-04 14:58:35 +02003559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003560 psa_status_t status;
3561 psa_key_type_t key_type = key_type_arg;
3562 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003563 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003564 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003565 size_t output_buffer_size = 0;
3566 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003567 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003568 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003570
Gilles Peskine8817f612018-12-18 00:18:46 +01003571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003572
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003573 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3574 psa_set_key_algorithm( &attributes, alg );
3575 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003576
Ronald Cron5425a212020-08-04 14:58:35 +02003577 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3578 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003579
Ronald Cron5425a212020-08-04 14:58:35 +02003580 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003581
Steven Cooreman177deba2020-09-07 17:14:14 +02003582 if( iv->len > 0 )
3583 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003584 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003585 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003586
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003587 output_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003588 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003589 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003590
Gilles Peskine8817f612018-12-18 00:18:46 +01003591 PSA_ASSERT( psa_cipher_update( &operation,
3592 input->x, input->len,
3593 output, output_buffer_size,
3594 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003595 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003596 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003597 output + total_output_length,
3598 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003599 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003600 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003601 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003602
3603 if( expected_status == PSA_SUCCESS )
3604 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003606 ASSERT_COMPARE( expected_output->x, expected_output->len,
3607 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003608 }
3609
Gilles Peskine50e586b2018-06-08 14:28:46 +02003610exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003611 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003612 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003613 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003614 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003615}
3616/* END_CASE */
3617
Gilles Peskine50e586b2018-06-08 14:28:46 +02003618/* BEGIN_CASE */
3619void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003620 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003621 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003622{
Ronald Cron5425a212020-08-04 14:58:35 +02003623 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003624 psa_key_type_t key_type = key_type_arg;
3625 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003626 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003627 size_t iv_size = 16;
3628 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003629 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003630 size_t output1_size = 0;
3631 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003632 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003633 size_t output2_size = 0;
3634 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003635 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003636 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3637 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003638 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003639
Gilles Peskine8817f612018-12-18 00:18:46 +01003640 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003641
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003642 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3643 psa_set_key_algorithm( &attributes, alg );
3644 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003645
Ronald Cron5425a212020-08-04 14:58:35 +02003646 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3647 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003648
Ronald Cron5425a212020-08-04 14:58:35 +02003649 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3650 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003651
Steven Cooreman177deba2020-09-07 17:14:14 +02003652 if( alg != PSA_ALG_ECB_NO_PADDING )
3653 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003654 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3655 iv, iv_size,
3656 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003657 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003658 output1_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003659 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003660 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003661
Gilles Peskine8817f612018-12-18 00:18:46 +01003662 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3663 output1, output1_size,
3664 &output1_length ) );
3665 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003666 output1 + output1_length,
3667 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003668 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003669
Gilles Peskine048b7f02018-06-08 14:20:49 +02003670 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003671
Gilles Peskine8817f612018-12-18 00:18:46 +01003672 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003673
3674 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003675 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003676
Steven Cooreman177deba2020-09-07 17:14:14 +02003677 if( iv_length > 0 )
3678 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003679 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3680 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003681 }
3682
Gilles Peskine8817f612018-12-18 00:18:46 +01003683 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3684 output2, output2_size,
3685 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003686 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003687 PSA_ASSERT( psa_cipher_finish( &operation2,
3688 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003689 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003690 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003691
Gilles Peskine048b7f02018-06-08 14:20:49 +02003692 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003693
Gilles Peskine8817f612018-12-18 00:18:46 +01003694 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003695
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003696 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003697
3698exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003699 psa_cipher_abort( &operation1 );
3700 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003701 mbedtls_free( output1 );
3702 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003703 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003704 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003705}
3706/* END_CASE */
3707
3708/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003709void cipher_verify_output_multipart( int alg_arg,
3710 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003711 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003712 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003713 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003714{
Ronald Cron5425a212020-08-04 14:58:35 +02003715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003716 psa_key_type_t key_type = key_type_arg;
3717 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003718 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003719 unsigned char iv[16] = {0};
3720 size_t iv_size = 16;
3721 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003722 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003723 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003724 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003725 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003726 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003727 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003728 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003729 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3730 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003731 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003732
Gilles Peskine8817f612018-12-18 00:18:46 +01003733 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003734
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003735 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3736 psa_set_key_algorithm( &attributes, alg );
3737 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003738
Ronald Cron5425a212020-08-04 14:58:35 +02003739 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3740 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003741
Ronald Cron5425a212020-08-04 14:58:35 +02003742 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3743 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003744
Steven Cooreman177deba2020-09-07 17:14:14 +02003745 if( alg != PSA_ALG_ECB_NO_PADDING )
3746 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003747 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3748 iv, iv_size,
3749 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003750 }
3751
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003752 output1_buffer_size = ( (size_t) input->len +
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003753 PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003754 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003755
Gilles Peskinee0866522019-02-19 19:44:00 +01003756 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003757
Gilles Peskine8817f612018-12-18 00:18:46 +01003758 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3759 output1, output1_buffer_size,
3760 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003761 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003762
Gilles Peskine8817f612018-12-18 00:18:46 +01003763 PSA_ASSERT( psa_cipher_update( &operation1,
3764 input->x + first_part_size,
3765 input->len - first_part_size,
3766 output1, output1_buffer_size,
3767 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003768 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003769
Gilles Peskine8817f612018-12-18 00:18:46 +01003770 PSA_ASSERT( psa_cipher_finish( &operation1,
3771 output1 + output1_length,
3772 output1_buffer_size - output1_length,
3773 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003774 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003775
Gilles Peskine8817f612018-12-18 00:18:46 +01003776 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003777
Gilles Peskine048b7f02018-06-08 14:20:49 +02003778 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003779 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003780
Steven Cooreman177deba2020-09-07 17:14:14 +02003781 if( iv_length > 0 )
3782 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003783 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3784 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003785 }
Moran Pekerded84402018-06-06 16:36:50 +03003786
Gilles Peskine8817f612018-12-18 00:18:46 +01003787 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3788 output2, output2_buffer_size,
3789 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003790 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003791
Gilles Peskine8817f612018-12-18 00:18:46 +01003792 PSA_ASSERT( psa_cipher_update( &operation2,
3793 output1 + first_part_size,
3794 output1_length - first_part_size,
3795 output2, output2_buffer_size,
3796 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003797 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003798
Gilles Peskine8817f612018-12-18 00:18:46 +01003799 PSA_ASSERT( psa_cipher_finish( &operation2,
3800 output2 + output2_length,
3801 output2_buffer_size - output2_length,
3802 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003803 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003804
Gilles Peskine8817f612018-12-18 00:18:46 +01003805 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003806
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003807 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003808
3809exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003810 psa_cipher_abort( &operation1 );
3811 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003812 mbedtls_free( output1 );
3813 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003814 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003815 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003816}
3817/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003818
Gilles Peskine20035e32018-02-03 22:44:14 +01003819/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003820void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003821 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003822 data_t *nonce,
3823 data_t *additional_data,
3824 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003825 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003826{
Ronald Cron5425a212020-08-04 14:58:35 +02003827 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003828 psa_key_type_t key_type = key_type_arg;
3829 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003830 unsigned char *output_data = NULL;
3831 size_t output_size = 0;
3832 size_t output_length = 0;
3833 unsigned char *output_data2 = NULL;
3834 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003835 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003836 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003838
Gilles Peskine4abf7412018-06-18 16:35:34 +02003839 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003840 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3841 * should be exact. */
3842 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3843 TEST_EQUAL( output_size,
3844 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003845 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003846
Gilles Peskine8817f612018-12-18 00:18:46 +01003847 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003848
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003849 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3850 psa_set_key_algorithm( &attributes, alg );
3851 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003852
Gilles Peskine049c7532019-05-15 20:22:09 +02003853 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003854 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003855
Ronald Cron5425a212020-08-04 14:58:35 +02003856 TEST_EQUAL( psa_aead_encrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003857 nonce->x, nonce->len,
3858 additional_data->x,
3859 additional_data->len,
3860 input_data->x, input_data->len,
3861 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003862 &output_length ),
3863 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003864
3865 if( PSA_SUCCESS == expected_result )
3866 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003867 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003868
Gilles Peskine003a4a92019-05-14 16:09:40 +02003869 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3870 * should be exact. */
3871 TEST_EQUAL( input_data->len,
3872 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3873
Ronald Cron5425a212020-08-04 14:58:35 +02003874 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003875 nonce->x, nonce->len,
3876 additional_data->x,
3877 additional_data->len,
3878 output_data, output_length,
3879 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003880 &output_length2 ),
3881 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003882
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003883 ASSERT_COMPARE( input_data->x, input_data->len,
3884 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003885 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003886
Gilles Peskinea1cac842018-06-11 19:33:02 +02003887exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003888 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003889 mbedtls_free( output_data );
3890 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003891 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003892}
3893/* END_CASE */
3894
3895/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003896void aead_encrypt( int key_type_arg, data_t *key_data,
3897 int alg_arg,
3898 data_t *nonce,
3899 data_t *additional_data,
3900 data_t *input_data,
3901 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003902{
Ronald Cron5425a212020-08-04 14:58:35 +02003903 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003904 psa_key_type_t key_type = key_type_arg;
3905 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003906 unsigned char *output_data = NULL;
3907 size_t output_size = 0;
3908 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003909 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003911 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003912
Gilles Peskine4abf7412018-06-18 16:35:34 +02003913 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003914 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3915 * should be exact. */
3916 TEST_EQUAL( output_size,
3917 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003918 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003919
Gilles Peskine8817f612018-12-18 00:18:46 +01003920 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003921
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003922 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3923 psa_set_key_algorithm( &attributes, alg );
3924 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003925
Gilles Peskine049c7532019-05-15 20:22:09 +02003926 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003927 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003928
Steven Cooremand588ea12021-01-11 19:36:04 +01003929 status = psa_aead_encrypt( key, alg,
3930 nonce->x, nonce->len,
3931 additional_data->x, additional_data->len,
3932 input_data->x, input_data->len,
3933 output_data, output_size,
3934 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003935
Ronald Cron28a45ed2021-02-09 20:35:42 +01003936 /* If the operation is not supported, just skip and not fail in case the
3937 * encryption involves a common limitation of cryptography hardwares and
3938 * an alternative implementation. */
3939 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003940 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003941 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3942 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003943 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003944
3945 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003946 ASSERT_COMPARE( expected_result->x, expected_result->len,
3947 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003948
Gilles Peskinea1cac842018-06-11 19:33:02 +02003949exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003950 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003951 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003952 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003953}
3954/* END_CASE */
3955
3956/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003957void aead_decrypt( int key_type_arg, data_t *key_data,
3958 int alg_arg,
3959 data_t *nonce,
3960 data_t *additional_data,
3961 data_t *input_data,
3962 data_t *expected_data,
3963 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003964{
Ronald Cron5425a212020-08-04 14:58:35 +02003965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003966 psa_key_type_t key_type = key_type_arg;
3967 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003968 unsigned char *output_data = NULL;
3969 size_t output_size = 0;
3970 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003971 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003972 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003973 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003974 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003975
Gilles Peskine003a4a92019-05-14 16:09:40 +02003976 output_size = input_data->len - tag_length;
3977 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3978 * should be exact. */
3979 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3980 TEST_EQUAL( output_size,
3981 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003982 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003983
Gilles Peskine8817f612018-12-18 00:18:46 +01003984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003985
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003986 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3987 psa_set_key_algorithm( &attributes, alg );
3988 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003989
Gilles Peskine049c7532019-05-15 20:22:09 +02003990 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003991 &key ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003992
Steven Cooremand588ea12021-01-11 19:36:04 +01003993 status = psa_aead_decrypt( key, alg,
3994 nonce->x, nonce->len,
3995 additional_data->x,
3996 additional_data->len,
3997 input_data->x, input_data->len,
3998 output_data, output_size,
3999 &output_length );
4000
Ronald Cron28a45ed2021-02-09 20:35:42 +01004001 /* If the operation is not supported, just skip and not fail in case the
4002 * decryption involves a common limitation of cryptography hardwares and
4003 * an alternative implementation. */
4004 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004005 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004006 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4007 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004008 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004009
4010 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004011
Gilles Peskine2d277862018-06-18 15:41:12 +02004012 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004013 ASSERT_COMPARE( expected_data->x, expected_data->len,
4014 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004015
Gilles Peskinea1cac842018-06-11 19:33:02 +02004016exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004017 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004018 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004019 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004020}
4021/* END_CASE */
4022
4023/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004024void signature_size( int type_arg,
4025 int bits,
4026 int alg_arg,
4027 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004028{
4029 psa_key_type_t type = type_arg;
4030 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004031 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004032
Gilles Peskinefe11b722018-12-18 00:24:04 +01004033 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004034#if defined(MBEDTLS_TEST_DEPRECATED)
4035 TEST_EQUAL( actual_size,
4036 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4037#endif /* MBEDTLS_TEST_DEPRECATED */
4038
Gilles Peskinee59236f2018-01-27 23:32:46 +01004039exit:
4040 ;
4041}
4042/* END_CASE */
4043
4044/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004045void sign_deterministic( int key_type_arg, data_t *key_data,
4046 int alg_arg, data_t *input_data,
4047 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004048{
Ronald Cron5425a212020-08-04 14:58:35 +02004049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004050 psa_key_type_t key_type = key_type_arg;
4051 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004052 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004053 unsigned char *signature = NULL;
4054 size_t signature_size;
4055 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004057
Gilles Peskine8817f612018-12-18 00:18:46 +01004058 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004059
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004060 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004061 psa_set_key_algorithm( &attributes, alg );
4062 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004063
Gilles Peskine049c7532019-05-15 20:22:09 +02004064 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004065 &key ) );
4066 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004067 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004068
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004069 /* Allocate a buffer which has the size advertized by the
4070 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004071 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004072 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004073 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004074 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004075 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004076
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004077 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004078 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004079 input_data->x, input_data->len,
4080 signature, signature_size,
4081 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004082 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004083 ASSERT_COMPARE( output_data->x, output_data->len,
4084 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004085
Gilles Peskine0627f982019-11-26 19:12:16 +01004086#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004087 memset( signature, 0, signature_size );
4088 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004089 PSA_ASSERT( psa_asymmetric_sign( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004090 input_data->x, input_data->len,
4091 signature, signature_size,
4092 &signature_length ) );
4093 ASSERT_COMPARE( output_data->x, output_data->len,
4094 signature, signature_length );
4095#endif /* MBEDTLS_TEST_DEPRECATED */
4096
Gilles Peskine20035e32018-02-03 22:44:14 +01004097exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004098 /*
4099 * Key attributes may have been returned by psa_get_key_attributes()
4100 * thus reset them as required.
4101 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004102 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004103
Ronald Cron5425a212020-08-04 14:58:35 +02004104 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01004105 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004106 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004107}
4108/* END_CASE */
4109
4110/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004111void sign_fail( int key_type_arg, data_t *key_data,
4112 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004113 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004114{
Ronald Cron5425a212020-08-04 14:58:35 +02004115 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004116 psa_key_type_t key_type = key_type_arg;
4117 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004118 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004119 psa_status_t actual_status;
4120 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004121 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004122 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004124
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004125 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004126
Gilles Peskine8817f612018-12-18 00:18:46 +01004127 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004128
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004129 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004130 psa_set_key_algorithm( &attributes, alg );
4131 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004132
Gilles Peskine049c7532019-05-15 20:22:09 +02004133 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004134 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004135
Ronald Cron5425a212020-08-04 14:58:35 +02004136 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004137 input_data->x, input_data->len,
4138 signature, signature_size,
4139 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004140 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004141 /* The value of *signature_length is unspecified on error, but
4142 * whatever it is, it should be less than signature_size, so that
4143 * if the caller tries to read *signature_length bytes without
4144 * checking the error code then they don't overflow a buffer. */
4145 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004146
Gilles Peskine895242b2019-11-29 12:15:40 +01004147#if defined(MBEDTLS_TEST_DEPRECATED)
4148 signature_length = INVALID_EXPORT_LENGTH;
Ronald Cron5425a212020-08-04 14:58:35 +02004149 TEST_EQUAL( psa_asymmetric_sign( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004150 input_data->x, input_data->len,
4151 signature, signature_size,
4152 &signature_length ),
4153 expected_status );
4154 TEST_ASSERT( signature_length <= signature_size );
4155#endif /* MBEDTLS_TEST_DEPRECATED */
4156
Gilles Peskine20035e32018-02-03 22:44:14 +01004157exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004158 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004159 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01004160 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004161 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004162}
4163/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004164
4165/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004166void sign_verify( int key_type_arg, data_t *key_data,
4167 int alg_arg, data_t *input_data )
4168{
Ronald Cron5425a212020-08-04 14:58:35 +02004169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004170 psa_key_type_t key_type = key_type_arg;
4171 psa_algorithm_t alg = alg_arg;
4172 size_t key_bits;
4173 unsigned char *signature = NULL;
4174 size_t signature_size;
4175 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004177
Gilles Peskine8817f612018-12-18 00:18:46 +01004178 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004179
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004180 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004181 psa_set_key_algorithm( &attributes, alg );
4182 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004183
Gilles Peskine049c7532019-05-15 20:22:09 +02004184 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004185 &key ) );
4186 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004187 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004188
4189 /* Allocate a buffer which has the size advertized by the
4190 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004191 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004192 key_bits, alg );
4193 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004194 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004195 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004196
4197 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02004198 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004199 input_data->x, input_data->len,
4200 signature, signature_size,
4201 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004202 /* Check that the signature length looks sensible. */
4203 TEST_ASSERT( signature_length <= signature_size );
4204 TEST_ASSERT( signature_length > 0 );
4205
4206 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02004207 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004208 input_data->x, input_data->len,
4209 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004210
4211 if( input_data->len != 0 )
4212 {
4213 /* Flip a bit in the input and verify that the signature is now
4214 * detected as invalid. Flip a bit at the beginning, not at the end,
4215 * because ECDSA may ignore the last few bits of the input. */
4216 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02004217 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004218 input_data->x, input_data->len,
4219 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004220 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004221 }
4222
4223exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004224 /*
4225 * Key attributes may have been returned by psa_get_key_attributes()
4226 * thus reset them as required.
4227 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004228 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004229
Ronald Cron5425a212020-08-04 14:58:35 +02004230 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02004231 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004232 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004233}
4234/* END_CASE */
4235
4236/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004237void asymmetric_verify( int key_type_arg, data_t *key_data,
4238 int alg_arg, data_t *hash_data,
4239 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004240{
Ronald Cron5425a212020-08-04 14:58:35 +02004241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004242 psa_key_type_t key_type = key_type_arg;
4243 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004244 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004245
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004246 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004247
Gilles Peskine8817f612018-12-18 00:18:46 +01004248 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004249
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004251 psa_set_key_algorithm( &attributes, alg );
4252 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004253
Gilles Peskine049c7532019-05-15 20:22:09 +02004254 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004255 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03004256
Ronald Cron5425a212020-08-04 14:58:35 +02004257 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004258 hash_data->x, hash_data->len,
4259 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004260
4261#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004262 PSA_ASSERT( psa_asymmetric_verify( key, alg,
Gilles Peskine0627f982019-11-26 19:12:16 +01004263 hash_data->x, hash_data->len,
4264 signature_data->x,
4265 signature_data->len ) );
4266
4267#endif /* MBEDTLS_TEST_DEPRECATED */
4268
itayzafrir5c753392018-05-08 11:18:38 +03004269exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004270 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004271 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004272 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004273}
4274/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004275
4276/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004277void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4278 int alg_arg, data_t *hash_data,
4279 data_t *signature_data,
4280 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004281{
Ronald Cron5425a212020-08-04 14:58:35 +02004282 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004283 psa_key_type_t key_type = key_type_arg;
4284 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004285 psa_status_t actual_status;
4286 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004287 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004288
Gilles Peskine8817f612018-12-18 00:18:46 +01004289 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004290
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004291 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004292 psa_set_key_algorithm( &attributes, alg );
4293 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004294
Gilles Peskine049c7532019-05-15 20:22:09 +02004295 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004296 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004297
Ronald Cron5425a212020-08-04 14:58:35 +02004298 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004299 hash_data->x, hash_data->len,
4300 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004301 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004302
Gilles Peskine895242b2019-11-29 12:15:40 +01004303#if defined(MBEDTLS_TEST_DEPRECATED)
Ronald Cron5425a212020-08-04 14:58:35 +02004304 TEST_EQUAL( psa_asymmetric_verify( key, alg,
Gilles Peskine895242b2019-11-29 12:15:40 +01004305 hash_data->x, hash_data->len,
4306 signature_data->x, signature_data->len ),
4307 expected_status );
4308#endif /* MBEDTLS_TEST_DEPRECATED */
4309
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004310exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004311 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004312 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004313 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004314}
4315/* END_CASE */
4316
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004317/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004318void asymmetric_encrypt( int key_type_arg,
4319 data_t *key_data,
4320 int alg_arg,
4321 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004322 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004323 int expected_output_length_arg,
4324 int expected_status_arg )
4325{
Ronald Cron5425a212020-08-04 14:58:35 +02004326 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004327 psa_key_type_t key_type = key_type_arg;
4328 psa_algorithm_t alg = alg_arg;
4329 size_t expected_output_length = expected_output_length_arg;
4330 size_t key_bits;
4331 unsigned char *output = NULL;
4332 size_t output_size;
4333 size_t output_length = ~0;
4334 psa_status_t actual_status;
4335 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004336 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004337
Gilles Peskine8817f612018-12-18 00:18:46 +01004338 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004339
Gilles Peskine656896e2018-06-29 19:12:28 +02004340 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004341 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4342 psa_set_key_algorithm( &attributes, alg );
4343 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004344 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004345 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004346
4347 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02004348 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004349 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004350 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004351 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004352
4353 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02004354 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004355 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004356 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004357 output, output_size,
4358 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004359 TEST_EQUAL( actual_status, expected_status );
4360 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004361
Gilles Peskine68428122018-06-30 18:42:41 +02004362 /* If the label is empty, the test framework puts a non-null pointer
4363 * in label->x. Test that a null pointer works as well. */
4364 if( label->len == 0 )
4365 {
4366 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004367 if( output_size != 0 )
4368 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004369 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004370 input_data->x, input_data->len,
4371 NULL, label->len,
4372 output, output_size,
4373 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004374 TEST_EQUAL( actual_status, expected_status );
4375 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004376 }
4377
Gilles Peskine656896e2018-06-29 19:12:28 +02004378exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004379 /*
4380 * Key attributes may have been returned by psa_get_key_attributes()
4381 * thus reset them as required.
4382 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004383 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004384
Ronald Cron5425a212020-08-04 14:58:35 +02004385 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02004386 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004387 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004388}
4389/* END_CASE */
4390
4391/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004392void asymmetric_encrypt_decrypt( int key_type_arg,
4393 data_t *key_data,
4394 int alg_arg,
4395 data_t *input_data,
4396 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004397{
Ronald Cron5425a212020-08-04 14:58:35 +02004398 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004399 psa_key_type_t key_type = key_type_arg;
4400 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004401 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004402 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004403 size_t output_size;
4404 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004405 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004406 size_t output2_size;
4407 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004409
Gilles Peskine8817f612018-12-18 00:18:46 +01004410 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004411
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4413 psa_set_key_algorithm( &attributes, alg );
4414 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004415
Gilles Peskine049c7532019-05-15 20:22:09 +02004416 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004417 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004418
4419 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02004420 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004421 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004422 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004423 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004424 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004425 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004426
Gilles Peskineeebd7382018-06-08 18:11:54 +02004427 /* We test encryption by checking that encrypt-then-decrypt gives back
4428 * the original plaintext because of the non-optional random
4429 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02004430 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004431 input_data->x, input_data->len,
4432 label->x, label->len,
4433 output, output_size,
4434 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004435 /* We don't know what ciphertext length to expect, but check that
4436 * it looks sensible. */
4437 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004438
Ronald Cron5425a212020-08-04 14:58:35 +02004439 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004440 output, output_length,
4441 label->x, label->len,
4442 output2, output2_size,
4443 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004444 ASSERT_COMPARE( input_data->x, input_data->len,
4445 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004446
4447exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004448 /*
4449 * Key attributes may have been returned by psa_get_key_attributes()
4450 * thus reset them as required.
4451 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004452 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004453
Ronald Cron5425a212020-08-04 14:58:35 +02004454 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004455 mbedtls_free( output );
4456 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004457 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004458}
4459/* END_CASE */
4460
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004461/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004462void asymmetric_decrypt( int key_type_arg,
4463 data_t *key_data,
4464 int alg_arg,
4465 data_t *input_data,
4466 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004467 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004468{
Ronald Cron5425a212020-08-04 14:58:35 +02004469 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004470 psa_key_type_t key_type = key_type_arg;
4471 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004472 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004473 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004474 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004475 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004476
Jaeden Amero412654a2019-02-06 12:57:46 +00004477 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004478 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004479
Gilles Peskine8817f612018-12-18 00:18:46 +01004480 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004481
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004482 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4483 psa_set_key_algorithm( &attributes, alg );
4484 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004485
Gilles Peskine049c7532019-05-15 20:22:09 +02004486 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004487 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004488
Ronald Cron5425a212020-08-04 14:58:35 +02004489 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004490 input_data->x, input_data->len,
4491 label->x, label->len,
4492 output,
4493 output_size,
4494 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004495 ASSERT_COMPARE( expected_data->x, expected_data->len,
4496 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004497
Gilles Peskine68428122018-06-30 18:42:41 +02004498 /* If the label is empty, the test framework puts a non-null pointer
4499 * in label->x. Test that a null pointer works as well. */
4500 if( label->len == 0 )
4501 {
4502 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004503 if( output_size != 0 )
4504 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004505 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004506 input_data->x, input_data->len,
4507 NULL, label->len,
4508 output,
4509 output_size,
4510 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004511 ASSERT_COMPARE( expected_data->x, expected_data->len,
4512 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004513 }
4514
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004515exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004516 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004517 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004518 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004519 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004520}
4521/* END_CASE */
4522
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004523/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004524void asymmetric_decrypt_fail( int key_type_arg,
4525 data_t *key_data,
4526 int alg_arg,
4527 data_t *input_data,
4528 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004529 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004530 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004531{
Ronald Cron5425a212020-08-04 14:58:35 +02004532 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004533 psa_key_type_t key_type = key_type_arg;
4534 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004535 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004536 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004537 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004538 psa_status_t actual_status;
4539 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004541
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004542 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004543
Gilles Peskine8817f612018-12-18 00:18:46 +01004544 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004545
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004546 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4547 psa_set_key_algorithm( &attributes, alg );
4548 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004549
Gilles Peskine049c7532019-05-15 20:22:09 +02004550 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004551 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004552
Ronald Cron5425a212020-08-04 14:58:35 +02004553 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004554 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004555 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004556 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004557 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004558 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004559 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004560
Gilles Peskine68428122018-06-30 18:42:41 +02004561 /* If the label is empty, the test framework puts a non-null pointer
4562 * in label->x. Test that a null pointer works as well. */
4563 if( label->len == 0 )
4564 {
4565 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004566 if( output_size != 0 )
4567 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004568 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004569 input_data->x, input_data->len,
4570 NULL, label->len,
4571 output, output_size,
4572 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004573 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004574 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004575 }
4576
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004577exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004578 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004579 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004580 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004581 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004582}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004583/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004584
4585/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004586void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004587{
4588 /* Test each valid way of initializing the object, except for `= {0}`, as
4589 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4590 * though it's OK by the C standard. We could test for this, but we'd need
4591 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004592 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004593 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4594 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4595 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004596
4597 memset( &zero, 0, sizeof( zero ) );
4598
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004599 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004600 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004601 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004602 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004603 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004604 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004605 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004606
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004607 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004608 PSA_ASSERT( psa_key_derivation_abort(&func) );
4609 PSA_ASSERT( psa_key_derivation_abort(&init) );
4610 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004611}
4612/* END_CASE */
4613
Janos Follath16de4a42019-06-13 16:32:24 +01004614/* BEGIN_CASE */
4615void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004616{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004617 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004618 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004619 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004620
Gilles Peskine8817f612018-12-18 00:18:46 +01004621 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004622
Janos Follath16de4a42019-06-13 16:32:24 +01004623 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004624 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004625
4626exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004627 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004628 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004629}
4630/* END_CASE */
4631
Janos Follathaf3c2a02019-06-12 12:34:34 +01004632/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004633void derive_set_capacity( int alg_arg, int capacity_arg,
4634 int expected_status_arg )
4635{
4636 psa_algorithm_t alg = alg_arg;
4637 size_t capacity = capacity_arg;
4638 psa_status_t expected_status = expected_status_arg;
4639 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4640
4641 PSA_ASSERT( psa_crypto_init( ) );
4642
4643 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4644
4645 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4646 expected_status );
4647
4648exit:
4649 psa_key_derivation_abort( &operation );
4650 PSA_DONE( );
4651}
4652/* END_CASE */
4653
4654/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004655void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004656 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004657 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004658 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004659 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004660 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004661 int expected_status_arg3,
4662 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004663{
4664 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004665 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4666 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004667 psa_status_t expected_statuses[] = {expected_status_arg1,
4668 expected_status_arg2,
4669 expected_status_arg3};
4670 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004671 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4672 MBEDTLS_SVC_KEY_ID_INIT,
4673 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004674 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4676 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004677 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004678 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004679 psa_status_t expected_output_status = expected_output_status_arg;
4680 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004681
4682 PSA_ASSERT( psa_crypto_init( ) );
4683
4684 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4685 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004686
4687 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4688
4689 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4690 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004691 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004692 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004693 psa_set_key_type( &attributes, key_types[i] );
4694 PSA_ASSERT( psa_import_key( &attributes,
4695 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004696 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004697 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4698 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4699 {
4700 // When taking a private key as secret input, use key agreement
4701 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004702 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4703 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004704 expected_statuses[i] );
4705 }
4706 else
4707 {
4708 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004709 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004710 expected_statuses[i] );
4711 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004712 }
4713 else
4714 {
4715 TEST_EQUAL( psa_key_derivation_input_bytes(
4716 &operation, steps[i],
4717 inputs[i]->x, inputs[i]->len ),
4718 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004719 }
4720 }
4721
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004722 if( output_key_type != PSA_KEY_TYPE_NONE )
4723 {
4724 psa_reset_key_attributes( &attributes );
4725 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4726 psa_set_key_bits( &attributes, 8 );
4727 actual_output_status =
4728 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004729 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004730 }
4731 else
4732 {
4733 uint8_t buffer[1];
4734 actual_output_status =
4735 psa_key_derivation_output_bytes( &operation,
4736 buffer, sizeof( buffer ) );
4737 }
4738 TEST_EQUAL( actual_output_status, expected_output_status );
4739
Janos Follathaf3c2a02019-06-12 12:34:34 +01004740exit:
4741 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004742 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4743 psa_destroy_key( keys[i] );
4744 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004745 PSA_DONE( );
4746}
4747/* END_CASE */
4748
Janos Follathd958bb72019-07-03 15:02:16 +01004749/* BEGIN_CASE */
4750void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004751{
Janos Follathd958bb72019-07-03 15:02:16 +01004752 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004753 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004754 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004756 unsigned char input1[] = "Input 1";
4757 size_t input1_length = sizeof( input1 );
4758 unsigned char input2[] = "Input 2";
4759 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004760 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004761 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004762 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4763 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4764 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004765 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004766
Gilles Peskine8817f612018-12-18 00:18:46 +01004767 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004768
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004769 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4770 psa_set_key_algorithm( &attributes, alg );
4771 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004772
Gilles Peskine73676cb2019-05-15 20:15:10 +02004773 PSA_ASSERT( psa_import_key( &attributes,
4774 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004775 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004776
4777 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004778 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4779 input1, input1_length,
4780 input2, input2_length,
4781 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004782 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004783
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004784 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004785 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004786 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004787
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004788 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004789
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004790 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004791 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004792
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004793exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004794 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004795 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004796 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004797}
4798/* END_CASE */
4799
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004800/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004801void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004802{
4803 uint8_t output_buffer[16];
4804 size_t buffer_size = 16;
4805 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004807
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004808 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4809 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004810 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004811
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004812 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004813 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004814
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004816
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004817 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4818 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004819 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004820
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004822 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004823
4824exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004825 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004826}
4827/* END_CASE */
4828
4829/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004830void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004831 int step1_arg, data_t *input1,
4832 int step2_arg, data_t *input2,
4833 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004834 int requested_capacity_arg,
4835 data_t *expected_output1,
4836 data_t *expected_output2 )
4837{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004838 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004839 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4840 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004841 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4842 MBEDTLS_SVC_KEY_ID_INIT,
4843 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004844 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004846 uint8_t *expected_outputs[2] =
4847 {expected_output1->x, expected_output2->x};
4848 size_t output_sizes[2] =
4849 {expected_output1->len, expected_output2->len};
4850 size_t output_buffer_size = 0;
4851 uint8_t *output_buffer = NULL;
4852 size_t expected_capacity;
4853 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004854 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004855 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004856 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004857
4858 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4859 {
4860 if( output_sizes[i] > output_buffer_size )
4861 output_buffer_size = output_sizes[i];
4862 if( output_sizes[i] == 0 )
4863 expected_outputs[i] = NULL;
4864 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004865 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004866 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004867
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004868 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4869 psa_set_key_algorithm( &attributes, alg );
4870 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004871
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004872 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004873 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4874 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4875 requested_capacity ) );
4876 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004877 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004878 switch( steps[i] )
4879 {
4880 case 0:
4881 break;
4882 case PSA_KEY_DERIVATION_INPUT_SECRET:
4883 PSA_ASSERT( psa_import_key( &attributes,
4884 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004885 &keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004886 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004887 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004888 break;
4889 default:
4890 PSA_ASSERT( psa_key_derivation_input_bytes(
4891 &operation, steps[i],
4892 inputs[i]->x, inputs[i]->len ) );
4893 break;
4894 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004895 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004896
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004897 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004898 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004899 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004900 expected_capacity = requested_capacity;
4901
4902 /* Expansion phase. */
4903 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4904 {
4905 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004907 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004908 if( expected_capacity == 0 && output_sizes[i] == 0 )
4909 {
4910 /* Reading 0 bytes when 0 bytes are available can go either way. */
4911 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004912 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004913 continue;
4914 }
4915 else if( expected_capacity == 0 ||
4916 output_sizes[i] > expected_capacity )
4917 {
4918 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004919 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004920 expected_capacity = 0;
4921 continue;
4922 }
4923 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004924 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004925 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004926 ASSERT_COMPARE( output_buffer, output_sizes[i],
4927 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004929 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004930 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004931 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004932 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004933 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004934 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004935
4936exit:
4937 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004938 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004939 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4940 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004941 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004942}
4943/* END_CASE */
4944
4945/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004946void derive_full( int alg_arg,
4947 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004948 data_t *input1,
4949 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004950 int requested_capacity_arg )
4951{
Ronald Cron5425a212020-08-04 14:58:35 +02004952 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004953 psa_algorithm_t alg = alg_arg;
4954 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004956 unsigned char output_buffer[16];
4957 size_t expected_capacity = requested_capacity;
4958 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004960
Gilles Peskine8817f612018-12-18 00:18:46 +01004961 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004962
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4964 psa_set_key_algorithm( &attributes, alg );
4965 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004966
Gilles Peskine049c7532019-05-15 20:22:09 +02004967 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004968 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004969
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004970 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4971 input1->x, input1->len,
4972 input2->x, input2->len,
4973 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004974 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004975
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004977 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004978 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004979
4980 /* Expansion phase. */
4981 while( current_capacity > 0 )
4982 {
4983 size_t read_size = sizeof( output_buffer );
4984 if( read_size > current_capacity )
4985 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004987 output_buffer,
4988 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004989 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004990 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004991 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004992 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004993 }
4994
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004995 /* Check that the operation refuses to go over capacity. */
4996 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004997 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004998
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004999 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005000
5001exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005002 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005003 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005004 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005005}
5006/* END_CASE */
5007
Janos Follathe60c9052019-07-03 13:51:30 +01005008/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005009void derive_key_exercise( int alg_arg,
5010 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005011 data_t *input1,
5012 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005013 int derived_type_arg,
5014 int derived_bits_arg,
5015 int derived_usage_arg,
5016 int derived_alg_arg )
5017{
Ronald Cron5425a212020-08-04 14:58:35 +02005018 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5019 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005020 psa_algorithm_t alg = alg_arg;
5021 psa_key_type_t derived_type = derived_type_arg;
5022 size_t derived_bits = derived_bits_arg;
5023 psa_key_usage_t derived_usage = derived_usage_arg;
5024 psa_algorithm_t derived_alg = derived_alg_arg;
5025 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005026 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005028 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005029
Gilles Peskine8817f612018-12-18 00:18:46 +01005030 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005031
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005032 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5033 psa_set_key_algorithm( &attributes, alg );
5034 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005035 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005036 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005037
5038 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005039 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5040 input1->x, input1->len,
5041 input2->x, input2->len,
5042 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01005043 goto exit;
5044
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005045 psa_set_key_usage_flags( &attributes, derived_usage );
5046 psa_set_key_algorithm( &attributes, derived_alg );
5047 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005048 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005049 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005050 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005051
5052 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005053 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005054 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5055 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005056
5057 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005058 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005059 goto exit;
5060
5061exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005062 /*
5063 * Key attributes may have been returned by psa_get_key_attributes()
5064 * thus reset them as required.
5065 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005066 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005067
5068 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005069 psa_destroy_key( base_key );
5070 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005071 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005072}
5073/* END_CASE */
5074
Janos Follath42fd8882019-07-03 14:17:09 +01005075/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005076void derive_key_export( int alg_arg,
5077 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005078 data_t *input1,
5079 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005080 int bytes1_arg,
5081 int bytes2_arg )
5082{
Ronald Cron5425a212020-08-04 14:58:35 +02005083 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5084 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005085 psa_algorithm_t alg = alg_arg;
5086 size_t bytes1 = bytes1_arg;
5087 size_t bytes2 = bytes2_arg;
5088 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005089 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005090 uint8_t *output_buffer = NULL;
5091 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005092 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5093 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005094 size_t length;
5095
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005096 ASSERT_ALLOC( output_buffer, capacity );
5097 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005099
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005100 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5101 psa_set_key_algorithm( &base_attributes, alg );
5102 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005103 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005104 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005105
5106 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005107 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5108 input1->x, input1->len,
5109 input2->x, input2->len,
5110 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005111 goto exit;
5112
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005113 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005114 output_buffer,
5115 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005117
5118 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005119 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5120 input1->x, input1->len,
5121 input2->x, input2->len,
5122 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01005123 goto exit;
5124
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005125 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5126 psa_set_key_algorithm( &derived_attributes, 0 );
5127 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005128 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005129 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005130 &derived_key ) );
5131 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005132 export_buffer, bytes1,
5133 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005134 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02005135 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005136 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005137 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005138 &derived_key ) );
5139 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01005140 export_buffer + bytes1, bytes2,
5141 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005142 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005143
5144 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005145 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5146 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005147
5148exit:
5149 mbedtls_free( output_buffer );
5150 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005152 psa_destroy_key( base_key );
5153 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005154 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005155}
5156/* END_CASE */
5157
5158/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005159void derive_key( int alg_arg,
5160 data_t *key_data, data_t *input1, data_t *input2,
5161 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005162 int expected_status_arg,
5163 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02005164{
Ronald Cron5425a212020-08-04 14:58:35 +02005165 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
5166 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02005167 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005168 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005169 size_t bits = bits_arg;
5170 psa_status_t expected_status = expected_status_arg;
5171 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5172 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5173 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5174
5175 PSA_ASSERT( psa_crypto_init( ) );
5176
5177 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5178 psa_set_key_algorithm( &base_attributes, alg );
5179 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5180 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005181 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02005182
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005183 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
5184 input1->x, input1->len,
5185 input2->x, input2->len,
5186 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02005187 goto exit;
5188
5189 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5190 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005191 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005192 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01005193
5194 psa_status_t status =
5195 psa_key_derivation_output_key( &derived_attributes,
5196 &operation,
5197 &derived_key );
5198 if( is_large_output > 0 )
5199 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5200 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02005201
5202exit:
5203 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005204 psa_destroy_key( base_key );
5205 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02005206 PSA_DONE( );
5207}
5208/* END_CASE */
5209
5210/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005211void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02005212 int our_key_type_arg, int our_key_alg_arg,
5213 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005214 int expected_status_arg )
5215{
Ronald Cron5425a212020-08-04 14:58:35 +02005216 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005217 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005218 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005219 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005220 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005222 psa_status_t expected_status = expected_status_arg;
5223 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005224
Gilles Peskine8817f612018-12-18 00:18:46 +01005225 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005226
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005227 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005228 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005229 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005230 PSA_ASSERT( psa_import_key( &attributes,
5231 our_key_data->x, our_key_data->len,
5232 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005233
Gilles Peskine77f40d82019-04-11 21:27:06 +02005234 /* The tests currently include inputs that should fail at either step.
5235 * Test cases that fail at the setup step should be changed to call
5236 * key_derivation_setup instead, and this function should be renamed
5237 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005238 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005239 if( status == PSA_SUCCESS )
5240 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005241 TEST_EQUAL( psa_key_derivation_key_agreement(
5242 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5243 our_key,
5244 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005245 expected_status );
5246 }
5247 else
5248 {
5249 TEST_ASSERT( status == expected_status );
5250 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005251
5252exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005253 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005254 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005255 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005256}
5257/* END_CASE */
5258
5259/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005260void raw_key_agreement( int alg_arg,
5261 int our_key_type_arg, data_t *our_key_data,
5262 data_t *peer_key_data,
5263 data_t *expected_output )
5264{
Ronald Cron5425a212020-08-04 14:58:35 +02005265 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005266 psa_algorithm_t alg = alg_arg;
5267 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005269 unsigned char *output = NULL;
5270 size_t output_length = ~0;
5271
5272 ASSERT_ALLOC( output, expected_output->len );
5273 PSA_ASSERT( psa_crypto_init( ) );
5274
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005275 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5276 psa_set_key_algorithm( &attributes, alg );
5277 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005278 PSA_ASSERT( psa_import_key( &attributes,
5279 our_key_data->x, our_key_data->len,
5280 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005281
Gilles Peskinebe697d82019-05-16 18:00:41 +02005282 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5283 peer_key_data->x, peer_key_data->len,
5284 output, expected_output->len,
5285 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005286 ASSERT_COMPARE( output, output_length,
5287 expected_output->x, expected_output->len );
5288
5289exit:
5290 mbedtls_free( output );
5291 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005292 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005293}
5294/* END_CASE */
5295
5296/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005297void key_agreement_capacity( int alg_arg,
5298 int our_key_type_arg, data_t *our_key_data,
5299 data_t *peer_key_data,
5300 int expected_capacity_arg )
5301{
Ronald Cron5425a212020-08-04 14:58:35 +02005302 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005303 psa_algorithm_t alg = alg_arg;
5304 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005305 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005307 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005308 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005309
Gilles Peskine8817f612018-12-18 00:18:46 +01005310 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005311
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005312 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5313 psa_set_key_algorithm( &attributes, alg );
5314 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005315 PSA_ASSERT( psa_import_key( &attributes,
5316 our_key_data->x, our_key_data->len,
5317 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005318
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005319 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005320 PSA_ASSERT( psa_key_derivation_key_agreement(
5321 &operation,
5322 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5323 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005324 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5325 {
5326 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005327 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005328 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005329 NULL, 0 ) );
5330 }
Gilles Peskine59685592018-09-18 12:11:34 +02005331
Gilles Peskinebf491972018-10-25 22:36:12 +02005332 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005333 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005335 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005336
Gilles Peskinebf491972018-10-25 22:36:12 +02005337 /* Test the actual capacity by reading the output. */
5338 while( actual_capacity > sizeof( output ) )
5339 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005340 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005341 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005342 actual_capacity -= sizeof( output );
5343 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005345 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005346 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005347 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005348
Gilles Peskine59685592018-09-18 12:11:34 +02005349exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005351 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005352 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005353}
5354/* END_CASE */
5355
5356/* BEGIN_CASE */
5357void key_agreement_output( int alg_arg,
5358 int our_key_type_arg, data_t *our_key_data,
5359 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005360 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005361{
Ronald Cron5425a212020-08-04 14:58:35 +02005362 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005363 psa_algorithm_t alg = alg_arg;
5364 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005365 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005366 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005367 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005368
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005369 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5370 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005371
Gilles Peskine8817f612018-12-18 00:18:46 +01005372 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005373
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005374 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5375 psa_set_key_algorithm( &attributes, alg );
5376 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005377 PSA_ASSERT( psa_import_key( &attributes,
5378 our_key_data->x, our_key_data->len,
5379 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005380
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005381 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005382 PSA_ASSERT( psa_key_derivation_key_agreement(
5383 &operation,
5384 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5385 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005386 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5387 {
5388 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005389 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005390 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005391 NULL, 0 ) );
5392 }
Gilles Peskine59685592018-09-18 12:11:34 +02005393
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005394 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005395 actual_output,
5396 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005397 ASSERT_COMPARE( actual_output, expected_output1->len,
5398 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005399 if( expected_output2->len != 0 )
5400 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005401 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005402 actual_output,
5403 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005404 ASSERT_COMPARE( actual_output, expected_output2->len,
5405 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005406 }
Gilles Peskine59685592018-09-18 12:11:34 +02005407
5408exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005410 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005411 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005412 mbedtls_free( actual_output );
5413}
5414/* END_CASE */
5415
5416/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005417void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005418{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005419 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005420 unsigned char *output = NULL;
5421 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005422 size_t i;
5423 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005424
Simon Butcher49f8e312020-03-03 15:51:50 +00005425 TEST_ASSERT( bytes_arg >= 0 );
5426
Gilles Peskine91892022021-02-08 19:50:26 +01005427 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005428 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02005429
Gilles Peskine8817f612018-12-18 00:18:46 +01005430 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005431
Gilles Peskinea50d7392018-06-21 10:22:13 +02005432 /* Run several times, to ensure that every output byte will be
5433 * nonzero at least once with overwhelming probability
5434 * (2^(-8*number_of_runs)). */
5435 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005436 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005437 if( bytes != 0 )
5438 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005439 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005440
Gilles Peskinea50d7392018-06-21 10:22:13 +02005441 for( i = 0; i < bytes; i++ )
5442 {
5443 if( output[i] != 0 )
5444 ++changed[i];
5445 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005446 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005447
5448 /* Check that every byte was changed to nonzero at least once. This
5449 * validates that psa_generate_random is overwriting every byte of
5450 * the output buffer. */
5451 for( i = 0; i < bytes; i++ )
5452 {
5453 TEST_ASSERT( changed[i] != 0 );
5454 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005455
5456exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005457 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005458 mbedtls_free( output );
5459 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005460}
5461/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005462
5463/* BEGIN_CASE */
5464void generate_key( int type_arg,
5465 int bits_arg,
5466 int usage_arg,
5467 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01005468 int expected_status_arg,
5469 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005470{
Ronald Cron5425a212020-08-04 14:58:35 +02005471 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005472 psa_key_type_t type = type_arg;
5473 psa_key_usage_t usage = usage_arg;
5474 size_t bits = bits_arg;
5475 psa_algorithm_t alg = alg_arg;
5476 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005478 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005479
Gilles Peskine8817f612018-12-18 00:18:46 +01005480 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005481
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005482 psa_set_key_usage_flags( &attributes, usage );
5483 psa_set_key_algorithm( &attributes, alg );
5484 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005485 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005486
5487 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01005488 psa_status_t status = psa_generate_key( &attributes, &key );
5489
5490 if( is_large_key > 0 )
5491 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
5492 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005493 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005494 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005495
5496 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005497 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005498 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5499 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005500
Gilles Peskine818ca122018-06-20 18:16:48 +02005501 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005502 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005503 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005504
5505exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005506 /*
5507 * Key attributes may have been returned by psa_get_key_attributes()
5508 * thus reset them as required.
5509 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005510 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005511
Ronald Cron5425a212020-08-04 14:58:35 +02005512 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005513 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005514}
5515/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005516
Gilles Peskinee56e8782019-04-26 17:34:02 +02005517/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5518void generate_key_rsa( int bits_arg,
5519 data_t *e_arg,
5520 int expected_status_arg )
5521{
Ronald Cron5425a212020-08-04 14:58:35 +02005522 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005523 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005524 size_t bits = bits_arg;
5525 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5526 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5527 psa_status_t expected_status = expected_status_arg;
5528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5529 uint8_t *exported = NULL;
5530 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005531 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005532 size_t exported_length = SIZE_MAX;
5533 uint8_t *e_read_buffer = NULL;
5534 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005535 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005536 size_t e_read_length = SIZE_MAX;
5537
5538 if( e_arg->len == 0 ||
5539 ( e_arg->len == 3 &&
5540 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5541 {
5542 is_default_public_exponent = 1;
5543 e_read_size = 0;
5544 }
5545 ASSERT_ALLOC( e_read_buffer, e_read_size );
5546 ASSERT_ALLOC( exported, exported_size );
5547
5548 PSA_ASSERT( psa_crypto_init( ) );
5549
5550 psa_set_key_usage_flags( &attributes, usage );
5551 psa_set_key_algorithm( &attributes, alg );
5552 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5553 e_arg->x, e_arg->len ) );
5554 psa_set_key_bits( &attributes, bits );
5555
5556 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005557 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005558 if( expected_status != PSA_SUCCESS )
5559 goto exit;
5560
5561 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005562 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005563 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5564 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5565 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5566 e_read_buffer, e_read_size,
5567 &e_read_length ) );
5568 if( is_default_public_exponent )
5569 TEST_EQUAL( e_read_length, 0 );
5570 else
5571 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5572
5573 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005574 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005575 goto exit;
5576
5577 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005578 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005579 exported, exported_size,
5580 &exported_length ) );
5581 {
5582 uint8_t *p = exported;
5583 uint8_t *end = exported + exported_length;
5584 size_t len;
5585 /* RSAPublicKey ::= SEQUENCE {
5586 * modulus INTEGER, -- n
5587 * publicExponent INTEGER } -- e
5588 */
5589 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005590 MBEDTLS_ASN1_SEQUENCE |
5591 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005592 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005593 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5594 MBEDTLS_ASN1_INTEGER ) );
5595 if( len >= 1 && p[0] == 0 )
5596 {
5597 ++p;
5598 --len;
5599 }
5600 if( e_arg->len == 0 )
5601 {
5602 TEST_EQUAL( len, 3 );
5603 TEST_EQUAL( p[0], 1 );
5604 TEST_EQUAL( p[1], 0 );
5605 TEST_EQUAL( p[2], 1 );
5606 }
5607 else
5608 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5609 }
5610
5611exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005612 /*
5613 * Key attributes may have been returned by psa_get_key_attributes() or
5614 * set by psa_set_key_domain_parameters() thus reset them as required.
5615 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005616 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005617
Ronald Cron5425a212020-08-04 14:58:35 +02005618 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005619 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005620 mbedtls_free( e_read_buffer );
5621 mbedtls_free( exported );
5622}
5623/* END_CASE */
5624
Darryl Greend49a4992018-06-18 17:27:26 +01005625/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005626void persistent_key_load_key_from_storage( data_t *data,
5627 int type_arg, int bits_arg,
5628 int usage_flags_arg, int alg_arg,
5629 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005630{
Ronald Cron71016a92020-08-28 19:01:50 +02005631 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005632 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005633 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5634 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005635 psa_key_type_t type = type_arg;
5636 size_t bits = bits_arg;
5637 psa_key_usage_t usage_flags = usage_flags_arg;
5638 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005639 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005640 unsigned char *first_export = NULL;
5641 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005642 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005643 size_t first_exported_length;
5644 size_t second_exported_length;
5645
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005646 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5647 {
5648 ASSERT_ALLOC( first_export, export_size );
5649 ASSERT_ALLOC( second_export, export_size );
5650 }
Darryl Greend49a4992018-06-18 17:27:26 +01005651
Gilles Peskine8817f612018-12-18 00:18:46 +01005652 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005653
Gilles Peskinec87af662019-05-15 16:12:22 +02005654 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005655 psa_set_key_usage_flags( &attributes, usage_flags );
5656 psa_set_key_algorithm( &attributes, alg );
5657 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005658 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005659
Darryl Green0c6575a2018-11-07 16:05:30 +00005660 switch( generation_method )
5661 {
5662 case IMPORT_KEY:
5663 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005664 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005665 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005666 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005667
Darryl Green0c6575a2018-11-07 16:05:30 +00005668 case GENERATE_KEY:
5669 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005670 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005671 break;
5672
5673 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005674#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005675 {
5676 /* Create base key */
5677 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5678 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5679 psa_set_key_usage_flags( &base_attributes,
5680 PSA_KEY_USAGE_DERIVE );
5681 psa_set_key_algorithm( &base_attributes, derive_alg );
5682 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005683 PSA_ASSERT( psa_import_key( &base_attributes,
5684 data->x, data->len,
5685 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005686 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005687 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005688 PSA_ASSERT( psa_key_derivation_input_key(
5689 &operation,
5690 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005691 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005692 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005693 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005694 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5695 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005696 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005697 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005698 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005699 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005700 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005701#else
5702 TEST_ASSUME( ! "KDF not supported in this configuration" );
5703#endif
5704 break;
5705
5706 default:
5707 TEST_ASSERT( ! "generation_method not implemented in test" );
5708 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005709 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005710 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005711
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005712 /* Export the key if permitted by the key policy. */
5713 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5714 {
Ronald Cron5425a212020-08-04 14:58:35 +02005715 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005716 first_export, export_size,
5717 &first_exported_length ) );
5718 if( generation_method == IMPORT_KEY )
5719 ASSERT_COMPARE( data->x, data->len,
5720 first_export, first_exported_length );
5721 }
Darryl Greend49a4992018-06-18 17:27:26 +01005722
5723 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005724 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005725 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005726 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005727
Darryl Greend49a4992018-06-18 17:27:26 +01005728 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005729 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005730 TEST_ASSERT( mbedtls_svc_key_id_equal(
5731 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005732 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5733 PSA_KEY_LIFETIME_PERSISTENT );
5734 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5735 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5736 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5737 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005738
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005739 /* Export the key again if permitted by the key policy. */
5740 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005741 {
Ronald Cron5425a212020-08-04 14:58:35 +02005742 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005743 second_export, export_size,
5744 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005745 ASSERT_COMPARE( first_export, first_exported_length,
5746 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005747 }
5748
5749 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005750 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005751 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005752
5753exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005754 /*
5755 * Key attributes may have been returned by psa_get_key_attributes()
5756 * thus reset them as required.
5757 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005758 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005759
Darryl Greend49a4992018-06-18 17:27:26 +01005760 mbedtls_free( first_export );
5761 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005762 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005763 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005764 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005765 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005766}
5767/* END_CASE */