blob: c9c45b7e146d445dcb5ab5a00fc6576810d4612c [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 Peskine1838e822019-06-20 12:40:56 +020012#include "psa_crypto_helpers.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskinec744d992019-07-30 17:26:54 +020014/* Tests that require more than 128kB of RAM plus change have this symbol
15 * as a dependency. Currently we always define this symbol, so the tests
16 * are always executed. In the future we should make this conditional
17 * so that tests that require a lot of memory are skipped on constrained
18 * platforms. */
Gilles Peskine49232e82019-08-07 11:01:30 +020019#define HAVE_RAM_AVAILABLE_128K
Gilles Peskinec744d992019-07-30 17:26:54 +020020
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020021#include "psa/crypto.h"
22
Jaeden Amerof24c7f82018-06-27 17:20:43 +010023/** An invalid export length that will never be set by psa_export_key(). */
24static const size_t INVALID_EXPORT_LENGTH = ~0U;
25
Gilles Peskinef426e0f2019-02-25 17:42:03 +010026/* A hash algorithm that is known to be supported.
27 *
28 * This is used in some smoke tests.
29 */
30#if defined(MBEDTLS_MD2_C)
31#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
32#elif defined(MBEDTLS_MD4_C)
33#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
34#elif defined(MBEDTLS_MD5_C)
35#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
36/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
37 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
38 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
39 * implausible anyway. */
40#elif defined(MBEDTLS_SHA1_C)
41#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
42#elif defined(MBEDTLS_SHA256_C)
43#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
44#elif defined(MBEDTLS_SHA512_C)
45#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
46#elif defined(MBEDTLS_SHA3_C)
47#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
48#else
49#undef KNOWN_SUPPORTED_HASH_ALG
50#endif
51
52/* A block cipher that is known to be supported.
53 *
54 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
55 */
56#if defined(MBEDTLS_AES_C)
57#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
58#elif defined(MBEDTLS_ARIA_C)
59#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
60#elif defined(MBEDTLS_CAMELLIA_C)
61#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
62#undef KNOWN_SUPPORTED_BLOCK_CIPHER
63#endif
64
65/* A MAC mode that is known to be supported.
66 *
67 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
68 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
69 *
70 * This is used in some smoke tests.
71 */
72#if defined(KNOWN_SUPPORTED_HASH_ALG)
73#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
74#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
75#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
76#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
77#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
78#else
79#undef KNOWN_SUPPORTED_MAC_ALG
80#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
81#endif
82
83/* A cipher algorithm and key type that are known to be supported.
84 *
85 * This is used in some smoke tests.
86 */
87#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
89#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
90#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
91#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
92#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
93#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
94#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
95#else
96#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
97#endif
98#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
99#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
100#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
101#elif defined(MBEDTLS_RC4_C)
102#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
103#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
104#else
105#undef KNOWN_SUPPORTED_CIPHER_ALG
106#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
107#endif
108
Gilles Peskine667c1112019-12-03 19:03:20 +0100109#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
110int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
111{
112 /* At the moment, anything that isn't a built-in lifetime is either
113 * a secure element or unassigned. */
114 return( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
115 lifetime != PSA_KEY_LIFETIME_PERSISTENT );
116}
117#else
118int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
119{
120 (void) lifetime;
121 return( 0 );
122}
123#endif
124
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200125/** Test if a buffer contains a constant byte value.
126 *
127 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200128 *
129 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200130 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200131 * \param size Size of the buffer in bytes.
132 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200133 * \return 1 if the buffer is all-bits-zero.
134 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200135 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200136static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200137{
138 size_t i;
139 for( i = 0; i < size; i++ )
140 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200141 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200142 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200143 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200144 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200145}
Gilles Peskine818ca122018-06-20 18:16:48 +0200146
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200147/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
148static int asn1_write_10x( unsigned char **p,
149 unsigned char *start,
150 size_t bits,
151 unsigned char x )
152{
153 int ret;
154 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200155 if( bits == 0 )
156 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
157 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200158 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300159 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200160 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
161 *p -= len;
162 ( *p )[len-1] = x;
163 if( bits % 8 == 0 )
164 ( *p )[1] |= 1;
165 else
166 ( *p )[0] |= 1 << ( bits % 8 );
167 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
168 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
169 MBEDTLS_ASN1_INTEGER ) );
170 return( len );
171}
172
173static int construct_fake_rsa_key( unsigned char *buffer,
174 size_t buffer_size,
175 unsigned char **p,
176 size_t bits,
177 int keypair )
178{
179 size_t half_bits = ( bits + 1 ) / 2;
180 int ret;
181 int len = 0;
182 /* Construct something that looks like a DER encoding of
183 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
184 * RSAPrivateKey ::= SEQUENCE {
185 * version Version,
186 * modulus INTEGER, -- n
187 * publicExponent INTEGER, -- e
188 * privateExponent INTEGER, -- d
189 * prime1 INTEGER, -- p
190 * prime2 INTEGER, -- q
191 * exponent1 INTEGER, -- d mod (p-1)
192 * exponent2 INTEGER, -- d mod (q-1)
193 * coefficient INTEGER, -- (inverse of q) mod p
194 * otherPrimeInfos OtherPrimeInfos OPTIONAL
195 * }
196 * Or, for a public key, the same structure with only
197 * version, modulus and publicExponent.
198 */
199 *p = buffer + buffer_size;
200 if( keypair )
201 {
202 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
203 asn1_write_10x( p, buffer, half_bits, 1 ) );
204 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
205 asn1_write_10x( p, buffer, half_bits, 1 ) );
206 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
207 asn1_write_10x( p, buffer, half_bits, 1 ) );
208 MBEDTLS_ASN1_CHK_ADD( len, /* q */
209 asn1_write_10x( p, buffer, half_bits, 1 ) );
210 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
211 asn1_write_10x( p, buffer, half_bits, 3 ) );
212 MBEDTLS_ASN1_CHK_ADD( len, /* d */
213 asn1_write_10x( p, buffer, bits, 1 ) );
214 }
215 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
216 asn1_write_10x( p, buffer, 17, 1 ) );
217 MBEDTLS_ASN1_CHK_ADD( len, /* n */
218 asn1_write_10x( p, buffer, bits, 1 ) );
219 if( keypair )
220 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
221 mbedtls_asn1_write_int( p, buffer, 0 ) );
222 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
223 {
224 const unsigned char tag =
225 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
226 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
227 }
228 return( len );
229}
230
Gilles Peskine667c1112019-12-03 19:03:20 +0100231int check_key_attributes_sanity( psa_key_handle_t key )
232{
233 int ok = 0;
234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
235 psa_key_lifetime_t lifetime;
236 psa_key_id_t id;
237 psa_key_type_t type;
238 psa_key_type_t bits;
239
240 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
241 lifetime = psa_get_key_lifetime( &attributes );
242 id = psa_get_key_id( &attributes );
243 type = psa_get_key_type( &attributes );
244 bits = psa_get_key_bits( &attributes );
245
246 /* Persistence */
247 if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
248 TEST_ASSERT( id == 0 );
249 else
250 {
251 TEST_ASSERT(
252 ( PSA_KEY_ID_USER_MIN <= id && id <= PSA_KEY_ID_USER_MAX ) ||
253 ( PSA_KEY_ID_USER_MIN <= id && id <= PSA_KEY_ID_USER_MAX ) );
254 }
255#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
256 /* randomly-generated 64-bit constant, should never appear in test data */
257 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
258 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
259 if( lifetime_is_secure_element( lifetime ) )
260 {
261 /* Mbed Crypto currently always exposes the slot number to
262 * applications. This is not mandated by the PSA specification
263 * and may change in future versions. */
264 TEST_EQUAL( status, 0 );
265 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
266 }
267 else
268 {
269 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
270 }
271#endif
272
273 /* Type and size */
274 TEST_ASSERT( type != 0 );
275 TEST_ASSERT( bits != 0 );
276 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
277 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
278 TEST_ASSERT( bits % 8 == 0 );
279
280 /* MAX macros concerning specific key types */
281 if( PSA_KEY_TYPE_IS_ECC( type ) )
282 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
283 else if( PSA_KEY_TYPE_IS_RSA( type ) )
284 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
285 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) <= PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE );
286
287 ok = 1;
288
289exit:
290 psa_reset_key_attributes( &attributes );
291 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{
301 psa_key_handle_t handle = 0;
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 );
Gilles Peskine049c7532019-05-15 20:22:09 +0200307 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
308 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100309
310 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100311 /* Whether setup succeeded or failed, abort must succeed. */
312 PSA_ASSERT( psa_mac_abort( operation ) );
313 /* If setup failed, reproduce the failure, so that the caller can
314 * test the resulting state of the operation object. */
315 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100316 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100317 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
318 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100319 }
320
321 psa_destroy_key( handle );
322 return( 1 );
323
324exit:
325 psa_destroy_key( handle );
326 return( 0 );
327}
328
329int exercise_cipher_setup( psa_key_type_t key_type,
330 const unsigned char *key_bytes,
331 size_t key_length,
332 psa_algorithm_t alg,
333 psa_cipher_operation_t *operation,
334 psa_status_t *status )
335{
336 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100338
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200339 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
340 psa_set_key_algorithm( &attributes, alg );
341 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200342 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
343 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100344
345 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100346 /* Whether setup succeeded or failed, abort must succeed. */
347 PSA_ASSERT( psa_cipher_abort( operation ) );
348 /* If setup failed, reproduce the failure, so that the caller can
349 * test the resulting state of the operation object. */
350 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100351 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100352 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
353 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100354 }
355
356 psa_destroy_key( handle );
357 return( 1 );
358
359exit:
360 psa_destroy_key( handle );
361 return( 0 );
362}
363
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100364static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200365 psa_key_usage_t usage,
366 psa_algorithm_t alg )
367{
Jaeden Amero769ce272019-01-04 11:48:03 +0000368 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200369 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200370 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200371 size_t mac_length = sizeof( mac );
372
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100373 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100375 PSA_ASSERT( psa_mac_sign_setup( &operation,
376 handle, alg ) );
377 PSA_ASSERT( psa_mac_update( &operation,
378 input, sizeof( input ) ) );
379 PSA_ASSERT( psa_mac_sign_finish( &operation,
380 mac, sizeof( mac ),
381 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200382 }
383
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100384 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200385 {
386 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100387 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200388 PSA_SUCCESS :
389 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100390 PSA_ASSERT( psa_mac_verify_setup( &operation,
391 handle, alg ) );
392 PSA_ASSERT( psa_mac_update( &operation,
393 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100394 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
395 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200396 }
397
398 return( 1 );
399
400exit:
401 psa_mac_abort( &operation );
402 return( 0 );
403}
404
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100405static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200406 psa_key_usage_t usage,
407 psa_algorithm_t alg )
408{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000409 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200410 unsigned char iv[16] = {0};
411 size_t iv_length = sizeof( iv );
412 const unsigned char plaintext[16] = "Hello, world...";
413 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
414 size_t ciphertext_length = sizeof( ciphertext );
415 unsigned char decrypted[sizeof( ciphertext )];
416 size_t part_length;
417
418 if( usage & PSA_KEY_USAGE_ENCRYPT )
419 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100420 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
421 handle, alg ) );
422 PSA_ASSERT( psa_cipher_generate_iv( &operation,
423 iv, sizeof( iv ),
424 &iv_length ) );
425 PSA_ASSERT( psa_cipher_update( &operation,
426 plaintext, sizeof( plaintext ),
427 ciphertext, sizeof( ciphertext ),
428 &ciphertext_length ) );
429 PSA_ASSERT( psa_cipher_finish( &operation,
430 ciphertext + ciphertext_length,
431 sizeof( ciphertext ) - ciphertext_length,
432 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200433 ciphertext_length += part_length;
434 }
435
436 if( usage & PSA_KEY_USAGE_DECRYPT )
437 {
438 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200439 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200440 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
441 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
443 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
444 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
445 * have this macro yet. */
446 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
447 psa_get_key_type( &attributes ) );
448 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200449 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100450 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
451 handle, alg ) );
452 PSA_ASSERT( psa_cipher_set_iv( &operation,
453 iv, iv_length ) );
454 PSA_ASSERT( psa_cipher_update( &operation,
455 ciphertext, ciphertext_length,
456 decrypted, sizeof( decrypted ),
457 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200458 status = psa_cipher_finish( &operation,
459 decrypted + part_length,
460 sizeof( decrypted ) - part_length,
461 &part_length );
462 /* For a stream cipher, all inputs are valid. For a block cipher,
463 * if the input is some aribtrary data rather than an actual
464 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200465 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200466 TEST_ASSERT( status == PSA_SUCCESS ||
467 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200468 else
469 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200470 }
471
472 return( 1 );
473
474exit:
475 psa_cipher_abort( &operation );
476 return( 0 );
477}
478
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100479static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200480 psa_key_usage_t usage,
481 psa_algorithm_t alg )
482{
483 unsigned char nonce[16] = {0};
484 size_t nonce_length = sizeof( nonce );
485 unsigned char plaintext[16] = "Hello, world...";
486 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
487 size_t ciphertext_length = sizeof( ciphertext );
488 size_t plaintext_length = sizeof( ciphertext );
489
490 if( usage & PSA_KEY_USAGE_ENCRYPT )
491 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100492 PSA_ASSERT( psa_aead_encrypt( handle, alg,
493 nonce, nonce_length,
494 NULL, 0,
495 plaintext, sizeof( plaintext ),
496 ciphertext, sizeof( ciphertext ),
497 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200498 }
499
500 if( usage & PSA_KEY_USAGE_DECRYPT )
501 {
502 psa_status_t verify_status =
503 ( usage & PSA_KEY_USAGE_ENCRYPT ?
504 PSA_SUCCESS :
505 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100506 TEST_EQUAL( psa_aead_decrypt( handle, alg,
507 nonce, nonce_length,
508 NULL, 0,
509 ciphertext, ciphertext_length,
510 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100511 &plaintext_length ),
512 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200513 }
514
515 return( 1 );
516
517exit:
518 return( 0 );
519}
520
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100521static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200522 psa_key_usage_t usage,
523 psa_algorithm_t alg )
524{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200525 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
526 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100527 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200528 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100529 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
530
531 /* If the policy allows signing with any hash, just pick one. */
532 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
533 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100534#if defined(KNOWN_SUPPORTED_HASH_ALG)
535 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
536 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100537#else
538 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100539 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100540#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100541 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200542
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100543 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200544 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200545 /* Some algorithms require the payload to have the size of
546 * the hash encoded in the algorithm. Use this input size
547 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200548 if( hash_alg != 0 )
549 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100550 PSA_ASSERT( psa_sign_hash( handle, alg,
551 payload, payload_length,
552 signature, sizeof( signature ),
553 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200554 }
555
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100556 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200557 {
558 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100559 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200560 PSA_SUCCESS :
561 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100562 TEST_EQUAL( psa_verify_hash( handle, alg,
563 payload, payload_length,
564 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100565 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200566 }
567
568 return( 1 );
569
570exit:
571 return( 0 );
572}
573
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100574static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200575 psa_key_usage_t usage,
576 psa_algorithm_t alg )
577{
578 unsigned char plaintext[256] = "Hello, world...";
579 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
580 size_t ciphertext_length = sizeof( ciphertext );
581 size_t plaintext_length = 16;
582
583 if( usage & PSA_KEY_USAGE_ENCRYPT )
584 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100585 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
586 plaintext, plaintext_length,
587 NULL, 0,
588 ciphertext, sizeof( ciphertext ),
589 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200590 }
591
592 if( usage & PSA_KEY_USAGE_DECRYPT )
593 {
594 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100595 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200596 ciphertext, ciphertext_length,
597 NULL, 0,
598 plaintext, sizeof( plaintext ),
599 &plaintext_length );
600 TEST_ASSERT( status == PSA_SUCCESS ||
601 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
602 ( status == PSA_ERROR_INVALID_ARGUMENT ||
603 status == PSA_ERROR_INVALID_PADDING ) ) );
604 }
605
606 return( 1 );
607
608exit:
609 return( 0 );
610}
Gilles Peskine02b75072018-07-01 22:31:34 +0200611
Janos Follathf2815ea2019-07-03 12:41:36 +0100612static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
613 psa_key_handle_t handle,
614 psa_algorithm_t alg,
615 unsigned char* input1, size_t input1_length,
616 unsigned char* input2, size_t input2_length,
617 size_t capacity )
618{
619 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
620 if( PSA_ALG_IS_HKDF( alg ) )
621 {
622 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
623 PSA_KEY_DERIVATION_INPUT_SALT,
624 input1, input1_length ) );
625 PSA_ASSERT( psa_key_derivation_input_key( operation,
626 PSA_KEY_DERIVATION_INPUT_SECRET,
627 handle ) );
628 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
629 PSA_KEY_DERIVATION_INPUT_INFO,
630 input2,
631 input2_length ) );
632 }
633 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
634 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
635 {
636 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
637 PSA_KEY_DERIVATION_INPUT_SEED,
638 input1, input1_length ) );
639 PSA_ASSERT( psa_key_derivation_input_key( operation,
640 PSA_KEY_DERIVATION_INPUT_SECRET,
641 handle ) );
642 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
643 PSA_KEY_DERIVATION_INPUT_LABEL,
644 input2, input2_length ) );
645 }
646 else
647 {
648 TEST_ASSERT( ! "Key derivation algorithm not supported" );
649 }
650
Gilles Peskinec744d992019-07-30 17:26:54 +0200651 if( capacity != SIZE_MAX )
652 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100653
654 return( 1 );
655
656exit:
657 return( 0 );
658}
659
660
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100661static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200662 psa_key_usage_t usage,
663 psa_algorithm_t alg )
664{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200665 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100666 unsigned char input1[] = "Input 1";
667 size_t input1_length = sizeof( input1 );
668 unsigned char input2[] = "Input 2";
669 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200670 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100671 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200672
673 if( usage & PSA_KEY_USAGE_DERIVE )
674 {
Janos Follathf2815ea2019-07-03 12:41:36 +0100675 if( !setup_key_derivation_wrap( &operation, handle, alg,
676 input1, input1_length,
677 input2, input2_length, capacity ) )
678 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200679
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200680 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200681 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100682 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200683 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200684 }
685
686 return( 1 );
687
688exit:
689 return( 0 );
690}
691
Gilles Peskinec7998b72018-11-07 18:45:02 +0100692/* We need two keys to exercise key agreement. Exercise the
693 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200694static psa_status_t key_agreement_with_self(
695 psa_key_derivation_operation_t *operation,
696 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100697{
698 psa_key_type_t private_key_type;
699 psa_key_type_t public_key_type;
700 size_t key_bits;
701 uint8_t *public_key = NULL;
702 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200703 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200704 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
705 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200706 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100708
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200709 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
710 private_key_type = psa_get_key_type( &attributes );
711 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200712 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100713 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
714 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100715 PSA_ASSERT( psa_export_public_key( handle,
716 public_key, public_key_length,
717 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100718
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200719 status = psa_key_derivation_key_agreement(
720 operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle,
721 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100722exit:
723 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200724 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100725 return( status );
726}
727
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200728/* We need two keys to exercise key agreement. Exercise the
729 * private key against its own public key. */
730static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
731 psa_key_handle_t handle )
732{
733 psa_key_type_t private_key_type;
734 psa_key_type_t public_key_type;
735 size_t key_bits;
736 uint8_t *public_key = NULL;
737 size_t public_key_length;
738 uint8_t output[1024];
739 size_t output_length;
740 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200741 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
742 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200743 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200745
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200746 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
747 private_key_type = psa_get_key_type( &attributes );
748 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200749 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200750 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
751 ASSERT_ALLOC( public_key, public_key_length );
752 PSA_ASSERT( psa_export_public_key( handle,
753 public_key, public_key_length,
754 &public_key_length ) );
755
Gilles Peskinebe697d82019-05-16 18:00:41 +0200756 status = psa_raw_key_agreement( alg, handle,
757 public_key, public_key_length,
758 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200759exit:
760 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200761 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200762 return( status );
763}
764
765static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
766 psa_key_usage_t usage,
767 psa_algorithm_t alg )
768{
769 int ok = 0;
770
771 if( usage & PSA_KEY_USAGE_DERIVE )
772 {
773 /* We need two keys to exercise key agreement. Exercise the
774 * private key against its own public key. */
775 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
776 }
777 ok = 1;
778
779exit:
780 return( ok );
781}
782
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100783static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200784 psa_key_usage_t usage,
785 psa_algorithm_t alg )
786{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200787 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200788 unsigned char output[1];
789 int ok = 0;
790
791 if( usage & PSA_KEY_USAGE_DERIVE )
792 {
793 /* We need two keys to exercise key agreement. Exercise the
794 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200795 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
796 PSA_ASSERT( key_agreement_with_self( &operation, handle ) );
797 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200798 output,
799 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200800 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200801 }
802 ok = 1;
803
804exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200805 return( ok );
806}
807
Jaeden Amerof7dca862019-06-27 17:31:33 +0100808int asn1_skip_integer( unsigned char **p, const unsigned char *end,
809 size_t min_bits, size_t max_bits,
810 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200811{
812 size_t len;
813 size_t actual_bits;
814 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100815 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100816 MBEDTLS_ASN1_INTEGER ),
817 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200818
819 /* Check if the retrieved length doesn't extend the actual buffer's size.
820 * It is assumed here, that end >= p, which validates casting to size_t. */
821 TEST_ASSERT( len <= (size_t)( end - *p) );
822
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200823 /* Tolerate a slight departure from DER encoding:
824 * - 0 may be represented by an empty string or a 1-byte string.
825 * - The sign bit may be used as a value bit. */
826 if( ( len == 1 && ( *p )[0] == 0 ) ||
827 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
828 {
829 ++( *p );
830 --len;
831 }
832 if( min_bits == 0 && len == 0 )
833 return( 1 );
834 msb = ( *p )[0];
835 TEST_ASSERT( msb != 0 );
836 actual_bits = 8 * ( len - 1 );
837 while( msb != 0 )
838 {
839 msb >>= 1;
840 ++actual_bits;
841 }
842 TEST_ASSERT( actual_bits >= min_bits );
843 TEST_ASSERT( actual_bits <= max_bits );
844 if( must_be_odd )
845 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
846 *p += len;
847 return( 1 );
848exit:
849 return( 0 );
850}
851
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200852static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
853 uint8_t *exported, size_t exported_length )
854{
855 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100856 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200857 else
858 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200859
860#if defined(MBEDTLS_DES_C)
861 if( type == PSA_KEY_TYPE_DES )
862 {
863 /* Check the parity bits. */
864 unsigned i;
865 for( i = 0; i < bits / 8; i++ )
866 {
867 unsigned bit_count = 0;
868 unsigned m;
869 for( m = 1; m <= 0x100; m <<= 1 )
870 {
871 if( exported[i] & m )
872 ++bit_count;
873 }
874 TEST_ASSERT( bit_count % 2 != 0 );
875 }
876 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200877 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200878#endif
879
880#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200881 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200882 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200883 uint8_t *p = exported;
884 uint8_t *end = exported + exported_length;
885 size_t len;
886 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200887 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200888 * modulus INTEGER, -- n
889 * publicExponent INTEGER, -- e
890 * privateExponent INTEGER, -- d
891 * prime1 INTEGER, -- p
892 * prime2 INTEGER, -- q
893 * exponent1 INTEGER, -- d mod (p-1)
894 * exponent2 INTEGER, -- d mod (q-1)
895 * coefficient INTEGER, -- (inverse of q) mod p
896 * }
897 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100898 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
899 MBEDTLS_ASN1_SEQUENCE |
900 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
901 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200902 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
903 goto exit;
904 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
905 goto exit;
906 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
907 goto exit;
908 /* Require d to be at least half the size of n. */
909 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
910 goto exit;
911 /* Require p and q to be at most half the size of n, rounded up. */
912 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
913 goto exit;
914 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
915 goto exit;
916 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
917 goto exit;
918 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
919 goto exit;
920 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
921 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100922 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100923 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200924 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200925#endif /* MBEDTLS_RSA_C */
926
927#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200928 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200929 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100930 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100931 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100932 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200933 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200934#endif /* MBEDTLS_ECP_C */
935
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200936 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
937 {
938 uint8_t *p = exported;
939 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200940#if defined(MBEDTLS_RSA_C)
941 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
942 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100943 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200944 /* RSAPublicKey ::= SEQUENCE {
945 * modulus INTEGER, -- n
946 * publicExponent INTEGER } -- e
947 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100948 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
949 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100950 MBEDTLS_ASN1_CONSTRUCTED ),
951 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100952 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200953 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
954 goto exit;
955 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
956 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100957 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200958 }
959 else
960#endif /* MBEDTLS_RSA_C */
961#if defined(MBEDTLS_ECP_C)
962 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
963 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000964 /* The representation of an ECC public key is:
965 * - The byte 0x04;
966 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
967 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
968 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000969 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100970 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
971 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200972 }
973 else
974#endif /* MBEDTLS_ECP_C */
975 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100976 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200977 mbedtls_snprintf( message, sizeof( message ),
978 "No sanity check for public key type=0x%08lx",
979 (unsigned long) type );
980 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200981 (void) p;
982 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200983 return( 0 );
984 }
985 }
986 else
987
988 {
989 /* No sanity checks for other types */
990 }
991
992 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200993
994exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200995 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200996}
997
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100998static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200999 psa_key_usage_t usage )
1000{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001002 uint8_t *exported = NULL;
1003 size_t exported_size = 0;
1004 size_t exported_length = 0;
1005 int ok = 0;
1006
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001007 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001008
1009 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001010 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001011 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001012 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
1013 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001014 ok = 1;
1015 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001016 }
1017
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001018 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
1019 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001020 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001021
Gilles Peskine8817f612018-12-18 00:18:46 +01001022 PSA_ASSERT( psa_export_key( handle,
1023 exported, exported_size,
1024 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001025 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1026 psa_get_key_bits( &attributes ),
1027 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001028
1029exit:
1030 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001031 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001032 return( ok );
1033}
1034
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001035static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +02001036{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001038 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001039 uint8_t *exported = NULL;
1040 size_t exported_size = 0;
1041 size_t exported_length = 0;
1042 int ok = 0;
1043
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001044 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1045 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001046 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001047 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001048 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +02001049 return( 1 );
1050 }
1051
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001052 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001053 psa_get_key_type( &attributes ) );
1054 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
1055 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001056 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001057
Gilles Peskine8817f612018-12-18 00:18:46 +01001058 PSA_ASSERT( psa_export_public_key( handle,
1059 exported, exported_size,
1060 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001061 ok = exported_key_sanity_check( public_type,
1062 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001063 exported, exported_length );
1064
1065exit:
1066 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001067 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001068 return( ok );
1069}
1070
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001071/** Do smoke tests on a key.
1072 *
1073 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1074 * sign/verify, or derivation) that is permitted according to \p usage.
1075 * \p usage and \p alg should correspond to the expected policy on the
1076 * key.
1077 *
1078 * Export the key if permitted by \p usage, and check that the output
1079 * looks sensible. If \p usage forbids export, check that
1080 * \p psa_export_key correctly rejects the attempt. If the key is
1081 * asymmetric, also check \p psa_export_public_key.
1082 *
1083 * If the key fails the tests, this function calls the test framework's
1084 * `test_fail` function and returns false. Otherwise this function returns
1085 * true. Therefore it should be used as follows:
1086 * ```
1087 * if( ! exercise_key( ... ) ) goto exit;
1088 * ```
1089 *
1090 * \param handle The key to exercise. It should be capable of performing
1091 * \p alg.
1092 * \param usage The usage flags to assume.
1093 * \param alg The algorithm to exercise.
1094 *
1095 * \retval 0 The key failed the smoke tests.
1096 * \retval 1 The key passed the smoke tests.
1097 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001098static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001099 psa_key_usage_t usage,
1100 psa_algorithm_t alg )
1101{
1102 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001103
1104 if( ! check_key_attributes_sanity( handle ) )
1105 return( 0 );
1106
Gilles Peskine02b75072018-07-01 22:31:34 +02001107 if( alg == 0 )
1108 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1109 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001110 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001111 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001112 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001113 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001114 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001115 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001116 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001117 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001118 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001119 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001120 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001121 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1122 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001123 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001124 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001125 else
1126 {
1127 char message[40];
1128 mbedtls_snprintf( message, sizeof( message ),
1129 "No code to exercise alg=0x%08lx",
1130 (unsigned long) alg );
1131 test_fail( message, __LINE__, __FILE__ );
1132 ok = 0;
1133 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001134
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001135 ok = ok && exercise_export_key( handle, usage );
1136 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001137
Gilles Peskine02b75072018-07-01 22:31:34 +02001138 return( ok );
1139}
1140
Gilles Peskine10df3412018-10-25 22:35:43 +02001141static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1142 psa_algorithm_t alg )
1143{
1144 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1145 {
1146 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001147 PSA_KEY_USAGE_VERIFY_HASH :
1148 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001149 }
1150 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1151 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1152 {
1153 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1154 PSA_KEY_USAGE_ENCRYPT :
1155 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1156 }
1157 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1158 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1159 {
1160 return( PSA_KEY_USAGE_DERIVE );
1161 }
1162 else
1163 {
1164 return( 0 );
1165 }
1166
1167}
Darryl Green0c6575a2018-11-07 16:05:30 +00001168
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001169static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1170{
1171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1172 uint8_t buffer[1];
1173 size_t length;
1174 int ok = 0;
1175
Gilles Peskinec87af662019-05-15 16:12:22 +02001176 psa_set_key_id( &attributes, 0x6964 );
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 );
1180 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1181 PSA_ERROR_INVALID_HANDLE );
1182 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001183 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001184 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1185 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1186 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1187 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1188
1189 TEST_EQUAL( psa_export_key( handle,
1190 buffer, sizeof( buffer ), &length ),
1191 PSA_ERROR_INVALID_HANDLE );
1192 TEST_EQUAL( psa_export_public_key( handle,
1193 buffer, sizeof( buffer ), &length ),
1194 PSA_ERROR_INVALID_HANDLE );
1195
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001196 ok = 1;
1197
1198exit:
1199 psa_reset_key_attributes( &attributes );
1200 return( ok );
1201}
1202
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001203/* Assert that a key isn't reported as having a slot number. */
1204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1205#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1206 do \
1207 { \
1208 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1209 TEST_EQUAL( psa_get_key_slot_number( \
1210 attributes, \
1211 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1212 PSA_ERROR_INVALID_ARGUMENT ); \
1213 } \
1214 while( 0 )
1215#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1216#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1217 ( (void) 0 )
1218#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1219
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001220/* An overapproximation of the amount of storage needed for a key of the
1221 * given type and with the given content. The API doesn't make it easy
1222 * to find a good value for the size. The current implementation doesn't
1223 * care about the value anyway. */
1224#define KEY_BITS_FROM_DATA( type, data ) \
1225 ( data )->len
1226
Darryl Green0c6575a2018-11-07 16:05:30 +00001227typedef enum {
1228 IMPORT_KEY = 0,
1229 GENERATE_KEY = 1,
1230 DERIVE_KEY = 2
1231} generate_method;
1232
Gilles Peskinee59236f2018-01-27 23:32:46 +01001233/* END_HEADER */
1234
1235/* BEGIN_DEPENDENCIES
1236 * depends_on:MBEDTLS_PSA_CRYPTO_C
1237 * END_DEPENDENCIES
1238 */
1239
1240/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001241void static_checks( )
1242{
1243 size_t max_truncated_mac_size =
1244 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1245
1246 /* Check that the length for a truncated MAC always fits in the algorithm
1247 * encoding. The shifted mask is the maximum truncated value. The
1248 * untruncated algorithm may be one byte larger. */
1249 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001250
1251#if defined(MBEDTLS_TEST_DEPRECATED)
1252 /* Check deprecated constants. */
1253 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1254 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1255 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1256 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1257 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1258 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1259 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1260 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
1261#endif /* MBEDTLS_TEST_DEPRECATED */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001262}
1263/* END_CASE */
1264
1265/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001266void attributes_set_get( int id_arg, int lifetime_arg,
1267 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001268 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269{
Gilles Peskine4747d192019-04-17 15:05:45 +02001270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001271 psa_key_id_t id = id_arg;
1272 psa_key_lifetime_t lifetime = lifetime_arg;
1273 psa_key_usage_t usage_flags = usage_flags_arg;
1274 psa_algorithm_t alg = alg_arg;
1275 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001276 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001277
1278 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1279 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1280 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1281 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1282 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001283 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001284
Gilles Peskinec87af662019-05-15 16:12:22 +02001285 psa_set_key_id( &attributes, id );
1286 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001287 psa_set_key_usage_flags( &attributes, usage_flags );
1288 psa_set_key_algorithm( &attributes, alg );
1289 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001290 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001291
1292 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1293 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1294 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1295 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1296 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001297 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001298
1299 psa_reset_key_attributes( &attributes );
1300
1301 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1302 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1303 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1304 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1305 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001306 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001307}
1308/* END_CASE */
1309
1310/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001311void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1312 int expected_id_arg, int expected_lifetime_arg )
1313{
1314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1315 psa_key_id_t id1 = id1_arg;
1316 psa_key_lifetime_t lifetime = lifetime_arg;
1317 psa_key_id_t id2 = id2_arg;
1318 psa_key_id_t expected_id = expected_id_arg;
1319 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1320
1321 if( id1_arg != -1 )
1322 psa_set_key_id( &attributes, id1 );
1323 if( lifetime_arg != -1 )
1324 psa_set_key_lifetime( &attributes, lifetime );
1325 if( id2_arg != -1 )
1326 psa_set_key_id( &attributes, id2 );
1327
1328 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1329 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1330}
1331/* END_CASE */
1332
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001333/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1334void slot_number_attribute( )
1335{
1336 psa_key_slot_number_t slot_number = 0xdeadbeef;
1337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1338
1339 /* Initially, there is no slot number. */
1340 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1341 PSA_ERROR_INVALID_ARGUMENT );
1342
1343 /* Test setting a slot number. */
1344 psa_set_key_slot_number( &attributes, 0 );
1345 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1346 TEST_EQUAL( slot_number, 0 );
1347
1348 /* Test changing the slot number. */
1349 psa_set_key_slot_number( &attributes, 42 );
1350 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1351 TEST_EQUAL( slot_number, 42 );
1352
1353 /* Test clearing the slot number. */
1354 psa_clear_key_slot_number( &attributes );
1355 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1356 PSA_ERROR_INVALID_ARGUMENT );
1357
1358 /* Clearing again should have no effect. */
1359 psa_clear_key_slot_number( &attributes );
1360 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1361 PSA_ERROR_INVALID_ARGUMENT );
1362
1363 /* Test that reset clears the slot number. */
1364 psa_set_key_slot_number( &attributes, 42 );
1365 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1366 TEST_EQUAL( slot_number, 42 );
1367 psa_reset_key_attributes( &attributes );
1368 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1369 PSA_ERROR_INVALID_ARGUMENT );
1370}
1371/* END_CASE */
1372
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001373/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001374void import_with_policy( int type_arg,
1375 int usage_arg, int alg_arg,
1376 int expected_status_arg )
1377{
1378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1379 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1380 psa_key_handle_t handle = 0;
1381 psa_key_type_t type = type_arg;
1382 psa_key_usage_t usage = usage_arg;
1383 psa_algorithm_t alg = alg_arg;
1384 psa_status_t expected_status = expected_status_arg;
1385 const uint8_t key_material[16] = {0};
1386 psa_status_t status;
1387
1388 PSA_ASSERT( psa_crypto_init( ) );
1389
1390 psa_set_key_type( &attributes, type );
1391 psa_set_key_usage_flags( &attributes, usage );
1392 psa_set_key_algorithm( &attributes, alg );
1393
1394 status = psa_import_key( &attributes,
1395 key_material, sizeof( key_material ),
1396 &handle );
1397 TEST_EQUAL( status, expected_status );
1398 if( status != PSA_SUCCESS )
1399 goto exit;
1400
1401 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1402 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1403 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1404 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001405 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001406
1407 PSA_ASSERT( psa_destroy_key( handle ) );
1408 test_operations_on_invalid_handle( handle );
1409
1410exit:
1411 psa_destroy_key( handle );
1412 psa_reset_key_attributes( &got_attributes );
1413 PSA_DONE( );
1414}
1415/* END_CASE */
1416
1417/* BEGIN_CASE */
1418void import_with_data( data_t *data, int type_arg,
1419 int attr_bits_arg,
1420 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001421{
1422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1423 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001424 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001425 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001426 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001427 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001428 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001429
Gilles Peskine8817f612018-12-18 00:18:46 +01001430 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001431
Gilles Peskine4747d192019-04-17 15:05:45 +02001432 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001433 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001434
Gilles Peskine73676cb2019-05-15 20:15:10 +02001435 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001436 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001437 if( status != PSA_SUCCESS )
1438 goto exit;
1439
1440 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1441 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001442 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001443 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001444 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001445
1446 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001447 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001448
1449exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001450 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001451 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001452 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001453}
1454/* END_CASE */
1455
1456/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001457void import_large_key( int type_arg, int byte_size_arg,
1458 int expected_status_arg )
1459{
1460 psa_key_type_t type = type_arg;
1461 size_t byte_size = byte_size_arg;
1462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1463 psa_status_t expected_status = expected_status_arg;
1464 psa_key_handle_t handle = 0;
1465 psa_status_t status;
1466 uint8_t *buffer = NULL;
1467 size_t buffer_size = byte_size + 1;
1468 size_t n;
1469
1470 /* It would be better to skip the test than fail it if the allocation
1471 * fails, but the test framework doesn't support this yet. */
1472 ASSERT_ALLOC( buffer, buffer_size );
1473 memset( buffer, 'K', byte_size );
1474
1475 PSA_ASSERT( psa_crypto_init( ) );
1476
1477 /* Try importing the key */
1478 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1479 psa_set_key_type( &attributes, type );
1480 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1481 TEST_EQUAL( status, expected_status );
1482
1483 if( status == PSA_SUCCESS )
1484 {
1485 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1486 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1487 TEST_EQUAL( psa_get_key_bits( &attributes ),
1488 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001489 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001490 memset( buffer, 0, byte_size + 1 );
1491 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1492 for( n = 0; n < byte_size; n++ )
1493 TEST_EQUAL( buffer[n], 'K' );
1494 for( n = byte_size; n < buffer_size; n++ )
1495 TEST_EQUAL( buffer[n], 0 );
1496 }
1497
1498exit:
1499 psa_destroy_key( handle );
1500 PSA_DONE( );
1501 mbedtls_free( buffer );
1502}
1503/* END_CASE */
1504
1505/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001506void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1507{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001508 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001509 size_t bits = bits_arg;
1510 psa_status_t expected_status = expected_status_arg;
1511 psa_status_t status;
1512 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001513 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001514 size_t buffer_size = /* Slight overapproximations */
1515 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001516 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001517 unsigned char *p;
1518 int ret;
1519 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001521
Gilles Peskine8817f612018-12-18 00:18:46 +01001522 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001523 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001524
1525 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1526 bits, keypair ) ) >= 0 );
1527 length = ret;
1528
1529 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001530 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001531 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001532 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001533
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001534 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001535 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001536
1537exit:
1538 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001539 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001540}
1541/* END_CASE */
1542
1543/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001544void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001545 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001546 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001547 int expected_bits,
1548 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001549 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001550 int canonical_input )
1551{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001552 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001554 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001555 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001556 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001557 unsigned char *exported = NULL;
1558 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001559 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001560 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001561 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001562 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001563 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564
Moran Pekercb088e72018-07-17 17:36:59 +03001565 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001566 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001568 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001569 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001570
Gilles Peskine4747d192019-04-17 15:05:45 +02001571 psa_set_key_usage_flags( &attributes, usage_arg );
1572 psa_set_key_algorithm( &attributes, alg );
1573 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001574
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001575 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001576 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001577
1578 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001579 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1580 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1581 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001582 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001583
1584 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001585 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001586 exported, export_size,
1587 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001588 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001589
1590 /* The exported length must be set by psa_export_key() to a value between 0
1591 * and export_size. On errors, the exported length must be 0. */
1592 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1593 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1594 TEST_ASSERT( exported_length <= export_size );
1595
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001596 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001597 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001598 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001599 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001600 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001601 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001602 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001604 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001605 goto exit;
1606
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001607 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001608 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001609 else
1610 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001611 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001612 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1613 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001614 PSA_ASSERT( psa_export_key( handle2,
1615 reexported,
1616 export_size,
1617 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001618 ASSERT_COMPARE( exported, exported_length,
1619 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001620 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001621 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001622 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001623
1624destroy:
1625 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001626 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001627 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001628
1629exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001630 mbedtls_free( exported );
1631 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001632 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001633 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001634}
1635/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001636
Moran Pekerf709f4a2018-06-06 17:26:04 +03001637/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001638void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001639 int type_arg,
1640 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001641 int export_size_delta,
1642 int expected_export_status_arg,
1643 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001644{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001645 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001646 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001647 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001648 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001649 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001650 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001651 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001652 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001653 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001654
Gilles Peskine8817f612018-12-18 00:18:46 +01001655 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001656
Gilles Peskine4747d192019-04-17 15:05:45 +02001657 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1658 psa_set_key_algorithm( &attributes, alg );
1659 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001660
1661 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001662 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001663
Gilles Peskine49c25912018-10-29 15:15:31 +01001664 /* Export the public key */
1665 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001666 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001667 exported, export_size,
1668 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001669 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001670 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001671 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001672 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001673 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001674 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1675 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001676 TEST_ASSERT( expected_public_key->len <=
1677 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001678 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1679 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001680 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001681
1682exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001683 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001684 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001685 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001686 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001687}
1688/* END_CASE */
1689
Gilles Peskine20035e32018-02-03 22:44:14 +01001690/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001691void import_and_exercise_key( data_t *data,
1692 int type_arg,
1693 int bits_arg,
1694 int alg_arg )
1695{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001696 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001697 psa_key_type_t type = type_arg;
1698 size_t bits = bits_arg;
1699 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001700 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001702 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001703
Gilles Peskine8817f612018-12-18 00:18:46 +01001704 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001705
Gilles Peskine4747d192019-04-17 15:05:45 +02001706 psa_set_key_usage_flags( &attributes, usage );
1707 psa_set_key_algorithm( &attributes, alg );
1708 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001709
1710 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001711 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001712
1713 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001714 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1715 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1716 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001717
1718 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001719 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001720 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001721
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001722 PSA_ASSERT( psa_destroy_key( handle ) );
1723 test_operations_on_invalid_handle( handle );
1724
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001725exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001726 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001727 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001728 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001729}
1730/* END_CASE */
1731
1732/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001733void effective_key_attributes( int type_arg, int expected_type_arg,
1734 int bits_arg, int expected_bits_arg,
1735 int usage_arg, int expected_usage_arg,
1736 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001737{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001738 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001739 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001740 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001741 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001742 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001743 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001744 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001745 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001746 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001748
Gilles Peskine8817f612018-12-18 00:18:46 +01001749 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001750
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001751 psa_set_key_usage_flags( &attributes, usage );
1752 psa_set_key_algorithm( &attributes, alg );
1753 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001754 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001755
Gilles Peskine1a960492019-11-26 17:12:21 +01001756 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1757 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001758
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001759 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001760 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1761 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1762 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1763 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001764
1765exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001766 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001767 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001768 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001769}
1770/* END_CASE */
1771
1772/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001773void check_key_policy( int type_arg, int bits_arg,
1774 int usage_arg, int alg_arg )
1775{
1776 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1777 usage_arg, usage_arg, alg_arg, alg_arg );
1778 goto exit;
1779}
1780/* END_CASE */
1781
1782/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001783void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001784{
1785 /* Test each valid way of initializing the object, except for `= {0}`, as
1786 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1787 * though it's OK by the C standard. We could test for this, but we'd need
1788 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001789 psa_key_attributes_t func = psa_key_attributes_init( );
1790 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1791 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001792
1793 memset( &zero, 0, sizeof( zero ) );
1794
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001795 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1796 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1797 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001798
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001799 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1800 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1801 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1802
1803 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1804 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1805 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1806
1807 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1808 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1809 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1810
1811 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1812 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1813 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001818void mac_key_policy( int policy_usage,
1819 int policy_alg,
1820 int key_type,
1821 data_t *key_data,
1822 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001823{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001824 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001826 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001827 psa_status_t status;
1828 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001829
Gilles Peskine8817f612018-12-18 00:18:46 +01001830 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001831
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001832 psa_set_key_usage_flags( &attributes, policy_usage );
1833 psa_set_key_algorithm( &attributes, policy_alg );
1834 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001835
Gilles Peskine049c7532019-05-15 20:22:09 +02001836 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1837 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001838
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001839 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001840 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001841 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001842 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001843 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001844 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001845 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001846
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001847 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001848 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001849 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001850 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001851 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001852 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001853 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001854
1855exit:
1856 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001857 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001858 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001859}
1860/* END_CASE */
1861
1862/* BEGIN_CASE */
1863void cipher_key_policy( int policy_usage,
1864 int policy_alg,
1865 int key_type,
1866 data_t *key_data,
1867 int exercise_alg )
1868{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001869 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001870 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001871 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001872 psa_status_t status;
1873
Gilles Peskine8817f612018-12-18 00:18:46 +01001874 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001875
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001876 psa_set_key_usage_flags( &attributes, policy_usage );
1877 psa_set_key_algorithm( &attributes, policy_alg );
1878 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001879
Gilles Peskine049c7532019-05-15 20:22:09 +02001880 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1881 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001882
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001883 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001884 if( policy_alg == exercise_alg &&
1885 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001886 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001887 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001888 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001889 psa_cipher_abort( &operation );
1890
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001891 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001892 if( policy_alg == exercise_alg &&
1893 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001894 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001895 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001896 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001897
1898exit:
1899 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001900 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001901 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001902}
1903/* END_CASE */
1904
1905/* BEGIN_CASE */
1906void aead_key_policy( int policy_usage,
1907 int policy_alg,
1908 int key_type,
1909 data_t *key_data,
1910 int nonce_length_arg,
1911 int tag_length_arg,
1912 int exercise_alg )
1913{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001914 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001916 psa_status_t status;
1917 unsigned char nonce[16] = {0};
1918 size_t nonce_length = nonce_length_arg;
1919 unsigned char tag[16];
1920 size_t tag_length = tag_length_arg;
1921 size_t output_length;
1922
1923 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1924 TEST_ASSERT( tag_length <= sizeof( tag ) );
1925
Gilles Peskine8817f612018-12-18 00:18:46 +01001926 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001927
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001928 psa_set_key_usage_flags( &attributes, policy_usage );
1929 psa_set_key_algorithm( &attributes, policy_alg );
1930 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931
Gilles Peskine049c7532019-05-15 20:22:09 +02001932 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1933 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001935 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001936 nonce, nonce_length,
1937 NULL, 0,
1938 NULL, 0,
1939 tag, tag_length,
1940 &output_length );
1941 if( policy_alg == exercise_alg &&
1942 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001943 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001944 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001945 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946
1947 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001948 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001949 nonce, nonce_length,
1950 NULL, 0,
1951 tag, tag_length,
1952 NULL, 0,
1953 &output_length );
1954 if( policy_alg == exercise_alg &&
1955 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001956 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001957 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001958 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001959
1960exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001961 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001962 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001963}
1964/* END_CASE */
1965
1966/* BEGIN_CASE */
1967void asymmetric_encryption_key_policy( int policy_usage,
1968 int policy_alg,
1969 int key_type,
1970 data_t *key_data,
1971 int exercise_alg )
1972{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001973 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001975 psa_status_t status;
1976 size_t key_bits;
1977 size_t buffer_length;
1978 unsigned char *buffer = NULL;
1979 size_t output_length;
1980
Gilles Peskine8817f612018-12-18 00:18:46 +01001981 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001982
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001983 psa_set_key_usage_flags( &attributes, policy_usage );
1984 psa_set_key_algorithm( &attributes, policy_alg );
1985 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986
Gilles Peskine049c7532019-05-15 20:22:09 +02001987 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1988 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001989
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001990 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1991 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001992 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1993 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001994 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001996 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001997 NULL, 0,
1998 NULL, 0,
1999 buffer, buffer_length,
2000 &output_length );
2001 if( policy_alg == exercise_alg &&
2002 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002003 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002005 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002007 if( buffer_length != 0 )
2008 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002009 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002010 buffer, buffer_length,
2011 NULL, 0,
2012 buffer, buffer_length,
2013 &output_length );
2014 if( policy_alg == exercise_alg &&
2015 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002016 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002018 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019
2020exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002021 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002022 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002023 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 mbedtls_free( buffer );
2025}
2026/* END_CASE */
2027
2028/* BEGIN_CASE */
2029void asymmetric_signature_key_policy( int policy_usage,
2030 int policy_alg,
2031 int key_type,
2032 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002033 int exercise_alg,
2034 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002036 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002039 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2040 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2041 * compatible with the policy and `payload_length_arg` is supposed to be
2042 * a valid input length to sign. If `payload_length_arg <= 0`,
2043 * `exercise_alg` is supposed to be forbidden by the policy. */
2044 int compatible_alg = payload_length_arg > 0;
2045 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002046 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002047 size_t signature_length;
2048
Gilles Peskine8817f612018-12-18 00:18:46 +01002049 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002051 psa_set_key_usage_flags( &attributes, policy_usage );
2052 psa_set_key_algorithm( &attributes, policy_alg );
2053 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002054
Gilles Peskine049c7532019-05-15 20:22:09 +02002055 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2056 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002057
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002058 status = psa_sign_hash( handle, exercise_alg,
2059 payload, payload_length,
2060 signature, sizeof( signature ),
2061 &signature_length );
2062 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002063 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002064 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002065 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066
2067 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002068 status = psa_verify_hash( handle, exercise_alg,
2069 payload, payload_length,
2070 signature, sizeof( signature ) );
2071 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002072 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002074 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002075
2076exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002077 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002078 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002079}
2080/* END_CASE */
2081
Janos Follathba3fab92019-06-11 14:50:16 +01002082/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002083void derive_key_policy( int policy_usage,
2084 int policy_alg,
2085 int key_type,
2086 data_t *key_data,
2087 int exercise_alg )
2088{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002089 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002091 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002092 psa_status_t status;
2093
Gilles Peskine8817f612018-12-18 00:18:46 +01002094 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002096 psa_set_key_usage_flags( &attributes, policy_usage );
2097 psa_set_key_algorithm( &attributes, policy_alg );
2098 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002099
Gilles Peskine049c7532019-05-15 20:22:09 +02002100 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2101 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002102
Janos Follathba3fab92019-06-11 14:50:16 +01002103 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2104
2105 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2106 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002107 {
Janos Follathba3fab92019-06-11 14:50:16 +01002108 PSA_ASSERT( psa_key_derivation_input_bytes(
2109 &operation,
2110 PSA_KEY_DERIVATION_INPUT_SEED,
2111 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002112 }
Janos Follathba3fab92019-06-11 14:50:16 +01002113
2114 status = psa_key_derivation_input_key( &operation,
2115 PSA_KEY_DERIVATION_INPUT_SECRET,
2116 handle );
2117
Gilles Peskineea0fb492018-07-12 17:17:20 +02002118 if( policy_alg == exercise_alg &&
2119 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002120 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002121 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002122 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002123
2124exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002125 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002126 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002127 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002128}
2129/* END_CASE */
2130
2131/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002132void agreement_key_policy( int policy_usage,
2133 int policy_alg,
2134 int key_type_arg,
2135 data_t *key_data,
2136 int exercise_alg )
2137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002138 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002140 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002141 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002142 psa_status_t status;
2143
Gilles Peskine8817f612018-12-18 00:18:46 +01002144 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002145
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002146 psa_set_key_usage_flags( &attributes, policy_usage );
2147 psa_set_key_algorithm( &attributes, policy_alg );
2148 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002149
Gilles Peskine049c7532019-05-15 20:22:09 +02002150 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2151 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002152
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002153 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2154 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002155
Gilles Peskine01d718c2018-09-18 12:01:02 +02002156 if( policy_alg == exercise_alg &&
2157 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002158 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002159 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002160 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002161
2162exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002163 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002164 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002165 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002166}
2167/* END_CASE */
2168
2169/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002170void key_policy_alg2( int key_type_arg, data_t *key_data,
2171 int usage_arg, int alg_arg, int alg2_arg )
2172{
2173 psa_key_handle_t handle = 0;
2174 psa_key_type_t key_type = key_type_arg;
2175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2176 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2177 psa_key_usage_t usage = usage_arg;
2178 psa_algorithm_t alg = alg_arg;
2179 psa_algorithm_t alg2 = alg2_arg;
2180
2181 PSA_ASSERT( psa_crypto_init( ) );
2182
2183 psa_set_key_usage_flags( &attributes, usage );
2184 psa_set_key_algorithm( &attributes, alg );
2185 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2186 psa_set_key_type( &attributes, key_type );
2187 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2188 &handle ) );
2189
2190 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2191 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2192 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2193 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2194
2195 if( ! exercise_key( handle, usage, alg ) )
2196 goto exit;
2197 if( ! exercise_key( handle, usage, alg2 ) )
2198 goto exit;
2199
2200exit:
2201 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002202 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002203}
2204/* END_CASE */
2205
2206/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002207void raw_agreement_key_policy( int policy_usage,
2208 int policy_alg,
2209 int key_type_arg,
2210 data_t *key_data,
2211 int exercise_alg )
2212{
2213 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002215 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002216 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002217 psa_status_t status;
2218
2219 PSA_ASSERT( psa_crypto_init( ) );
2220
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002221 psa_set_key_usage_flags( &attributes, policy_usage );
2222 psa_set_key_algorithm( &attributes, policy_alg );
2223 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002224
Gilles Peskine049c7532019-05-15 20:22:09 +02002225 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2226 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002227
2228 status = raw_key_agreement_with_self( exercise_alg, handle );
2229
2230 if( policy_alg == exercise_alg &&
2231 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2232 PSA_ASSERT( status );
2233 else
2234 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2235
2236exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002237 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002238 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002239 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002240}
2241/* END_CASE */
2242
2243/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002244void copy_success( int source_usage_arg,
2245 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002246 int type_arg, data_t *material,
2247 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002248 int target_usage_arg,
2249 int target_alg_arg, int target_alg2_arg,
2250 int expected_usage_arg,
2251 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002252{
Gilles Peskineca25db92019-04-19 11:43:08 +02002253 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2254 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002255 psa_key_usage_t expected_usage = expected_usage_arg;
2256 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002257 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002258 psa_key_handle_t source_handle = 0;
2259 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002260 uint8_t *export_buffer = NULL;
2261
Gilles Peskine57ab7212019-01-28 13:03:09 +01002262 PSA_ASSERT( psa_crypto_init( ) );
2263
Gilles Peskineca25db92019-04-19 11:43:08 +02002264 /* Prepare the source key. */
2265 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2266 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002267 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002268 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002269 PSA_ASSERT( psa_import_key( &source_attributes,
2270 material->x, material->len,
2271 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002272 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002273
Gilles Peskineca25db92019-04-19 11:43:08 +02002274 /* Prepare the target attributes. */
2275 if( copy_attributes )
2276 target_attributes = source_attributes;
2277 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. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002285 PSA_ASSERT( psa_copy_key( source_handle,
2286 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002287
2288 /* Destroy the source to ensure that this doesn't affect the target. */
2289 PSA_ASSERT( psa_destroy_key( source_handle ) );
2290
2291 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002292 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2293 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 );
2305 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2306 material->len, &length ) );
2307 ASSERT_COMPARE( material->x, material->len,
2308 export_buffer, length );
2309 }
2310 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2311 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002312 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2313 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002314
2315 PSA_ASSERT( psa_close_key( target_handle ) );
2316
2317exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002318 psa_reset_key_attributes( &source_attributes );
2319 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002320 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002321 mbedtls_free( export_buffer );
2322}
2323/* END_CASE */
2324
2325/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002326void copy_fail( int source_usage_arg,
2327 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002328 int type_arg, data_t *material,
2329 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002330 int target_usage_arg,
2331 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002332 int expected_status_arg )
2333{
2334 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2335 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2336 psa_key_handle_t source_handle = 0;
2337 psa_key_handle_t target_handle = 0;
2338
2339 PSA_ASSERT( psa_crypto_init( ) );
2340
2341 /* Prepare the source key. */
2342 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2343 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002344 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002345 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002346 PSA_ASSERT( psa_import_key( &source_attributes,
2347 material->x, material->len,
2348 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002349
2350 /* Prepare the target attributes. */
2351 psa_set_key_type( &target_attributes, target_type_arg );
2352 psa_set_key_bits( &target_attributes, target_bits_arg );
2353 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2354 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002355 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002356
2357 /* Try to copy the key. */
2358 TEST_EQUAL( psa_copy_key( source_handle,
2359 &target_attributes, &target_handle ),
2360 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002361
2362 PSA_ASSERT( psa_destroy_key( source_handle ) );
2363
Gilles Peskine4a644642019-05-03 17:14:08 +02002364exit:
2365 psa_reset_key_attributes( &source_attributes );
2366 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002367 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002368}
2369/* END_CASE */
2370
2371/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002372void hash_operation_init( )
2373{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002374 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002375 /* Test each valid way of initializing the object, except for `= {0}`, as
2376 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2377 * though it's OK by the C standard. We could test for this, but we'd need
2378 * to supress the Clang warning for the test. */
2379 psa_hash_operation_t func = psa_hash_operation_init( );
2380 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2381 psa_hash_operation_t zero;
2382
2383 memset( &zero, 0, sizeof( zero ) );
2384
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002385 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002386 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2387 PSA_ERROR_BAD_STATE );
2388 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2389 PSA_ERROR_BAD_STATE );
2390 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2391 PSA_ERROR_BAD_STATE );
2392
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002393 /* A default hash operation should be abortable without error. */
2394 PSA_ASSERT( psa_hash_abort( &func ) );
2395 PSA_ASSERT( psa_hash_abort( &init ) );
2396 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002397}
2398/* END_CASE */
2399
2400/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002401void hash_setup( int alg_arg,
2402 int expected_status_arg )
2403{
2404 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002405 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002406 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002407 psa_status_t status;
2408
Gilles Peskine8817f612018-12-18 00:18:46 +01002409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002410
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002411 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002412 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002413
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002414 /* Whether setup succeeded or failed, abort must succeed. */
2415 PSA_ASSERT( psa_hash_abort( &operation ) );
2416
2417 /* If setup failed, reproduce the failure, so as to
2418 * test the resulting state of the operation object. */
2419 if( status != PSA_SUCCESS )
2420 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2421
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002422 /* Now the operation object should be reusable. */
2423#if defined(KNOWN_SUPPORTED_HASH_ALG)
2424 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2425 PSA_ASSERT( psa_hash_abort( &operation ) );
2426#endif
2427
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002428exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002429 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002430}
2431/* END_CASE */
2432
2433/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002434void hash_compute_fail( int alg_arg, data_t *input,
2435 int output_size_arg, int expected_status_arg )
2436{
2437 psa_algorithm_t alg = alg_arg;
2438 uint8_t *output = NULL;
2439 size_t output_size = output_size_arg;
2440 size_t output_length = INVALID_EXPORT_LENGTH;
2441 psa_status_t expected_status = expected_status_arg;
2442 psa_status_t status;
2443
2444 ASSERT_ALLOC( output, output_size );
2445
2446 PSA_ASSERT( psa_crypto_init( ) );
2447
2448 status = psa_hash_compute( alg, input->x, input->len,
2449 output, output_size, &output_length );
2450 TEST_EQUAL( status, expected_status );
2451 TEST_ASSERT( output_length <= output_size );
2452
2453exit:
2454 mbedtls_free( output );
2455 PSA_DONE( );
2456}
2457/* END_CASE */
2458
2459/* BEGIN_CASE */
2460void hash_compute_compare( int alg_arg, data_t *input,
2461 data_t *expected_output )
2462{
2463 psa_algorithm_t alg = alg_arg;
2464 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2465 size_t output_length = INVALID_EXPORT_LENGTH;
2466 size_t i;
2467
2468 PSA_ASSERT( psa_crypto_init( ) );
2469
2470 /* Compute with tight buffer */
2471 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2472 output, PSA_HASH_SIZE( alg ),
2473 &output_length ) );
2474 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2475 ASSERT_COMPARE( output, output_length,
2476 expected_output->x, expected_output->len );
2477
2478 /* Compute with larger buffer */
2479 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2480 output, sizeof( output ),
2481 &output_length ) );
2482 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2483 ASSERT_COMPARE( output, output_length,
2484 expected_output->x, expected_output->len );
2485
2486 /* Compare with correct hash */
2487 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2488 output, output_length ) );
2489
2490 /* Compare with trailing garbage */
2491 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2492 output, output_length + 1 ),
2493 PSA_ERROR_INVALID_SIGNATURE );
2494
2495 /* Compare with truncated hash */
2496 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2497 output, output_length - 1 ),
2498 PSA_ERROR_INVALID_SIGNATURE );
2499
2500 /* Compare with corrupted value */
2501 for( i = 0; i < output_length; i++ )
2502 {
2503 test_set_step( i );
2504 output[i] ^= 1;
2505 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2506 output, output_length ),
2507 PSA_ERROR_INVALID_SIGNATURE );
2508 output[i] ^= 1;
2509 }
2510
2511exit:
2512 PSA_DONE( );
2513}
2514/* END_CASE */
2515
2516/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002517void hash_bad_order( )
2518{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002519 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002520 unsigned char input[] = "";
2521 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002522 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002523 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2524 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2525 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002526 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002527 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002528 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002529
Gilles Peskine8817f612018-12-18 00:18:46 +01002530 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002531
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002532 /* Call setup twice in a row. */
2533 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2534 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2535 PSA_ERROR_BAD_STATE );
2536 PSA_ASSERT( psa_hash_abort( &operation ) );
2537
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002538 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002539 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002540 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002541 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002542
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002543 /* Call update after finish. */
2544 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2545 PSA_ASSERT( psa_hash_finish( &operation,
2546 hash, sizeof( hash ), &hash_len ) );
2547 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002548 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002549 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002550
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002551 /* Call verify without calling setup beforehand. */
2552 TEST_EQUAL( psa_hash_verify( &operation,
2553 valid_hash, sizeof( valid_hash ) ),
2554 PSA_ERROR_BAD_STATE );
2555 PSA_ASSERT( psa_hash_abort( &operation ) );
2556
2557 /* Call verify after finish. */
2558 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2559 PSA_ASSERT( psa_hash_finish( &operation,
2560 hash, sizeof( hash ), &hash_len ) );
2561 TEST_EQUAL( psa_hash_verify( &operation,
2562 valid_hash, sizeof( valid_hash ) ),
2563 PSA_ERROR_BAD_STATE );
2564 PSA_ASSERT( psa_hash_abort( &operation ) );
2565
2566 /* Call verify twice in a row. */
2567 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2568 PSA_ASSERT( psa_hash_verify( &operation,
2569 valid_hash, sizeof( valid_hash ) ) );
2570 TEST_EQUAL( psa_hash_verify( &operation,
2571 valid_hash, sizeof( valid_hash ) ),
2572 PSA_ERROR_BAD_STATE );
2573 PSA_ASSERT( psa_hash_abort( &operation ) );
2574
2575 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002576 TEST_EQUAL( psa_hash_finish( &operation,
2577 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002578 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002579 PSA_ASSERT( psa_hash_abort( &operation ) );
2580
2581 /* Call finish twice in a row. */
2582 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2583 PSA_ASSERT( psa_hash_finish( &operation,
2584 hash, sizeof( hash ), &hash_len ) );
2585 TEST_EQUAL( psa_hash_finish( &operation,
2586 hash, sizeof( hash ), &hash_len ),
2587 PSA_ERROR_BAD_STATE );
2588 PSA_ASSERT( psa_hash_abort( &operation ) );
2589
2590 /* Call finish after calling verify. */
2591 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2592 PSA_ASSERT( psa_hash_verify( &operation,
2593 valid_hash, sizeof( valid_hash ) ) );
2594 TEST_EQUAL( psa_hash_finish( &operation,
2595 hash, sizeof( hash ), &hash_len ),
2596 PSA_ERROR_BAD_STATE );
2597 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002598
2599exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002600 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002601}
2602/* END_CASE */
2603
itayzafrir27e69452018-11-01 14:26:34 +02002604/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2605void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002606{
2607 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002608 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2609 * appended to it */
2610 unsigned char hash[] = {
2611 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2612 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2613 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002614 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002615 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002616
Gilles Peskine8817f612018-12-18 00:18:46 +01002617 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002618
itayzafrir27e69452018-11-01 14:26:34 +02002619 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002620 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002621 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002622 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002623
itayzafrir27e69452018-11-01 14:26:34 +02002624 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002626 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002627 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002628
itayzafrir27e69452018-11-01 14:26:34 +02002629 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002630 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002631 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002632 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002633
itayzafrirec93d302018-10-18 18:01:10 +03002634exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002635 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002636}
2637/* END_CASE */
2638
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002639/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2640void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002641{
2642 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002643 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002644 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002645 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002646 size_t hash_len;
2647
Gilles Peskine8817f612018-12-18 00:18:46 +01002648 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002649
itayzafrir58028322018-10-25 10:22:01 +03002650 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002651 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002652 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002653 hash, expected_size - 1, &hash_len ),
2654 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002655
2656exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002657 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002658}
2659/* END_CASE */
2660
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002661/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2662void hash_clone_source_state( )
2663{
2664 psa_algorithm_t alg = PSA_ALG_SHA_256;
2665 unsigned char hash[PSA_HASH_MAX_SIZE];
2666 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2667 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2668 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2669 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2670 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2671 size_t hash_len;
2672
2673 PSA_ASSERT( psa_crypto_init( ) );
2674 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2675
2676 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2677 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2678 PSA_ASSERT( psa_hash_finish( &op_finished,
2679 hash, sizeof( hash ), &hash_len ) );
2680 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2681 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2682
2683 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2684 PSA_ERROR_BAD_STATE );
2685
2686 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2687 PSA_ASSERT( psa_hash_finish( &op_init,
2688 hash, sizeof( hash ), &hash_len ) );
2689 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2690 PSA_ASSERT( psa_hash_finish( &op_finished,
2691 hash, sizeof( hash ), &hash_len ) );
2692 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2693 PSA_ASSERT( psa_hash_finish( &op_aborted,
2694 hash, sizeof( hash ), &hash_len ) );
2695
2696exit:
2697 psa_hash_abort( &op_source );
2698 psa_hash_abort( &op_init );
2699 psa_hash_abort( &op_setup );
2700 psa_hash_abort( &op_finished );
2701 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002702 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002703}
2704/* END_CASE */
2705
2706/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2707void hash_clone_target_state( )
2708{
2709 psa_algorithm_t alg = PSA_ALG_SHA_256;
2710 unsigned char hash[PSA_HASH_MAX_SIZE];
2711 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2712 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2713 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2714 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2715 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2716 size_t hash_len;
2717
2718 PSA_ASSERT( psa_crypto_init( ) );
2719
2720 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2721 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2722 PSA_ASSERT( psa_hash_finish( &op_finished,
2723 hash, sizeof( hash ), &hash_len ) );
2724 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2725 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2726
2727 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2728 PSA_ASSERT( psa_hash_finish( &op_target,
2729 hash, sizeof( hash ), &hash_len ) );
2730
2731 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2732 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2733 PSA_ERROR_BAD_STATE );
2734 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2735 PSA_ERROR_BAD_STATE );
2736
2737exit:
2738 psa_hash_abort( &op_target );
2739 psa_hash_abort( &op_init );
2740 psa_hash_abort( &op_setup );
2741 psa_hash_abort( &op_finished );
2742 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002743 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002744}
2745/* END_CASE */
2746
itayzafrir58028322018-10-25 10:22:01 +03002747/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002748void mac_operation_init( )
2749{
Jaeden Amero252ef282019-02-15 14:05:35 +00002750 const uint8_t input[1] = { 0 };
2751
Jaeden Amero769ce272019-01-04 11:48:03 +00002752 /* Test each valid way of initializing the object, except for `= {0}`, as
2753 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2754 * though it's OK by the C standard. We could test for this, but we'd need
2755 * to supress the Clang warning for the test. */
2756 psa_mac_operation_t func = psa_mac_operation_init( );
2757 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2758 psa_mac_operation_t zero;
2759
2760 memset( &zero, 0, sizeof( zero ) );
2761
Jaeden Amero252ef282019-02-15 14:05:35 +00002762 /* A freshly-initialized MAC operation should not be usable. */
2763 TEST_EQUAL( psa_mac_update( &func,
2764 input, sizeof( input ) ),
2765 PSA_ERROR_BAD_STATE );
2766 TEST_EQUAL( psa_mac_update( &init,
2767 input, sizeof( input ) ),
2768 PSA_ERROR_BAD_STATE );
2769 TEST_EQUAL( psa_mac_update( &zero,
2770 input, sizeof( input ) ),
2771 PSA_ERROR_BAD_STATE );
2772
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002773 /* A default MAC operation should be abortable without error. */
2774 PSA_ASSERT( psa_mac_abort( &func ) );
2775 PSA_ASSERT( psa_mac_abort( &init ) );
2776 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002777}
2778/* END_CASE */
2779
2780/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002781void mac_setup( int key_type_arg,
2782 data_t *key,
2783 int alg_arg,
2784 int expected_status_arg )
2785{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002786 psa_key_type_t key_type = key_type_arg;
2787 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002788 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002789 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002790 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2791#if defined(KNOWN_SUPPORTED_MAC_ALG)
2792 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2793#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002794
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002796
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002797 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2798 &operation, &status ) )
2799 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002800 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002801
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002802 /* The operation object should be reusable. */
2803#if defined(KNOWN_SUPPORTED_MAC_ALG)
2804 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2805 smoke_test_key_data,
2806 sizeof( smoke_test_key_data ),
2807 KNOWN_SUPPORTED_MAC_ALG,
2808 &operation, &status ) )
2809 goto exit;
2810 TEST_EQUAL( status, PSA_SUCCESS );
2811#endif
2812
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002813exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002814 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002815}
2816/* END_CASE */
2817
2818/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002819void mac_bad_order( )
2820{
2821 psa_key_handle_t handle = 0;
2822 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2823 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2824 const uint8_t key[] = {
2825 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2826 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2827 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002829 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2830 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2831 size_t sign_mac_length = 0;
2832 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2833 const uint8_t verify_mac[] = {
2834 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2835 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2836 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2837
2838 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002839 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002840 psa_set_key_algorithm( &attributes, alg );
2841 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002842
Gilles Peskine73676cb2019-05-15 20:15:10 +02002843 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002844
Jaeden Amero252ef282019-02-15 14:05:35 +00002845 /* Call update without calling setup beforehand. */
2846 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2847 PSA_ERROR_BAD_STATE );
2848 PSA_ASSERT( psa_mac_abort( &operation ) );
2849
2850 /* Call sign finish without calling setup beforehand. */
2851 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2852 &sign_mac_length),
2853 PSA_ERROR_BAD_STATE );
2854 PSA_ASSERT( psa_mac_abort( &operation ) );
2855
2856 /* Call verify finish without calling setup beforehand. */
2857 TEST_EQUAL( psa_mac_verify_finish( &operation,
2858 verify_mac, sizeof( verify_mac ) ),
2859 PSA_ERROR_BAD_STATE );
2860 PSA_ASSERT( psa_mac_abort( &operation ) );
2861
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002862 /* Call setup twice in a row. */
2863 PSA_ASSERT( psa_mac_sign_setup( &operation,
2864 handle, alg ) );
2865 TEST_EQUAL( psa_mac_sign_setup( &operation,
2866 handle, alg ),
2867 PSA_ERROR_BAD_STATE );
2868 PSA_ASSERT( psa_mac_abort( &operation ) );
2869
Jaeden Amero252ef282019-02-15 14:05:35 +00002870 /* Call update after sign finish. */
2871 PSA_ASSERT( psa_mac_sign_setup( &operation,
2872 handle, alg ) );
2873 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2874 PSA_ASSERT( psa_mac_sign_finish( &operation,
2875 sign_mac, sizeof( sign_mac ),
2876 &sign_mac_length ) );
2877 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2878 PSA_ERROR_BAD_STATE );
2879 PSA_ASSERT( psa_mac_abort( &operation ) );
2880
2881 /* Call update after verify finish. */
2882 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002883 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002884 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2885 PSA_ASSERT( psa_mac_verify_finish( &operation,
2886 verify_mac, sizeof( verify_mac ) ) );
2887 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2888 PSA_ERROR_BAD_STATE );
2889 PSA_ASSERT( psa_mac_abort( &operation ) );
2890
2891 /* Call sign finish twice in a row. */
2892 PSA_ASSERT( psa_mac_sign_setup( &operation,
2893 handle, alg ) );
2894 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2895 PSA_ASSERT( psa_mac_sign_finish( &operation,
2896 sign_mac, sizeof( sign_mac ),
2897 &sign_mac_length ) );
2898 TEST_EQUAL( psa_mac_sign_finish( &operation,
2899 sign_mac, sizeof( sign_mac ),
2900 &sign_mac_length ),
2901 PSA_ERROR_BAD_STATE );
2902 PSA_ASSERT( psa_mac_abort( &operation ) );
2903
2904 /* Call verify finish twice in a row. */
2905 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002906 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002907 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2908 PSA_ASSERT( psa_mac_verify_finish( &operation,
2909 verify_mac, sizeof( verify_mac ) ) );
2910 TEST_EQUAL( psa_mac_verify_finish( &operation,
2911 verify_mac, sizeof( verify_mac ) ),
2912 PSA_ERROR_BAD_STATE );
2913 PSA_ASSERT( psa_mac_abort( &operation ) );
2914
2915 /* Setup sign but try verify. */
2916 PSA_ASSERT( psa_mac_sign_setup( &operation,
2917 handle, alg ) );
2918 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2919 TEST_EQUAL( psa_mac_verify_finish( &operation,
2920 verify_mac, sizeof( verify_mac ) ),
2921 PSA_ERROR_BAD_STATE );
2922 PSA_ASSERT( psa_mac_abort( &operation ) );
2923
2924 /* Setup verify but try sign. */
2925 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002926 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002927 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2928 TEST_EQUAL( psa_mac_sign_finish( &operation,
2929 sign_mac, sizeof( sign_mac ),
2930 &sign_mac_length ),
2931 PSA_ERROR_BAD_STATE );
2932 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002933
Gilles Peskine76b29a72019-05-28 14:08:50 +02002934 PSA_ASSERT( psa_destroy_key( handle ) );
2935
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002936exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002937 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002938}
2939/* END_CASE */
2940
2941/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002942void mac_sign( int key_type_arg,
2943 data_t *key,
2944 int alg_arg,
2945 data_t *input,
2946 data_t *expected_mac )
2947{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002948 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002949 psa_key_type_t key_type = key_type_arg;
2950 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002951 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002952 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002953 /* Leave a little extra room in the output buffer. At the end of the
2954 * test, we'll check that the implementation didn't overwrite onto
2955 * this extra room. */
2956 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2957 size_t mac_buffer_size =
2958 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2959 size_t mac_length = 0;
2960
2961 memset( actual_mac, '+', sizeof( actual_mac ) );
2962 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2963 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2964
Gilles Peskine8817f612018-12-18 00:18:46 +01002965 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002966
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002967 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002968 psa_set_key_algorithm( &attributes, alg );
2969 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002970
Gilles Peskine73676cb2019-05-15 20:15:10 +02002971 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002972
2973 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002974 PSA_ASSERT( psa_mac_sign_setup( &operation,
2975 handle, alg ) );
2976 PSA_ASSERT( psa_mac_update( &operation,
2977 input->x, input->len ) );
2978 PSA_ASSERT( psa_mac_sign_finish( &operation,
2979 actual_mac, mac_buffer_size,
2980 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002981
2982 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002983 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2984 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002985
2986 /* Verify that the end of the buffer is untouched. */
2987 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2988 sizeof( actual_mac ) - mac_length ) );
2989
2990exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002991 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002992 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002993}
2994/* END_CASE */
2995
2996/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002997void mac_verify( int key_type_arg,
2998 data_t *key,
2999 int alg_arg,
3000 data_t *input,
3001 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003003 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003004 psa_key_type_t key_type = key_type_arg;
3005 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003006 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008
Gilles Peskine69c12672018-06-28 00:07:19 +02003009 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3010
Gilles Peskine8817f612018-12-18 00:18:46 +01003011 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003012
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003013 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003014 psa_set_key_algorithm( &attributes, alg );
3015 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003016
Gilles Peskine73676cb2019-05-15 20:15:10 +02003017 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003018
Gilles Peskine8817f612018-12-18 00:18:46 +01003019 PSA_ASSERT( psa_mac_verify_setup( &operation,
3020 handle, alg ) );
3021 PSA_ASSERT( psa_destroy_key( handle ) );
3022 PSA_ASSERT( psa_mac_update( &operation,
3023 input->x, input->len ) );
3024 PSA_ASSERT( psa_mac_verify_finish( &operation,
3025 expected_mac->x,
3026 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003027
3028exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003029 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003030 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003031}
3032/* END_CASE */
3033
3034/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003035void cipher_operation_init( )
3036{
Jaeden Ameroab439972019-02-15 14:12:05 +00003037 const uint8_t input[1] = { 0 };
3038 unsigned char output[1] = { 0 };
3039 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003040 /* Test each valid way of initializing the object, except for `= {0}`, as
3041 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3042 * though it's OK by the C standard. We could test for this, but we'd need
3043 * to supress the Clang warning for the test. */
3044 psa_cipher_operation_t func = psa_cipher_operation_init( );
3045 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3046 psa_cipher_operation_t zero;
3047
3048 memset( &zero, 0, sizeof( zero ) );
3049
Jaeden Ameroab439972019-02-15 14:12:05 +00003050 /* A freshly-initialized cipher operation should not be usable. */
3051 TEST_EQUAL( psa_cipher_update( &func,
3052 input, sizeof( input ),
3053 output, sizeof( output ),
3054 &output_length ),
3055 PSA_ERROR_BAD_STATE );
3056 TEST_EQUAL( psa_cipher_update( &init,
3057 input, sizeof( input ),
3058 output, sizeof( output ),
3059 &output_length ),
3060 PSA_ERROR_BAD_STATE );
3061 TEST_EQUAL( psa_cipher_update( &zero,
3062 input, sizeof( input ),
3063 output, sizeof( output ),
3064 &output_length ),
3065 PSA_ERROR_BAD_STATE );
3066
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003067 /* A default cipher operation should be abortable without error. */
3068 PSA_ASSERT( psa_cipher_abort( &func ) );
3069 PSA_ASSERT( psa_cipher_abort( &init ) );
3070 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003071}
3072/* END_CASE */
3073
3074/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003075void cipher_setup( int key_type_arg,
3076 data_t *key,
3077 int alg_arg,
3078 int expected_status_arg )
3079{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003080 psa_key_type_t key_type = key_type_arg;
3081 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003082 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003083 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003084 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003085#if defined(KNOWN_SUPPORTED_MAC_ALG)
3086 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3087#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003090
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003091 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3092 &operation, &status ) )
3093 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003094 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003095
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003096 /* The operation object should be reusable. */
3097#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3098 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3099 smoke_test_key_data,
3100 sizeof( smoke_test_key_data ),
3101 KNOWN_SUPPORTED_CIPHER_ALG,
3102 &operation, &status ) )
3103 goto exit;
3104 TEST_EQUAL( status, PSA_SUCCESS );
3105#endif
3106
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003107exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003108 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003109}
3110/* END_CASE */
3111
3112/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003113void cipher_bad_order( )
3114{
3115 psa_key_handle_t handle = 0;
3116 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3117 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003119 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3120 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3121 const uint8_t key[] = {
3122 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3123 0xaa, 0xaa, 0xaa, 0xaa };
3124 const uint8_t text[] = {
3125 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3126 0xbb, 0xbb, 0xbb, 0xbb };
3127 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3128 size_t length = 0;
3129
3130 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003131 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3132 psa_set_key_algorithm( &attributes, alg );
3133 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003134 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003135
3136
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003137 /* Call encrypt setup twice in a row. */
3138 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3139 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
3140 PSA_ERROR_BAD_STATE );
3141 PSA_ASSERT( psa_cipher_abort( &operation ) );
3142
3143 /* Call decrypt setup twice in a row. */
3144 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
3145 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
3146 PSA_ERROR_BAD_STATE );
3147 PSA_ASSERT( psa_cipher_abort( &operation ) );
3148
Jaeden Ameroab439972019-02-15 14:12:05 +00003149 /* Generate an IV without calling setup beforehand. */
3150 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3151 buffer, sizeof( buffer ),
3152 &length ),
3153 PSA_ERROR_BAD_STATE );
3154 PSA_ASSERT( psa_cipher_abort( &operation ) );
3155
3156 /* Generate an IV twice in a row. */
3157 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3158 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3159 buffer, sizeof( buffer ),
3160 &length ) );
3161 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3162 buffer, sizeof( buffer ),
3163 &length ),
3164 PSA_ERROR_BAD_STATE );
3165 PSA_ASSERT( psa_cipher_abort( &operation ) );
3166
3167 /* Generate an IV after it's already set. */
3168 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3169 PSA_ASSERT( psa_cipher_set_iv( &operation,
3170 iv, sizeof( iv ) ) );
3171 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3172 buffer, sizeof( buffer ),
3173 &length ),
3174 PSA_ERROR_BAD_STATE );
3175 PSA_ASSERT( psa_cipher_abort( &operation ) );
3176
3177 /* Set an IV without calling setup beforehand. */
3178 TEST_EQUAL( psa_cipher_set_iv( &operation,
3179 iv, sizeof( iv ) ),
3180 PSA_ERROR_BAD_STATE );
3181 PSA_ASSERT( psa_cipher_abort( &operation ) );
3182
3183 /* Set an IV after it's already set. */
3184 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3185 PSA_ASSERT( psa_cipher_set_iv( &operation,
3186 iv, sizeof( iv ) ) );
3187 TEST_EQUAL( psa_cipher_set_iv( &operation,
3188 iv, sizeof( iv ) ),
3189 PSA_ERROR_BAD_STATE );
3190 PSA_ASSERT( psa_cipher_abort( &operation ) );
3191
3192 /* Set an IV after it's already generated. */
3193 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3194 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3195 buffer, sizeof( buffer ),
3196 &length ) );
3197 TEST_EQUAL( psa_cipher_set_iv( &operation,
3198 iv, sizeof( iv ) ),
3199 PSA_ERROR_BAD_STATE );
3200 PSA_ASSERT( psa_cipher_abort( &operation ) );
3201
3202 /* Call update without calling setup beforehand. */
3203 TEST_EQUAL( psa_cipher_update( &operation,
3204 text, sizeof( text ),
3205 buffer, sizeof( buffer ),
3206 &length ),
3207 PSA_ERROR_BAD_STATE );
3208 PSA_ASSERT( psa_cipher_abort( &operation ) );
3209
3210 /* Call update without an IV where an IV is required. */
3211 TEST_EQUAL( psa_cipher_update( &operation,
3212 text, sizeof( text ),
3213 buffer, sizeof( buffer ),
3214 &length ),
3215 PSA_ERROR_BAD_STATE );
3216 PSA_ASSERT( psa_cipher_abort( &operation ) );
3217
3218 /* Call update after finish. */
3219 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3220 PSA_ASSERT( psa_cipher_set_iv( &operation,
3221 iv, sizeof( iv ) ) );
3222 PSA_ASSERT( psa_cipher_finish( &operation,
3223 buffer, sizeof( buffer ), &length ) );
3224 TEST_EQUAL( psa_cipher_update( &operation,
3225 text, sizeof( text ),
3226 buffer, sizeof( buffer ),
3227 &length ),
3228 PSA_ERROR_BAD_STATE );
3229 PSA_ASSERT( psa_cipher_abort( &operation ) );
3230
3231 /* Call finish without calling setup beforehand. */
3232 TEST_EQUAL( psa_cipher_finish( &operation,
3233 buffer, sizeof( buffer ), &length ),
3234 PSA_ERROR_BAD_STATE );
3235 PSA_ASSERT( psa_cipher_abort( &operation ) );
3236
3237 /* Call finish without an IV where an IV is required. */
3238 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3239 /* Not calling update means we are encrypting an empty buffer, which is OK
3240 * for cipher modes with padding. */
3241 TEST_EQUAL( psa_cipher_finish( &operation,
3242 buffer, sizeof( buffer ), &length ),
3243 PSA_ERROR_BAD_STATE );
3244 PSA_ASSERT( psa_cipher_abort( &operation ) );
3245
3246 /* Call finish twice in a row. */
3247 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3248 PSA_ASSERT( psa_cipher_set_iv( &operation,
3249 iv, sizeof( iv ) ) );
3250 PSA_ASSERT( psa_cipher_finish( &operation,
3251 buffer, sizeof( buffer ), &length ) );
3252 TEST_EQUAL( psa_cipher_finish( &operation,
3253 buffer, sizeof( buffer ), &length ),
3254 PSA_ERROR_BAD_STATE );
3255 PSA_ASSERT( psa_cipher_abort( &operation ) );
3256
Gilles Peskine76b29a72019-05-28 14:08:50 +02003257 PSA_ASSERT( psa_destroy_key( handle ) );
3258
Jaeden Ameroab439972019-02-15 14:12:05 +00003259exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003260 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003261}
3262/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003263
Gilles Peskine50e586b2018-06-08 14:28:46 +02003264/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003265void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003266 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003267 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003268 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003269{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003270 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003271 psa_status_t status;
3272 psa_key_type_t key_type = key_type_arg;
3273 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003274 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003275 unsigned char *output = NULL;
3276 size_t output_buffer_size = 0;
3277 size_t function_output_length = 0;
3278 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003279 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003281
Gilles Peskine8817f612018-12-18 00:18:46 +01003282 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003283
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003284 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3285 psa_set_key_algorithm( &attributes, alg );
3286 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003287
Gilles Peskine73676cb2019-05-15 20:15:10 +02003288 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003289
Gilles Peskine8817f612018-12-18 00:18:46 +01003290 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3291 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003292
Gilles Peskine423005e2019-05-06 15:22:57 +02003293 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003294 output_buffer_size = ( (size_t) input->len +
3295 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003296 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003297
Gilles Peskine8817f612018-12-18 00:18:46 +01003298 PSA_ASSERT( psa_cipher_update( &operation,
3299 input->x, input->len,
3300 output, output_buffer_size,
3301 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003302 total_output_length += function_output_length;
3303 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003304 output + total_output_length,
3305 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003306 &function_output_length );
3307 total_output_length += function_output_length;
3308
Gilles Peskinefe11b722018-12-18 00:24:04 +01003309 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003310 if( expected_status == PSA_SUCCESS )
3311 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003312 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003313 ASSERT_COMPARE( expected_output->x, expected_output->len,
3314 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003315 }
3316
3317exit:
3318 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003319 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003320 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003321}
3322/* END_CASE */
3323
3324/* BEGIN_CASE */
3325void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003326 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003327 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003328 int first_part_size_arg,
3329 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003330 data_t *expected_output )
3331{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003332 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003333 psa_key_type_t key_type = key_type_arg;
3334 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003335 size_t first_part_size = first_part_size_arg;
3336 size_t output1_length = output1_length_arg;
3337 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003338 unsigned char *output = NULL;
3339 size_t output_buffer_size = 0;
3340 size_t function_output_length = 0;
3341 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003342 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003344
Gilles Peskine8817f612018-12-18 00:18:46 +01003345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003346
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3348 psa_set_key_algorithm( &attributes, alg );
3349 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003350
Gilles Peskine73676cb2019-05-15 20:15:10 +02003351 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003352
Gilles Peskine8817f612018-12-18 00:18:46 +01003353 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3354 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003355
Gilles Peskine423005e2019-05-06 15:22:57 +02003356 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003357 output_buffer_size = ( (size_t) input->len +
3358 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003359 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003360
Gilles Peskinee0866522019-02-19 19:44:00 +01003361 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003362 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3363 output, output_buffer_size,
3364 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003365 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003366 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003367 PSA_ASSERT( psa_cipher_update( &operation,
3368 input->x + first_part_size,
3369 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003370 output + total_output_length,
3371 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003372 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003373 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003374 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003375 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003376 output + total_output_length,
3377 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003378 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003379 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003380 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003381
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003382 ASSERT_COMPARE( expected_output->x, expected_output->len,
3383 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003384
3385exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003386 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003387 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003388 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003389}
3390/* END_CASE */
3391
3392/* BEGIN_CASE */
3393void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003394 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003395 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003396 int first_part_size_arg,
3397 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003398 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003399{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003400 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003401
3402 psa_key_type_t key_type = key_type_arg;
3403 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003404 size_t first_part_size = first_part_size_arg;
3405 size_t output1_length = output1_length_arg;
3406 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003407 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003408 size_t output_buffer_size = 0;
3409 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003410 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003411 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003413
Gilles Peskine8817f612018-12-18 00:18:46 +01003414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003415
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003416 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3417 psa_set_key_algorithm( &attributes, alg );
3418 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003419
Gilles Peskine73676cb2019-05-15 20:15:10 +02003420 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003421
Gilles Peskine8817f612018-12-18 00:18:46 +01003422 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3423 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003424
Gilles Peskine423005e2019-05-06 15:22:57 +02003425 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003426
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003427 output_buffer_size = ( (size_t) input->len +
3428 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003429 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003430
Gilles Peskinee0866522019-02-19 19:44:00 +01003431 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003432 PSA_ASSERT( psa_cipher_update( &operation,
3433 input->x, first_part_size,
3434 output, output_buffer_size,
3435 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003436 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003437 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003438 PSA_ASSERT( psa_cipher_update( &operation,
3439 input->x + first_part_size,
3440 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003441 output + total_output_length,
3442 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003444 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003445 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003446 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003447 output + total_output_length,
3448 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003449 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003450 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003451 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003452
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003453 ASSERT_COMPARE( expected_output->x, expected_output->len,
3454 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003455
3456exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003457 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003458 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003459 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003460}
3461/* END_CASE */
3462
Gilles Peskine50e586b2018-06-08 14:28:46 +02003463/* BEGIN_CASE */
3464void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003465 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003466 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003467 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003468{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003469 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003470 psa_status_t status;
3471 psa_key_type_t key_type = key_type_arg;
3472 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003473 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003474 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003475 size_t output_buffer_size = 0;
3476 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003477 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003478 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003479 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003480
Gilles Peskine8817f612018-12-18 00:18:46 +01003481 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003482
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003483 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3484 psa_set_key_algorithm( &attributes, alg );
3485 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003486
Gilles Peskine73676cb2019-05-15 20:15:10 +02003487 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003488
Gilles Peskine8817f612018-12-18 00:18:46 +01003489 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3490 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003491
Gilles Peskine423005e2019-05-06 15:22:57 +02003492 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003493
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003494 output_buffer_size = ( (size_t) input->len +
3495 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003496 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003497
Gilles Peskine8817f612018-12-18 00:18:46 +01003498 PSA_ASSERT( psa_cipher_update( &operation,
3499 input->x, input->len,
3500 output, output_buffer_size,
3501 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003502 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003503 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003504 output + total_output_length,
3505 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003506 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003507 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003508 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003509
3510 if( expected_status == PSA_SUCCESS )
3511 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003513 ASSERT_COMPARE( expected_output->x, expected_output->len,
3514 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003515 }
3516
Gilles Peskine50e586b2018-06-08 14:28:46 +02003517exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003518 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003519 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003520 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003521}
3522/* END_CASE */
3523
Gilles Peskine50e586b2018-06-08 14:28:46 +02003524/* BEGIN_CASE */
3525void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003526 data_t *key,
3527 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003528{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003529 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003530 psa_key_type_t key_type = key_type_arg;
3531 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003532 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003533 size_t iv_size = 16;
3534 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003535 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003536 size_t output1_size = 0;
3537 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003538 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003539 size_t output2_size = 0;
3540 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003541 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003542 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3543 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003545
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003547
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003548 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3549 psa_set_key_algorithm( &attributes, alg );
3550 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003551
Gilles Peskine73676cb2019-05-15 20:15:10 +02003552 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003553
Gilles Peskine8817f612018-12-18 00:18:46 +01003554 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3555 handle, alg ) );
3556 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3557 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003558
Gilles Peskine8817f612018-12-18 00:18:46 +01003559 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3560 iv, iv_size,
3561 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003562 output1_size = ( (size_t) input->len +
3563 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003564 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003565
Gilles Peskine8817f612018-12-18 00:18:46 +01003566 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3567 output1, output1_size,
3568 &output1_length ) );
3569 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003570 output1 + output1_length,
3571 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003572 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003573
Gilles Peskine048b7f02018-06-08 14:20:49 +02003574 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003575
Gilles Peskine8817f612018-12-18 00:18:46 +01003576 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003577
3578 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003579 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003580
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3582 iv, iv_length ) );
3583 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3584 output2, output2_size,
3585 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003586 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003587 PSA_ASSERT( psa_cipher_finish( &operation2,
3588 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003589 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003590 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003591
Gilles Peskine048b7f02018-06-08 14:20:49 +02003592 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003593
Gilles Peskine8817f612018-12-18 00:18:46 +01003594 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003595
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003596 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003597
3598exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003599 mbedtls_free( output1 );
3600 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003601 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003602 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003603}
3604/* END_CASE */
3605
3606/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003607void cipher_verify_output_multipart( int alg_arg,
3608 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003609 data_t *key,
3610 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003611 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003612{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003613 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003614 psa_key_type_t key_type = key_type_arg;
3615 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003616 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003617 unsigned char iv[16] = {0};
3618 size_t iv_size = 16;
3619 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003620 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003621 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003622 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003623 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003624 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003625 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003626 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003627 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3628 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003629 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003630
Gilles Peskine8817f612018-12-18 00:18:46 +01003631 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003632
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003633 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3634 psa_set_key_algorithm( &attributes, alg );
3635 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003636
Gilles Peskine73676cb2019-05-15 20:15:10 +02003637 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003638
Gilles Peskine8817f612018-12-18 00:18:46 +01003639 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3640 handle, alg ) );
3641 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3642 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003643
Gilles Peskine8817f612018-12-18 00:18:46 +01003644 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3645 iv, iv_size,
3646 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003647 output1_buffer_size = ( (size_t) input->len +
3648 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003649 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003650
Gilles Peskinee0866522019-02-19 19:44:00 +01003651 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003652
Gilles Peskine8817f612018-12-18 00:18:46 +01003653 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3654 output1, output1_buffer_size,
3655 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003656 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003657
Gilles Peskine8817f612018-12-18 00:18:46 +01003658 PSA_ASSERT( psa_cipher_update( &operation1,
3659 input->x + first_part_size,
3660 input->len - first_part_size,
3661 output1, output1_buffer_size,
3662 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003663 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003664
Gilles Peskine8817f612018-12-18 00:18:46 +01003665 PSA_ASSERT( psa_cipher_finish( &operation1,
3666 output1 + output1_length,
3667 output1_buffer_size - output1_length,
3668 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003669 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003670
Gilles Peskine8817f612018-12-18 00:18:46 +01003671 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003672
Gilles Peskine048b7f02018-06-08 14:20:49 +02003673 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003674 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003675
Gilles Peskine8817f612018-12-18 00:18:46 +01003676 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3677 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003678
Gilles Peskine8817f612018-12-18 00:18:46 +01003679 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3680 output2, output2_buffer_size,
3681 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003682 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003683
Gilles Peskine8817f612018-12-18 00:18:46 +01003684 PSA_ASSERT( psa_cipher_update( &operation2,
3685 output1 + first_part_size,
3686 output1_length - first_part_size,
3687 output2, output2_buffer_size,
3688 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003689 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003690
Gilles Peskine8817f612018-12-18 00:18:46 +01003691 PSA_ASSERT( psa_cipher_finish( &operation2,
3692 output2 + output2_length,
3693 output2_buffer_size - output2_length,
3694 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003695 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003696
Gilles Peskine8817f612018-12-18 00:18:46 +01003697 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003698
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003699 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003700
3701exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003702 mbedtls_free( output1 );
3703 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003704 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003705 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003706}
3707/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003708
Gilles Peskine20035e32018-02-03 22:44:14 +01003709/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003710void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003711 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003712 data_t *nonce,
3713 data_t *additional_data,
3714 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003715 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003716{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003717 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003718 psa_key_type_t key_type = key_type_arg;
3719 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003720 unsigned char *output_data = NULL;
3721 size_t output_size = 0;
3722 size_t output_length = 0;
3723 unsigned char *output_data2 = NULL;
3724 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003725 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003726 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003727 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003728
Gilles Peskine4abf7412018-06-18 16:35:34 +02003729 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003730 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3731 * should be exact. */
3732 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3733 TEST_EQUAL( output_size,
3734 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003735 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003736
Gilles Peskine8817f612018-12-18 00:18:46 +01003737 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003738
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003739 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3740 psa_set_key_algorithm( &attributes, alg );
3741 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003742
Gilles Peskine049c7532019-05-15 20:22:09 +02003743 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3744 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003745
Gilles Peskinefe11b722018-12-18 00:24:04 +01003746 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3747 nonce->x, nonce->len,
3748 additional_data->x,
3749 additional_data->len,
3750 input_data->x, input_data->len,
3751 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003752 &output_length ),
3753 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003754
3755 if( PSA_SUCCESS == expected_result )
3756 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003757 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003758
Gilles Peskine003a4a92019-05-14 16:09:40 +02003759 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3760 * should be exact. */
3761 TEST_EQUAL( input_data->len,
3762 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3763
Gilles Peskinefe11b722018-12-18 00:24:04 +01003764 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3765 nonce->x, nonce->len,
3766 additional_data->x,
3767 additional_data->len,
3768 output_data, output_length,
3769 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003770 &output_length2 ),
3771 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003772
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003773 ASSERT_COMPARE( input_data->x, input_data->len,
3774 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003775 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003776
Gilles Peskinea1cac842018-06-11 19:33:02 +02003777exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003778 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003779 mbedtls_free( output_data );
3780 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003781 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003782}
3783/* END_CASE */
3784
3785/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003786void aead_encrypt( int key_type_arg, data_t *key_data,
3787 int alg_arg,
3788 data_t *nonce,
3789 data_t *additional_data,
3790 data_t *input_data,
3791 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003792{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003793 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003794 psa_key_type_t key_type = key_type_arg;
3795 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003796 unsigned char *output_data = NULL;
3797 size_t output_size = 0;
3798 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003799 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003801
Gilles Peskine4abf7412018-06-18 16:35:34 +02003802 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003803 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3804 * should be exact. */
3805 TEST_EQUAL( output_size,
3806 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003807 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003808
Gilles Peskine8817f612018-12-18 00:18:46 +01003809 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003810
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003811 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3812 psa_set_key_algorithm( &attributes, alg );
3813 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003814
Gilles Peskine049c7532019-05-15 20:22:09 +02003815 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3816 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003817
Gilles Peskine8817f612018-12-18 00:18:46 +01003818 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3819 nonce->x, nonce->len,
3820 additional_data->x, additional_data->len,
3821 input_data->x, input_data->len,
3822 output_data, output_size,
3823 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003824
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003825 ASSERT_COMPARE( expected_result->x, expected_result->len,
3826 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003827
Gilles Peskinea1cac842018-06-11 19:33:02 +02003828exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003829 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003830 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003831 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003832}
3833/* END_CASE */
3834
3835/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003836void aead_decrypt( int key_type_arg, data_t *key_data,
3837 int alg_arg,
3838 data_t *nonce,
3839 data_t *additional_data,
3840 data_t *input_data,
3841 data_t *expected_data,
3842 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003843{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003844 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003845 psa_key_type_t key_type = key_type_arg;
3846 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003847 unsigned char *output_data = NULL;
3848 size_t output_size = 0;
3849 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003850 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003851 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003852 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003853
Gilles Peskine003a4a92019-05-14 16:09:40 +02003854 output_size = input_data->len - tag_length;
3855 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3856 * should be exact. */
3857 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3858 TEST_EQUAL( output_size,
3859 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003860 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003861
Gilles Peskine8817f612018-12-18 00:18:46 +01003862 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003863
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003864 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3865 psa_set_key_algorithm( &attributes, alg );
3866 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003867
Gilles Peskine049c7532019-05-15 20:22:09 +02003868 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3869 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003870
Gilles Peskinefe11b722018-12-18 00:24:04 +01003871 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3872 nonce->x, nonce->len,
3873 additional_data->x,
3874 additional_data->len,
3875 input_data->x, input_data->len,
3876 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003877 &output_length ),
3878 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003879
Gilles Peskine2d277862018-06-18 15:41:12 +02003880 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003881 ASSERT_COMPARE( expected_data->x, expected_data->len,
3882 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003883
Gilles Peskinea1cac842018-06-11 19:33:02 +02003884exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003885 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003886 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003887 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003888}
3889/* END_CASE */
3890
3891/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003892void signature_size( int type_arg,
3893 int bits,
3894 int alg_arg,
3895 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003896{
3897 psa_key_type_t type = type_arg;
3898 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003899 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003900
Gilles Peskinefe11b722018-12-18 00:24:04 +01003901 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003902#if defined(MBEDTLS_TEST_DEPRECATED)
3903 TEST_EQUAL( actual_size,
3904 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
3905#endif /* MBEDTLS_TEST_DEPRECATED */
3906
Gilles Peskinee59236f2018-01-27 23:32:46 +01003907exit:
3908 ;
3909}
3910/* END_CASE */
3911
3912/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003913void sign_deterministic( int key_type_arg, data_t *key_data,
3914 int alg_arg, data_t *input_data,
3915 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003916{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003917 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003918 psa_key_type_t key_type = key_type_arg;
3919 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003920 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003921 unsigned char *signature = NULL;
3922 size_t signature_size;
3923 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003925
Gilles Peskine8817f612018-12-18 00:18:46 +01003926 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003927
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003928 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003929 psa_set_key_algorithm( &attributes, alg );
3930 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003931
Gilles Peskine049c7532019-05-15 20:22:09 +02003932 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3933 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003934 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3935 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003936
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003937 /* Allocate a buffer which has the size advertized by the
3938 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003939 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003940 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003941 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003942 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003943 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003944
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003945 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003946 PSA_ASSERT( psa_sign_hash( handle, alg,
3947 input_data->x, input_data->len,
3948 signature, signature_size,
3949 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003950 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003951 ASSERT_COMPARE( output_data->x, output_data->len,
3952 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003953
Gilles Peskine0627f982019-11-26 19:12:16 +01003954#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01003955 memset( signature, 0, signature_size );
3956 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01003957 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3958 input_data->x, input_data->len,
3959 signature, signature_size,
3960 &signature_length ) );
3961 ASSERT_COMPARE( output_data->x, output_data->len,
3962 signature, signature_length );
3963#endif /* MBEDTLS_TEST_DEPRECATED */
3964
Gilles Peskine20035e32018-02-03 22:44:14 +01003965exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003966 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003967 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003968 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003969 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003970}
3971/* END_CASE */
3972
3973/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003974void sign_fail( int key_type_arg, data_t *key_data,
3975 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003976 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003977{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003978 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003979 psa_key_type_t key_type = key_type_arg;
3980 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003981 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003982 psa_status_t actual_status;
3983 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003984 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003985 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003987
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003988 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003989
Gilles Peskine8817f612018-12-18 00:18:46 +01003990 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003991
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003992 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003993 psa_set_key_algorithm( &attributes, alg );
3994 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003995
Gilles Peskine049c7532019-05-15 20:22:09 +02003996 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3997 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003998
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003999 actual_status = psa_sign_hash( handle, alg,
4000 input_data->x, input_data->len,
4001 signature, signature_size,
4002 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004003 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004004 /* The value of *signature_length is unspecified on error, but
4005 * whatever it is, it should be less than signature_size, so that
4006 * if the caller tries to read *signature_length bytes without
4007 * checking the error code then they don't overflow a buffer. */
4008 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004009
Gilles Peskine895242b2019-11-29 12:15:40 +01004010#if defined(MBEDTLS_TEST_DEPRECATED)
4011 signature_length = INVALID_EXPORT_LENGTH;
4012 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
4013 input_data->x, input_data->len,
4014 signature, signature_size,
4015 &signature_length ),
4016 expected_status );
4017 TEST_ASSERT( signature_length <= signature_size );
4018#endif /* MBEDTLS_TEST_DEPRECATED */
4019
Gilles Peskine20035e32018-02-03 22:44:14 +01004020exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004021 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004022 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01004023 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004024 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004025}
4026/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004027
4028/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004029void sign_verify( int key_type_arg, data_t *key_data,
4030 int alg_arg, data_t *input_data )
4031{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004032 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02004033 psa_key_type_t key_type = key_type_arg;
4034 psa_algorithm_t alg = alg_arg;
4035 size_t key_bits;
4036 unsigned char *signature = NULL;
4037 size_t signature_size;
4038 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004039 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004040
Gilles Peskine8817f612018-12-18 00:18:46 +01004041 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004042
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004043 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004044 psa_set_key_algorithm( &attributes, alg );
4045 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004046
Gilles Peskine049c7532019-05-15 20:22:09 +02004047 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4048 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004049 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4050 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004051
4052 /* Allocate a buffer which has the size advertized by the
4053 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004054 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004055 key_bits, alg );
4056 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004057 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004058 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004059
4060 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004061 PSA_ASSERT( psa_sign_hash( handle, alg,
4062 input_data->x, input_data->len,
4063 signature, signature_size,
4064 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004065 /* Check that the signature length looks sensible. */
4066 TEST_ASSERT( signature_length <= signature_size );
4067 TEST_ASSERT( signature_length > 0 );
4068
4069 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004070 PSA_ASSERT( psa_verify_hash( handle, alg,
4071 input_data->x, input_data->len,
4072 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004073
4074 if( input_data->len != 0 )
4075 {
4076 /* Flip a bit in the input and verify that the signature is now
4077 * detected as invalid. Flip a bit at the beginning, not at the end,
4078 * because ECDSA may ignore the last few bits of the input. */
4079 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004080 TEST_EQUAL( psa_verify_hash( handle, alg,
4081 input_data->x, input_data->len,
4082 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004083 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004084 }
4085
4086exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004087 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004088 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02004089 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004090 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004091}
4092/* END_CASE */
4093
4094/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004095void asymmetric_verify( int key_type_arg, data_t *key_data,
4096 int alg_arg, data_t *hash_data,
4097 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004098{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004099 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03004100 psa_key_type_t key_type = key_type_arg;
4101 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004102 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004103
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004104 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004105
Gilles Peskine8817f612018-12-18 00:18:46 +01004106 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004107
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004108 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004109 psa_set_key_algorithm( &attributes, alg );
4110 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004111
Gilles Peskine049c7532019-05-15 20:22:09 +02004112 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4113 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03004114
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004115 PSA_ASSERT( psa_verify_hash( handle, alg,
4116 hash_data->x, hash_data->len,
4117 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004118
4119#if defined(MBEDTLS_TEST_DEPRECATED)
4120 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
4121 hash_data->x, hash_data->len,
4122 signature_data->x,
4123 signature_data->len ) );
4124
4125#endif /* MBEDTLS_TEST_DEPRECATED */
4126
itayzafrir5c753392018-05-08 11:18:38 +03004127exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004128 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004129 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004130 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004131}
4132/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004133
4134/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004135void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4136 int alg_arg, data_t *hash_data,
4137 data_t *signature_data,
4138 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004139{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004140 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004141 psa_key_type_t key_type = key_type_arg;
4142 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004143 psa_status_t actual_status;
4144 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004146
Gilles Peskine8817f612018-12-18 00:18:46 +01004147 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004148
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004149 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004150 psa_set_key_algorithm( &attributes, alg );
4151 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004152
Gilles Peskine049c7532019-05-15 20:22:09 +02004153 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4154 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004155
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004156 actual_status = psa_verify_hash( handle, alg,
4157 hash_data->x, hash_data->len,
4158 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004159 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004160
Gilles Peskine895242b2019-11-29 12:15:40 +01004161#if defined(MBEDTLS_TEST_DEPRECATED)
4162 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
4163 hash_data->x, hash_data->len,
4164 signature_data->x, signature_data->len ),
4165 expected_status );
4166#endif /* MBEDTLS_TEST_DEPRECATED */
4167
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004168exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004169 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004170 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004171 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004172}
4173/* END_CASE */
4174
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004175/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004176void asymmetric_encrypt( int key_type_arg,
4177 data_t *key_data,
4178 int alg_arg,
4179 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004180 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004181 int expected_output_length_arg,
4182 int expected_status_arg )
4183{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004184 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004185 psa_key_type_t key_type = key_type_arg;
4186 psa_algorithm_t alg = alg_arg;
4187 size_t expected_output_length = expected_output_length_arg;
4188 size_t key_bits;
4189 unsigned char *output = NULL;
4190 size_t output_size;
4191 size_t output_length = ~0;
4192 psa_status_t actual_status;
4193 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004194 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004195
Gilles Peskine8817f612018-12-18 00:18:46 +01004196 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004197
Gilles Peskine656896e2018-06-29 19:12:28 +02004198 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004199 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4200 psa_set_key_algorithm( &attributes, alg );
4201 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004202 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4203 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004204
4205 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004206 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4207 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004208 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004209 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004210
4211 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004212 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004213 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004214 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004215 output, output_size,
4216 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004217 TEST_EQUAL( actual_status, expected_status );
4218 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004219
Gilles Peskine68428122018-06-30 18:42:41 +02004220 /* If the label is empty, the test framework puts a non-null pointer
4221 * in label->x. Test that a null pointer works as well. */
4222 if( label->len == 0 )
4223 {
4224 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004225 if( output_size != 0 )
4226 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004227 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004228 input_data->x, input_data->len,
4229 NULL, label->len,
4230 output, output_size,
4231 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004232 TEST_EQUAL( actual_status, expected_status );
4233 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004234 }
4235
Gilles Peskine656896e2018-06-29 19:12:28 +02004236exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004237 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004238 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004239 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004240 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004241}
4242/* END_CASE */
4243
4244/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004245void asymmetric_encrypt_decrypt( int key_type_arg,
4246 data_t *key_data,
4247 int alg_arg,
4248 data_t *input_data,
4249 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004250{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004251 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004252 psa_key_type_t key_type = key_type_arg;
4253 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004254 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004255 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004256 size_t output_size;
4257 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004258 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004259 size_t output2_size;
4260 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004261 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004262
Gilles Peskine8817f612018-12-18 00:18:46 +01004263 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004264
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004265 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4266 psa_set_key_algorithm( &attributes, alg );
4267 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004268
Gilles Peskine049c7532019-05-15 20:22:09 +02004269 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4270 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004271
4272 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004273 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4274 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004275 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004276 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004277 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004278 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004279
Gilles Peskineeebd7382018-06-08 18:11:54 +02004280 /* We test encryption by checking that encrypt-then-decrypt gives back
4281 * the original plaintext because of the non-optional random
4282 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004283 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4284 input_data->x, input_data->len,
4285 label->x, label->len,
4286 output, output_size,
4287 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004288 /* We don't know what ciphertext length to expect, but check that
4289 * it looks sensible. */
4290 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004291
Gilles Peskine8817f612018-12-18 00:18:46 +01004292 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4293 output, output_length,
4294 label->x, label->len,
4295 output2, output2_size,
4296 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004297 ASSERT_COMPARE( input_data->x, input_data->len,
4298 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004299
4300exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004301 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004302 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004303 mbedtls_free( output );
4304 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004305 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004306}
4307/* END_CASE */
4308
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004309/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004310void asymmetric_decrypt( int key_type_arg,
4311 data_t *key_data,
4312 int alg_arg,
4313 data_t *input_data,
4314 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004315 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004316{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004317 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004318 psa_key_type_t key_type = key_type_arg;
4319 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004320 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004321 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004322 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004323 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004324
Jaeden Amero412654a2019-02-06 12:57:46 +00004325 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004326 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004327
Gilles Peskine8817f612018-12-18 00:18:46 +01004328 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004329
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004330 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4331 psa_set_key_algorithm( &attributes, alg );
4332 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004333
Gilles Peskine049c7532019-05-15 20:22:09 +02004334 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4335 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004336
Gilles Peskine8817f612018-12-18 00:18:46 +01004337 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4338 input_data->x, input_data->len,
4339 label->x, label->len,
4340 output,
4341 output_size,
4342 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004343 ASSERT_COMPARE( expected_data->x, expected_data->len,
4344 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004345
Gilles Peskine68428122018-06-30 18:42:41 +02004346 /* If the label is empty, the test framework puts a non-null pointer
4347 * in label->x. Test that a null pointer works as well. */
4348 if( label->len == 0 )
4349 {
4350 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004351 if( output_size != 0 )
4352 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004353 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4354 input_data->x, input_data->len,
4355 NULL, label->len,
4356 output,
4357 output_size,
4358 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004359 ASSERT_COMPARE( expected_data->x, expected_data->len,
4360 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004361 }
4362
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004363exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004364 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004365 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004366 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004367 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004368}
4369/* END_CASE */
4370
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004371/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004372void asymmetric_decrypt_fail( int key_type_arg,
4373 data_t *key_data,
4374 int alg_arg,
4375 data_t *input_data,
4376 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004377 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004378 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004379{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004380 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004381 psa_key_type_t key_type = key_type_arg;
4382 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004383 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004384 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004385 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004386 psa_status_t actual_status;
4387 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004388 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004389
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004390 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004391
Gilles Peskine8817f612018-12-18 00:18:46 +01004392 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004393
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004394 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4395 psa_set_key_algorithm( &attributes, alg );
4396 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004397
Gilles Peskine049c7532019-05-15 20:22:09 +02004398 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4399 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004400
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004401 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004402 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004403 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004404 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004405 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004406 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004407 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004408
Gilles Peskine68428122018-06-30 18:42:41 +02004409 /* If the label is empty, the test framework puts a non-null pointer
4410 * in label->x. Test that a null pointer works as well. */
4411 if( label->len == 0 )
4412 {
4413 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004414 if( output_size != 0 )
4415 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004416 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004417 input_data->x, input_data->len,
4418 NULL, label->len,
4419 output, output_size,
4420 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004421 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004422 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004423 }
4424
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004425exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004426 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004427 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004428 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004429 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004430}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004431/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004432
4433/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004434void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004435{
4436 /* Test each valid way of initializing the object, except for `= {0}`, as
4437 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4438 * though it's OK by the C standard. We could test for this, but we'd need
4439 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004440 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004441 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4442 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4443 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004444
4445 memset( &zero, 0, sizeof( zero ) );
4446
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004448 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004449 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004450 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004451 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004452 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004453 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004454
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004455 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004456 PSA_ASSERT( psa_key_derivation_abort(&func) );
4457 PSA_ASSERT( psa_key_derivation_abort(&init) );
4458 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004459}
4460/* END_CASE */
4461
Janos Follath16de4a42019-06-13 16:32:24 +01004462/* BEGIN_CASE */
4463void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004464{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004465 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004466 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004467 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004468
Gilles Peskine8817f612018-12-18 00:18:46 +01004469 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004470
Janos Follath16de4a42019-06-13 16:32:24 +01004471 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004472 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004473
4474exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004475 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004476 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004477}
4478/* END_CASE */
4479
Janos Follathaf3c2a02019-06-12 12:34:34 +01004480/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004481void derive_set_capacity( int alg_arg, int capacity_arg,
4482 int expected_status_arg )
4483{
4484 psa_algorithm_t alg = alg_arg;
4485 size_t capacity = capacity_arg;
4486 psa_status_t expected_status = expected_status_arg;
4487 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4488
4489 PSA_ASSERT( psa_crypto_init( ) );
4490
4491 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4492
4493 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4494 expected_status );
4495
4496exit:
4497 psa_key_derivation_abort( &operation );
4498 PSA_DONE( );
4499}
4500/* END_CASE */
4501
4502/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004503void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004504 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004505 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004506 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004507 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004508 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004509 int expected_status_arg3,
4510 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004511{
4512 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004513 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4514 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004515 psa_status_t expected_statuses[] = {expected_status_arg1,
4516 expected_status_arg2,
4517 expected_status_arg3};
4518 data_t *inputs[] = {input1, input2, input3};
4519 psa_key_handle_t handles[] = {0, 0, 0};
4520 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4522 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004523 psa_key_type_t output_key_type = output_key_type_arg;
4524 psa_key_handle_t output_handle = 0;
4525 psa_status_t expected_output_status = expected_output_status_arg;
4526 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004527
4528 PSA_ASSERT( psa_crypto_init( ) );
4529
4530 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4531 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004532
4533 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4534
4535 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4536 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004537 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004538 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004539 psa_set_key_type( &attributes, key_types[i] );
4540 PSA_ASSERT( psa_import_key( &attributes,
4541 inputs[i]->x, inputs[i]->len,
4542 &handles[i] ) );
4543 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4544 handles[i] ),
4545 expected_statuses[i] );
4546 }
4547 else
4548 {
4549 TEST_EQUAL( psa_key_derivation_input_bytes(
4550 &operation, steps[i],
4551 inputs[i]->x, inputs[i]->len ),
4552 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004553 }
4554 }
4555
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004556 if( output_key_type != PSA_KEY_TYPE_NONE )
4557 {
4558 psa_reset_key_attributes( &attributes );
4559 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4560 psa_set_key_bits( &attributes, 8 );
4561 actual_output_status =
4562 psa_key_derivation_output_key( &attributes, &operation,
4563 &output_handle );
4564 }
4565 else
4566 {
4567 uint8_t buffer[1];
4568 actual_output_status =
4569 psa_key_derivation_output_bytes( &operation,
4570 buffer, sizeof( buffer ) );
4571 }
4572 TEST_EQUAL( actual_output_status, expected_output_status );
4573
Janos Follathaf3c2a02019-06-12 12:34:34 +01004574exit:
4575 psa_key_derivation_abort( &operation );
4576 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4577 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004578 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004579 PSA_DONE( );
4580}
4581/* END_CASE */
4582
Janos Follathd958bb72019-07-03 15:02:16 +01004583/* BEGIN_CASE */
4584void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004585{
Janos Follathd958bb72019-07-03 15:02:16 +01004586 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004587 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004588 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004589 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004590 unsigned char input1[] = "Input 1";
4591 size_t input1_length = sizeof( input1 );
4592 unsigned char input2[] = "Input 2";
4593 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004594 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004595 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004596 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4597 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4598 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004600
Gilles Peskine8817f612018-12-18 00:18:46 +01004601 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004602
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004603 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4604 psa_set_key_algorithm( &attributes, alg );
4605 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004606
Gilles Peskine73676cb2019-05-15 20:15:10 +02004607 PSA_ASSERT( psa_import_key( &attributes,
4608 key_data, sizeof( key_data ),
4609 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004610
4611 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004612 if( !setup_key_derivation_wrap( &operation, handle, alg,
4613 input1, input1_length,
4614 input2, input2_length,
4615 capacity ) )
4616 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004617
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004618 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004619 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004620 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004621
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004623
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004624 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004625 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004626
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004627exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004628 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004629 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004630 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004631}
4632/* END_CASE */
4633
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004634/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004635void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004636{
4637 uint8_t output_buffer[16];
4638 size_t buffer_size = 16;
4639 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004640 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004641
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004642 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4643 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004644 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004645
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004646 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004647 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004648
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004649 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004650
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004651 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4652 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004653 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004654
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004655 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004656 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004657
4658exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004660}
4661/* END_CASE */
4662
4663/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004664void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004665 int step1_arg, data_t *input1,
4666 int step2_arg, data_t *input2,
4667 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004668 int requested_capacity_arg,
4669 data_t *expected_output1,
4670 data_t *expected_output2 )
4671{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004672 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004673 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4674 data_t *inputs[] = {input1, input2, input3};
4675 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004676 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004677 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004678 uint8_t *expected_outputs[2] =
4679 {expected_output1->x, expected_output2->x};
4680 size_t output_sizes[2] =
4681 {expected_output1->len, expected_output2->len};
4682 size_t output_buffer_size = 0;
4683 uint8_t *output_buffer = NULL;
4684 size_t expected_capacity;
4685 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004686 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004687 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004688 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004689
4690 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4691 {
4692 if( output_sizes[i] > output_buffer_size )
4693 output_buffer_size = output_sizes[i];
4694 if( output_sizes[i] == 0 )
4695 expected_outputs[i] = NULL;
4696 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004697 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004698 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004699
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004700 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4701 psa_set_key_algorithm( &attributes, alg );
4702 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004703
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004704 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004705 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4706 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4707 requested_capacity ) );
4708 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004709 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004710 switch( steps[i] )
4711 {
4712 case 0:
4713 break;
4714 case PSA_KEY_DERIVATION_INPUT_SECRET:
4715 PSA_ASSERT( psa_import_key( &attributes,
4716 inputs[i]->x, inputs[i]->len,
4717 &handles[i] ) );
4718 PSA_ASSERT( psa_key_derivation_input_key(
4719 &operation, steps[i],
4720 handles[i] ) );
4721 break;
4722 default:
4723 PSA_ASSERT( psa_key_derivation_input_bytes(
4724 &operation, steps[i],
4725 inputs[i]->x, inputs[i]->len ) );
4726 break;
4727 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004728 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004729
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004731 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004732 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004733 expected_capacity = requested_capacity;
4734
4735 /* Expansion phase. */
4736 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4737 {
4738 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004740 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004741 if( expected_capacity == 0 && output_sizes[i] == 0 )
4742 {
4743 /* Reading 0 bytes when 0 bytes are available can go either way. */
4744 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004745 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004746 continue;
4747 }
4748 else if( expected_capacity == 0 ||
4749 output_sizes[i] > expected_capacity )
4750 {
4751 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004752 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004753 expected_capacity = 0;
4754 continue;
4755 }
4756 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004757 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004758 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004759 ASSERT_COMPARE( output_buffer, output_sizes[i],
4760 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004761 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004762 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004763 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004764 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004765 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004766 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004767 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004768
4769exit:
4770 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004771 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004772 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4773 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004774 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004775}
4776/* END_CASE */
4777
4778/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004779void derive_full( int alg_arg,
4780 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004781 data_t *input1,
4782 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004783 int requested_capacity_arg )
4784{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004785 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004786 psa_algorithm_t alg = alg_arg;
4787 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004788 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004789 unsigned char output_buffer[16];
4790 size_t expected_capacity = requested_capacity;
4791 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004792 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004793
Gilles Peskine8817f612018-12-18 00:18:46 +01004794 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004795
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004796 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4797 psa_set_key_algorithm( &attributes, alg );
4798 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004799
Gilles Peskine049c7532019-05-15 20:22:09 +02004800 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4801 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004802
Janos Follathf2815ea2019-07-03 12:41:36 +01004803 if( !setup_key_derivation_wrap( &operation, handle, alg,
4804 input1->x, input1->len,
4805 input2->x, input2->len,
4806 requested_capacity ) )
4807 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004808
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004809 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004810 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004811 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004812
4813 /* Expansion phase. */
4814 while( current_capacity > 0 )
4815 {
4816 size_t read_size = sizeof( output_buffer );
4817 if( read_size > current_capacity )
4818 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004819 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004820 output_buffer,
4821 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004822 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004823 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004824 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004825 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004826 }
4827
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 /* Check that the operation refuses to go over capacity. */
4829 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004830 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004831
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004832 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004833
4834exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004836 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004837 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004838}
4839/* END_CASE */
4840
Janos Follathe60c9052019-07-03 13:51:30 +01004841/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004842void derive_key_exercise( int alg_arg,
4843 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004844 data_t *input1,
4845 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004846 int derived_type_arg,
4847 int derived_bits_arg,
4848 int derived_usage_arg,
4849 int derived_alg_arg )
4850{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004851 psa_key_handle_t base_handle = 0;
4852 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004853 psa_algorithm_t alg = alg_arg;
4854 psa_key_type_t derived_type = derived_type_arg;
4855 size_t derived_bits = derived_bits_arg;
4856 psa_key_usage_t derived_usage = derived_usage_arg;
4857 psa_algorithm_t derived_alg = derived_alg_arg;
4858 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004859 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004861 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004862
Gilles Peskine8817f612018-12-18 00:18:46 +01004863 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004864
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004865 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4866 psa_set_key_algorithm( &attributes, alg );
4867 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004868 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4869 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004870
4871 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004872 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4873 input1->x, input1->len,
4874 input2->x, input2->len, capacity ) )
4875 goto exit;
4876
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004877 psa_set_key_usage_flags( &attributes, derived_usage );
4878 psa_set_key_algorithm( &attributes, derived_alg );
4879 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004880 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004881 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004882 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004883
4884 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004885 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4886 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4887 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004888
4889 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004890 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004891 goto exit;
4892
4893exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004895 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004896 psa_destroy_key( base_handle );
4897 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004898 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004899}
4900/* END_CASE */
4901
Janos Follath42fd8882019-07-03 14:17:09 +01004902/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004903void derive_key_export( int alg_arg,
4904 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004905 data_t *input1,
4906 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004907 int bytes1_arg,
4908 int bytes2_arg )
4909{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004910 psa_key_handle_t base_handle = 0;
4911 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004912 psa_algorithm_t alg = alg_arg;
4913 size_t bytes1 = bytes1_arg;
4914 size_t bytes2 = bytes2_arg;
4915 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004917 uint8_t *output_buffer = NULL;
4918 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004919 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4920 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004921 size_t length;
4922
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004923 ASSERT_ALLOC( output_buffer, capacity );
4924 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004925 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004926
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004927 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4928 psa_set_key_algorithm( &base_attributes, alg );
4929 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004930 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4931 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004932
4933 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01004934 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4935 input1->x, input1->len,
4936 input2->x, input2->len, capacity ) )
4937 goto exit;
4938
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004939 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004940 output_buffer,
4941 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004942 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004943
4944 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01004945 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4946 input1->x, input1->len,
4947 input2->x, input2->len, capacity ) )
4948 goto exit;
4949
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004950 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4951 psa_set_key_algorithm( &derived_attributes, 0 );
4952 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004953 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004955 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004956 PSA_ASSERT( psa_export_key( derived_handle,
4957 export_buffer, bytes1,
4958 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004959 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004960 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004961 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004963 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004964 PSA_ASSERT( psa_export_key( derived_handle,
4965 export_buffer + bytes1, bytes2,
4966 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004967 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004968
4969 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004970 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4971 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004972
4973exit:
4974 mbedtls_free( output_buffer );
4975 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004977 psa_destroy_key( base_handle );
4978 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004979 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004980}
4981/* END_CASE */
4982
4983/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004984void derive_key( int alg_arg,
4985 data_t *key_data, data_t *input1, data_t *input2,
4986 int type_arg, int bits_arg,
4987 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02004988{
4989 psa_key_handle_t base_handle = 0;
4990 psa_key_handle_t derived_handle = 0;
4991 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004992 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004993 size_t bits = bits_arg;
4994 psa_status_t expected_status = expected_status_arg;
4995 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4996 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4997 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4998
4999 PSA_ASSERT( psa_crypto_init( ) );
5000
5001 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5002 psa_set_key_algorithm( &base_attributes, alg );
5003 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5004 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5005 &base_handle ) );
5006
5007 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5008 input1->x, input1->len,
5009 input2->x, input2->len, SIZE_MAX ) )
5010 goto exit;
5011
5012 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5013 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005014 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005015 psa_set_key_bits( &derived_attributes, bits );
5016 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
5017 &derived_handle ),
5018 expected_status );
5019
5020exit:
5021 psa_key_derivation_abort( &operation );
5022 psa_destroy_key( base_handle );
5023 psa_destroy_key( derived_handle );
5024 PSA_DONE( );
5025}
5026/* END_CASE */
5027
5028/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005029void key_agreement_setup( int alg_arg,
5030 int our_key_type_arg, data_t *our_key_data,
5031 data_t *peer_key_data,
5032 int expected_status_arg )
5033{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005034 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005035 psa_algorithm_t alg = alg_arg;
5036 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005037 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005038 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005039 psa_status_t expected_status = expected_status_arg;
5040 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005041
Gilles Peskine8817f612018-12-18 00:18:46 +01005042 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005043
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005044 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5045 psa_set_key_algorithm( &attributes, alg );
5046 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005047 PSA_ASSERT( psa_import_key( &attributes,
5048 our_key_data->x, our_key_data->len,
5049 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005050
Gilles Peskine77f40d82019-04-11 21:27:06 +02005051 /* The tests currently include inputs that should fail at either step.
5052 * Test cases that fail at the setup step should be changed to call
5053 * key_derivation_setup instead, and this function should be renamed
5054 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005055 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005056 if( status == PSA_SUCCESS )
5057 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005058 TEST_EQUAL( psa_key_derivation_key_agreement(
5059 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5060 our_key,
5061 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005062 expected_status );
5063 }
5064 else
5065 {
5066 TEST_ASSERT( status == expected_status );
5067 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005068
5069exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005070 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005071 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005072 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005073}
5074/* END_CASE */
5075
5076/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005077void raw_key_agreement( int alg_arg,
5078 int our_key_type_arg, data_t *our_key_data,
5079 data_t *peer_key_data,
5080 data_t *expected_output )
5081{
5082 psa_key_handle_t our_key = 0;
5083 psa_algorithm_t alg = alg_arg;
5084 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005086 unsigned char *output = NULL;
5087 size_t output_length = ~0;
5088
5089 ASSERT_ALLOC( output, expected_output->len );
5090 PSA_ASSERT( psa_crypto_init( ) );
5091
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005092 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5093 psa_set_key_algorithm( &attributes, alg );
5094 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005095 PSA_ASSERT( psa_import_key( &attributes,
5096 our_key_data->x, our_key_data->len,
5097 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005098
Gilles Peskinebe697d82019-05-16 18:00:41 +02005099 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5100 peer_key_data->x, peer_key_data->len,
5101 output, expected_output->len,
5102 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005103 ASSERT_COMPARE( output, output_length,
5104 expected_output->x, expected_output->len );
5105
5106exit:
5107 mbedtls_free( output );
5108 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005109 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005110}
5111/* END_CASE */
5112
5113/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005114void key_agreement_capacity( int alg_arg,
5115 int our_key_type_arg, data_t *our_key_data,
5116 data_t *peer_key_data,
5117 int expected_capacity_arg )
5118{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005119 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005120 psa_algorithm_t alg = alg_arg;
5121 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005122 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005124 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005125 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005126
Gilles Peskine8817f612018-12-18 00:18:46 +01005127 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005128
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005129 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5130 psa_set_key_algorithm( &attributes, alg );
5131 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005132 PSA_ASSERT( psa_import_key( &attributes,
5133 our_key_data->x, our_key_data->len,
5134 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005135
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005136 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005137 PSA_ASSERT( psa_key_derivation_key_agreement(
5138 &operation,
5139 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5140 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005141 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5142 {
5143 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005145 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005146 NULL, 0 ) );
5147 }
Gilles Peskine59685592018-09-18 12:11:34 +02005148
Gilles Peskinebf491972018-10-25 22:36:12 +02005149 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005150 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005152 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005153
Gilles Peskinebf491972018-10-25 22:36:12 +02005154 /* Test the actual capacity by reading the output. */
5155 while( actual_capacity > sizeof( output ) )
5156 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005157 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005158 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005159 actual_capacity -= sizeof( output );
5160 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005161 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005162 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005163 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005164 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005165
Gilles Peskine59685592018-09-18 12:11:34 +02005166exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005167 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005168 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005169 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005170}
5171/* END_CASE */
5172
5173/* BEGIN_CASE */
5174void key_agreement_output( int alg_arg,
5175 int our_key_type_arg, data_t *our_key_data,
5176 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005177 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005178{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005179 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005180 psa_algorithm_t alg = alg_arg;
5181 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005182 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005184 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005185
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005186 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5187 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005188
Gilles Peskine8817f612018-12-18 00:18:46 +01005189 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005190
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005191 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5192 psa_set_key_algorithm( &attributes, alg );
5193 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005194 PSA_ASSERT( psa_import_key( &attributes,
5195 our_key_data->x, our_key_data->len,
5196 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005197
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005198 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005199 PSA_ASSERT( psa_key_derivation_key_agreement(
5200 &operation,
5201 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5202 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005203 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5204 {
5205 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005207 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005208 NULL, 0 ) );
5209 }
Gilles Peskine59685592018-09-18 12:11:34 +02005210
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005212 actual_output,
5213 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005214 ASSERT_COMPARE( actual_output, expected_output1->len,
5215 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005216 if( expected_output2->len != 0 )
5217 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005219 actual_output,
5220 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005221 ASSERT_COMPARE( actual_output, expected_output2->len,
5222 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005223 }
Gilles Peskine59685592018-09-18 12:11:34 +02005224
5225exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005227 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005228 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005229 mbedtls_free( actual_output );
5230}
5231/* END_CASE */
5232
5233/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005234void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005235{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005236 size_t bytes = bytes_arg;
5237 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005238 unsigned char *output = NULL;
5239 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005240 size_t i;
5241 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005242
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005243 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5244 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005245 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005246
Gilles Peskine8817f612018-12-18 00:18:46 +01005247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005248
Gilles Peskinea50d7392018-06-21 10:22:13 +02005249 /* Run several times, to ensure that every output byte will be
5250 * nonzero at least once with overwhelming probability
5251 * (2^(-8*number_of_runs)). */
5252 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005253 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005254 if( bytes != 0 )
5255 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005256 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005257
5258 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005259 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5260 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005261
5262 for( i = 0; i < bytes; i++ )
5263 {
5264 if( output[i] != 0 )
5265 ++changed[i];
5266 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005267 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005268
5269 /* Check that every byte was changed to nonzero at least once. This
5270 * validates that psa_generate_random is overwriting every byte of
5271 * the output buffer. */
5272 for( i = 0; i < bytes; i++ )
5273 {
5274 TEST_ASSERT( changed[i] != 0 );
5275 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005276
5277exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005278 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005279 mbedtls_free( output );
5280 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005281}
5282/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005283
5284/* BEGIN_CASE */
5285void generate_key( int type_arg,
5286 int bits_arg,
5287 int usage_arg,
5288 int alg_arg,
5289 int expected_status_arg )
5290{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005291 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005292 psa_key_type_t type = type_arg;
5293 psa_key_usage_t usage = usage_arg;
5294 size_t bits = bits_arg;
5295 psa_algorithm_t alg = alg_arg;
5296 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005297 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005298 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005299
Gilles Peskine8817f612018-12-18 00:18:46 +01005300 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005301
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005302 psa_set_key_usage_flags( &attributes, usage );
5303 psa_set_key_algorithm( &attributes, alg );
5304 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005305 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005306
5307 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005308 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005309 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005310 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005311
5312 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005313 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5314 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5315 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005316
Gilles Peskine818ca122018-06-20 18:16:48 +02005317 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005318 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005319 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005320
5321exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005322 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005323 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005324 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005325}
5326/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005327
Gilles Peskinee56e8782019-04-26 17:34:02 +02005328/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5329void generate_key_rsa( int bits_arg,
5330 data_t *e_arg,
5331 int expected_status_arg )
5332{
5333 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005334 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005335 size_t bits = bits_arg;
5336 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5337 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5338 psa_status_t expected_status = expected_status_arg;
5339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5340 uint8_t *exported = NULL;
5341 size_t exported_size =
5342 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5343 size_t exported_length = SIZE_MAX;
5344 uint8_t *e_read_buffer = NULL;
5345 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005346 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005347 size_t e_read_length = SIZE_MAX;
5348
5349 if( e_arg->len == 0 ||
5350 ( e_arg->len == 3 &&
5351 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5352 {
5353 is_default_public_exponent = 1;
5354 e_read_size = 0;
5355 }
5356 ASSERT_ALLOC( e_read_buffer, e_read_size );
5357 ASSERT_ALLOC( exported, exported_size );
5358
5359 PSA_ASSERT( psa_crypto_init( ) );
5360
5361 psa_set_key_usage_flags( &attributes, usage );
5362 psa_set_key_algorithm( &attributes, alg );
5363 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5364 e_arg->x, e_arg->len ) );
5365 psa_set_key_bits( &attributes, bits );
5366
5367 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005368 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005369 if( expected_status != PSA_SUCCESS )
5370 goto exit;
5371
5372 /* Test the key information */
5373 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5374 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5375 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5376 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5377 e_read_buffer, e_read_size,
5378 &e_read_length ) );
5379 if( is_default_public_exponent )
5380 TEST_EQUAL( e_read_length, 0 );
5381 else
5382 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5383
5384 /* Do something with the key according to its type and permitted usage. */
5385 if( ! exercise_key( handle, usage, alg ) )
5386 goto exit;
5387
5388 /* Export the key and check the public exponent. */
5389 PSA_ASSERT( psa_export_public_key( handle,
5390 exported, exported_size,
5391 &exported_length ) );
5392 {
5393 uint8_t *p = exported;
5394 uint8_t *end = exported + exported_length;
5395 size_t len;
5396 /* RSAPublicKey ::= SEQUENCE {
5397 * modulus INTEGER, -- n
5398 * publicExponent INTEGER } -- e
5399 */
5400 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005401 MBEDTLS_ASN1_SEQUENCE |
5402 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005403 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5404 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5405 MBEDTLS_ASN1_INTEGER ) );
5406 if( len >= 1 && p[0] == 0 )
5407 {
5408 ++p;
5409 --len;
5410 }
5411 if( e_arg->len == 0 )
5412 {
5413 TEST_EQUAL( len, 3 );
5414 TEST_EQUAL( p[0], 1 );
5415 TEST_EQUAL( p[1], 0 );
5416 TEST_EQUAL( p[2], 1 );
5417 }
5418 else
5419 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5420 }
5421
5422exit:
5423 psa_reset_key_attributes( &attributes );
5424 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005425 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005426 mbedtls_free( e_read_buffer );
5427 mbedtls_free( exported );
5428}
5429/* END_CASE */
5430
Darryl Greend49a4992018-06-18 17:27:26 +01005431/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005432void persistent_key_load_key_from_storage( data_t *data,
5433 int type_arg, int bits_arg,
5434 int usage_flags_arg, int alg_arg,
5435 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005436{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005437 psa_key_id_t key_id = 1;
5438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005439 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005440 psa_key_handle_t base_key = 0;
5441 psa_key_type_t type = type_arg;
5442 size_t bits = bits_arg;
5443 psa_key_usage_t usage_flags = usage_flags_arg;
5444 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005445 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005446 unsigned char *first_export = NULL;
5447 unsigned char *second_export = NULL;
5448 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5449 size_t first_exported_length;
5450 size_t second_exported_length;
5451
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005452 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5453 {
5454 ASSERT_ALLOC( first_export, export_size );
5455 ASSERT_ALLOC( second_export, export_size );
5456 }
Darryl Greend49a4992018-06-18 17:27:26 +01005457
Gilles Peskine8817f612018-12-18 00:18:46 +01005458 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005459
Gilles Peskinec87af662019-05-15 16:12:22 +02005460 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005461 psa_set_key_usage_flags( &attributes, usage_flags );
5462 psa_set_key_algorithm( &attributes, alg );
5463 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005464 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005465
Darryl Green0c6575a2018-11-07 16:05:30 +00005466 switch( generation_method )
5467 {
5468 case IMPORT_KEY:
5469 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005470 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5471 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005472 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005473
Darryl Green0c6575a2018-11-07 16:05:30 +00005474 case GENERATE_KEY:
5475 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005476 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005477 break;
5478
5479 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005480 {
5481 /* Create base key */
5482 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5483 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5484 psa_set_key_usage_flags( &base_attributes,
5485 PSA_KEY_USAGE_DERIVE );
5486 psa_set_key_algorithm( &base_attributes, derive_alg );
5487 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005488 PSA_ASSERT( psa_import_key( &base_attributes,
5489 data->x, data->len,
5490 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005491 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005492 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005493 PSA_ASSERT( psa_key_derivation_input_key(
5494 &operation,
5495 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005496 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005497 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005498 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005499 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5500 &operation,
5501 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005502 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005503 PSA_ASSERT( psa_destroy_key( base_key ) );
5504 base_key = 0;
5505 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005506 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005507 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005508 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005509
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005510 /* Export the key if permitted by the key policy. */
5511 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5512 {
5513 PSA_ASSERT( psa_export_key( handle,
5514 first_export, export_size,
5515 &first_exported_length ) );
5516 if( generation_method == IMPORT_KEY )
5517 ASSERT_COMPARE( data->x, data->len,
5518 first_export, first_exported_length );
5519 }
Darryl Greend49a4992018-06-18 17:27:26 +01005520
5521 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005522 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005523 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005524 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005525
Darryl Greend49a4992018-06-18 17:27:26 +01005526 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005527 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005528 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5529 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5530 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5531 PSA_KEY_LIFETIME_PERSISTENT );
5532 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5533 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5534 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5535 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005536
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005537 /* Export the key again if permitted by the key policy. */
5538 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005539 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005540 PSA_ASSERT( psa_export_key( handle,
5541 second_export, export_size,
5542 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005543 ASSERT_COMPARE( first_export, first_exported_length,
5544 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005545 }
5546
5547 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005548 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005549 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005550
5551exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005552 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005553 mbedtls_free( first_export );
5554 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005556 psa_destroy_key( base_key );
5557 if( handle == 0 )
5558 {
5559 /* In case there was a test failure after creating the persistent key
5560 * but while it was not open, try to re-open the persistent key
5561 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005562 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005563 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005564 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005565 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005566}
5567/* END_CASE */