Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
| 7 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 8 | /* 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 Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 12 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 13 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 14 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 15 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 16 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 17 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 18 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 19 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 20 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 21 | #else |
| 22 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 23 | #endif |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 24 | #include "mbedtls/legacy_or_psa.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 25 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 26 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 27 | #define UNUSED 0xdeadbeef |
| 28 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 29 | /* Assert that an operation is (not) active. |
| 30 | * This serves as a proxy for checking if the operation is aborted. */ |
| 31 | #define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 ) |
| 32 | #define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 33 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 34 | #if defined(PSA_WANT_ALG_JPAKE) |
| 35 | void ecjpake_operation_setup( psa_pake_operation_t *operation, |
| 36 | psa_pake_cipher_suite_t *cipher_suite, |
| 37 | psa_pake_role_t role, |
| 38 | mbedtls_svc_key_id_t key, |
| 39 | size_t key_available ) |
| 40 | { |
| 41 | *operation = psa_pake_operation_init(); |
| 42 | |
| 43 | TEST_EQUAL( psa_pake_setup( operation, cipher_suite ), |
| 44 | PSA_SUCCESS ); |
| 45 | |
| 46 | TEST_EQUAL( psa_pake_set_role( operation, role), |
| 47 | PSA_SUCCESS ); |
| 48 | |
| 49 | if( key_available ) |
| 50 | TEST_EQUAL( psa_pake_set_password_key( operation, key ), |
| 51 | PSA_SUCCESS ); |
| 52 | exit: |
| 53 | return; |
| 54 | } |
| 55 | #endif |
| 56 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 57 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 58 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 59 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 60 | /** Test if a buffer contains a constant byte value. |
| 61 | * |
| 62 | * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 63 | * |
| 64 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 65 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 66 | * \param size Size of the buffer in bytes. |
| 67 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 68 | * \return 1 if the buffer is all-bits-zero. |
| 69 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 70 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 71 | static int mem_is_char( void *buffer, unsigned char c, size_t size ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 72 | { |
| 73 | size_t i; |
| 74 | for( i = 0; i < size; i++ ) |
| 75 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 76 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 77 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 78 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 79 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 80 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 81 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 82 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 83 | static int asn1_write_10x( unsigned char **p, |
| 84 | unsigned char *start, |
| 85 | size_t bits, |
| 86 | unsigned char x ) |
| 87 | { |
| 88 | int ret; |
| 89 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 90 | if( bits == 0 ) |
| 91 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 92 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 93 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 94 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 95 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 96 | *p -= len; |
| 97 | ( *p )[len-1] = x; |
| 98 | if( bits % 8 == 0 ) |
| 99 | ( *p )[1] |= 1; |
| 100 | else |
| 101 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 102 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 103 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 104 | MBEDTLS_ASN1_INTEGER ) ); |
| 105 | return( len ); |
| 106 | } |
| 107 | |
| 108 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 109 | size_t buffer_size, |
| 110 | unsigned char **p, |
| 111 | size_t bits, |
| 112 | int keypair ) |
| 113 | { |
| 114 | size_t half_bits = ( bits + 1 ) / 2; |
| 115 | int ret; |
| 116 | int len = 0; |
| 117 | /* Construct something that looks like a DER encoding of |
| 118 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 119 | * RSAPrivateKey ::= SEQUENCE { |
| 120 | * version Version, |
| 121 | * modulus INTEGER, -- n |
| 122 | * publicExponent INTEGER, -- e |
| 123 | * privateExponent INTEGER, -- d |
| 124 | * prime1 INTEGER, -- p |
| 125 | * prime2 INTEGER, -- q |
| 126 | * exponent1 INTEGER, -- d mod (p-1) |
| 127 | * exponent2 INTEGER, -- d mod (q-1) |
| 128 | * coefficient INTEGER, -- (inverse of q) mod p |
| 129 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 130 | * } |
| 131 | * Or, for a public key, the same structure with only |
| 132 | * version, modulus and publicExponent. |
| 133 | */ |
| 134 | *p = buffer + buffer_size; |
| 135 | if( keypair ) |
| 136 | { |
| 137 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 138 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 139 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 140 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 141 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 142 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 143 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 144 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 145 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 146 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 147 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 148 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 149 | } |
| 150 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 151 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 152 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 153 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 154 | if( keypair ) |
| 155 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 156 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 157 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 158 | { |
| 159 | const unsigned char tag = |
| 160 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 161 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 162 | } |
| 163 | return( len ); |
| 164 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 165 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 166 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 167 | int exercise_mac_setup( psa_key_type_t key_type, |
| 168 | const unsigned char *key_bytes, |
| 169 | size_t key_length, |
| 170 | psa_algorithm_t alg, |
| 171 | psa_mac_operation_t *operation, |
| 172 | psa_status_t *status ) |
| 173 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 174 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 175 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 176 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 177 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 178 | psa_set_key_algorithm( &attributes, alg ); |
| 179 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 180 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 181 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 182 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 183 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 184 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 185 | /* If setup failed, reproduce the failure, so that the caller can |
| 186 | * test the resulting state of the operation object. */ |
| 187 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 188 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 189 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 192 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 193 | return( 1 ); |
| 194 | |
| 195 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 196 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 197 | return( 0 ); |
| 198 | } |
| 199 | |
| 200 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 201 | const unsigned char *key_bytes, |
| 202 | size_t key_length, |
| 203 | psa_algorithm_t alg, |
| 204 | psa_cipher_operation_t *operation, |
| 205 | psa_status_t *status ) |
| 206 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 207 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 208 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 209 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 210 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 211 | psa_set_key_algorithm( &attributes, alg ); |
| 212 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 213 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 214 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 215 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 216 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 217 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 218 | /* If setup failed, reproduce the failure, so that the caller can |
| 219 | * test the resulting state of the operation object. */ |
| 220 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 221 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 222 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 223 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 226 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 227 | return( 1 ); |
| 228 | |
| 229 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 230 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 231 | return( 0 ); |
| 232 | } |
| 233 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 234 | static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 235 | { |
| 236 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 237 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 238 | uint8_t buffer[1]; |
| 239 | size_t length; |
| 240 | int ok = 0; |
| 241 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 242 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 243 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 244 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 245 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 246 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 247 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 248 | TEST_EQUAL( |
| 249 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 250 | TEST_EQUAL( |
| 251 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 252 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 253 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 254 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 255 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 256 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 257 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 258 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 259 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 260 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 261 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 262 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 263 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 264 | ok = 1; |
| 265 | |
| 266 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 267 | /* |
| 268 | * Key attributes may have been returned by psa_get_key_attributes() |
| 269 | * thus reset them as required. |
| 270 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 271 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 272 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 273 | return( ok ); |
| 274 | } |
| 275 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 276 | /* Assert that a key isn't reported as having a slot number. */ |
| 277 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 278 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 279 | do \ |
| 280 | { \ |
| 281 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 282 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 283 | attributes, \ |
| 284 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 285 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 286 | } \ |
| 287 | while( 0 ) |
| 288 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 289 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 290 | ( (void) 0 ) |
| 291 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 292 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 293 | /* An overapproximation of the amount of storage needed for a key of the |
| 294 | * given type and with the given content. The API doesn't make it easy |
| 295 | * to find a good value for the size. The current implementation doesn't |
| 296 | * care about the value anyway. */ |
| 297 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 298 | ( data )->len |
| 299 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 300 | typedef enum { |
| 301 | IMPORT_KEY = 0, |
| 302 | GENERATE_KEY = 1, |
| 303 | DERIVE_KEY = 2 |
| 304 | } generate_method; |
| 305 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 306 | typedef enum |
| 307 | { |
| 308 | DO_NOT_SET_LENGTHS = 0, |
| 309 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 310 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 311 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 312 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 313 | typedef enum |
| 314 | { |
| 315 | USE_NULL_TAG = 0, |
| 316 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 317 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 318 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 319 | /*! |
| 320 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 321 | * \param key_type_arg Type of key passed in |
| 322 | * \param key_data The encryption / decryption key data |
| 323 | * \param alg_arg The type of algorithm used |
| 324 | * \param nonce Nonce data |
| 325 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 326 | * \param ad_part_len_arg If not -1, the length of chunks to |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 327 | * feed additional data in to be encrypted / |
| 328 | * decrypted. If -1, no chunking. |
| 329 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 330 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 331 | * the data in to be encrypted / decrypted. If |
| 332 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 333 | * \param set_lengths_method A member of the set_lengths_method_t enum is |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 334 | * expected here, this controls whether or not |
| 335 | * to set lengths, and in what order with |
| 336 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 337 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 338 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 339 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 340 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 341 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 342 | */ |
| 343 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 344 | int alg_arg, |
| 345 | data_t *nonce, |
| 346 | data_t *additional_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 347 | int ad_part_len_arg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 348 | data_t *input_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 349 | int data_part_len_arg, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 350 | set_lengths_method_t set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 351 | data_t *expected_output, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 352 | int is_encrypt, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 353 | int do_zero_parts ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 354 | { |
| 355 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 356 | psa_key_type_t key_type = key_type_arg; |
| 357 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 358 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 359 | unsigned char *output_data = NULL; |
| 360 | unsigned char *part_data = NULL; |
| 361 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 362 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 363 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 364 | size_t output_size = 0; |
| 365 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 366 | size_t output_length = 0; |
| 367 | size_t key_bits = 0; |
| 368 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 369 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 370 | size_t part_length = 0; |
| 371 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 372 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 373 | size_t ad_part_len = 0; |
| 374 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 375 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 376 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 377 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 378 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 379 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 380 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 381 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 382 | PSA_ASSERT( psa_crypto_init( ) ); |
| 383 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 384 | if( is_encrypt ) |
| 385 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 386 | else |
| 387 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 388 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 389 | psa_set_key_algorithm( &attributes, alg ); |
| 390 | psa_set_key_type( &attributes, key_type ); |
| 391 | |
| 392 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 393 | &key ) ); |
| 394 | |
| 395 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 396 | key_bits = psa_get_key_bits( &attributes ); |
| 397 | |
| 398 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 399 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 400 | if( is_encrypt ) |
| 401 | { |
| 402 | /* Tag gets written at end of buffer. */ |
| 403 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 404 | ( input_data->len + |
| 405 | tag_length ) ); |
| 406 | data_true_size = input_data->len; |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 411 | ( input_data->len - |
| 412 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 413 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 414 | /* Do not want to attempt to decrypt tag. */ |
| 415 | data_true_size = input_data->len - tag_length; |
| 416 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 417 | |
| 418 | ASSERT_ALLOC( output_data, output_size ); |
| 419 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 420 | if( is_encrypt ) |
| 421 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 422 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 423 | TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 424 | } |
| 425 | else |
| 426 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 427 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 428 | TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 429 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 430 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 431 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 432 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 433 | if( is_encrypt ) |
| 434 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 435 | else |
| 436 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 437 | |
| 438 | /* If the operation is not supported, just skip and not fail in case the |
| 439 | * encryption involves a common limitation of cryptography hardwares and |
| 440 | * an alternative implementation. */ |
| 441 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 442 | { |
| 443 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 444 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 445 | } |
| 446 | |
| 447 | PSA_ASSERT( status ); |
| 448 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 449 | if( set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 450 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 451 | else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 452 | { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 453 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 454 | data_true_size ) ); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 455 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 456 | } |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 457 | else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 458 | { |
| 459 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 460 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 461 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 462 | data_true_size ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 463 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 464 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 465 | if( ad_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 466 | { |
| 467 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 468 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 469 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 470 | for( part_offset = 0, part_count = 0; |
| 471 | part_offset < additional_data->len; |
| 472 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 473 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 474 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 475 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 476 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 477 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 478 | else if( additional_data->len - part_offset < ad_part_len ) |
| 479 | { |
| 480 | part_length = additional_data->len - part_offset; |
| 481 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 482 | else |
| 483 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 484 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 488 | additional_data->x + part_offset, |
| 489 | part_length ) ); |
| 490 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | /* Pass additional data in one go. */ |
| 496 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 497 | additional_data->len ) ); |
| 498 | } |
| 499 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 500 | if( data_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 501 | { |
| 502 | /* Pass data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 503 | data_part_len = ( size_t ) data_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 504 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 505 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 506 | |
| 507 | ASSERT_ALLOC( part_data, part_data_size ); |
| 508 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 509 | for( part_offset = 0, part_count = 0; |
| 510 | part_offset < data_true_size; |
| 511 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 512 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 513 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 514 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 515 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 516 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 517 | else if( ( data_true_size - part_offset ) < data_part_len ) |
| 518 | { |
| 519 | part_length = ( data_true_size - part_offset ); |
| 520 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 521 | else |
| 522 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 523 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | PSA_ASSERT( psa_aead_update( &operation, |
| 527 | ( input_data->x + part_offset ), |
| 528 | part_length, part_data, |
| 529 | part_data_size, |
| 530 | &output_part_length ) ); |
| 531 | |
| 532 | if( output_data && output_part_length ) |
| 533 | { |
Mircea Udrea | 657ff4f | 2022-01-31 13:51:56 +0100 | [diff] [blame] | 534 | memcpy( ( output_data + output_length ), part_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 535 | output_part_length ); |
| 536 | } |
| 537 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 538 | output_length += output_part_length; |
| 539 | } |
| 540 | } |
| 541 | else |
| 542 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 543 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 544 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 545 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 546 | output_size, &output_length ) ); |
| 547 | } |
| 548 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 549 | if( is_encrypt ) |
| 550 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 551 | final_output_size, |
| 552 | &output_part_length, |
| 553 | tag_buffer, tag_length, |
| 554 | &tag_size ) ); |
| 555 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 556 | { |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 557 | PSA_ASSERT( psa_aead_verify( &operation, final_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 558 | final_output_size, |
| 559 | &output_part_length, |
| 560 | ( input_data->x + data_true_size ), |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 561 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 562 | } |
| 563 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 564 | if( output_data && output_part_length ) |
| 565 | memcpy( ( output_data + output_length ), final_data, |
| 566 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 567 | |
| 568 | output_length += output_part_length; |
| 569 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 570 | |
| 571 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 572 | * should be exact.*/ |
| 573 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 574 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 575 | TEST_EQUAL( tag_length, tag_size ); |
| 576 | |
| 577 | if( output_data && tag_length ) |
| 578 | memcpy( ( output_data + output_length ), tag_buffer, |
| 579 | tag_length ); |
| 580 | |
| 581 | output_length += tag_length; |
| 582 | |
| 583 | TEST_EQUAL( output_length, |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 584 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 585 | input_data->len ) ); |
| 586 | TEST_LE_U( output_length, |
| 587 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 588 | } |
| 589 | else |
| 590 | { |
| 591 | TEST_EQUAL( output_length, |
| 592 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 593 | input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 594 | TEST_LE_U( output_length, |
| 595 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 598 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 599 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 600 | output_data, output_length ); |
| 601 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 602 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 603 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 604 | |
| 605 | exit: |
| 606 | psa_destroy_key( key ); |
| 607 | psa_aead_abort( &operation ); |
| 608 | mbedtls_free( output_data ); |
| 609 | mbedtls_free( part_data ); |
| 610 | mbedtls_free( final_data ); |
| 611 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 612 | |
| 613 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 614 | } |
| 615 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 616 | /*! |
| 617 | * \brief Internal Function for MAC multipart tests. |
| 618 | * \param key_type_arg Type of key passed in |
| 619 | * \param key_data The encryption / decryption key data |
| 620 | * \param alg_arg The type of algorithm used |
| 621 | * \param input_data Data to encrypt / decrypt |
| 622 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 623 | * the data in to be encrypted / decrypted. If |
| 624 | * -1, no chunking |
| 625 | * \param expected_output Expected output |
| 626 | * \param is_verify If non-zero this is an verify operation. |
| 627 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 628 | * with normal length chunks. |
| 629 | * \return int Zero on failure, non-zero on success. |
| 630 | */ |
| 631 | static int mac_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 632 | int alg_arg, |
| 633 | data_t *input_data, |
| 634 | int data_part_len_arg, |
| 635 | data_t *expected_output, |
| 636 | int is_verify, |
| 637 | int do_zero_parts ) |
| 638 | { |
| 639 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 640 | psa_key_type_t key_type = key_type_arg; |
| 641 | psa_algorithm_t alg = alg_arg; |
| 642 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 643 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 644 | size_t part_offset = 0; |
| 645 | size_t part_length = 0; |
| 646 | size_t data_part_len = 0; |
| 647 | size_t mac_len = 0; |
| 648 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 649 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 650 | |
| 651 | int test_ok = 0; |
| 652 | size_t part_count = 0; |
| 653 | |
Neil Armstrong | fd4c259 | 2022-03-07 10:11:11 +0100 | [diff] [blame] | 654 | PSA_INIT( ); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 655 | |
| 656 | if( is_verify ) |
| 657 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 658 | else |
| 659 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 660 | |
| 661 | psa_set_key_algorithm( &attributes, alg ); |
| 662 | psa_set_key_type( &attributes, key_type ); |
| 663 | |
| 664 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 665 | &key ) ); |
| 666 | |
| 667 | if( is_verify ) |
| 668 | status = psa_mac_verify_setup( &operation, key, alg ); |
| 669 | else |
| 670 | status = psa_mac_sign_setup( &operation, key, alg ); |
| 671 | |
| 672 | PSA_ASSERT( status ); |
| 673 | |
| 674 | if( data_part_len_arg != -1 ) |
| 675 | { |
| 676 | /* Pass data in parts */ |
| 677 | data_part_len = ( size_t ) data_part_len_arg; |
| 678 | |
| 679 | for( part_offset = 0, part_count = 0; |
| 680 | part_offset < input_data->len; |
| 681 | part_offset += part_length, part_count++ ) |
| 682 | { |
| 683 | if( do_zero_parts && ( part_count & 0x01 ) ) |
| 684 | { |
| 685 | part_length = 0; |
| 686 | } |
| 687 | else if( ( input_data->len - part_offset ) < data_part_len ) |
| 688 | { |
| 689 | part_length = ( input_data->len - part_offset ); |
| 690 | } |
| 691 | else |
| 692 | { |
| 693 | part_length = data_part_len; |
| 694 | } |
| 695 | |
| 696 | PSA_ASSERT( psa_mac_update( &operation, |
| 697 | ( input_data->x + part_offset ), |
| 698 | part_length ) ); |
| 699 | } |
| 700 | } |
| 701 | else |
| 702 | { |
| 703 | /* Pass all data in one go. */ |
| 704 | PSA_ASSERT( psa_mac_update( &operation, input_data->x, |
| 705 | input_data->len ) ); |
| 706 | } |
| 707 | |
| 708 | if( is_verify ) |
| 709 | { |
| 710 | PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x, |
| 711 | expected_output->len ) ); |
| 712 | } |
| 713 | else |
| 714 | { |
| 715 | PSA_ASSERT( psa_mac_sign_finish( &operation, mac, |
| 716 | PSA_MAC_MAX_SIZE, &mac_len ) ); |
| 717 | |
| 718 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 719 | mac, mac_len ); |
| 720 | } |
| 721 | |
| 722 | test_ok = 1; |
| 723 | |
| 724 | exit: |
| 725 | psa_destroy_key( key ); |
| 726 | psa_mac_abort( &operation ); |
| 727 | PSA_DONE( ); |
| 728 | |
| 729 | return( test_ok ); |
| 730 | } |
| 731 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 732 | #if defined(PSA_WANT_ALG_JPAKE) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 733 | static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive, |
| 734 | psa_pake_operation_t *server, |
| 735 | psa_pake_operation_t *client, |
| 736 | int client_input_first, |
| 737 | int round, int inject_error ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 738 | { |
| 739 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 740 | size_t buffer_length = ( |
| 741 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 742 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 743 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2; |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 744 | /* The output should be exactly this size according to the spec */ |
| 745 | const size_t expected_size_key_share = |
| 746 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 747 | /* The output should be exactly this size according to the spec */ |
| 748 | const size_t expected_size_zk_public = |
| 749 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 750 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 751 | const size_t max_expected_size_zk_proof = |
| 752 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 753 | size_t buffer0_off = 0; |
| 754 | size_t buffer1_off = 0; |
| 755 | size_t s_g1_len, s_g2_len, s_a_len; |
| 756 | size_t s_g1_off, s_g2_off, s_a_off; |
| 757 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 758 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 759 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 760 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 761 | size_t c_g1_len, c_g2_len, c_a_len; |
| 762 | size_t c_g1_off, c_g2_off, c_a_off; |
| 763 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 764 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 765 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 766 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 767 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 768 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 769 | |
| 770 | ASSERT_ALLOC( buffer0, buffer_length ); |
| 771 | ASSERT_ALLOC( buffer1, buffer_length ); |
| 772 | |
| 773 | switch( round ) |
| 774 | { |
| 775 | case 1: |
| 776 | /* Server first round Output */ |
| 777 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 778 | buffer0 + buffer0_off, |
| 779 | 512 - buffer0_off, &s_g1_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 780 | TEST_EQUAL( s_g1_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 781 | s_g1_off = buffer0_off; |
| 782 | buffer0_off += s_g1_len; |
| 783 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 784 | buffer0 + buffer0_off, |
| 785 | 512 - buffer0_off, &s_x1_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 786 | TEST_EQUAL( s_x1_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 787 | s_x1_pk_off = buffer0_off; |
| 788 | buffer0_off += s_x1_pk_len; |
| 789 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 790 | buffer0 + buffer0_off, |
| 791 | 512 - buffer0_off, &s_x1_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 792 | TEST_LE_U( s_x1_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 793 | s_x1_pr_off = buffer0_off; |
| 794 | buffer0_off += s_x1_pr_len; |
| 795 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 796 | buffer0 + buffer0_off, |
| 797 | 512 - buffer0_off, &s_g2_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 798 | TEST_EQUAL( s_g2_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 799 | s_g2_off = buffer0_off; |
| 800 | buffer0_off += s_g2_len; |
| 801 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 802 | buffer0 + buffer0_off, |
| 803 | 512 - buffer0_off, &s_x2_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 804 | TEST_EQUAL( s_x2_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 805 | s_x2_pk_off = buffer0_off; |
| 806 | buffer0_off += s_x2_pk_len; |
| 807 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 808 | buffer0 + buffer0_off, |
| 809 | 512 - buffer0_off, &s_x2_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 810 | TEST_LE_U( s_x2_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 811 | s_x2_pr_off = buffer0_off; |
| 812 | buffer0_off += s_x2_pr_len; |
| 813 | |
| 814 | if( inject_error == 1 ) |
| 815 | { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 816 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 817 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 818 | expected_status = PSA_ERROR_DATA_INVALID; |
| 819 | } |
| 820 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 821 | /* |
| 822 | * When injecting errors in inputs, the implementation is |
| 823 | * free to detect it right away of with a delay. |
| 824 | * This permits delaying the error until the end of the input |
| 825 | * sequence, if no error appears then, this will be treated |
| 826 | * as an error. |
| 827 | */ |
| 828 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 829 | if( client_input_first == 1 ) |
| 830 | { |
| 831 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 832 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 833 | buffer0 + s_g1_off, s_g1_len ); |
| 834 | if( inject_error == 1 && status != PSA_SUCCESS ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 835 | { |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 836 | TEST_EQUAL( status, expected_status ); |
| 837 | break; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 838 | } |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 839 | else |
| 840 | { |
| 841 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 842 | } |
| 843 | |
| 844 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 845 | buffer0 + s_x1_pk_off, |
| 846 | s_x1_pk_len ); |
| 847 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 848 | { |
| 849 | TEST_EQUAL( status, expected_status ); |
| 850 | break; |
| 851 | } |
| 852 | else |
| 853 | { |
| 854 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 855 | } |
| 856 | |
| 857 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 858 | buffer0 + s_x1_pr_off, |
| 859 | s_x1_pr_len ); |
| 860 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 861 | { |
| 862 | TEST_EQUAL( status, expected_status ); |
| 863 | break; |
| 864 | } |
| 865 | else |
| 866 | { |
| 867 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 868 | } |
| 869 | |
| 870 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 871 | buffer0 + s_g2_off, |
| 872 | s_g2_len ); |
| 873 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 874 | { |
| 875 | TEST_EQUAL( status, expected_status ); |
| 876 | break; |
| 877 | } |
| 878 | else |
| 879 | { |
| 880 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 881 | } |
| 882 | |
| 883 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 884 | buffer0 + s_x2_pk_off, |
| 885 | s_x2_pk_len ); |
| 886 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 887 | { |
| 888 | TEST_EQUAL( status, expected_status ); |
| 889 | break; |
| 890 | } |
| 891 | else |
| 892 | { |
| 893 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 894 | } |
| 895 | |
| 896 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 897 | buffer0 + s_x2_pr_off, |
| 898 | s_x2_pr_len ); |
| 899 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 900 | { |
| 901 | TEST_EQUAL( status, expected_status ); |
| 902 | break; |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 907 | } |
| 908 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 909 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 910 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 911 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /* Client first round Output */ |
| 915 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 916 | buffer1 + buffer1_off, |
| 917 | 512 - buffer1_off, &c_g1_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 918 | TEST_EQUAL( c_g1_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 919 | c_g1_off = buffer1_off; |
| 920 | buffer1_off += c_g1_len; |
| 921 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 922 | buffer1 + buffer1_off, |
| 923 | 512 - buffer1_off, &c_x1_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 924 | TEST_EQUAL( c_x1_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 925 | c_x1_pk_off = buffer1_off; |
| 926 | buffer1_off += c_x1_pk_len; |
| 927 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 928 | buffer1 + buffer1_off, |
| 929 | 512 - buffer1_off, &c_x1_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 930 | TEST_LE_U( c_x1_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 931 | c_x1_pr_off = buffer1_off; |
| 932 | buffer1_off += c_x1_pr_len; |
| 933 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 934 | buffer1 + buffer1_off, |
| 935 | 512 - buffer1_off, &c_g2_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 936 | TEST_EQUAL( c_g2_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 937 | c_g2_off = buffer1_off; |
| 938 | buffer1_off += c_g2_len; |
| 939 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 940 | buffer1 + buffer1_off, |
| 941 | 512 - buffer1_off, &c_x2_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 942 | TEST_EQUAL( c_x2_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 943 | c_x2_pk_off = buffer1_off; |
| 944 | buffer1_off += c_x2_pk_len; |
| 945 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 946 | buffer1 + buffer1_off, |
| 947 | 512 - buffer1_off, &c_x2_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 948 | TEST_LE_U( c_x2_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 949 | c_x2_pr_off = buffer1_off; |
| 950 | buffer1_off += c_x2_pr_len; |
| 951 | |
| 952 | if( client_input_first == 0 ) |
| 953 | { |
| 954 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 955 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 956 | buffer0 + s_g1_off, s_g1_len ); |
| 957 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 958 | { |
| 959 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 960 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 961 | } |
| 962 | else |
| 963 | { |
| 964 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 965 | } |
| 966 | |
| 967 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 968 | buffer0 + s_x1_pk_off, |
| 969 | s_x1_pk_len ); |
| 970 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 971 | { |
| 972 | TEST_EQUAL( status, expected_status ); |
| 973 | break; |
| 974 | } |
| 975 | else |
| 976 | { |
| 977 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 978 | } |
| 979 | |
| 980 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 981 | buffer0 + s_x1_pr_off, |
| 982 | s_x1_pr_len ); |
| 983 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 984 | { |
| 985 | TEST_EQUAL( status, expected_status ); |
| 986 | break; |
| 987 | } |
| 988 | else |
| 989 | { |
| 990 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 991 | } |
| 992 | |
| 993 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 994 | buffer0 + s_g2_off, |
| 995 | s_g2_len ); |
| 996 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 997 | { |
| 998 | TEST_EQUAL( status, expected_status ); |
| 999 | break; |
| 1000 | } |
| 1001 | else |
| 1002 | { |
| 1003 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1004 | } |
| 1005 | |
| 1006 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1007 | buffer0 + s_x2_pk_off, |
| 1008 | s_x2_pk_len ); |
| 1009 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 1010 | { |
| 1011 | TEST_EQUAL( status, expected_status ); |
| 1012 | break; |
| 1013 | } |
| 1014 | else |
| 1015 | { |
| 1016 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1017 | } |
| 1018 | |
| 1019 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1020 | buffer0 + s_x2_pr_off, |
| 1021 | s_x2_pr_len ); |
| 1022 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 1023 | { |
| 1024 | TEST_EQUAL( status, expected_status ); |
| 1025 | break; |
| 1026 | } |
| 1027 | else |
| 1028 | { |
| 1029 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1030 | } |
| 1031 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1032 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1033 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1034 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | if( inject_error == 2 ) |
| 1038 | { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 1039 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 1040 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1041 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1042 | } |
| 1043 | |
| 1044 | /* Server first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1045 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1046 | buffer1 + c_g1_off, c_g1_len ); |
| 1047 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1048 | { |
| 1049 | TEST_EQUAL( status, expected_status ); |
| 1050 | break; |
| 1051 | } |
| 1052 | else |
| 1053 | { |
| 1054 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1055 | } |
| 1056 | |
| 1057 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1058 | buffer1 + c_x1_pk_off, c_x1_pk_len ); |
| 1059 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1060 | { |
| 1061 | TEST_EQUAL( status, expected_status ); |
| 1062 | break; |
| 1063 | } |
| 1064 | else |
| 1065 | { |
| 1066 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1067 | } |
| 1068 | |
| 1069 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1070 | buffer1 + c_x1_pr_off, c_x1_pr_len ); |
| 1071 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1072 | { |
| 1073 | TEST_EQUAL( status, expected_status ); |
| 1074 | break; |
| 1075 | } |
| 1076 | else |
| 1077 | { |
| 1078 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1079 | } |
| 1080 | |
| 1081 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1082 | buffer1 + c_g2_off, c_g2_len ); |
| 1083 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1084 | { |
| 1085 | TEST_EQUAL( status, expected_status ); |
| 1086 | break; |
| 1087 | } |
| 1088 | else |
| 1089 | { |
| 1090 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1091 | } |
| 1092 | |
| 1093 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1094 | buffer1 + c_x2_pk_off, c_x2_pk_len ); |
| 1095 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1096 | { |
| 1097 | TEST_EQUAL( status, expected_status ); |
| 1098 | break; |
| 1099 | } |
| 1100 | else |
| 1101 | { |
| 1102 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1103 | } |
| 1104 | |
| 1105 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1106 | buffer1 + c_x2_pr_off, c_x2_pr_len ); |
| 1107 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1108 | { |
| 1109 | TEST_EQUAL( status, expected_status ); |
| 1110 | break; |
| 1111 | } |
| 1112 | else |
| 1113 | { |
| 1114 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1115 | } |
| 1116 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1117 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1118 | if( inject_error == 2 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1119 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1120 | |
| 1121 | break; |
| 1122 | |
| 1123 | case 2: |
| 1124 | /* Server second round Output */ |
| 1125 | buffer0_off = 0; |
| 1126 | |
| 1127 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1128 | buffer0 + buffer0_off, |
| 1129 | 512 - buffer0_off, &s_a_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1130 | TEST_EQUAL( s_a_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1131 | s_a_off = buffer0_off; |
| 1132 | buffer0_off += s_a_len; |
| 1133 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1134 | buffer0 + buffer0_off, |
| 1135 | 512 - buffer0_off, &s_x2s_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1136 | TEST_EQUAL( s_x2s_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1137 | s_x2s_pk_off = buffer0_off; |
| 1138 | buffer0_off += s_x2s_pk_len; |
| 1139 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1140 | buffer0 + buffer0_off, |
| 1141 | 512 - buffer0_off, &s_x2s_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1142 | TEST_LE_U( s_x2s_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1143 | s_x2s_pr_off = buffer0_off; |
| 1144 | buffer0_off += s_x2s_pr_len; |
| 1145 | |
| 1146 | if( inject_error == 3 ) |
| 1147 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1148 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1149 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1150 | } |
| 1151 | |
| 1152 | if( client_input_first == 1 ) |
| 1153 | { |
| 1154 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1155 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1156 | buffer0 + s_a_off, s_a_len ); |
| 1157 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1158 | { |
| 1159 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1160 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1161 | } |
| 1162 | else |
| 1163 | { |
| 1164 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1165 | } |
| 1166 | |
| 1167 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1168 | buffer0 + s_x2s_pk_off, |
| 1169 | s_x2s_pk_len ); |
| 1170 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1171 | { |
| 1172 | TEST_EQUAL( status, expected_status ); |
| 1173 | break; |
| 1174 | } |
| 1175 | else |
| 1176 | { |
| 1177 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1178 | } |
| 1179 | |
| 1180 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1181 | buffer0 + s_x2s_pr_off, |
| 1182 | s_x2s_pr_len ); |
| 1183 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1184 | { |
| 1185 | TEST_EQUAL( status, expected_status ); |
| 1186 | break; |
| 1187 | } |
| 1188 | else |
| 1189 | { |
| 1190 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1191 | } |
| 1192 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1193 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1194 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1195 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /* Client second round Output */ |
| 1199 | buffer1_off = 0; |
| 1200 | |
| 1201 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1202 | buffer1 + buffer1_off, |
| 1203 | 512 - buffer1_off, &c_a_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1204 | TEST_EQUAL( c_a_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1205 | c_a_off = buffer1_off; |
| 1206 | buffer1_off += c_a_len; |
| 1207 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1208 | buffer1 + buffer1_off, |
| 1209 | 512 - buffer1_off, &c_x2s_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1210 | TEST_EQUAL( c_x2s_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1211 | c_x2s_pk_off = buffer1_off; |
| 1212 | buffer1_off += c_x2s_pk_len; |
| 1213 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1214 | buffer1 + buffer1_off, |
| 1215 | 512 - buffer1_off, &c_x2s_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1216 | TEST_LE_U( c_x2s_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1217 | c_x2s_pr_off = buffer1_off; |
| 1218 | buffer1_off += c_x2s_pr_len; |
| 1219 | |
| 1220 | if( client_input_first == 0 ) |
| 1221 | { |
| 1222 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1223 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1224 | buffer0 + s_a_off, s_a_len ); |
| 1225 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1226 | { |
| 1227 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1228 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1229 | } |
| 1230 | else |
| 1231 | { |
| 1232 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1233 | } |
| 1234 | |
| 1235 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1236 | buffer0 + s_x2s_pk_off, |
| 1237 | s_x2s_pk_len ); |
| 1238 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1239 | { |
| 1240 | TEST_EQUAL( status, expected_status ); |
| 1241 | break; |
| 1242 | } |
| 1243 | else |
| 1244 | { |
| 1245 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1246 | } |
| 1247 | |
| 1248 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1249 | buffer0 + s_x2s_pr_off, |
| 1250 | s_x2s_pr_len ); |
| 1251 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1252 | { |
| 1253 | TEST_EQUAL( status, expected_status ); |
| 1254 | break; |
| 1255 | } |
| 1256 | else |
| 1257 | { |
| 1258 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1259 | } |
| 1260 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1261 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1262 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1263 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | if( inject_error == 4 ) |
| 1267 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1268 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1269 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1270 | } |
| 1271 | |
| 1272 | /* Server second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1273 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1274 | buffer1 + c_a_off, c_a_len ); |
| 1275 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1276 | { |
| 1277 | TEST_EQUAL( status, expected_status ); |
| 1278 | break; |
| 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1283 | } |
| 1284 | |
| 1285 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1286 | buffer1 + c_x2s_pk_off, c_x2s_pk_len ); |
| 1287 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1288 | { |
| 1289 | TEST_EQUAL( status, expected_status ); |
| 1290 | break; |
| 1291 | } |
| 1292 | else |
| 1293 | { |
| 1294 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1295 | } |
| 1296 | |
| 1297 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1298 | buffer1 + c_x2s_pr_off, c_x2s_pr_len ); |
| 1299 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1300 | { |
| 1301 | TEST_EQUAL( status, expected_status ); |
| 1302 | break; |
| 1303 | } |
| 1304 | else |
| 1305 | { |
| 1306 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1307 | } |
| 1308 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1309 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1310 | if( inject_error == 4 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1311 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1312 | |
| 1313 | break; |
| 1314 | |
| 1315 | } |
| 1316 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1317 | exit: |
| 1318 | mbedtls_free( buffer0 ); |
| 1319 | mbedtls_free( buffer1 ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1320 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1321 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1322 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1323 | /* END_HEADER */ |
| 1324 | |
| 1325 | /* BEGIN_DEPENDENCIES |
| 1326 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1327 | * END_DEPENDENCIES |
| 1328 | */ |
| 1329 | |
| 1330 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1331 | void static_checks( ) |
| 1332 | { |
| 1333 | size_t max_truncated_mac_size = |
| 1334 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1335 | |
| 1336 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1337 | * encoding. The shifted mask is the maximum truncated value. The |
| 1338 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1339 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1340 | } |
| 1341 | /* END_CASE */ |
| 1342 | |
| 1343 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1344 | void import_with_policy( int type_arg, |
| 1345 | int usage_arg, int alg_arg, |
| 1346 | int expected_status_arg ) |
| 1347 | { |
| 1348 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1349 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1350 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1351 | psa_key_type_t type = type_arg; |
| 1352 | psa_key_usage_t usage = usage_arg; |
| 1353 | psa_algorithm_t alg = alg_arg; |
| 1354 | psa_status_t expected_status = expected_status_arg; |
| 1355 | const uint8_t key_material[16] = {0}; |
| 1356 | psa_status_t status; |
| 1357 | |
| 1358 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1359 | |
| 1360 | psa_set_key_type( &attributes, type ); |
| 1361 | psa_set_key_usage_flags( &attributes, usage ); |
| 1362 | psa_set_key_algorithm( &attributes, alg ); |
| 1363 | |
| 1364 | status = psa_import_key( &attributes, |
| 1365 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1366 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1367 | TEST_EQUAL( status, expected_status ); |
| 1368 | if( status != PSA_SUCCESS ) |
| 1369 | goto exit; |
| 1370 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1371 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1372 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1373 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1374 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1375 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1376 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1377 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1378 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1379 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1380 | |
| 1381 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1382 | /* |
| 1383 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1384 | * thus reset them as required. |
| 1385 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1386 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1387 | |
| 1388 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1389 | PSA_DONE( ); |
| 1390 | } |
| 1391 | /* END_CASE */ |
| 1392 | |
| 1393 | /* BEGIN_CASE */ |
| 1394 | void import_with_data( data_t *data, int type_arg, |
| 1395 | int attr_bits_arg, |
| 1396 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1397 | { |
| 1398 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1399 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1400 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1401 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1402 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1403 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1404 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1405 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1406 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1407 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1408 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1409 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1410 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1411 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1412 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1413 | if( status != PSA_SUCCESS ) |
| 1414 | goto exit; |
| 1415 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1416 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1417 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1418 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1419 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1420 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1421 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1422 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1423 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1424 | |
| 1425 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1426 | /* |
| 1427 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1428 | * thus reset them as required. |
| 1429 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1430 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1431 | |
| 1432 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1433 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1434 | } |
| 1435 | /* END_CASE */ |
| 1436 | |
| 1437 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1438 | void import_large_key( int type_arg, int byte_size_arg, |
| 1439 | int expected_status_arg ) |
| 1440 | { |
| 1441 | psa_key_type_t type = type_arg; |
| 1442 | size_t byte_size = byte_size_arg; |
| 1443 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1444 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1445 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1446 | psa_status_t status; |
| 1447 | uint8_t *buffer = NULL; |
| 1448 | size_t buffer_size = byte_size + 1; |
| 1449 | size_t n; |
| 1450 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1451 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1452 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1453 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1454 | memset( buffer, 'K', byte_size ); |
| 1455 | |
| 1456 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1457 | |
| 1458 | /* Try importing the key */ |
| 1459 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1460 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1461 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1462 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1463 | TEST_EQUAL( status, expected_status ); |
| 1464 | |
| 1465 | if( status == PSA_SUCCESS ) |
| 1466 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1467 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1468 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1469 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1470 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1471 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1472 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1473 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1474 | for( n = 0; n < byte_size; n++ ) |
| 1475 | TEST_EQUAL( buffer[n], 'K' ); |
| 1476 | for( n = byte_size; n < buffer_size; n++ ) |
| 1477 | TEST_EQUAL( buffer[n], 0 ); |
| 1478 | } |
| 1479 | |
| 1480 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1481 | /* |
| 1482 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1483 | * thus reset them as required. |
| 1484 | */ |
| 1485 | psa_reset_key_attributes( &attributes ); |
| 1486 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1487 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1488 | PSA_DONE( ); |
| 1489 | mbedtls_free( buffer ); |
| 1490 | } |
| 1491 | /* END_CASE */ |
| 1492 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1493 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1494 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1495 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1496 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1497 | size_t bits = bits_arg; |
| 1498 | psa_status_t expected_status = expected_status_arg; |
| 1499 | psa_status_t status; |
| 1500 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1501 | keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1502 | size_t buffer_size = /* Slight overapproximations */ |
| 1503 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1504 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1505 | unsigned char *p; |
| 1506 | int ret; |
| 1507 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1508 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1509 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1510 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1511 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1512 | |
| 1513 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1514 | bits, keypair ) ) >= 0 ); |
| 1515 | length = ret; |
| 1516 | |
| 1517 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1518 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1519 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1520 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1521 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1522 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1523 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1524 | |
| 1525 | exit: |
| 1526 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1527 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1528 | } |
| 1529 | /* END_CASE */ |
| 1530 | |
| 1531 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1532 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1533 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1534 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1535 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1536 | int expected_bits, |
| 1537 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1538 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1539 | int canonical_input ) |
| 1540 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1541 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1542 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1543 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1544 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1545 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1546 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1547 | unsigned char *exported = NULL; |
| 1548 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1549 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1550 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1551 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1552 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1553 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1554 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1555 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1556 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1557 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1558 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1559 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1560 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1561 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1562 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1563 | psa_set_key_algorithm( &attributes, alg ); |
| 1564 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1565 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1566 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1567 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1568 | |
| 1569 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1570 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1571 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1572 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1573 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1574 | |
| 1575 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1576 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1577 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1578 | |
| 1579 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1580 | * and export_size. On errors, the exported length must be 0. */ |
| 1581 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1582 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1583 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1584 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1585 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1586 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1587 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1588 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1589 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1590 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1591 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1592 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1593 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1594 | * this validates the canonical representations. For canonical inputs, |
| 1595 | * this doesn't directly validate the implementation, but it still helps |
| 1596 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1597 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1598 | { |
| 1599 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1600 | goto exit; |
| 1601 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1602 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1603 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1604 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1605 | else |
| 1606 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1607 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1608 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1609 | &key2 ) ); |
| 1610 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1611 | reexported, |
| 1612 | export_size, |
| 1613 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1614 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1615 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1616 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1617 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1618 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1619 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1620 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1621 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1622 | |
| 1623 | destroy: |
| 1624 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1625 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1626 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1627 | |
| 1628 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1629 | /* |
| 1630 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1631 | * thus reset them as required. |
| 1632 | */ |
| 1633 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1634 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1635 | mbedtls_free( exported ); |
| 1636 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1637 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1638 | } |
| 1639 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1640 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1641 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1642 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1643 | int type_arg, |
| 1644 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1645 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1646 | int export_size_delta, |
| 1647 | int expected_export_status_arg, |
| 1648 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1649 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1650 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1651 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1652 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1653 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1654 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1655 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1656 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1657 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1658 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1659 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1660 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1661 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1662 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1663 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1664 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1665 | psa_set_key_algorithm( &attributes, alg ); |
| 1666 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1667 | |
| 1668 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1669 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1670 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1671 | /* Export the public key */ |
| 1672 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1673 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1674 | exported, export_size, |
| 1675 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1676 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1677 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1678 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1679 | psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1680 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1681 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1682 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1683 | TEST_LE_U( expected_public_key->len, |
| 1684 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1685 | TEST_LE_U( expected_public_key->len, |
| 1686 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1687 | TEST_LE_U( expected_public_key->len, |
| 1688 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1689 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1690 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1691 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1692 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1693 | /* |
| 1694 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1695 | * thus reset them as required. |
| 1696 | */ |
| 1697 | psa_reset_key_attributes( &attributes ); |
| 1698 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1699 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1700 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1701 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1702 | } |
| 1703 | /* END_CASE */ |
| 1704 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1705 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1706 | void import_and_exercise_key( data_t *data, |
| 1707 | int type_arg, |
| 1708 | int bits_arg, |
| 1709 | int alg_arg ) |
| 1710 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1711 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1712 | psa_key_type_t type = type_arg; |
| 1713 | size_t bits = bits_arg; |
| 1714 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1715 | psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1716 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1717 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1718 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1719 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1720 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1721 | psa_set_key_usage_flags( &attributes, usage ); |
| 1722 | psa_set_key_algorithm( &attributes, alg ); |
| 1723 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1724 | |
| 1725 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1726 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1727 | |
| 1728 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1729 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1730 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1731 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1732 | |
| 1733 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1734 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1735 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1736 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1737 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1738 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1739 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1740 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1741 | /* |
| 1742 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1743 | * thus reset them as required. |
| 1744 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1745 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1746 | |
| 1747 | psa_reset_key_attributes( &attributes ); |
| 1748 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1749 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1750 | } |
| 1751 | /* END_CASE */ |
| 1752 | |
| 1753 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1754 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1755 | int bits_arg, int expected_bits_arg, |
| 1756 | int usage_arg, int expected_usage_arg, |
| 1757 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1758 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1759 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1760 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1761 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1762 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1763 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1764 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1765 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1766 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1767 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1768 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1769 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1770 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1771 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1772 | psa_set_key_usage_flags( &attributes, usage ); |
| 1773 | psa_set_key_algorithm( &attributes, alg ); |
| 1774 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1775 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1776 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1777 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1778 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1779 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1780 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1781 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1782 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1783 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1784 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1785 | |
| 1786 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1787 | /* |
| 1788 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1789 | * thus reset them as required. |
| 1790 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1791 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1792 | |
| 1793 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1794 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1795 | } |
| 1796 | /* END_CASE */ |
| 1797 | |
| 1798 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1799 | void check_key_policy( int type_arg, int bits_arg, |
| 1800 | int usage_arg, int alg_arg ) |
| 1801 | { |
| 1802 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
gabor-mezei-arm | ff8264c | 2021-06-28 14:36:03 +0200 | [diff] [blame] | 1803 | usage_arg, |
| 1804 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1805 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1806 | goto exit; |
| 1807 | } |
| 1808 | /* END_CASE */ |
| 1809 | |
| 1810 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1811 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1812 | { |
| 1813 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1814 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1815 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1816 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1817 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1818 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1819 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1820 | |
| 1821 | memset( &zero, 0, sizeof( zero ) ); |
| 1822 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1823 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1824 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1825 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1826 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1827 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1828 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1829 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1830 | |
| 1831 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1832 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1833 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1834 | |
| 1835 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1836 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1837 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1838 | |
| 1839 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1840 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1841 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1842 | } |
| 1843 | /* END_CASE */ |
| 1844 | |
| 1845 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1846 | void mac_key_policy( int policy_usage_arg, |
| 1847 | int policy_alg_arg, |
| 1848 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1849 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1850 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1851 | int expected_status_sign_arg, |
| 1852 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1853 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1854 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1855 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1856 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1857 | psa_key_type_t key_type = key_type_arg; |
| 1858 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1859 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1860 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1861 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1862 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1863 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1864 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1865 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1866 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1867 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1868 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1869 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1870 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1871 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1872 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1873 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1874 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1875 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1876 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1877 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1878 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1879 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1880 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1881 | /* Calculate the MAC, one-shot case. */ |
| 1882 | uint8_t input[128] = {0}; |
| 1883 | size_t mac_len; |
| 1884 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1885 | input, 128, |
| 1886 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1887 | expected_status_sign ); |
| 1888 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1889 | /* Calculate the MAC, multi-part case. */ |
| 1890 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1891 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1892 | if( status == PSA_SUCCESS ) |
| 1893 | { |
| 1894 | status = psa_mac_update( &operation, input, 128 ); |
| 1895 | if( status == PSA_SUCCESS ) |
| 1896 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1897 | &mac_len ), |
| 1898 | expected_status_sign ); |
| 1899 | else |
| 1900 | TEST_EQUAL( status, expected_status_sign ); |
| 1901 | } |
| 1902 | else |
| 1903 | { |
| 1904 | TEST_EQUAL( status, expected_status_sign ); |
| 1905 | } |
| 1906 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1907 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1908 | /* Verify correct MAC, one-shot case. */ |
| 1909 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1910 | mac, mac_len ); |
| 1911 | |
| 1912 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1913 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1914 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1915 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1916 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1917 | /* Verify correct MAC, multi-part case. */ |
| 1918 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1919 | if( status == PSA_SUCCESS ) |
| 1920 | { |
| 1921 | status = psa_mac_update( &operation, input, 128 ); |
| 1922 | if( status == PSA_SUCCESS ) |
| 1923 | { |
| 1924 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1925 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1926 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1927 | else |
| 1928 | TEST_EQUAL( status, expected_status_verify ); |
| 1929 | } |
| 1930 | else |
| 1931 | { |
| 1932 | TEST_EQUAL( status, expected_status_verify ); |
| 1933 | } |
| 1934 | } |
| 1935 | else |
| 1936 | { |
| 1937 | TEST_EQUAL( status, expected_status_verify ); |
| 1938 | } |
| 1939 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1940 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1941 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1942 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1943 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1944 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1945 | |
| 1946 | exit: |
| 1947 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1948 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1949 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1950 | } |
| 1951 | /* END_CASE */ |
| 1952 | |
| 1953 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1954 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1955 | int policy_alg, |
| 1956 | int key_type, |
| 1957 | data_t *key_data, |
| 1958 | int exercise_alg ) |
| 1959 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1960 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1961 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1962 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1963 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1964 | size_t output_buffer_size = 0; |
| 1965 | size_t input_buffer_size = 0; |
| 1966 | size_t output_length = 0; |
| 1967 | uint8_t *output = NULL; |
| 1968 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1969 | psa_status_t status; |
| 1970 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1971 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1972 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1973 | input_buffer_size ); |
| 1974 | |
| 1975 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1976 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1977 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1978 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1979 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1980 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1981 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1982 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1983 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1984 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1985 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1986 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1987 | /* Check if no key usage flag implication is done */ |
| 1988 | TEST_EQUAL( policy_usage, |
| 1989 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1990 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1991 | /* Encrypt check, one-shot */ |
| 1992 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1993 | output, output_buffer_size, |
| 1994 | &output_length); |
| 1995 | if( policy_alg == exercise_alg && |
| 1996 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1997 | PSA_ASSERT( status ); |
| 1998 | else |
| 1999 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2000 | |
| 2001 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2002 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2003 | if( policy_alg == exercise_alg && |
| 2004 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2005 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2006 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2007 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2008 | psa_cipher_abort( &operation ); |
| 2009 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2010 | /* Decrypt check, one-shot */ |
| 2011 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 2012 | input, input_buffer_size, |
| 2013 | &output_length); |
| 2014 | if( policy_alg == exercise_alg && |
| 2015 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 2016 | PSA_ASSERT( status ); |
| 2017 | else |
| 2018 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2019 | |
| 2020 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2021 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2022 | if( policy_alg == exercise_alg && |
| 2023 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2024 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2025 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2026 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2027 | |
| 2028 | exit: |
| 2029 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2030 | mbedtls_free( input ); |
| 2031 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2032 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2033 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2034 | } |
| 2035 | /* END_CASE */ |
| 2036 | |
| 2037 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2038 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2039 | int policy_alg, |
| 2040 | int key_type, |
| 2041 | data_t *key_data, |
| 2042 | int nonce_length_arg, |
| 2043 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2044 | int exercise_alg, |
| 2045 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2046 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2047 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2048 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2049 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2050 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2051 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2052 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2053 | unsigned char nonce[16] = {0}; |
| 2054 | size_t nonce_length = nonce_length_arg; |
| 2055 | unsigned char tag[16]; |
| 2056 | size_t tag_length = tag_length_arg; |
| 2057 | size_t output_length; |
| 2058 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2059 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 2060 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2061 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2062 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2063 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2064 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2065 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2066 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2067 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2068 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2069 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2070 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2071 | /* Check if no key usage implication is done */ |
| 2072 | TEST_EQUAL( policy_usage, |
| 2073 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2074 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2075 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2076 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2077 | nonce, nonce_length, |
| 2078 | NULL, 0, |
| 2079 | NULL, 0, |
| 2080 | tag, tag_length, |
| 2081 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2082 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2083 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2084 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2085 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2086 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2087 | /* Encrypt check, multi-part */ |
| 2088 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 2089 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2090 | TEST_EQUAL( status, expected_status ); |
| 2091 | else |
| 2092 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2093 | |
| 2094 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2095 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2096 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2097 | nonce, nonce_length, |
| 2098 | NULL, 0, |
| 2099 | tag, tag_length, |
| 2100 | NULL, 0, |
| 2101 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2102 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2103 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2104 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2105 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2106 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2107 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2108 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2109 | /* Decrypt check, multi-part */ |
| 2110 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 2111 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 2112 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2113 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2114 | else |
| 2115 | TEST_EQUAL( status, expected_status ); |
| 2116 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2117 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2118 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2119 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2120 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2121 | } |
| 2122 | /* END_CASE */ |
| 2123 | |
| 2124 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2125 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2126 | int policy_alg, |
| 2127 | int key_type, |
| 2128 | data_t *key_data, |
| 2129 | int exercise_alg ) |
| 2130 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2131 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2132 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2133 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2134 | psa_status_t status; |
| 2135 | size_t key_bits; |
| 2136 | size_t buffer_length; |
| 2137 | unsigned char *buffer = NULL; |
| 2138 | size_t output_length; |
| 2139 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2140 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2141 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2142 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2143 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2144 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2145 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2146 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2147 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2148 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2149 | /* Check if no key usage implication is done */ |
| 2150 | TEST_EQUAL( policy_usage, |
| 2151 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2152 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2153 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2154 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2155 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2156 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2157 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2158 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2159 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2160 | NULL, 0, |
| 2161 | NULL, 0, |
| 2162 | buffer, buffer_length, |
| 2163 | &output_length ); |
| 2164 | if( policy_alg == exercise_alg && |
| 2165 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2166 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2167 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2168 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2169 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2170 | if( buffer_length != 0 ) |
| 2171 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2172 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2173 | buffer, buffer_length, |
| 2174 | NULL, 0, |
| 2175 | buffer, buffer_length, |
| 2176 | &output_length ); |
| 2177 | if( policy_alg == exercise_alg && |
| 2178 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2179 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2180 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2181 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2182 | |
| 2183 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2184 | /* |
| 2185 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2186 | * thus reset them as required. |
| 2187 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2188 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2189 | |
| 2190 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2191 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2192 | mbedtls_free( buffer ); |
| 2193 | } |
| 2194 | /* END_CASE */ |
| 2195 | |
| 2196 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2197 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2198 | int policy_alg, |
| 2199 | int key_type, |
| 2200 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2201 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2202 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2203 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2204 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2205 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2206 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2207 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2208 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2209 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2210 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2211 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2212 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2213 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2214 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2215 | int compatible_alg = payload_length_arg > 0; |
| 2216 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2217 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2218 | size_t signature_length; |
| 2219 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2220 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2221 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2222 | TEST_EQUAL( expected_usage, |
| 2223 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2224 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2225 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2226 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2227 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2228 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2229 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2230 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2231 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2232 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2233 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2234 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 2235 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2236 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2237 | payload, payload_length, |
| 2238 | signature, sizeof( signature ), |
| 2239 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2240 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2241 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2242 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2243 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2244 | |
| 2245 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2246 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2247 | payload, payload_length, |
| 2248 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2249 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2250 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2251 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2252 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2253 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2254 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 2255 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2256 | { |
| 2257 | status = psa_sign_message( key, exercise_alg, |
| 2258 | payload, payload_length, |
| 2259 | signature, sizeof( signature ), |
| 2260 | &signature_length ); |
| 2261 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 2262 | PSA_ASSERT( status ); |
| 2263 | else |
| 2264 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2265 | |
| 2266 | memset( signature, 0, sizeof( signature ) ); |
| 2267 | status = psa_verify_message( key, exercise_alg, |
| 2268 | payload, payload_length, |
| 2269 | signature, sizeof( signature ) ); |
| 2270 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 2271 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 2272 | else |
| 2273 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2274 | } |
| 2275 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2276 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2277 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2278 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2279 | } |
| 2280 | /* END_CASE */ |
| 2281 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2282 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2283 | void derive_key_policy( int policy_usage, |
| 2284 | int policy_alg, |
| 2285 | int key_type, |
| 2286 | data_t *key_data, |
| 2287 | int exercise_alg ) |
| 2288 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2289 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2290 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2291 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2292 | psa_status_t status; |
| 2293 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2294 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2295 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2296 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2297 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2298 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2299 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2300 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2301 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2302 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2303 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2304 | |
| 2305 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2306 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2307 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2308 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2309 | &operation, |
| 2310 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2311 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2312 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2313 | |
| 2314 | status = psa_key_derivation_input_key( &operation, |
| 2315 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2316 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2317 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2318 | if( policy_alg == exercise_alg && |
| 2319 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2320 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2321 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2322 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2323 | |
| 2324 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2325 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2326 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2327 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2328 | } |
| 2329 | /* END_CASE */ |
| 2330 | |
| 2331 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2332 | void agreement_key_policy( int policy_usage, |
| 2333 | int policy_alg, |
| 2334 | int key_type_arg, |
| 2335 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2336 | int exercise_alg, |
| 2337 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2338 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2339 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2340 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2341 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2342 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2343 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2344 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2345 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2346 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2347 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2348 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2349 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2350 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2351 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2352 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2353 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2354 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2355 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2356 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2357 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2358 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2359 | |
| 2360 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2361 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2362 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2363 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2364 | } |
| 2365 | /* END_CASE */ |
| 2366 | |
| 2367 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2368 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2369 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2370 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2371 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2372 | psa_key_type_t key_type = key_type_arg; |
| 2373 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2374 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2375 | psa_key_usage_t usage = usage_arg; |
| 2376 | psa_algorithm_t alg = alg_arg; |
| 2377 | psa_algorithm_t alg2 = alg2_arg; |
| 2378 | |
| 2379 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2380 | |
| 2381 | psa_set_key_usage_flags( &attributes, usage ); |
| 2382 | psa_set_key_algorithm( &attributes, alg ); |
| 2383 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2384 | psa_set_key_type( &attributes, key_type ); |
| 2385 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2386 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2387 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2388 | /* Update the usage flags to obtain implicit usage flags */ |
| 2389 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2390 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2391 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2392 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2393 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2394 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2395 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2396 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2397 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2398 | goto exit; |
| 2399 | |
| 2400 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2401 | /* |
| 2402 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2403 | * thus reset them as required. |
| 2404 | */ |
| 2405 | psa_reset_key_attributes( &got_attributes ); |
| 2406 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2407 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2408 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2409 | } |
| 2410 | /* END_CASE */ |
| 2411 | |
| 2412 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2413 | void raw_agreement_key_policy( int policy_usage, |
| 2414 | int policy_alg, |
| 2415 | int key_type_arg, |
| 2416 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2417 | int exercise_alg, |
| 2418 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2419 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2420 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2421 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2422 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2423 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2424 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2425 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2426 | |
| 2427 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2428 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2429 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2430 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2431 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2432 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2433 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2434 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2435 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2436 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2437 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2438 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2439 | |
| 2440 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2441 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2442 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2443 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2444 | } |
| 2445 | /* END_CASE */ |
| 2446 | |
| 2447 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2448 | void copy_success( int source_usage_arg, |
| 2449 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2450 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2451 | int type_arg, data_t *material, |
| 2452 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2453 | int target_usage_arg, |
| 2454 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2455 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2456 | int expected_usage_arg, |
| 2457 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2458 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2459 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2460 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2461 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2462 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2463 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2464 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2465 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2466 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2467 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2468 | uint8_t *export_buffer = NULL; |
| 2469 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2470 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2471 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2472 | /* Prepare the source key. */ |
| 2473 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2474 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2475 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2476 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2477 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2478 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2479 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2480 | &source_key ) ); |
| 2481 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2482 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2483 | /* Prepare the target attributes. */ |
| 2484 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2485 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2486 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2487 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2488 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2489 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2490 | if( target_usage_arg != -1 ) |
| 2491 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2492 | if( target_alg_arg != -1 ) |
| 2493 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2494 | if( target_alg2_arg != -1 ) |
| 2495 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2496 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2497 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2498 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2499 | PSA_ASSERT( psa_copy_key( source_key, |
| 2500 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2501 | |
| 2502 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2503 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2504 | |
| 2505 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2506 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2507 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2508 | psa_get_key_type( &target_attributes ) ); |
| 2509 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2510 | psa_get_key_bits( &target_attributes ) ); |
| 2511 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2512 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2513 | TEST_EQUAL( expected_alg2, |
| 2514 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2515 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2516 | { |
| 2517 | size_t length; |
| 2518 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2519 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2520 | material->len, &length ) ); |
| 2521 | ASSERT_COMPARE( material->x, material->len, |
| 2522 | export_buffer, length ); |
| 2523 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2524 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2525 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2526 | { |
| 2527 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2528 | goto exit; |
| 2529 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2530 | goto exit; |
| 2531 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2532 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2533 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2534 | |
| 2535 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2536 | /* |
| 2537 | * Source and target key attributes may have been returned by |
| 2538 | * psa_get_key_attributes() thus reset them as required. |
| 2539 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2540 | psa_reset_key_attributes( &source_attributes ); |
| 2541 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2542 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2543 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2544 | mbedtls_free( export_buffer ); |
| 2545 | } |
| 2546 | /* END_CASE */ |
| 2547 | |
| 2548 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2549 | void copy_fail( int source_usage_arg, |
| 2550 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2551 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2552 | int type_arg, data_t *material, |
| 2553 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2554 | int target_usage_arg, |
| 2555 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2556 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2557 | int expected_status_arg ) |
| 2558 | { |
| 2559 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2560 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2561 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2562 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2563 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2564 | |
| 2565 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2566 | |
| 2567 | /* Prepare the source key. */ |
| 2568 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2569 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2570 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2571 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2572 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2573 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2574 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2575 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2576 | |
| 2577 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2578 | psa_set_key_id( &target_attributes, key_id ); |
| 2579 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2580 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2581 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2582 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2583 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2584 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2585 | |
| 2586 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2587 | TEST_EQUAL( psa_copy_key( source_key, |
| 2588 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2589 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2590 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2591 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2592 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2593 | exit: |
| 2594 | psa_reset_key_attributes( &source_attributes ); |
| 2595 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2596 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2597 | } |
| 2598 | /* END_CASE */ |
| 2599 | |
| 2600 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2601 | void hash_operation_init( ) |
| 2602 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2603 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2604 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2605 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2606 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2607 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2608 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2609 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2610 | psa_hash_operation_t zero; |
| 2611 | |
| 2612 | memset( &zero, 0, sizeof( zero ) ); |
| 2613 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2614 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2615 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2616 | PSA_ERROR_BAD_STATE ); |
| 2617 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2618 | PSA_ERROR_BAD_STATE ); |
| 2619 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2620 | PSA_ERROR_BAD_STATE ); |
| 2621 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2622 | /* A default hash operation should be abortable without error. */ |
| 2623 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2624 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2625 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2626 | } |
| 2627 | /* END_CASE */ |
| 2628 | |
| 2629 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2630 | void hash_setup( int alg_arg, |
| 2631 | int expected_status_arg ) |
| 2632 | { |
| 2633 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2634 | uint8_t *output = NULL; |
| 2635 | size_t output_size = 0; |
| 2636 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2637 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2638 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2639 | psa_status_t status; |
| 2640 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2641 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2642 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2643 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2644 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2645 | ASSERT_ALLOC( output, output_size ); |
| 2646 | |
| 2647 | status = psa_hash_compute( alg, NULL, 0, |
| 2648 | output, output_size, &output_length ); |
| 2649 | TEST_EQUAL( status, expected_status ); |
| 2650 | |
| 2651 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2652 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2653 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2654 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2655 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2656 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2657 | |
| 2658 | /* If setup failed, reproduce the failure, so as to |
| 2659 | * test the resulting state of the operation object. */ |
| 2660 | if( status != PSA_SUCCESS ) |
| 2661 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2662 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2663 | /* Now the operation object should be reusable. */ |
| 2664 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2665 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2666 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2667 | #endif |
| 2668 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2669 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2670 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2671 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2672 | } |
| 2673 | /* END_CASE */ |
| 2674 | |
| 2675 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2676 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2677 | int output_size_arg, int expected_status_arg ) |
| 2678 | { |
| 2679 | psa_algorithm_t alg = alg_arg; |
| 2680 | uint8_t *output = NULL; |
| 2681 | size_t output_size = output_size_arg; |
| 2682 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2683 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2684 | psa_status_t expected_status = expected_status_arg; |
| 2685 | psa_status_t status; |
| 2686 | |
| 2687 | ASSERT_ALLOC( output, output_size ); |
| 2688 | |
| 2689 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2690 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2691 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2692 | status = psa_hash_compute( alg, input->x, input->len, |
| 2693 | output, output_size, &output_length ); |
| 2694 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2695 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2696 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2697 | /* Hash Compute, multi-part */ |
| 2698 | status = psa_hash_setup( &operation, alg ); |
| 2699 | if( status == PSA_SUCCESS ) |
| 2700 | { |
| 2701 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2702 | if( status == PSA_SUCCESS ) |
| 2703 | { |
| 2704 | status = psa_hash_finish( &operation, output, output_size, |
| 2705 | &output_length ); |
| 2706 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2707 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2708 | else |
| 2709 | TEST_EQUAL( status, expected_status ); |
| 2710 | } |
| 2711 | else |
| 2712 | { |
| 2713 | TEST_EQUAL( status, expected_status ); |
| 2714 | } |
| 2715 | } |
| 2716 | else |
| 2717 | { |
| 2718 | TEST_EQUAL( status, expected_status ); |
| 2719 | } |
| 2720 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2721 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2722 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2723 | mbedtls_free( output ); |
| 2724 | PSA_DONE( ); |
| 2725 | } |
| 2726 | /* END_CASE */ |
| 2727 | |
| 2728 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2729 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2730 | data_t *reference_hash, |
| 2731 | int expected_status_arg ) |
| 2732 | { |
| 2733 | psa_algorithm_t alg = alg_arg; |
| 2734 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2735 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2736 | psa_status_t status; |
| 2737 | |
| 2738 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2739 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2740 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2741 | status = psa_hash_compare( alg, input->x, input->len, |
| 2742 | reference_hash->x, reference_hash->len ); |
| 2743 | TEST_EQUAL( status, expected_status ); |
| 2744 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2745 | /* Hash Compare, multi-part */ |
| 2746 | status = psa_hash_setup( &operation, alg ); |
| 2747 | if( status == PSA_SUCCESS ) |
| 2748 | { |
| 2749 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2750 | if( status == PSA_SUCCESS ) |
| 2751 | { |
| 2752 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2753 | reference_hash->len ); |
| 2754 | TEST_EQUAL( status, expected_status ); |
| 2755 | } |
| 2756 | else |
| 2757 | { |
| 2758 | TEST_EQUAL( status, expected_status ); |
| 2759 | } |
| 2760 | } |
| 2761 | else |
| 2762 | { |
| 2763 | TEST_EQUAL( status, expected_status ); |
| 2764 | } |
| 2765 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2766 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2767 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2768 | PSA_DONE( ); |
| 2769 | } |
| 2770 | /* END_CASE */ |
| 2771 | |
| 2772 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2773 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2774 | data_t *expected_output ) |
| 2775 | { |
| 2776 | psa_algorithm_t alg = alg_arg; |
| 2777 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2778 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2779 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2780 | size_t i; |
| 2781 | |
| 2782 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2783 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2784 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2785 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2786 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2787 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2788 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2789 | ASSERT_COMPARE( output, output_length, |
| 2790 | expected_output->x, expected_output->len ); |
| 2791 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2792 | /* Compute with tight buffer, multi-part */ |
| 2793 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2794 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2795 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2796 | PSA_HASH_LENGTH( alg ), |
| 2797 | &output_length ) ); |
| 2798 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2799 | ASSERT_COMPARE( output, output_length, |
| 2800 | expected_output->x, expected_output->len ); |
| 2801 | |
| 2802 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2803 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2804 | output, sizeof( output ), |
| 2805 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2806 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2807 | ASSERT_COMPARE( output, output_length, |
| 2808 | expected_output->x, expected_output->len ); |
| 2809 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2810 | /* Compute with larger buffer, multi-part */ |
| 2811 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2812 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2813 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2814 | sizeof( output ), &output_length ) ); |
| 2815 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2816 | ASSERT_COMPARE( output, output_length, |
| 2817 | expected_output->x, expected_output->len ); |
| 2818 | |
| 2819 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2820 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2821 | output, output_length ) ); |
| 2822 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2823 | /* Compare with correct hash, multi-part */ |
| 2824 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2825 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2826 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2827 | output_length ) ); |
| 2828 | |
| 2829 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2830 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2831 | output, output_length + 1 ), |
| 2832 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2833 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2834 | /* Compare with trailing garbage, multi-part */ |
| 2835 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2836 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2837 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2838 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2839 | |
| 2840 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2841 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2842 | output, output_length - 1 ), |
| 2843 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2844 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2845 | /* Compare with truncated hash, multi-part */ |
| 2846 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2847 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2848 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2849 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2850 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2851 | /* Compare with corrupted value */ |
| 2852 | for( i = 0; i < output_length; i++ ) |
| 2853 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2854 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2855 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2856 | |
| 2857 | /* One-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2858 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2859 | output, output_length ), |
| 2860 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2861 | |
| 2862 | /* Multi-Part */ |
| 2863 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2864 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2865 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length ), |
| 2866 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2867 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2868 | output[i] ^= 1; |
| 2869 | } |
| 2870 | |
| 2871 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2872 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2873 | PSA_DONE( ); |
| 2874 | } |
| 2875 | /* END_CASE */ |
| 2876 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2877 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2878 | void hash_bad_order( ) |
| 2879 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2880 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2881 | unsigned char input[] = ""; |
| 2882 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2883 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2884 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2885 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2886 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2887 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2888 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2889 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2890 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2891 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2892 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2893 | /* Call setup twice in a row. */ |
| 2894 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2895 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2896 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2897 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2898 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2899 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2900 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2901 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2902 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2903 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2904 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2905 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2906 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2907 | /* Check that update calls abort on error. */ |
| 2908 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2909 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2910 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2911 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2912 | PSA_ERROR_BAD_STATE ); |
| 2913 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2914 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2915 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2916 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2917 | /* Call update after finish. */ |
| 2918 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2919 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2920 | hash, sizeof( hash ), &hash_len ) ); |
| 2921 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2922 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2923 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2924 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2925 | /* Call verify without calling setup beforehand. */ |
| 2926 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2927 | valid_hash, sizeof( valid_hash ) ), |
| 2928 | PSA_ERROR_BAD_STATE ); |
| 2929 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2930 | |
| 2931 | /* Call verify after finish. */ |
| 2932 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2933 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2934 | hash, sizeof( hash ), &hash_len ) ); |
| 2935 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2936 | valid_hash, sizeof( valid_hash ) ), |
| 2937 | PSA_ERROR_BAD_STATE ); |
| 2938 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2939 | |
| 2940 | /* Call verify twice in a row. */ |
| 2941 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2942 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2943 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2944 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2945 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2946 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2947 | valid_hash, sizeof( valid_hash ) ), |
| 2948 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2949 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2950 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2951 | |
| 2952 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2953 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2954 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2955 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2956 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2957 | |
| 2958 | /* Call finish twice in a row. */ |
| 2959 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2960 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2961 | hash, sizeof( hash ), &hash_len ) ); |
| 2962 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2963 | hash, sizeof( hash ), &hash_len ), |
| 2964 | PSA_ERROR_BAD_STATE ); |
| 2965 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2966 | |
| 2967 | /* Call finish after calling verify. */ |
| 2968 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2969 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2970 | valid_hash, sizeof( valid_hash ) ) ); |
| 2971 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2972 | hash, sizeof( hash ), &hash_len ), |
| 2973 | PSA_ERROR_BAD_STATE ); |
| 2974 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2975 | |
| 2976 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2977 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2978 | } |
| 2979 | /* END_CASE */ |
| 2980 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2981 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2982 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2983 | { |
| 2984 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2985 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2986 | * appended to it */ |
| 2987 | unsigned char hash[] = { |
| 2988 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2989 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2990 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2991 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2992 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2993 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2994 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2995 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2996 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2997 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2998 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2999 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3000 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3001 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 3002 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 3003 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3004 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3005 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3006 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3007 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3008 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3009 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3010 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3011 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3012 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3013 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3014 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3015 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3016 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3017 | } |
| 3018 | /* END_CASE */ |
| 3019 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3020 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3021 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3022 | { |
| 3023 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3024 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3025 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3026 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3027 | size_t hash_len; |
| 3028 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3029 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3030 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3031 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3032 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3033 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3034 | hash, expected_size - 1, &hash_len ), |
| 3035 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3036 | |
| 3037 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3038 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3039 | } |
| 3040 | /* END_CASE */ |
| 3041 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3042 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3043 | void hash_clone_source_state( ) |
| 3044 | { |
| 3045 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3046 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3047 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3048 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3049 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3050 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3051 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3052 | size_t hash_len; |
| 3053 | |
| 3054 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3055 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 3056 | |
| 3057 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3058 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3059 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3060 | hash, sizeof( hash ), &hash_len ) ); |
| 3061 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3062 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3063 | |
| 3064 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 3065 | PSA_ERROR_BAD_STATE ); |
| 3066 | |
| 3067 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 3068 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 3069 | hash, sizeof( hash ), &hash_len ) ); |
| 3070 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 3071 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3072 | hash, sizeof( hash ), &hash_len ) ); |
| 3073 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 3074 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 3075 | hash, sizeof( hash ), &hash_len ) ); |
| 3076 | |
| 3077 | exit: |
| 3078 | psa_hash_abort( &op_source ); |
| 3079 | psa_hash_abort( &op_init ); |
| 3080 | psa_hash_abort( &op_setup ); |
| 3081 | psa_hash_abort( &op_finished ); |
| 3082 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3083 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3084 | } |
| 3085 | /* END_CASE */ |
| 3086 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3087 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3088 | void hash_clone_target_state( ) |
| 3089 | { |
| 3090 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3091 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3092 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3093 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3094 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3095 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3096 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3097 | size_t hash_len; |
| 3098 | |
| 3099 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3100 | |
| 3101 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3102 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3103 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3104 | hash, sizeof( hash ), &hash_len ) ); |
| 3105 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3106 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3107 | |
| 3108 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 3109 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 3110 | hash, sizeof( hash ), &hash_len ) ); |
| 3111 | |
| 3112 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 3113 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 3114 | PSA_ERROR_BAD_STATE ); |
| 3115 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 3116 | PSA_ERROR_BAD_STATE ); |
| 3117 | |
| 3118 | exit: |
| 3119 | psa_hash_abort( &op_target ); |
| 3120 | psa_hash_abort( &op_init ); |
| 3121 | psa_hash_abort( &op_setup ); |
| 3122 | psa_hash_abort( &op_finished ); |
| 3123 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3124 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3125 | } |
| 3126 | /* END_CASE */ |
| 3127 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3128 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3129 | void mac_operation_init( ) |
| 3130 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3131 | const uint8_t input[1] = { 0 }; |
| 3132 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3133 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3134 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3135 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3136 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3137 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 3138 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3139 | psa_mac_operation_t zero; |
| 3140 | |
| 3141 | memset( &zero, 0, sizeof( zero ) ); |
| 3142 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3143 | /* A freshly-initialized MAC operation should not be usable. */ |
| 3144 | TEST_EQUAL( psa_mac_update( &func, |
| 3145 | input, sizeof( input ) ), |
| 3146 | PSA_ERROR_BAD_STATE ); |
| 3147 | TEST_EQUAL( psa_mac_update( &init, |
| 3148 | input, sizeof( input ) ), |
| 3149 | PSA_ERROR_BAD_STATE ); |
| 3150 | TEST_EQUAL( psa_mac_update( &zero, |
| 3151 | input, sizeof( input ) ), |
| 3152 | PSA_ERROR_BAD_STATE ); |
| 3153 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3154 | /* A default MAC operation should be abortable without error. */ |
| 3155 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 3156 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 3157 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3158 | } |
| 3159 | /* END_CASE */ |
| 3160 | |
| 3161 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3162 | void mac_setup( int key_type_arg, |
| 3163 | data_t *key, |
| 3164 | int alg_arg, |
| 3165 | int expected_status_arg ) |
| 3166 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3167 | psa_key_type_t key_type = key_type_arg; |
| 3168 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3169 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3170 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3171 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3172 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3173 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3174 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3175 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3176 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3177 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3178 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 3179 | &operation, &status ) ) |
| 3180 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3181 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3182 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3183 | /* The operation object should be reusable. */ |
| 3184 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3185 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3186 | smoke_test_key_data, |
| 3187 | sizeof( smoke_test_key_data ), |
| 3188 | KNOWN_SUPPORTED_MAC_ALG, |
| 3189 | &operation, &status ) ) |
| 3190 | goto exit; |
| 3191 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3192 | #endif |
| 3193 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3194 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3195 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3196 | } |
| 3197 | /* END_CASE */ |
| 3198 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3199 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */ |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3200 | void mac_bad_order( ) |
| 3201 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3202 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3203 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3204 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3205 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3206 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3207 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3208 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3209 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3210 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3211 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3212 | size_t sign_mac_length = 0; |
| 3213 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3214 | const uint8_t verify_mac[] = { |
| 3215 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3216 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 3217 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 3218 | |
| 3219 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3220 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3221 | psa_set_key_algorithm( &attributes, alg ); |
| 3222 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3223 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3224 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3225 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3226 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3227 | /* Call update without calling setup beforehand. */ |
| 3228 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3229 | PSA_ERROR_BAD_STATE ); |
| 3230 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3231 | |
| 3232 | /* Call sign finish without calling setup beforehand. */ |
| 3233 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 3234 | &sign_mac_length), |
| 3235 | PSA_ERROR_BAD_STATE ); |
| 3236 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3237 | |
| 3238 | /* Call verify finish without calling setup beforehand. */ |
| 3239 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3240 | verify_mac, sizeof( verify_mac ) ), |
| 3241 | PSA_ERROR_BAD_STATE ); |
| 3242 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3243 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3244 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3245 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3246 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3247 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3248 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3249 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3250 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3251 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3252 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3253 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3254 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3255 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3256 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3257 | sign_mac, sizeof( sign_mac ), |
| 3258 | &sign_mac_length ) ); |
| 3259 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3260 | PSA_ERROR_BAD_STATE ); |
| 3261 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3262 | |
| 3263 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3264 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3265 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3266 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3267 | verify_mac, sizeof( verify_mac ) ) ); |
| 3268 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3269 | PSA_ERROR_BAD_STATE ); |
| 3270 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3271 | |
| 3272 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3273 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3274 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3275 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3276 | sign_mac, sizeof( sign_mac ), |
| 3277 | &sign_mac_length ) ); |
| 3278 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3279 | sign_mac, sizeof( sign_mac ), |
| 3280 | &sign_mac_length ), |
| 3281 | PSA_ERROR_BAD_STATE ); |
| 3282 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3283 | |
| 3284 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3285 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3286 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3287 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3288 | verify_mac, sizeof( verify_mac ) ) ); |
| 3289 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3290 | verify_mac, sizeof( verify_mac ) ), |
| 3291 | PSA_ERROR_BAD_STATE ); |
| 3292 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3293 | |
| 3294 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3295 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3296 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3297 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3298 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3299 | verify_mac, sizeof( verify_mac ) ), |
| 3300 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3301 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3302 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3303 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3304 | |
| 3305 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3306 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3307 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3308 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3309 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3310 | sign_mac, sizeof( sign_mac ), |
| 3311 | &sign_mac_length ), |
| 3312 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3313 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3314 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3315 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3316 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3317 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3318 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3319 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3320 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3321 | } |
| 3322 | /* END_CASE */ |
| 3323 | |
| 3324 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3325 | void mac_sign_verify_multi( int key_type_arg, |
| 3326 | data_t *key_data, |
| 3327 | int alg_arg, |
| 3328 | data_t *input, |
| 3329 | int is_verify, |
| 3330 | data_t *expected_mac ) |
| 3331 | { |
| 3332 | size_t data_part_len = 0; |
| 3333 | |
| 3334 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3335 | { |
| 3336 | /* Split data into length(data_part_len) parts. */ |
| 3337 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3338 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3339 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3340 | alg_arg, |
| 3341 | input, data_part_len, |
| 3342 | expected_mac, |
| 3343 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3344 | break; |
| 3345 | |
| 3346 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3347 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3348 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3349 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3350 | alg_arg, |
| 3351 | input, data_part_len, |
| 3352 | expected_mac, |
| 3353 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3354 | break; |
| 3355 | } |
| 3356 | |
| 3357 | /* Goto is required to silence warnings about unused labels, as we |
| 3358 | * don't actually do any test assertions in this function. */ |
| 3359 | goto exit; |
| 3360 | } |
| 3361 | /* END_CASE */ |
| 3362 | |
| 3363 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3364 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3365 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3366 | int alg_arg, |
| 3367 | data_t *input, |
| 3368 | data_t *expected_mac ) |
| 3369 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3370 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3371 | psa_key_type_t key_type = key_type_arg; |
| 3372 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3373 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3374 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3375 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3376 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3377 | PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3378 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3379 | const size_t output_sizes_to_test[] = { |
| 3380 | 0, |
| 3381 | 1, |
| 3382 | expected_mac->len - 1, |
| 3383 | expected_mac->len, |
| 3384 | expected_mac->len + 1, |
| 3385 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3386 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3387 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3388 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3389 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3390 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3391 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3392 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3393 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3394 | psa_set_key_algorithm( &attributes, alg ); |
| 3395 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3396 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3397 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3398 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3399 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3400 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3401 | { |
| 3402 | const size_t output_size = output_sizes_to_test[i]; |
| 3403 | psa_status_t expected_status = |
| 3404 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3405 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3406 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3407 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3408 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3409 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3410 | /* Calculate the MAC, one-shot case. */ |
| 3411 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3412 | input->x, input->len, |
| 3413 | actual_mac, output_size, &mac_length ), |
| 3414 | expected_status ); |
| 3415 | if( expected_status == PSA_SUCCESS ) |
| 3416 | { |
| 3417 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3418 | actual_mac, mac_length ); |
| 3419 | } |
| 3420 | |
| 3421 | if( output_size > 0 ) |
| 3422 | memset( actual_mac, 0, output_size ); |
| 3423 | |
| 3424 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3425 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3426 | PSA_ASSERT( psa_mac_update( &operation, |
| 3427 | input->x, input->len ) ); |
| 3428 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3429 | actual_mac, output_size, |
| 3430 | &mac_length ), |
| 3431 | expected_status ); |
| 3432 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3433 | |
| 3434 | if( expected_status == PSA_SUCCESS ) |
| 3435 | { |
| 3436 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3437 | actual_mac, mac_length ); |
| 3438 | } |
| 3439 | mbedtls_free( actual_mac ); |
| 3440 | actual_mac = NULL; |
| 3441 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3442 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3443 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3444 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3445 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3446 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3447 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3448 | } |
| 3449 | /* END_CASE */ |
| 3450 | |
| 3451 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3452 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3453 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3454 | int alg_arg, |
| 3455 | data_t *input, |
| 3456 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3457 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3458 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3459 | psa_key_type_t key_type = key_type_arg; |
| 3460 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3461 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3462 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3463 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3464 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3465 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3466 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3467 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3468 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3469 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3470 | psa_set_key_algorithm( &attributes, alg ); |
| 3471 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3472 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3473 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3474 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3475 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3476 | /* Verify correct MAC, one-shot case. */ |
| 3477 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3478 | expected_mac->x, expected_mac->len ) ); |
| 3479 | |
| 3480 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3481 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3482 | PSA_ASSERT( psa_mac_update( &operation, |
| 3483 | input->x, input->len ) ); |
| 3484 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3485 | expected_mac->x, |
| 3486 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3487 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3488 | /* Test a MAC that's too short, one-shot case. */ |
| 3489 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3490 | input->x, input->len, |
| 3491 | expected_mac->x, |
| 3492 | expected_mac->len - 1 ), |
| 3493 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3494 | |
| 3495 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3496 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3497 | PSA_ASSERT( psa_mac_update( &operation, |
| 3498 | input->x, input->len ) ); |
| 3499 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3500 | expected_mac->x, |
| 3501 | expected_mac->len - 1 ), |
| 3502 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3503 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3504 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3505 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3506 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3507 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3508 | input->x, input->len, |
| 3509 | perturbed_mac, expected_mac->len + 1 ), |
| 3510 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3511 | |
| 3512 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3513 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3514 | PSA_ASSERT( psa_mac_update( &operation, |
| 3515 | input->x, input->len ) ); |
| 3516 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3517 | perturbed_mac, |
| 3518 | expected_mac->len + 1 ), |
| 3519 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3520 | |
| 3521 | /* Test changing one byte. */ |
| 3522 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3523 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3524 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3525 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3526 | |
| 3527 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3528 | input->x, input->len, |
| 3529 | perturbed_mac, expected_mac->len ), |
| 3530 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3531 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3532 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3533 | PSA_ASSERT( psa_mac_update( &operation, |
| 3534 | input->x, input->len ) ); |
| 3535 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3536 | perturbed_mac, |
| 3537 | expected_mac->len ), |
| 3538 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3539 | perturbed_mac[i] ^= 1; |
| 3540 | } |
| 3541 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3542 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3543 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3544 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3545 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3546 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3547 | } |
| 3548 | /* END_CASE */ |
| 3549 | |
| 3550 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3551 | void cipher_operation_init( ) |
| 3552 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3553 | const uint8_t input[1] = { 0 }; |
| 3554 | unsigned char output[1] = { 0 }; |
| 3555 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3556 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3557 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3558 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3559 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3560 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3561 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3562 | psa_cipher_operation_t zero; |
| 3563 | |
| 3564 | memset( &zero, 0, sizeof( zero ) ); |
| 3565 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3566 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3567 | TEST_EQUAL( psa_cipher_update( &func, |
| 3568 | input, sizeof( input ), |
| 3569 | output, sizeof( output ), |
| 3570 | &output_length ), |
| 3571 | PSA_ERROR_BAD_STATE ); |
| 3572 | TEST_EQUAL( psa_cipher_update( &init, |
| 3573 | input, sizeof( input ), |
| 3574 | output, sizeof( output ), |
| 3575 | &output_length ), |
| 3576 | PSA_ERROR_BAD_STATE ); |
| 3577 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3578 | input, sizeof( input ), |
| 3579 | output, sizeof( output ), |
| 3580 | &output_length ), |
| 3581 | PSA_ERROR_BAD_STATE ); |
| 3582 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3583 | /* A default cipher operation should be abortable without error. */ |
| 3584 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3585 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3586 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3587 | } |
| 3588 | /* END_CASE */ |
| 3589 | |
| 3590 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3591 | void cipher_setup( int key_type_arg, |
| 3592 | data_t *key, |
| 3593 | int alg_arg, |
| 3594 | int expected_status_arg ) |
| 3595 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3596 | psa_key_type_t key_type = key_type_arg; |
| 3597 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3598 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3599 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3600 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3601 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3602 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3603 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3604 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3605 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3606 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3607 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3608 | &operation, &status ) ) |
| 3609 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3610 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3611 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3612 | /* The operation object should be reusable. */ |
| 3613 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3614 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3615 | smoke_test_key_data, |
| 3616 | sizeof( smoke_test_key_data ), |
| 3617 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3618 | &operation, &status ) ) |
| 3619 | goto exit; |
| 3620 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3621 | #endif |
| 3622 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3623 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3624 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3625 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3626 | } |
| 3627 | /* END_CASE */ |
| 3628 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3629 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3630 | void cipher_bad_order( ) |
| 3631 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3632 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3633 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3634 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3635 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3636 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3637 | unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3638 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3639 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3640 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3641 | const uint8_t text[] = { |
| 3642 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3643 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3644 | uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3645 | size_t length = 0; |
| 3646 | |
| 3647 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3648 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3649 | psa_set_key_algorithm( &attributes, alg ); |
| 3650 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3651 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3652 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3653 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3654 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3655 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3656 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3657 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3658 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3659 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3660 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3661 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3662 | |
| 3663 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3664 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3665 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3666 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3667 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3668 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3669 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3670 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3671 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3672 | /* Generate an IV without calling setup beforehand. */ |
| 3673 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3674 | buffer, sizeof( buffer ), |
| 3675 | &length ), |
| 3676 | PSA_ERROR_BAD_STATE ); |
| 3677 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3678 | |
| 3679 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3680 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3681 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3682 | buffer, sizeof( buffer ), |
| 3683 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3684 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3685 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3686 | buffer, sizeof( buffer ), |
| 3687 | &length ), |
| 3688 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3689 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3690 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3691 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3692 | |
| 3693 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3694 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3695 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3696 | iv, sizeof( iv ) ) ); |
| 3697 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3698 | buffer, sizeof( buffer ), |
| 3699 | &length ), |
| 3700 | PSA_ERROR_BAD_STATE ); |
| 3701 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3702 | |
| 3703 | /* Set an IV without calling setup beforehand. */ |
| 3704 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3705 | iv, sizeof( iv ) ), |
| 3706 | PSA_ERROR_BAD_STATE ); |
| 3707 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3708 | |
| 3709 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3710 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3711 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3712 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3713 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3714 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3715 | iv, sizeof( iv ) ), |
| 3716 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3717 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3718 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3719 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3720 | |
| 3721 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3722 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3723 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3724 | buffer, sizeof( buffer ), |
| 3725 | &length ) ); |
| 3726 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3727 | iv, sizeof( iv ) ), |
| 3728 | PSA_ERROR_BAD_STATE ); |
| 3729 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3730 | |
| 3731 | /* Call update without calling setup beforehand. */ |
| 3732 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3733 | text, sizeof( text ), |
| 3734 | buffer, sizeof( buffer ), |
| 3735 | &length ), |
| 3736 | PSA_ERROR_BAD_STATE ); |
| 3737 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3738 | |
| 3739 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3740 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3741 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3742 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3743 | text, sizeof( text ), |
| 3744 | buffer, sizeof( buffer ), |
| 3745 | &length ), |
| 3746 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3747 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3748 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3749 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3750 | |
| 3751 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3752 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3753 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3754 | iv, sizeof( iv ) ) ); |
| 3755 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3756 | buffer, sizeof( buffer ), &length ) ); |
| 3757 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3758 | text, sizeof( text ), |
| 3759 | buffer, sizeof( buffer ), |
| 3760 | &length ), |
| 3761 | PSA_ERROR_BAD_STATE ); |
| 3762 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3763 | |
| 3764 | /* Call finish without calling setup beforehand. */ |
| 3765 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3766 | buffer, sizeof( buffer ), &length ), |
| 3767 | PSA_ERROR_BAD_STATE ); |
| 3768 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3769 | |
| 3770 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3771 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3772 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3773 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3774 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3775 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3776 | buffer, sizeof( buffer ), &length ), |
| 3777 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3778 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3779 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3780 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3781 | |
| 3782 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3783 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3784 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3785 | iv, sizeof( iv ) ) ); |
| 3786 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3787 | buffer, sizeof( buffer ), &length ) ); |
| 3788 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3789 | buffer, sizeof( buffer ), &length ), |
| 3790 | PSA_ERROR_BAD_STATE ); |
| 3791 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3792 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3793 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3794 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3795 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3796 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3797 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3798 | } |
| 3799 | /* END_CASE */ |
| 3800 | |
| 3801 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3802 | void cipher_encrypt_fail( int alg_arg, |
| 3803 | int key_type_arg, |
| 3804 | data_t *key_data, |
| 3805 | data_t *input, |
| 3806 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3807 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3808 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3809 | psa_status_t status; |
| 3810 | psa_key_type_t key_type = key_type_arg; |
| 3811 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3812 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3813 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3814 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3815 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3816 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3817 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3818 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3819 | size_t function_output_length; |
| 3820 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3821 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3822 | |
| 3823 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3824 | { |
| 3825 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3826 | |
| 3827 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3828 | psa_set_key_algorithm( &attributes, alg ); |
| 3829 | psa_set_key_type( &attributes, key_type ); |
| 3830 | |
| 3831 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3832 | input->len ); |
| 3833 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3834 | |
| 3835 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3836 | &key ) ); |
| 3837 | } |
| 3838 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3839 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3840 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3841 | output_buffer_size, &output_length ); |
| 3842 | |
| 3843 | TEST_EQUAL( status, expected_status ); |
| 3844 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3845 | /* Encrypt, multi-part */ |
| 3846 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3847 | if( status == PSA_SUCCESS ) |
| 3848 | { |
| 3849 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3850 | { |
| 3851 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3852 | iv, iv_size, |
| 3853 | &iv_length ) ); |
| 3854 | } |
| 3855 | |
| 3856 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3857 | output, output_buffer_size, |
| 3858 | &function_output_length ); |
| 3859 | if( status == PSA_SUCCESS ) |
| 3860 | { |
| 3861 | output_length += function_output_length; |
| 3862 | |
| 3863 | status = psa_cipher_finish( &operation, output + output_length, |
| 3864 | output_buffer_size - output_length, |
| 3865 | &function_output_length ); |
| 3866 | |
| 3867 | TEST_EQUAL( status, expected_status ); |
| 3868 | } |
| 3869 | else |
| 3870 | { |
| 3871 | TEST_EQUAL( status, expected_status ); |
| 3872 | } |
| 3873 | } |
| 3874 | else |
| 3875 | { |
| 3876 | TEST_EQUAL( status, expected_status ); |
| 3877 | } |
| 3878 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3879 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3880 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3881 | mbedtls_free( output ); |
| 3882 | psa_destroy_key( key ); |
| 3883 | PSA_DONE( ); |
| 3884 | } |
| 3885 | /* END_CASE */ |
| 3886 | |
| 3887 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3888 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3889 | data_t *input, int iv_length, |
| 3890 | int expected_result ) |
| 3891 | { |
| 3892 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3893 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3894 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3895 | size_t output_buffer_size = 0; |
| 3896 | unsigned char *output = NULL; |
| 3897 | |
| 3898 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3899 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3900 | |
| 3901 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3902 | |
| 3903 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3904 | psa_set_key_algorithm( &attributes, alg ); |
| 3905 | psa_set_key_type( &attributes, key_type ); |
| 3906 | |
| 3907 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3908 | &key ) ); |
| 3909 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3910 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3911 | iv_length ) ); |
| 3912 | |
| 3913 | exit: |
| 3914 | psa_cipher_abort( &operation ); |
| 3915 | mbedtls_free( output ); |
| 3916 | psa_destroy_key( key ); |
| 3917 | PSA_DONE( ); |
| 3918 | } |
| 3919 | /* END_CASE */ |
| 3920 | |
| 3921 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3922 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3923 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3924 | { |
| 3925 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3926 | psa_key_type_t key_type = key_type_arg; |
| 3927 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3928 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3929 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3930 | unsigned char *output = NULL; |
| 3931 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3932 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3933 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3934 | |
| 3935 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3936 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3937 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3938 | TEST_LE_U( ciphertext->len, |
| 3939 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3940 | TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3941 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3942 | TEST_LE_U( plaintext->len, |
| 3943 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3944 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3945 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3946 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3947 | |
| 3948 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3949 | psa_set_key_usage_flags( &attributes, |
| 3950 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3951 | psa_set_key_algorithm( &attributes, alg ); |
| 3952 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3953 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3954 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3955 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3956 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3957 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3958 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3959 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3960 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3961 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3962 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3963 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3964 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3965 | PSA_ERROR_BAD_STATE ); |
| 3966 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3967 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3968 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3969 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3970 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3971 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3972 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3973 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3974 | &length ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3975 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3976 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3977 | /* Multipart encryption */ |
| 3978 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3979 | output_length = 0; |
| 3980 | length = ~0; |
| 3981 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3982 | plaintext->x, plaintext->len, |
| 3983 | output, output_buffer_size, |
| 3984 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3985 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3986 | output_length += length; |
| 3987 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3988 | output + output_length, |
| 3989 | output_buffer_size - output_length, |
| 3990 | &length ) ); |
| 3991 | output_length += length; |
| 3992 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3993 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3994 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3995 | /* Multipart encryption */ |
| 3996 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3997 | output_length = 0; |
| 3998 | length = ~0; |
| 3999 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4000 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4001 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4002 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4003 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4004 | output_length += length; |
| 4005 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4006 | output + output_length, |
| 4007 | output_buffer_size - output_length, |
| 4008 | &length ) ); |
| 4009 | output_length += length; |
| 4010 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 4011 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4012 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4013 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4014 | output_length = ~0; |
| 4015 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 4016 | output, output_buffer_size, |
| 4017 | &output_length ) ); |
| 4018 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 4019 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4020 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4021 | /* One-shot decryption */ |
| 4022 | output_length = ~0; |
| 4023 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 4024 | output, output_buffer_size, |
| 4025 | &output_length ) ); |
| 4026 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4027 | output, output_length ); |
| 4028 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4029 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4030 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4031 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4032 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4033 | psa_destroy_key( key ); |
| 4034 | PSA_DONE( ); |
| 4035 | } |
| 4036 | /* END_CASE */ |
| 4037 | |
| 4038 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4039 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 4040 | { |
| 4041 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4042 | psa_algorithm_t alg = alg_arg; |
| 4043 | psa_key_type_t key_type = key_type_arg; |
| 4044 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4045 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4046 | psa_status_t status; |
| 4047 | |
| 4048 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4049 | |
| 4050 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4051 | psa_set_key_algorithm( &attributes, alg ); |
| 4052 | psa_set_key_type( &attributes, key_type ); |
| 4053 | |
| 4054 | /* Usage of either of these two size macros would cause divide by zero |
| 4055 | * with incorrect key types previously. Input length should be irrelevant |
| 4056 | * here. */ |
| 4057 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 4058 | 0 ); |
| 4059 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 4060 | |
| 4061 | |
| 4062 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4063 | &key ) ); |
| 4064 | |
| 4065 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4066 | * Encrypt or decrypt will end up in the same place. */ |
| 4067 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 4068 | |
| 4069 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 4070 | |
| 4071 | exit: |
| 4072 | psa_cipher_abort( &operation ); |
| 4073 | psa_destroy_key( key ); |
| 4074 | PSA_DONE( ); |
| 4075 | } |
| 4076 | /* END_CASE */ |
| 4077 | |
| 4078 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4079 | void cipher_encrypt_validation( int alg_arg, |
| 4080 | int key_type_arg, |
| 4081 | data_t *key_data, |
| 4082 | data_t *input ) |
| 4083 | { |
| 4084 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4085 | psa_key_type_t key_type = key_type_arg; |
| 4086 | psa_algorithm_t alg = alg_arg; |
| 4087 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 4088 | unsigned char *output1 = NULL; |
| 4089 | size_t output1_buffer_size = 0; |
| 4090 | size_t output1_length = 0; |
| 4091 | unsigned char *output2 = NULL; |
| 4092 | size_t output2_buffer_size = 0; |
| 4093 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4094 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4095 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4096 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4097 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4098 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4099 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4100 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4101 | psa_set_key_algorithm( &attributes, alg ); |
| 4102 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4103 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4104 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 4105 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4106 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4107 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 4108 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 4109 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4110 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4111 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4112 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4113 | /* The one-shot cipher encryption uses generated iv so validating |
| 4114 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4115 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 4116 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4117 | TEST_LE_U( output1_length, |
| 4118 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4119 | TEST_LE_U( output1_length, |
| 4120 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4121 | |
| 4122 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 4123 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4124 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4125 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4126 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4127 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4128 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4129 | TEST_LE_U( function_output_length, |
| 4130 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4131 | TEST_LE_U( function_output_length, |
| 4132 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4133 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4134 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4135 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4136 | output2 + output2_length, |
| 4137 | output2_buffer_size - output2_length, |
| 4138 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4139 | TEST_LE_U( function_output_length, |
| 4140 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4141 | TEST_LE_U( function_output_length, |
| 4142 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4143 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4144 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4145 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4146 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 4147 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4148 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4149 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4150 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4151 | mbedtls_free( output1 ); |
| 4152 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4153 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4154 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4155 | } |
| 4156 | /* END_CASE */ |
| 4157 | |
| 4158 | /* BEGIN_CASE */ |
| 4159 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4160 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4161 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4162 | int first_part_size_arg, |
| 4163 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4164 | data_t *expected_output, |
| 4165 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4166 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4167 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4168 | psa_key_type_t key_type = key_type_arg; |
| 4169 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4170 | psa_status_t status; |
| 4171 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4172 | size_t first_part_size = first_part_size_arg; |
| 4173 | size_t output1_length = output1_length_arg; |
| 4174 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4175 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4176 | size_t output_buffer_size = 0; |
| 4177 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4178 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4179 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4180 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4181 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4182 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4183 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4184 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4185 | psa_set_key_algorithm( &attributes, alg ); |
| 4186 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4187 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4188 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4189 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4190 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4191 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4192 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4193 | if( iv->len > 0 ) |
| 4194 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4195 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4196 | } |
| 4197 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4198 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4199 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4200 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4201 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4202 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4203 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 4204 | output, output_buffer_size, |
| 4205 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4206 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4207 | TEST_LE_U( function_output_length, |
| 4208 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4209 | TEST_LE_U( function_output_length, |
| 4210 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4211 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4212 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4213 | if( first_part_size < input->len ) |
| 4214 | { |
| 4215 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4216 | input->x + first_part_size, |
| 4217 | input->len - first_part_size, |
| 4218 | ( output_buffer_size == 0 ? NULL : |
| 4219 | output + total_output_length ), |
| 4220 | output_buffer_size - total_output_length, |
| 4221 | &function_output_length ) ); |
| 4222 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4223 | TEST_LE_U( function_output_length, |
| 4224 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4225 | alg, |
| 4226 | input->len - first_part_size ) ); |
| 4227 | TEST_LE_U( function_output_length, |
| 4228 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4229 | total_output_length += function_output_length; |
| 4230 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4231 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4232 | status = psa_cipher_finish( &operation, |
| 4233 | ( output_buffer_size == 0 ? NULL : |
| 4234 | output + total_output_length ), |
| 4235 | output_buffer_size - total_output_length, |
| 4236 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4237 | TEST_LE_U( function_output_length, |
| 4238 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4239 | TEST_LE_U( function_output_length, |
| 4240 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4241 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4242 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4243 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4244 | if( expected_status == PSA_SUCCESS ) |
| 4245 | { |
| 4246 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4247 | |
| 4248 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4249 | output, total_output_length ); |
| 4250 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4251 | |
| 4252 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4253 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4254 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4255 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4256 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4257 | } |
| 4258 | /* END_CASE */ |
| 4259 | |
| 4260 | /* BEGIN_CASE */ |
| 4261 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4262 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4263 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4264 | int first_part_size_arg, |
| 4265 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4266 | data_t *expected_output, |
| 4267 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4268 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4269 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4270 | psa_key_type_t key_type = key_type_arg; |
| 4271 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4272 | psa_status_t status; |
| 4273 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4274 | size_t first_part_size = first_part_size_arg; |
| 4275 | size_t output1_length = output1_length_arg; |
| 4276 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4277 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4278 | size_t output_buffer_size = 0; |
| 4279 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4280 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4281 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4282 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4283 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4284 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4285 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4286 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4287 | psa_set_key_algorithm( &attributes, alg ); |
| 4288 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4289 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4290 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4291 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4292 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4293 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4294 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4295 | if( iv->len > 0 ) |
| 4296 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4297 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4298 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4299 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4300 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4301 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4302 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4303 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4304 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4305 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4306 | input->x, first_part_size, |
| 4307 | output, output_buffer_size, |
| 4308 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4309 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4310 | TEST_LE_U( function_output_length, |
| 4311 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4312 | TEST_LE_U( function_output_length, |
| 4313 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4314 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4315 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4316 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4317 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4318 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4319 | input->x + first_part_size, |
| 4320 | input->len - first_part_size, |
| 4321 | ( output_buffer_size == 0 ? NULL : |
| 4322 | output + total_output_length ), |
| 4323 | output_buffer_size - total_output_length, |
| 4324 | &function_output_length ) ); |
| 4325 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4326 | TEST_LE_U( function_output_length, |
| 4327 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4328 | alg, |
| 4329 | input->len - first_part_size ) ); |
| 4330 | TEST_LE_U( function_output_length, |
| 4331 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4332 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4333 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4334 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4335 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4336 | ( output_buffer_size == 0 ? NULL : |
| 4337 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4338 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4339 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4340 | TEST_LE_U( function_output_length, |
| 4341 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4342 | TEST_LE_U( function_output_length, |
| 4343 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4344 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4345 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4346 | |
| 4347 | if( expected_status == PSA_SUCCESS ) |
| 4348 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4349 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4350 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4351 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4352 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4353 | } |
| 4354 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4355 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4356 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4357 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4358 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4359 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4360 | } |
| 4361 | /* END_CASE */ |
| 4362 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4363 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4364 | void cipher_decrypt_fail( int alg_arg, |
| 4365 | int key_type_arg, |
| 4366 | data_t *key_data, |
| 4367 | data_t *iv, |
| 4368 | data_t *input_arg, |
| 4369 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4370 | { |
| 4371 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4372 | psa_status_t status; |
| 4373 | psa_key_type_t key_type = key_type_arg; |
| 4374 | psa_algorithm_t alg = alg_arg; |
| 4375 | psa_status_t expected_status = expected_status_arg; |
| 4376 | unsigned char *input = NULL; |
| 4377 | size_t input_buffer_size = 0; |
| 4378 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4379 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4380 | size_t output_buffer_size = 0; |
| 4381 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4382 | size_t function_output_length; |
| 4383 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4384 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4385 | |
| 4386 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4387 | { |
| 4388 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4389 | |
| 4390 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4391 | psa_set_key_algorithm( &attributes, alg ); |
| 4392 | psa_set_key_type( &attributes, key_type ); |
| 4393 | |
| 4394 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4395 | &key ) ); |
| 4396 | } |
| 4397 | |
| 4398 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4399 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4400 | if ( input_buffer_size > 0 ) |
| 4401 | { |
| 4402 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4403 | memcpy( input, iv->x, iv->len ); |
| 4404 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4405 | } |
| 4406 | |
| 4407 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4408 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4409 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4410 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4411 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4412 | output_buffer_size, &output_length ); |
| 4413 | TEST_EQUAL( status, expected_status ); |
| 4414 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4415 | /* Decrypt, multi-part */ |
| 4416 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4417 | if( status == PSA_SUCCESS ) |
| 4418 | { |
| 4419 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4420 | input_arg->len ) + |
| 4421 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4422 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4423 | |
| 4424 | if( iv->len > 0 ) |
| 4425 | { |
| 4426 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4427 | |
| 4428 | if( status != PSA_SUCCESS ) |
| 4429 | TEST_EQUAL( status, expected_status ); |
| 4430 | } |
| 4431 | |
| 4432 | if( status == PSA_SUCCESS ) |
| 4433 | { |
| 4434 | status = psa_cipher_update( &operation, |
| 4435 | input_arg->x, input_arg->len, |
| 4436 | output_multi, output_buffer_size, |
| 4437 | &function_output_length ); |
| 4438 | if( status == PSA_SUCCESS ) |
| 4439 | { |
| 4440 | output_length = function_output_length; |
| 4441 | |
| 4442 | status = psa_cipher_finish( &operation, |
| 4443 | output_multi + output_length, |
| 4444 | output_buffer_size - output_length, |
| 4445 | &function_output_length ); |
| 4446 | |
| 4447 | TEST_EQUAL( status, expected_status ); |
| 4448 | } |
| 4449 | else |
| 4450 | { |
| 4451 | TEST_EQUAL( status, expected_status ); |
| 4452 | } |
| 4453 | } |
| 4454 | else |
| 4455 | { |
| 4456 | TEST_EQUAL( status, expected_status ); |
| 4457 | } |
| 4458 | } |
| 4459 | else |
| 4460 | { |
| 4461 | TEST_EQUAL( status, expected_status ); |
| 4462 | } |
| 4463 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4464 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4465 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4466 | mbedtls_free( input ); |
| 4467 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4468 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4469 | psa_destroy_key( key ); |
| 4470 | PSA_DONE( ); |
| 4471 | } |
| 4472 | /* END_CASE */ |
| 4473 | |
| 4474 | /* BEGIN_CASE */ |
| 4475 | void cipher_decrypt( int alg_arg, |
| 4476 | int key_type_arg, |
| 4477 | data_t *key_data, |
| 4478 | data_t *iv, |
| 4479 | data_t *input_arg, |
| 4480 | data_t *expected_output ) |
| 4481 | { |
| 4482 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4483 | psa_key_type_t key_type = key_type_arg; |
| 4484 | psa_algorithm_t alg = alg_arg; |
| 4485 | unsigned char *input = NULL; |
| 4486 | size_t input_buffer_size = 0; |
| 4487 | unsigned char *output = NULL; |
| 4488 | size_t output_buffer_size = 0; |
| 4489 | size_t output_length = 0; |
| 4490 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4491 | |
| 4492 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4493 | |
| 4494 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4495 | psa_set_key_algorithm( &attributes, alg ); |
| 4496 | psa_set_key_type( &attributes, key_type ); |
| 4497 | |
| 4498 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4499 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4500 | if ( input_buffer_size > 0 ) |
| 4501 | { |
| 4502 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4503 | memcpy( input, iv->x, iv->len ); |
| 4504 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4505 | } |
| 4506 | |
| 4507 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4508 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4509 | |
| 4510 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4511 | &key ) ); |
| 4512 | |
| 4513 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4514 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4515 | TEST_LE_U( output_length, |
| 4516 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4517 | TEST_LE_U( output_length, |
| 4518 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4519 | |
| 4520 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4521 | output, output_length ); |
| 4522 | exit: |
| 4523 | mbedtls_free( input ); |
| 4524 | mbedtls_free( output ); |
| 4525 | psa_destroy_key( key ); |
| 4526 | PSA_DONE( ); |
| 4527 | } |
| 4528 | /* END_CASE */ |
| 4529 | |
| 4530 | /* BEGIN_CASE */ |
| 4531 | void cipher_verify_output( int alg_arg, |
| 4532 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4533 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4534 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4535 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4536 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4537 | psa_key_type_t key_type = key_type_arg; |
| 4538 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4539 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4540 | size_t output1_size = 0; |
| 4541 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4542 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4543 | size_t output2_size = 0; |
| 4544 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4545 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4546 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4547 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4548 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4549 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4550 | psa_set_key_algorithm( &attributes, alg ); |
| 4551 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4552 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4553 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4554 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4555 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4556 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4557 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4558 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4559 | output1, output1_size, |
| 4560 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4561 | TEST_LE_U( output1_length, |
| 4562 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4563 | TEST_LE_U( output1_length, |
| 4564 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4565 | |
| 4566 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4567 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4568 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4569 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4570 | output2, output2_size, |
| 4571 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4572 | TEST_LE_U( output2_length, |
| 4573 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4574 | TEST_LE_U( output2_length, |
| 4575 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4576 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4577 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4578 | |
| 4579 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4580 | mbedtls_free( output1 ); |
| 4581 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4582 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4583 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4584 | } |
| 4585 | /* END_CASE */ |
| 4586 | |
| 4587 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4588 | void cipher_verify_output_multipart( int alg_arg, |
| 4589 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4590 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4591 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4592 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4593 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4594 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4595 | psa_key_type_t key_type = key_type_arg; |
| 4596 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4597 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4598 | unsigned char iv[16] = {0}; |
| 4599 | size_t iv_size = 16; |
| 4600 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4601 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4602 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4603 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4604 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4605 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4606 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4607 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4608 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4609 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4610 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4611 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4612 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4613 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4614 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4615 | psa_set_key_algorithm( &attributes, alg ); |
| 4616 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4617 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4618 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4619 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4620 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4621 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4622 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4623 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4624 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4625 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4626 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4627 | iv, iv_size, |
| 4628 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4629 | } |
| 4630 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4631 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4632 | TEST_LE_U( output1_buffer_size, |
| 4633 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4634 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4635 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4636 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4637 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4638 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4639 | output1, output1_buffer_size, |
| 4640 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4641 | TEST_LE_U( function_output_length, |
| 4642 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4643 | TEST_LE_U( function_output_length, |
| 4644 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4645 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4646 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4647 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4648 | input->x + first_part_size, |
| 4649 | input->len - first_part_size, |
| 4650 | output1, output1_buffer_size, |
| 4651 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4652 | TEST_LE_U( function_output_length, |
| 4653 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4654 | alg, |
| 4655 | input->len - first_part_size ) ); |
| 4656 | TEST_LE_U( function_output_length, |
| 4657 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4658 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4659 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4660 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4661 | output1 + output1_length, |
| 4662 | output1_buffer_size - output1_length, |
| 4663 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4664 | TEST_LE_U( function_output_length, |
| 4665 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4666 | TEST_LE_U( function_output_length, |
| 4667 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4668 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4669 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4670 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4671 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4672 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4673 | TEST_LE_U( output2_buffer_size, |
| 4674 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4675 | TEST_LE_U( output2_buffer_size, |
| 4676 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4677 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4678 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4679 | if( iv_length > 0 ) |
| 4680 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4681 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4682 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4683 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4684 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4685 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4686 | output2, output2_buffer_size, |
| 4687 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4688 | TEST_LE_U( function_output_length, |
| 4689 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4690 | TEST_LE_U( function_output_length, |
| 4691 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4692 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4693 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4694 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4695 | output1 + first_part_size, |
| 4696 | output1_length - first_part_size, |
| 4697 | output2, output2_buffer_size, |
| 4698 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4699 | TEST_LE_U( function_output_length, |
| 4700 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4701 | alg, |
| 4702 | output1_length - first_part_size ) ); |
| 4703 | TEST_LE_U( function_output_length, |
| 4704 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4705 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4706 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4707 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4708 | output2 + output2_length, |
| 4709 | output2_buffer_size - output2_length, |
| 4710 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4711 | TEST_LE_U( function_output_length, |
| 4712 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4713 | TEST_LE_U( function_output_length, |
| 4714 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4715 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4716 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4717 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4718 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4719 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4720 | |
| 4721 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4722 | psa_cipher_abort( &operation1 ); |
| 4723 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4724 | mbedtls_free( output1 ); |
| 4725 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4726 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4727 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4728 | } |
| 4729 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4730 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4731 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4732 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4733 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4734 | data_t *nonce, |
| 4735 | data_t *additional_data, |
| 4736 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4737 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4738 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4739 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4740 | psa_key_type_t key_type = key_type_arg; |
| 4741 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4742 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4743 | unsigned char *output_data = NULL; |
| 4744 | size_t output_size = 0; |
| 4745 | size_t output_length = 0; |
| 4746 | unsigned char *output_data2 = NULL; |
| 4747 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4748 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4749 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4750 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4751 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4752 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4753 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4754 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4755 | psa_set_key_algorithm( &attributes, alg ); |
| 4756 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4757 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4758 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4759 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4760 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4761 | key_bits = psa_get_key_bits( &attributes ); |
| 4762 | |
| 4763 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4764 | alg ); |
| 4765 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4766 | * should be exact. */ |
| 4767 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4768 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4769 | { |
| 4770 | TEST_EQUAL( output_size, |
| 4771 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4772 | TEST_LE_U( output_size, |
| 4773 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4774 | } |
| 4775 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4776 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4777 | status = psa_aead_encrypt( key, alg, |
| 4778 | nonce->x, nonce->len, |
| 4779 | additional_data->x, |
| 4780 | additional_data->len, |
| 4781 | input_data->x, input_data->len, |
| 4782 | output_data, output_size, |
| 4783 | &output_length ); |
| 4784 | |
| 4785 | /* If the operation is not supported, just skip and not fail in case the |
| 4786 | * encryption involves a common limitation of cryptography hardwares and |
| 4787 | * an alternative implementation. */ |
| 4788 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4789 | { |
| 4790 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4791 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4792 | } |
| 4793 | |
| 4794 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4795 | |
| 4796 | if( PSA_SUCCESS == expected_result ) |
| 4797 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4798 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4799 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4800 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4801 | * should be exact. */ |
| 4802 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4803 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4804 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4805 | TEST_LE_U( input_data->len, |
| 4806 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4807 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4808 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4809 | nonce->x, nonce->len, |
| 4810 | additional_data->x, |
| 4811 | additional_data->len, |
| 4812 | output_data, output_length, |
| 4813 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4814 | &output_length2 ), |
| 4815 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4816 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4817 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4818 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4819 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4820 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4821 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4822 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4823 | mbedtls_free( output_data ); |
| 4824 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4825 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4826 | } |
| 4827 | /* END_CASE */ |
| 4828 | |
| 4829 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4830 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4831 | int alg_arg, |
| 4832 | data_t *nonce, |
| 4833 | data_t *additional_data, |
| 4834 | data_t *input_data, |
| 4835 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4836 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4837 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4838 | psa_key_type_t key_type = key_type_arg; |
| 4839 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4840 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4841 | unsigned char *output_data = NULL; |
| 4842 | size_t output_size = 0; |
| 4843 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4844 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4845 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4846 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4847 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4848 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4849 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4850 | psa_set_key_algorithm( &attributes, alg ); |
| 4851 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4852 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4853 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4854 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4855 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4856 | key_bits = psa_get_key_bits( &attributes ); |
| 4857 | |
| 4858 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4859 | alg ); |
| 4860 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4861 | * should be exact. */ |
| 4862 | TEST_EQUAL( output_size, |
| 4863 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4864 | TEST_LE_U( output_size, |
| 4865 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4866 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4867 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4868 | status = psa_aead_encrypt( key, alg, |
| 4869 | nonce->x, nonce->len, |
| 4870 | additional_data->x, additional_data->len, |
| 4871 | input_data->x, input_data->len, |
| 4872 | output_data, output_size, |
| 4873 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4874 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4875 | /* If the operation is not supported, just skip and not fail in case the |
| 4876 | * encryption involves a common limitation of cryptography hardwares and |
| 4877 | * an alternative implementation. */ |
| 4878 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4879 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4880 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4881 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4882 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4883 | |
| 4884 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4885 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4886 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4887 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4888 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4889 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4890 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4891 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4892 | } |
| 4893 | /* END_CASE */ |
| 4894 | |
| 4895 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4896 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4897 | int alg_arg, |
| 4898 | data_t *nonce, |
| 4899 | data_t *additional_data, |
| 4900 | data_t *input_data, |
| 4901 | data_t *expected_data, |
| 4902 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4903 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4904 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4905 | psa_key_type_t key_type = key_type_arg; |
| 4906 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4907 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4908 | unsigned char *output_data = NULL; |
| 4909 | size_t output_size = 0; |
| 4910 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4911 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4912 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4913 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4914 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4915 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4916 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4917 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4918 | psa_set_key_algorithm( &attributes, alg ); |
| 4919 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4920 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4921 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4922 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4923 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4924 | key_bits = psa_get_key_bits( &attributes ); |
| 4925 | |
| 4926 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4927 | alg ); |
| 4928 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4929 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4930 | { |
| 4931 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4932 | * should be exact. */ |
| 4933 | TEST_EQUAL( output_size, |
| 4934 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4935 | TEST_LE_U( output_size, |
| 4936 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4937 | } |
| 4938 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4939 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4940 | status = psa_aead_decrypt( key, alg, |
| 4941 | nonce->x, nonce->len, |
| 4942 | additional_data->x, |
| 4943 | additional_data->len, |
| 4944 | input_data->x, input_data->len, |
| 4945 | output_data, output_size, |
| 4946 | &output_length ); |
| 4947 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4948 | /* If the operation is not supported, just skip and not fail in case the |
| 4949 | * decryption involves a common limitation of cryptography hardwares and |
| 4950 | * an alternative implementation. */ |
| 4951 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4952 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4953 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4954 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4955 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4956 | |
| 4957 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4958 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4959 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4960 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4961 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4962 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4963 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4964 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4965 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4966 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4967 | } |
| 4968 | /* END_CASE */ |
| 4969 | |
| 4970 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4971 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4972 | int alg_arg, |
| 4973 | data_t *nonce, |
| 4974 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4975 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4976 | int do_set_lengths, |
| 4977 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4978 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4979 | size_t ad_part_len = 0; |
| 4980 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4981 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4982 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4983 | for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4984 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4985 | mbedtls_test_set_step( ad_part_len ); |
| 4986 | |
| 4987 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4988 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4989 | if( ad_part_len & 0x01 ) |
| 4990 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4991 | else |
| 4992 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4993 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4994 | |
| 4995 | /* Split ad into length(ad_part_len) parts. */ |
| 4996 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4997 | alg_arg, nonce, |
| 4998 | additional_data, |
| 4999 | ad_part_len, |
| 5000 | input_data, -1, |
| 5001 | set_lengths_method, |
| 5002 | expected_output, |
| 5003 | 1, 0 ) ) |
| 5004 | break; |
| 5005 | |
| 5006 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5007 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5008 | |
| 5009 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5010 | alg_arg, nonce, |
| 5011 | additional_data, |
| 5012 | ad_part_len, |
| 5013 | input_data, -1, |
| 5014 | set_lengths_method, |
| 5015 | expected_output, |
| 5016 | 1, 1 ) ) |
| 5017 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5018 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5019 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5020 | for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5021 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5022 | /* Split data into length(data_part_len) parts. */ |
| 5023 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5024 | |
| 5025 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5026 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5027 | if( data_part_len & 0x01 ) |
| 5028 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5029 | else |
| 5030 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5031 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5032 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5033 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5034 | alg_arg, nonce, |
| 5035 | additional_data, -1, |
| 5036 | input_data, data_part_len, |
| 5037 | set_lengths_method, |
| 5038 | expected_output, |
| 5039 | 1, 0 ) ) |
| 5040 | break; |
| 5041 | |
| 5042 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5043 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5044 | |
| 5045 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5046 | alg_arg, nonce, |
| 5047 | additional_data, -1, |
| 5048 | input_data, data_part_len, |
| 5049 | set_lengths_method, |
| 5050 | expected_output, |
| 5051 | 1, 1 ) ) |
| 5052 | break; |
| 5053 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5054 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5055 | /* Goto is required to silence warnings about unused labels, as we |
| 5056 | * don't actually do any test assertions in this function. */ |
| 5057 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5058 | } |
| 5059 | /* END_CASE */ |
| 5060 | |
| 5061 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5062 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 5063 | int alg_arg, |
| 5064 | data_t *nonce, |
| 5065 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5066 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 5067 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5068 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5069 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5070 | size_t ad_part_len = 0; |
| 5071 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5072 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5073 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5074 | for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5075 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5076 | /* Split ad into length(ad_part_len) parts. */ |
| 5077 | mbedtls_test_set_step( ad_part_len ); |
| 5078 | |
| 5079 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5080 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5081 | if( ad_part_len & 0x01 ) |
| 5082 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5083 | else |
| 5084 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5085 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5086 | |
| 5087 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5088 | alg_arg, nonce, |
| 5089 | additional_data, |
| 5090 | ad_part_len, |
| 5091 | input_data, -1, |
| 5092 | set_lengths_method, |
| 5093 | expected_output, |
| 5094 | 0, 0 ) ) |
| 5095 | break; |
| 5096 | |
| 5097 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5098 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5099 | |
| 5100 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5101 | alg_arg, nonce, |
| 5102 | additional_data, |
| 5103 | ad_part_len, |
| 5104 | input_data, -1, |
| 5105 | set_lengths_method, |
| 5106 | expected_output, |
| 5107 | 0, 1 ) ) |
| 5108 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5109 | } |
| 5110 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5111 | for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5112 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5113 | /* Split data into length(data_part_len) parts. */ |
| 5114 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5115 | |
| 5116 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5117 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5118 | if( data_part_len & 0x01 ) |
| 5119 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5120 | else |
| 5121 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5122 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5123 | |
| 5124 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5125 | alg_arg, nonce, |
| 5126 | additional_data, -1, |
| 5127 | input_data, data_part_len, |
| 5128 | set_lengths_method, |
| 5129 | expected_output, |
| 5130 | 0, 0 ) ) |
| 5131 | break; |
| 5132 | |
| 5133 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5134 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5135 | |
| 5136 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5137 | alg_arg, nonce, |
| 5138 | additional_data, -1, |
| 5139 | input_data, data_part_len, |
| 5140 | set_lengths_method, |
| 5141 | expected_output, |
| 5142 | 0, 1 ) ) |
| 5143 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5144 | } |
| 5145 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5146 | /* Goto is required to silence warnings about unused labels, as we |
| 5147 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5148 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5149 | } |
| 5150 | /* END_CASE */ |
| 5151 | |
| 5152 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5153 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 5154 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5155 | int nonce_length, |
| 5156 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5157 | data_t *additional_data, |
| 5158 | data_t *input_data, |
| 5159 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5160 | { |
| 5161 | |
| 5162 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5163 | psa_key_type_t key_type = key_type_arg; |
| 5164 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5165 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5166 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5167 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5168 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5169 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5170 | size_t actual_nonce_length = 0; |
| 5171 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5172 | unsigned char *output = NULL; |
| 5173 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5174 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5175 | size_t ciphertext_size = 0; |
| 5176 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5177 | size_t tag_length = 0; |
| 5178 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5179 | |
| 5180 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5181 | |
| 5182 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5183 | psa_set_key_algorithm( & attributes, alg ); |
| 5184 | psa_set_key_type( & attributes, key_type ); |
| 5185 | |
| 5186 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5187 | &key ) ); |
| 5188 | |
| 5189 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5190 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5191 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5192 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5193 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5194 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5195 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5196 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5197 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5198 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5199 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5200 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5201 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5202 | |
| 5203 | /* If the operation is not supported, just skip and not fail in case the |
| 5204 | * encryption involves a common limitation of cryptography hardwares and |
| 5205 | * an alternative implementation. */ |
| 5206 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5207 | { |
| 5208 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5209 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5210 | } |
| 5211 | |
| 5212 | PSA_ASSERT( status ); |
| 5213 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5214 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5215 | nonce_length, |
| 5216 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5217 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5218 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5219 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5220 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5221 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5222 | if( expected_status == PSA_SUCCESS ) |
| 5223 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 5224 | alg ) ); |
| 5225 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5226 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5227 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5228 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5229 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5230 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 5231 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5232 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5233 | |
| 5234 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5235 | additional_data->len ) ); |
| 5236 | |
| 5237 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5238 | output, output_size, |
| 5239 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5240 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5241 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5242 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5243 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5244 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5245 | |
| 5246 | exit: |
| 5247 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5248 | mbedtls_free( output ); |
| 5249 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5250 | psa_aead_abort( &operation ); |
| 5251 | PSA_DONE( ); |
| 5252 | } |
| 5253 | /* END_CASE */ |
| 5254 | |
| 5255 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5256 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 5257 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5258 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5259 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5260 | data_t *additional_data, |
| 5261 | data_t *input_data, |
| 5262 | int expected_status_arg ) |
| 5263 | { |
| 5264 | |
| 5265 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5266 | psa_key_type_t key_type = key_type_arg; |
| 5267 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5268 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5269 | uint8_t *nonce_buffer = NULL; |
| 5270 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5271 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5272 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5273 | unsigned char *output = NULL; |
| 5274 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5275 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5276 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5277 | size_t ciphertext_size = 0; |
| 5278 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5279 | size_t tag_length = 0; |
| 5280 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5281 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5282 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5283 | |
| 5284 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5285 | |
| 5286 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5287 | psa_set_key_algorithm( &attributes, alg ); |
| 5288 | psa_set_key_type( &attributes, key_type ); |
| 5289 | |
| 5290 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5291 | &key ) ); |
| 5292 | |
| 5293 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5294 | |
| 5295 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5296 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5297 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5298 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5299 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5300 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5301 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5302 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5303 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5304 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5305 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5306 | |
| 5307 | /* If the operation is not supported, just skip and not fail in case the |
| 5308 | * encryption involves a common limitation of cryptography hardwares and |
| 5309 | * an alternative implementation. */ |
| 5310 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5311 | { |
| 5312 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5313 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5314 | } |
| 5315 | |
| 5316 | PSA_ASSERT( status ); |
| 5317 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5318 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 5319 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5320 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 5321 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5322 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5323 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5324 | } |
| 5325 | else |
| 5326 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5327 | /* If length is zero, then this will return NULL. */ |
| 5328 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5329 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5330 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5331 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5332 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5333 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5334 | { |
| 5335 | nonce_buffer[index] = 'a' + index; |
| 5336 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5337 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5338 | } |
| 5339 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5340 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5341 | { |
| 5342 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5343 | input_data->len ) ); |
| 5344 | } |
| 5345 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5346 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5347 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5348 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5349 | |
| 5350 | if( expected_status == PSA_SUCCESS ) |
| 5351 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5352 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5353 | { |
| 5354 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5355 | input_data->len ) ); |
| 5356 | } |
| 5357 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5358 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5359 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5360 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5361 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5362 | additional_data->len ), |
| 5363 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5364 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5365 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5366 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5367 | &ciphertext_length ), |
| 5368 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5369 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5370 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5371 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5372 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5373 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | exit: |
| 5377 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5378 | mbedtls_free( output ); |
| 5379 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5380 | mbedtls_free( nonce_buffer ); |
| 5381 | psa_aead_abort( &operation ); |
| 5382 | PSA_DONE( ); |
| 5383 | } |
| 5384 | /* END_CASE */ |
| 5385 | |
| 5386 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5387 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5388 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5389 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5390 | data_t *nonce, |
| 5391 | data_t *additional_data, |
| 5392 | data_t *input_data, |
| 5393 | int expected_status_arg ) |
| 5394 | { |
| 5395 | |
| 5396 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5397 | psa_key_type_t key_type = key_type_arg; |
| 5398 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5399 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5400 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5401 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5402 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5403 | unsigned char *output = NULL; |
| 5404 | unsigned char *ciphertext = NULL; |
| 5405 | size_t output_size = output_size_arg; |
| 5406 | size_t ciphertext_size = 0; |
| 5407 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5408 | size_t tag_length = 0; |
| 5409 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5410 | |
| 5411 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5412 | |
| 5413 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5414 | psa_set_key_algorithm( &attributes, alg ); |
| 5415 | psa_set_key_type( &attributes, key_type ); |
| 5416 | |
| 5417 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5418 | &key ) ); |
| 5419 | |
| 5420 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5421 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5422 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5423 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5424 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5425 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5426 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5427 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5428 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5429 | |
| 5430 | /* If the operation is not supported, just skip and not fail in case the |
| 5431 | * encryption involves a common limitation of cryptography hardwares and |
| 5432 | * an alternative implementation. */ |
| 5433 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5434 | { |
| 5435 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5436 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5437 | } |
| 5438 | |
| 5439 | PSA_ASSERT( status ); |
| 5440 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5441 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5442 | input_data->len ) ); |
| 5443 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5444 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5445 | |
| 5446 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5447 | additional_data->len ) ); |
| 5448 | |
| 5449 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5450 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5451 | |
| 5452 | TEST_EQUAL( status, expected_status ); |
| 5453 | |
| 5454 | if( expected_status == PSA_SUCCESS ) |
| 5455 | { |
| 5456 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5457 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5458 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5459 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5460 | } |
| 5461 | |
| 5462 | exit: |
| 5463 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5464 | mbedtls_free( output ); |
| 5465 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5466 | psa_aead_abort( &operation ); |
| 5467 | PSA_DONE( ); |
| 5468 | } |
| 5469 | /* END_CASE */ |
| 5470 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5471 | /* BEGIN_CASE */ |
| 5472 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5473 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5474 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5475 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5476 | data_t *nonce, |
| 5477 | data_t *additional_data, |
| 5478 | data_t *input_data, |
| 5479 | int expected_status_arg ) |
| 5480 | { |
| 5481 | |
| 5482 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5483 | psa_key_type_t key_type = key_type_arg; |
| 5484 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5485 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5486 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5487 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5488 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5489 | unsigned char *ciphertext = NULL; |
| 5490 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5491 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5492 | size_t ciphertext_size = 0; |
| 5493 | size_t ciphertext_length = 0; |
| 5494 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5495 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5496 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5497 | |
| 5498 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5499 | |
| 5500 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5501 | psa_set_key_algorithm( &attributes, alg ); |
| 5502 | psa_set_key_type( &attributes, key_type ); |
| 5503 | |
| 5504 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5505 | &key ) ); |
| 5506 | |
| 5507 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5508 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5509 | ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5510 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5511 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5512 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5513 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5514 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5515 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5516 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5517 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5518 | |
| 5519 | /* If the operation is not supported, just skip and not fail in case the |
| 5520 | * encryption involves a common limitation of cryptography hardwares and |
| 5521 | * an alternative implementation. */ |
| 5522 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5523 | { |
| 5524 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5525 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5526 | } |
| 5527 | |
| 5528 | PSA_ASSERT( status ); |
| 5529 | |
| 5530 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5531 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5532 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5533 | input_data->len ) ); |
| 5534 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5535 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5536 | additional_data->len ) ); |
| 5537 | |
| 5538 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5539 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5540 | |
| 5541 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5542 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5543 | finish_ciphertext_size, |
| 5544 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5545 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5546 | |
| 5547 | TEST_EQUAL( status, expected_status ); |
| 5548 | |
| 5549 | exit: |
| 5550 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5551 | mbedtls_free( ciphertext ); |
| 5552 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5553 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5554 | psa_aead_abort( &operation ); |
| 5555 | PSA_DONE( ); |
| 5556 | } |
| 5557 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5558 | |
| 5559 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5560 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5561 | int alg_arg, |
| 5562 | data_t *nonce, |
| 5563 | data_t *additional_data, |
| 5564 | data_t *input_data, |
| 5565 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5566 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5567 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5568 | int expected_status_arg ) |
| 5569 | { |
| 5570 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5571 | psa_key_type_t key_type = key_type_arg; |
| 5572 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5573 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5574 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5575 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5576 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5577 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5578 | unsigned char *plaintext = NULL; |
| 5579 | unsigned char *finish_plaintext = NULL; |
| 5580 | size_t plaintext_size = 0; |
| 5581 | size_t plaintext_length = 0; |
| 5582 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5583 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5584 | unsigned char *tag_buffer = NULL; |
| 5585 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5586 | |
| 5587 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5588 | |
| 5589 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5590 | psa_set_key_algorithm( &attributes, alg ); |
| 5591 | psa_set_key_type( &attributes, key_type ); |
| 5592 | |
| 5593 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5594 | &key ) ); |
| 5595 | |
| 5596 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5597 | |
| 5598 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5599 | input_data->len ); |
| 5600 | |
| 5601 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5602 | |
| 5603 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5604 | |
| 5605 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5606 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5607 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5608 | |
| 5609 | /* If the operation is not supported, just skip and not fail in case the |
| 5610 | * encryption involves a common limitation of cryptography hardwares and |
| 5611 | * an alternative implementation. */ |
| 5612 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5613 | { |
| 5614 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5615 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5616 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5617 | TEST_EQUAL( status, expected_setup_status ); |
| 5618 | |
| 5619 | if( status != PSA_SUCCESS ) |
| 5620 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5621 | |
| 5622 | PSA_ASSERT( status ); |
| 5623 | |
| 5624 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5625 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5626 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5627 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5628 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5629 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5630 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5631 | additional_data->len ) ); |
| 5632 | |
| 5633 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5634 | input_data->len, |
| 5635 | plaintext, plaintext_size, |
| 5636 | &plaintext_length ) ); |
| 5637 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5638 | if( tag_usage == USE_GIVEN_TAG ) |
| 5639 | { |
| 5640 | tag_buffer = tag->x; |
| 5641 | tag_size = tag->len; |
| 5642 | } |
| 5643 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5644 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5645 | verify_plaintext_size, |
| 5646 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5647 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5648 | |
| 5649 | TEST_EQUAL( status, expected_status ); |
| 5650 | |
| 5651 | exit: |
| 5652 | psa_destroy_key( key ); |
| 5653 | mbedtls_free( plaintext ); |
| 5654 | mbedtls_free( finish_plaintext ); |
| 5655 | psa_aead_abort( &operation ); |
| 5656 | PSA_DONE( ); |
| 5657 | } |
| 5658 | /* END_CASE */ |
| 5659 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5660 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5661 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5662 | int alg_arg, int expected_status_arg ) |
| 5663 | { |
| 5664 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5665 | psa_key_type_t key_type = key_type_arg; |
| 5666 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5667 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5668 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5669 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5670 | psa_status_t expected_status = expected_status_arg; |
| 5671 | |
| 5672 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5673 | |
| 5674 | psa_set_key_usage_flags( &attributes, |
| 5675 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5676 | psa_set_key_algorithm( &attributes, alg ); |
| 5677 | psa_set_key_type( &attributes, key_type ); |
| 5678 | |
| 5679 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5680 | &key ) ); |
| 5681 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5682 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5683 | |
| 5684 | TEST_EQUAL( status, expected_status ); |
| 5685 | |
| 5686 | psa_aead_abort( &operation ); |
| 5687 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5688 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5689 | |
| 5690 | TEST_EQUAL(status, expected_status ); |
| 5691 | |
| 5692 | exit: |
| 5693 | psa_destroy_key( key ); |
| 5694 | psa_aead_abort( &operation ); |
| 5695 | PSA_DONE( ); |
| 5696 | } |
| 5697 | /* END_CASE */ |
| 5698 | |
| 5699 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5700 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5701 | int alg_arg, |
| 5702 | data_t *nonce, |
| 5703 | data_t *additional_data, |
| 5704 | data_t *input_data ) |
| 5705 | { |
| 5706 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5707 | psa_key_type_t key_type = key_type_arg; |
| 5708 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5709 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5710 | unsigned char *output_data = NULL; |
| 5711 | unsigned char *final_data = NULL; |
| 5712 | size_t output_size = 0; |
| 5713 | size_t finish_output_size = 0; |
| 5714 | size_t output_length = 0; |
| 5715 | size_t key_bits = 0; |
| 5716 | size_t tag_length = 0; |
| 5717 | size_t tag_size = 0; |
| 5718 | size_t nonce_length = 0; |
| 5719 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5720 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5721 | size_t output_part_length = 0; |
| 5722 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5723 | |
| 5724 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5725 | |
| 5726 | psa_set_key_usage_flags( & attributes, |
| 5727 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5728 | psa_set_key_algorithm( & attributes, alg ); |
| 5729 | psa_set_key_type( & attributes, key_type ); |
| 5730 | |
| 5731 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5732 | &key ) ); |
| 5733 | |
| 5734 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5735 | key_bits = psa_get_key_bits( &attributes ); |
| 5736 | |
| 5737 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5738 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5739 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5740 | |
| 5741 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5742 | |
| 5743 | ASSERT_ALLOC( output_data, output_size ); |
| 5744 | |
| 5745 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5746 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5747 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5748 | |
| 5749 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5750 | |
| 5751 | /* Test all operations error without calling setup first. */ |
| 5752 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5753 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5754 | PSA_ERROR_BAD_STATE ); |
| 5755 | |
| 5756 | psa_aead_abort( &operation ); |
| 5757 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5758 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5759 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5760 | &nonce_length ), |
| 5761 | PSA_ERROR_BAD_STATE ); |
| 5762 | |
| 5763 | psa_aead_abort( &operation ); |
| 5764 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5765 | /* ------------------------------------------------------- */ |
| 5766 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5767 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5768 | input_data->len ), |
| 5769 | PSA_ERROR_BAD_STATE ); |
| 5770 | |
| 5771 | psa_aead_abort( &operation ); |
| 5772 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5773 | /* ------------------------------------------------------- */ |
| 5774 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5775 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5776 | additional_data->len ), |
| 5777 | PSA_ERROR_BAD_STATE ); |
| 5778 | |
| 5779 | psa_aead_abort( &operation ); |
| 5780 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5781 | /* ------------------------------------------------------- */ |
| 5782 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5783 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5784 | input_data->len, output_data, |
| 5785 | output_size, &output_length ), |
| 5786 | PSA_ERROR_BAD_STATE ); |
| 5787 | |
| 5788 | psa_aead_abort( &operation ); |
| 5789 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5790 | /* ------------------------------------------------------- */ |
| 5791 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5792 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5793 | finish_output_size, |
| 5794 | &output_part_length, |
| 5795 | tag_buffer, tag_length, |
| 5796 | &tag_size ), |
| 5797 | PSA_ERROR_BAD_STATE ); |
| 5798 | |
| 5799 | psa_aead_abort( &operation ); |
| 5800 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5801 | /* ------------------------------------------------------- */ |
| 5802 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5803 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5804 | finish_output_size, |
| 5805 | &output_part_length, |
| 5806 | tag_buffer, |
| 5807 | tag_length ), |
| 5808 | PSA_ERROR_BAD_STATE ); |
| 5809 | |
| 5810 | psa_aead_abort( &operation ); |
| 5811 | |
| 5812 | /* Test for double setups. */ |
| 5813 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5814 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5815 | |
| 5816 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5817 | PSA_ERROR_BAD_STATE ); |
| 5818 | |
| 5819 | psa_aead_abort( &operation ); |
| 5820 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5821 | /* ------------------------------------------------------- */ |
| 5822 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5823 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5824 | |
| 5825 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5826 | PSA_ERROR_BAD_STATE ); |
| 5827 | |
| 5828 | psa_aead_abort( &operation ); |
| 5829 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5830 | /* ------------------------------------------------------- */ |
| 5831 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5832 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5833 | |
| 5834 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5835 | PSA_ERROR_BAD_STATE ); |
| 5836 | |
| 5837 | psa_aead_abort( &operation ); |
| 5838 | |
| 5839 | /* ------------------------------------------------------- */ |
| 5840 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5841 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5842 | |
| 5843 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5844 | PSA_ERROR_BAD_STATE ); |
| 5845 | |
| 5846 | psa_aead_abort( &operation ); |
| 5847 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5848 | /* Test for not setting a nonce. */ |
| 5849 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5850 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5851 | |
| 5852 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5853 | additional_data->len ), |
| 5854 | PSA_ERROR_BAD_STATE ); |
| 5855 | |
| 5856 | psa_aead_abort( &operation ); |
| 5857 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5858 | /* ------------------------------------------------------- */ |
| 5859 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5860 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5861 | |
| 5862 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5863 | input_data->len, output_data, |
| 5864 | output_size, &output_length ), |
| 5865 | PSA_ERROR_BAD_STATE ); |
| 5866 | |
| 5867 | psa_aead_abort( &operation ); |
| 5868 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5869 | /* ------------------------------------------------------- */ |
| 5870 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5871 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5872 | |
| 5873 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5874 | finish_output_size, |
| 5875 | &output_part_length, |
| 5876 | tag_buffer, tag_length, |
| 5877 | &tag_size ), |
| 5878 | PSA_ERROR_BAD_STATE ); |
| 5879 | |
| 5880 | psa_aead_abort( &operation ); |
| 5881 | |
| 5882 | /* ------------------------------------------------------- */ |
| 5883 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5884 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5885 | |
| 5886 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5887 | finish_output_size, |
| 5888 | &output_part_length, |
| 5889 | tag_buffer, |
| 5890 | tag_length ), |
| 5891 | PSA_ERROR_BAD_STATE ); |
| 5892 | |
| 5893 | psa_aead_abort( &operation ); |
| 5894 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5895 | /* Test for double setting nonce. */ |
| 5896 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5897 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5898 | |
| 5899 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5900 | |
| 5901 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5902 | PSA_ERROR_BAD_STATE ); |
| 5903 | |
| 5904 | psa_aead_abort( &operation ); |
| 5905 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5906 | /* Test for double generating nonce. */ |
| 5907 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5908 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5909 | |
| 5910 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5911 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5912 | &nonce_length ) ); |
| 5913 | |
| 5914 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5915 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5916 | &nonce_length ), |
| 5917 | PSA_ERROR_BAD_STATE ); |
| 5918 | |
| 5919 | |
| 5920 | psa_aead_abort( &operation ); |
| 5921 | |
| 5922 | /* Test for generate nonce then set and vice versa */ |
| 5923 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5924 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5925 | |
| 5926 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5927 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5928 | &nonce_length ) ); |
| 5929 | |
| 5930 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5931 | PSA_ERROR_BAD_STATE ); |
| 5932 | |
| 5933 | psa_aead_abort( &operation ); |
| 5934 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5935 | /* Test for generating nonce after calling set lengths */ |
| 5936 | |
| 5937 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5938 | |
| 5939 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5940 | input_data->len ) ); |
| 5941 | |
| 5942 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5943 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5944 | &nonce_length ) ); |
| 5945 | |
| 5946 | psa_aead_abort( &operation ); |
| 5947 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5948 | /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */ |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5949 | |
| 5950 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5951 | |
| 5952 | if( operation.alg == PSA_ALG_CCM ) |
| 5953 | { |
| 5954 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5955 | input_data->len ), |
| 5956 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5957 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5958 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5959 | &nonce_length ), |
| 5960 | PSA_ERROR_BAD_STATE ); |
| 5961 | } |
| 5962 | else |
| 5963 | { |
| 5964 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5965 | input_data->len ) ); |
| 5966 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5967 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5968 | &nonce_length ) ); |
| 5969 | } |
| 5970 | |
| 5971 | psa_aead_abort( &operation ); |
| 5972 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5973 | /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */ |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5974 | #if SIZE_MAX > UINT32_MAX |
| 5975 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5976 | |
| 5977 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5978 | { |
| 5979 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5980 | input_data->len ), |
| 5981 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5982 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5983 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5984 | &nonce_length ), |
| 5985 | PSA_ERROR_BAD_STATE ); |
| 5986 | } |
| 5987 | else |
| 5988 | { |
| 5989 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5990 | input_data->len ) ); |
| 5991 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5992 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5993 | &nonce_length ) ); |
| 5994 | } |
| 5995 | |
| 5996 | psa_aead_abort( &operation ); |
| 5997 | #endif |
| 5998 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5999 | /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */ |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6000 | |
| 6001 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6002 | |
| 6003 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6004 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6005 | &nonce_length ) ); |
| 6006 | |
| 6007 | if( operation.alg == PSA_ALG_CCM ) |
| 6008 | { |
| 6009 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6010 | input_data->len ), |
| 6011 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6012 | } |
| 6013 | else |
| 6014 | { |
| 6015 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6016 | input_data->len ) ); |
| 6017 | } |
| 6018 | |
| 6019 | psa_aead_abort( &operation ); |
| 6020 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6021 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6022 | /* Test for setting nonce after calling set lengths */ |
| 6023 | |
| 6024 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6025 | |
| 6026 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6027 | input_data->len ) ); |
| 6028 | |
| 6029 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6030 | |
| 6031 | psa_aead_abort( &operation ); |
| 6032 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6033 | /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6034 | |
| 6035 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6036 | |
| 6037 | if( operation.alg == PSA_ALG_CCM ) |
| 6038 | { |
| 6039 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6040 | input_data->len ), |
| 6041 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6042 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6043 | PSA_ERROR_BAD_STATE ); |
| 6044 | } |
| 6045 | else |
| 6046 | { |
| 6047 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6048 | input_data->len ) ); |
| 6049 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6050 | } |
| 6051 | |
| 6052 | psa_aead_abort( &operation ); |
| 6053 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6054 | /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6055 | #if SIZE_MAX > UINT32_MAX |
| 6056 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6057 | |
| 6058 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 6059 | { |
| 6060 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6061 | input_data->len ), |
| 6062 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6063 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6064 | PSA_ERROR_BAD_STATE ); |
| 6065 | } |
| 6066 | else |
| 6067 | { |
| 6068 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6069 | input_data->len ) ); |
| 6070 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6071 | } |
| 6072 | |
| 6073 | psa_aead_abort( &operation ); |
| 6074 | #endif |
| 6075 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6076 | /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6077 | |
| 6078 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6079 | |
| 6080 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6081 | |
| 6082 | if( operation.alg == PSA_ALG_CCM ) |
| 6083 | { |
| 6084 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6085 | input_data->len ), |
| 6086 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6087 | } |
| 6088 | else |
| 6089 | { |
| 6090 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6091 | input_data->len ) ); |
| 6092 | } |
| 6093 | |
| 6094 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6095 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6096 | /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */ |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6097 | #if SIZE_MAX > UINT32_MAX |
| 6098 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6099 | |
| 6100 | if( operation.alg == PSA_ALG_GCM ) |
| 6101 | { |
| 6102 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6103 | SIZE_MAX ), |
| 6104 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6105 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6106 | PSA_ERROR_BAD_STATE ); |
| 6107 | } |
| 6108 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6109 | { |
| 6110 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6111 | SIZE_MAX ) ); |
| 6112 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6113 | } |
| 6114 | |
| 6115 | psa_aead_abort( &operation ); |
| 6116 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6117 | /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */ |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6118 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6119 | |
| 6120 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6121 | |
| 6122 | if( operation.alg == PSA_ALG_GCM ) |
| 6123 | { |
| 6124 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6125 | SIZE_MAX ), |
| 6126 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6127 | } |
| 6128 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6129 | { |
| 6130 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6131 | SIZE_MAX ) ); |
| 6132 | } |
| 6133 | |
| 6134 | psa_aead_abort( &operation ); |
| 6135 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6136 | |
| 6137 | /* ------------------------------------------------------- */ |
| 6138 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6139 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6140 | |
| 6141 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6142 | |
| 6143 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6144 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6145 | &nonce_length ), |
| 6146 | PSA_ERROR_BAD_STATE ); |
| 6147 | |
| 6148 | psa_aead_abort( &operation ); |
| 6149 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6150 | /* Test for generating nonce in decrypt setup. */ |
| 6151 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6152 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6153 | |
| 6154 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6155 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6156 | &nonce_length ), |
| 6157 | PSA_ERROR_BAD_STATE ); |
| 6158 | |
| 6159 | psa_aead_abort( &operation ); |
| 6160 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6161 | /* Test for setting lengths twice. */ |
| 6162 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6163 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6164 | |
| 6165 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6166 | |
| 6167 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6168 | input_data->len ) ); |
| 6169 | |
| 6170 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6171 | input_data->len ), |
| 6172 | PSA_ERROR_BAD_STATE ); |
| 6173 | |
| 6174 | psa_aead_abort( &operation ); |
| 6175 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6176 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6177 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6178 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6179 | |
| 6180 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6181 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6182 | if( operation.alg == PSA_ALG_CCM ) |
| 6183 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6184 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6185 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6186 | additional_data->len ), |
| 6187 | PSA_ERROR_BAD_STATE ); |
| 6188 | } |
| 6189 | else |
| 6190 | { |
| 6191 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6192 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6193 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6194 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6195 | input_data->len ), |
| 6196 | PSA_ERROR_BAD_STATE ); |
| 6197 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6198 | psa_aead_abort( &operation ); |
| 6199 | |
| 6200 | /* ------------------------------------------------------- */ |
| 6201 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6202 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6203 | |
| 6204 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6205 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6206 | if( operation.alg == PSA_ALG_CCM ) |
| 6207 | { |
| 6208 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6209 | input_data->len, output_data, |
| 6210 | output_size, &output_length ), |
| 6211 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6212 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6213 | } |
| 6214 | else |
| 6215 | { |
| 6216 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6217 | input_data->len, output_data, |
| 6218 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6219 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6220 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6221 | input_data->len ), |
| 6222 | PSA_ERROR_BAD_STATE ); |
| 6223 | } |
| 6224 | psa_aead_abort( &operation ); |
| 6225 | |
| 6226 | /* ------------------------------------------------------- */ |
| 6227 | |
| 6228 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6229 | |
| 6230 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6231 | |
| 6232 | if( operation.alg == PSA_ALG_CCM ) |
| 6233 | { |
| 6234 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6235 | finish_output_size, |
| 6236 | &output_part_length, |
| 6237 | tag_buffer, tag_length, |
| 6238 | &tag_size ) ); |
| 6239 | } |
| 6240 | else |
| 6241 | { |
| 6242 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6243 | finish_output_size, |
| 6244 | &output_part_length, |
| 6245 | tag_buffer, tag_length, |
| 6246 | &tag_size ) ); |
| 6247 | |
| 6248 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6249 | input_data->len ), |
| 6250 | PSA_ERROR_BAD_STATE ); |
| 6251 | } |
| 6252 | psa_aead_abort( &operation ); |
| 6253 | |
| 6254 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6255 | |
| 6256 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6257 | |
| 6258 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6259 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6260 | &nonce_length ) ); |
| 6261 | if( operation.alg == PSA_ALG_CCM ) |
| 6262 | { |
| 6263 | |
| 6264 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6265 | additional_data->len ), |
| 6266 | PSA_ERROR_BAD_STATE ); |
| 6267 | } |
| 6268 | else |
| 6269 | { |
| 6270 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6271 | additional_data->len ) ); |
| 6272 | |
| 6273 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6274 | input_data->len ), |
| 6275 | PSA_ERROR_BAD_STATE ); |
| 6276 | } |
| 6277 | psa_aead_abort( &operation ); |
| 6278 | |
| 6279 | /* ------------------------------------------------------- */ |
| 6280 | |
| 6281 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6282 | |
| 6283 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6284 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6285 | &nonce_length ) ); |
| 6286 | if( operation.alg == PSA_ALG_CCM ) |
| 6287 | { |
| 6288 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6289 | input_data->len, output_data, |
| 6290 | output_size, &output_length ), |
| 6291 | PSA_ERROR_BAD_STATE ); |
| 6292 | |
| 6293 | } |
| 6294 | else |
| 6295 | { |
| 6296 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6297 | input_data->len, output_data, |
| 6298 | output_size, &output_length ) ); |
| 6299 | |
| 6300 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6301 | input_data->len ), |
| 6302 | PSA_ERROR_BAD_STATE ); |
| 6303 | } |
| 6304 | psa_aead_abort( &operation ); |
| 6305 | |
| 6306 | /* ------------------------------------------------------- */ |
| 6307 | |
| 6308 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6309 | |
| 6310 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6311 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6312 | &nonce_length ) ); |
| 6313 | if( operation.alg == PSA_ALG_CCM ) |
| 6314 | { |
| 6315 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6316 | finish_output_size, |
| 6317 | &output_part_length, |
| 6318 | tag_buffer, tag_length, |
| 6319 | &tag_size ) ); |
| 6320 | } |
| 6321 | else |
| 6322 | { |
| 6323 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6324 | finish_output_size, |
| 6325 | &output_part_length, |
| 6326 | tag_buffer, tag_length, |
| 6327 | &tag_size ) ); |
| 6328 | |
| 6329 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6330 | input_data->len ), |
| 6331 | PSA_ERROR_BAD_STATE ); |
| 6332 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6333 | psa_aead_abort( &operation ); |
| 6334 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6335 | /* Test for not sending any additional data or data after setting non zero |
| 6336 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6337 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6338 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6339 | |
| 6340 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6341 | |
| 6342 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6343 | input_data->len ) ); |
| 6344 | |
| 6345 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6346 | finish_output_size, |
| 6347 | &output_part_length, |
| 6348 | tag_buffer, tag_length, |
| 6349 | &tag_size ), |
| 6350 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6351 | |
| 6352 | psa_aead_abort( &operation ); |
| 6353 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6354 | /* Test for not sending any additional data or data after setting non-zero |
| 6355 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6356 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6357 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6358 | |
| 6359 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6360 | |
| 6361 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6362 | input_data->len ) ); |
| 6363 | |
| 6364 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6365 | finish_output_size, |
| 6366 | &output_part_length, |
| 6367 | tag_buffer, |
| 6368 | tag_length ), |
| 6369 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6370 | |
| 6371 | psa_aead_abort( &operation ); |
| 6372 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6373 | /* Test for not sending any additional data after setting a non-zero length |
| 6374 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6375 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6376 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6377 | |
| 6378 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6379 | |
| 6380 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6381 | input_data->len ) ); |
| 6382 | |
| 6383 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6384 | input_data->len, output_data, |
| 6385 | output_size, &output_length ), |
| 6386 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6387 | |
| 6388 | psa_aead_abort( &operation ); |
| 6389 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6390 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6391 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6392 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6393 | |
| 6394 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6395 | |
| 6396 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6397 | input_data->len ) ); |
| 6398 | |
| 6399 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6400 | additional_data->len ) ); |
| 6401 | |
| 6402 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6403 | finish_output_size, |
| 6404 | &output_part_length, |
| 6405 | tag_buffer, tag_length, |
| 6406 | &tag_size ), |
| 6407 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6408 | |
| 6409 | psa_aead_abort( &operation ); |
| 6410 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6411 | /* Test for sending too much additional data after setting lengths. */ |
| 6412 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6413 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6414 | |
| 6415 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6416 | |
| 6417 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6418 | |
| 6419 | |
| 6420 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6421 | additional_data->len ), |
| 6422 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6423 | |
| 6424 | psa_aead_abort( &operation ); |
| 6425 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6426 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6427 | |
| 6428 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6429 | |
| 6430 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6431 | |
| 6432 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6433 | input_data->len ) ); |
| 6434 | |
| 6435 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6436 | additional_data->len ) ); |
| 6437 | |
| 6438 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6439 | 1 ), |
| 6440 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6441 | |
| 6442 | psa_aead_abort( &operation ); |
| 6443 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6444 | /* Test for sending too much data after setting lengths. */ |
| 6445 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6446 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6447 | |
| 6448 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6449 | |
| 6450 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6451 | |
| 6452 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6453 | input_data->len, output_data, |
| 6454 | output_size, &output_length ), |
| 6455 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6456 | |
| 6457 | psa_aead_abort( &operation ); |
| 6458 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6459 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6460 | |
| 6461 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6462 | |
| 6463 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6464 | |
| 6465 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6466 | input_data->len ) ); |
| 6467 | |
| 6468 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6469 | additional_data->len ) ); |
| 6470 | |
| 6471 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6472 | input_data->len, output_data, |
| 6473 | output_size, &output_length ) ); |
| 6474 | |
| 6475 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6476 | 1, output_data, |
| 6477 | output_size, &output_length ), |
| 6478 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6479 | |
| 6480 | psa_aead_abort( &operation ); |
| 6481 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6482 | /* Test sending additional data after data. */ |
| 6483 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6484 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6485 | |
| 6486 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6487 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6488 | if( operation.alg != PSA_ALG_CCM ) |
| 6489 | { |
| 6490 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6491 | input_data->len, output_data, |
| 6492 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6493 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6494 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6495 | additional_data->len ), |
| 6496 | PSA_ERROR_BAD_STATE ); |
| 6497 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6498 | psa_aead_abort( &operation ); |
| 6499 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6500 | /* Test calling finish on decryption. */ |
| 6501 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6502 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6503 | |
| 6504 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6505 | |
| 6506 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6507 | finish_output_size, |
| 6508 | &output_part_length, |
| 6509 | tag_buffer, tag_length, |
| 6510 | &tag_size ), |
| 6511 | PSA_ERROR_BAD_STATE ); |
| 6512 | |
| 6513 | psa_aead_abort( &operation ); |
| 6514 | |
| 6515 | /* Test calling verify on encryption. */ |
| 6516 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6517 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6518 | |
| 6519 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6520 | |
| 6521 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6522 | finish_output_size, |
| 6523 | &output_part_length, |
| 6524 | tag_buffer, |
| 6525 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6526 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6527 | |
| 6528 | psa_aead_abort( &operation ); |
| 6529 | |
| 6530 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6531 | exit: |
| 6532 | psa_destroy_key( key ); |
| 6533 | psa_aead_abort( &operation ); |
| 6534 | mbedtls_free( output_data ); |
| 6535 | mbedtls_free( final_data ); |
| 6536 | PSA_DONE( ); |
| 6537 | } |
| 6538 | /* END_CASE */ |
| 6539 | |
| 6540 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6541 | void signature_size( int type_arg, |
| 6542 | int bits, |
| 6543 | int alg_arg, |
| 6544 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6545 | { |
| 6546 | psa_key_type_t type = type_arg; |
| 6547 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6548 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6549 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6550 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6551 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6552 | exit: |
| 6553 | ; |
| 6554 | } |
| 6555 | /* END_CASE */ |
| 6556 | |
| 6557 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6558 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6559 | int alg_arg, data_t *input_data, |
| 6560 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6561 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6562 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6563 | psa_key_type_t key_type = key_type_arg; |
| 6564 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6565 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6566 | unsigned char *signature = NULL; |
| 6567 | size_t signature_size; |
| 6568 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6569 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6570 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6571 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6572 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6573 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6574 | psa_set_key_algorithm( &attributes, alg ); |
| 6575 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6576 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6577 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6578 | &key ) ); |
| 6579 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6580 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6581 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6582 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6583 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6584 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6585 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6586 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6587 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6588 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6589 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6590 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6591 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6592 | input_data->x, input_data->len, |
| 6593 | signature, signature_size, |
| 6594 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6595 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6596 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6597 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6598 | |
| 6599 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6600 | /* |
| 6601 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6602 | * thus reset them as required. |
| 6603 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6604 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6605 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6606 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6607 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6608 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6609 | } |
| 6610 | /* END_CASE */ |
| 6611 | |
| 6612 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6613 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6614 | int alg_arg, data_t *input_data, |
| 6615 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6616 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6617 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6618 | psa_key_type_t key_type = key_type_arg; |
| 6619 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6620 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6621 | psa_status_t actual_status; |
| 6622 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6623 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6624 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6625 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6626 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6627 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6628 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6629 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6630 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6631 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6632 | psa_set_key_algorithm( &attributes, alg ); |
| 6633 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6634 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6635 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6636 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6637 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6638 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6639 | input_data->x, input_data->len, |
| 6640 | signature, signature_size, |
| 6641 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6642 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6643 | /* The value of *signature_length is unspecified on error, but |
| 6644 | * whatever it is, it should be less than signature_size, so that |
| 6645 | * if the caller tries to read *signature_length bytes without |
| 6646 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6647 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6648 | |
| 6649 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6650 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6651 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6652 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6653 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6654 | } |
| 6655 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6656 | |
| 6657 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6658 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6659 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6660 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6661 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6662 | psa_key_type_t key_type = key_type_arg; |
| 6663 | psa_algorithm_t alg = alg_arg; |
| 6664 | size_t key_bits; |
| 6665 | unsigned char *signature = NULL; |
| 6666 | size_t signature_size; |
| 6667 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6668 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6669 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6670 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6671 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6672 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6673 | psa_set_key_algorithm( &attributes, alg ); |
| 6674 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6675 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6676 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6677 | &key ) ); |
| 6678 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6679 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6680 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6681 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6682 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6683 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6684 | key_bits, alg ); |
| 6685 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6686 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6687 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6688 | |
| 6689 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6690 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6691 | input_data->x, input_data->len, |
| 6692 | signature, signature_size, |
| 6693 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6694 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6695 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6696 | TEST_ASSERT( signature_length > 0 ); |
| 6697 | |
| 6698 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6699 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6700 | input_data->x, input_data->len, |
| 6701 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6702 | |
| 6703 | if( input_data->len != 0 ) |
| 6704 | { |
| 6705 | /* Flip a bit in the input and verify that the signature is now |
| 6706 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6707 | * because ECDSA may ignore the last few bits of the input. */ |
| 6708 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6709 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6710 | input_data->x, input_data->len, |
| 6711 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6712 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6713 | } |
| 6714 | |
| 6715 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6716 | /* |
| 6717 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6718 | * thus reset them as required. |
| 6719 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6720 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6721 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6722 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6723 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6724 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6725 | } |
| 6726 | /* END_CASE */ |
| 6727 | |
| 6728 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6729 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6730 | int alg_arg, data_t *hash_data, |
| 6731 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6732 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6733 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6734 | psa_key_type_t key_type = key_type_arg; |
| 6735 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6736 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6737 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6738 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6739 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6740 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6741 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6742 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6743 | psa_set_key_algorithm( &attributes, alg ); |
| 6744 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6745 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6746 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6747 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6748 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6749 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6750 | hash_data->x, hash_data->len, |
| 6751 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6752 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6753 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6754 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6755 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6756 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6757 | } |
| 6758 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6759 | |
| 6760 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6761 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6762 | int alg_arg, data_t *hash_data, |
| 6763 | data_t *signature_data, |
| 6764 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6765 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6766 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6767 | psa_key_type_t key_type = key_type_arg; |
| 6768 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6769 | psa_status_t actual_status; |
| 6770 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6771 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6772 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6773 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6774 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6775 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6776 | psa_set_key_algorithm( &attributes, alg ); |
| 6777 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6778 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6779 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6780 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6781 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6782 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6783 | hash_data->x, hash_data->len, |
| 6784 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6785 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6786 | |
| 6787 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6788 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6789 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6790 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6791 | } |
| 6792 | /* END_CASE */ |
| 6793 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6794 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6795 | void sign_message_deterministic( int key_type_arg, |
| 6796 | data_t *key_data, |
| 6797 | int alg_arg, |
| 6798 | data_t *input_data, |
| 6799 | data_t *output_data ) |
| 6800 | { |
| 6801 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6802 | psa_key_type_t key_type = key_type_arg; |
| 6803 | psa_algorithm_t alg = alg_arg; |
| 6804 | size_t key_bits; |
| 6805 | unsigned char *signature = NULL; |
| 6806 | size_t signature_size; |
| 6807 | size_t signature_length = 0xdeadbeef; |
| 6808 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6809 | |
| 6810 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6811 | |
| 6812 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6813 | psa_set_key_algorithm( &attributes, alg ); |
| 6814 | psa_set_key_type( &attributes, key_type ); |
| 6815 | |
| 6816 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6817 | &key ) ); |
| 6818 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6819 | key_bits = psa_get_key_bits( &attributes ); |
| 6820 | |
| 6821 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6822 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6823 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6824 | ASSERT_ALLOC( signature, signature_size ); |
| 6825 | |
| 6826 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6827 | input_data->x, input_data->len, |
| 6828 | signature, signature_size, |
| 6829 | &signature_length ) ); |
| 6830 | |
| 6831 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6832 | signature, signature_length ); |
| 6833 | |
| 6834 | exit: |
| 6835 | psa_reset_key_attributes( &attributes ); |
| 6836 | |
| 6837 | psa_destroy_key( key ); |
| 6838 | mbedtls_free( signature ); |
| 6839 | PSA_DONE( ); |
| 6840 | |
| 6841 | } |
| 6842 | /* END_CASE */ |
| 6843 | |
| 6844 | /* BEGIN_CASE */ |
| 6845 | void sign_message_fail( int key_type_arg, |
| 6846 | data_t *key_data, |
| 6847 | int alg_arg, |
| 6848 | data_t *input_data, |
| 6849 | int signature_size_arg, |
| 6850 | int expected_status_arg ) |
| 6851 | { |
| 6852 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6853 | psa_key_type_t key_type = key_type_arg; |
| 6854 | psa_algorithm_t alg = alg_arg; |
| 6855 | size_t signature_size = signature_size_arg; |
| 6856 | psa_status_t actual_status; |
| 6857 | psa_status_t expected_status = expected_status_arg; |
| 6858 | unsigned char *signature = NULL; |
| 6859 | size_t signature_length = 0xdeadbeef; |
| 6860 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6861 | |
| 6862 | ASSERT_ALLOC( signature, signature_size ); |
| 6863 | |
| 6864 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6865 | |
| 6866 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6867 | psa_set_key_algorithm( &attributes, alg ); |
| 6868 | psa_set_key_type( &attributes, key_type ); |
| 6869 | |
| 6870 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6871 | &key ) ); |
| 6872 | |
| 6873 | actual_status = psa_sign_message( key, alg, |
| 6874 | input_data->x, input_data->len, |
| 6875 | signature, signature_size, |
| 6876 | &signature_length ); |
| 6877 | TEST_EQUAL( actual_status, expected_status ); |
| 6878 | /* The value of *signature_length is unspecified on error, but |
| 6879 | * whatever it is, it should be less than signature_size, so that |
| 6880 | * if the caller tries to read *signature_length bytes without |
| 6881 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6882 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6883 | |
| 6884 | exit: |
| 6885 | psa_reset_key_attributes( &attributes ); |
| 6886 | psa_destroy_key( key ); |
| 6887 | mbedtls_free( signature ); |
| 6888 | PSA_DONE( ); |
| 6889 | } |
| 6890 | /* END_CASE */ |
| 6891 | |
| 6892 | /* BEGIN_CASE */ |
| 6893 | void sign_verify_message( int key_type_arg, |
| 6894 | data_t *key_data, |
| 6895 | int alg_arg, |
| 6896 | data_t *input_data ) |
| 6897 | { |
| 6898 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6899 | psa_key_type_t key_type = key_type_arg; |
| 6900 | psa_algorithm_t alg = alg_arg; |
| 6901 | size_t key_bits; |
| 6902 | unsigned char *signature = NULL; |
| 6903 | size_t signature_size; |
| 6904 | size_t signature_length = 0xdeadbeef; |
| 6905 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6906 | |
| 6907 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6908 | |
| 6909 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6910 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6911 | psa_set_key_algorithm( &attributes, alg ); |
| 6912 | psa_set_key_type( &attributes, key_type ); |
| 6913 | |
| 6914 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6915 | &key ) ); |
| 6916 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6917 | key_bits = psa_get_key_bits( &attributes ); |
| 6918 | |
| 6919 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6920 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6921 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6922 | ASSERT_ALLOC( signature, signature_size ); |
| 6923 | |
| 6924 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6925 | input_data->x, input_data->len, |
| 6926 | signature, signature_size, |
| 6927 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6928 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6929 | TEST_ASSERT( signature_length > 0 ); |
| 6930 | |
| 6931 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6932 | input_data->x, input_data->len, |
| 6933 | signature, signature_length ) ); |
| 6934 | |
| 6935 | if( input_data->len != 0 ) |
| 6936 | { |
| 6937 | /* Flip a bit in the input and verify that the signature is now |
| 6938 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6939 | * because ECDSA may ignore the last few bits of the input. */ |
| 6940 | input_data->x[0] ^= 1; |
| 6941 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6942 | input_data->x, input_data->len, |
| 6943 | signature, signature_length ), |
| 6944 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6945 | } |
| 6946 | |
| 6947 | exit: |
| 6948 | psa_reset_key_attributes( &attributes ); |
| 6949 | |
| 6950 | psa_destroy_key( key ); |
| 6951 | mbedtls_free( signature ); |
| 6952 | PSA_DONE( ); |
| 6953 | } |
| 6954 | /* END_CASE */ |
| 6955 | |
| 6956 | /* BEGIN_CASE */ |
| 6957 | void verify_message( int key_type_arg, |
| 6958 | data_t *key_data, |
| 6959 | int alg_arg, |
| 6960 | data_t *input_data, |
| 6961 | data_t *signature_data ) |
| 6962 | { |
| 6963 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6964 | psa_key_type_t key_type = key_type_arg; |
| 6965 | psa_algorithm_t alg = alg_arg; |
| 6966 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6967 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6968 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6969 | |
| 6970 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6971 | |
| 6972 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6973 | psa_set_key_algorithm( &attributes, alg ); |
| 6974 | psa_set_key_type( &attributes, key_type ); |
| 6975 | |
| 6976 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6977 | &key ) ); |
| 6978 | |
| 6979 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6980 | input_data->x, input_data->len, |
| 6981 | signature_data->x, signature_data->len ) ); |
| 6982 | |
| 6983 | exit: |
| 6984 | psa_reset_key_attributes( &attributes ); |
| 6985 | psa_destroy_key( key ); |
| 6986 | PSA_DONE( ); |
| 6987 | } |
| 6988 | /* END_CASE */ |
| 6989 | |
| 6990 | /* BEGIN_CASE */ |
| 6991 | void verify_message_fail( int key_type_arg, |
| 6992 | data_t *key_data, |
| 6993 | int alg_arg, |
| 6994 | data_t *hash_data, |
| 6995 | data_t *signature_data, |
| 6996 | int expected_status_arg ) |
| 6997 | { |
| 6998 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6999 | psa_key_type_t key_type = key_type_arg; |
| 7000 | psa_algorithm_t alg = alg_arg; |
| 7001 | psa_status_t actual_status; |
| 7002 | psa_status_t expected_status = expected_status_arg; |
| 7003 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7004 | |
| 7005 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7006 | |
| 7007 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 7008 | psa_set_key_algorithm( &attributes, alg ); |
| 7009 | psa_set_key_type( &attributes, key_type ); |
| 7010 | |
| 7011 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 7012 | &key ) ); |
| 7013 | |
| 7014 | actual_status = psa_verify_message( key, alg, |
| 7015 | hash_data->x, hash_data->len, |
| 7016 | signature_data->x, |
| 7017 | signature_data->len ); |
| 7018 | TEST_EQUAL( actual_status, expected_status ); |
| 7019 | |
| 7020 | exit: |
| 7021 | psa_reset_key_attributes( &attributes ); |
| 7022 | psa_destroy_key( key ); |
| 7023 | PSA_DONE( ); |
| 7024 | } |
| 7025 | /* END_CASE */ |
| 7026 | |
| 7027 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7028 | void asymmetric_encrypt( int key_type_arg, |
| 7029 | data_t *key_data, |
| 7030 | int alg_arg, |
| 7031 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7032 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7033 | int expected_output_length_arg, |
| 7034 | int expected_status_arg ) |
| 7035 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7036 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7037 | psa_key_type_t key_type = key_type_arg; |
| 7038 | psa_algorithm_t alg = alg_arg; |
| 7039 | size_t expected_output_length = expected_output_length_arg; |
| 7040 | size_t key_bits; |
| 7041 | unsigned char *output = NULL; |
| 7042 | size_t output_size; |
| 7043 | size_t output_length = ~0; |
| 7044 | psa_status_t actual_status; |
| 7045 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7046 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7047 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7048 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 7049 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7050 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7051 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7052 | psa_set_key_algorithm( &attributes, alg ); |
| 7053 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7054 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7055 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7056 | |
| 7057 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7058 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7059 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7060 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7061 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7062 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7063 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7064 | |
| 7065 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7066 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7067 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7068 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7069 | output, output_size, |
| 7070 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7071 | TEST_EQUAL( actual_status, expected_status ); |
| 7072 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7073 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7074 | /* If the label is empty, the test framework puts a non-null pointer |
| 7075 | * in label->x. Test that a null pointer works as well. */ |
| 7076 | if( label->len == 0 ) |
| 7077 | { |
| 7078 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7079 | if( output_size != 0 ) |
| 7080 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7081 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7082 | input_data->x, input_data->len, |
| 7083 | NULL, label->len, |
| 7084 | output, output_size, |
| 7085 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7086 | TEST_EQUAL( actual_status, expected_status ); |
| 7087 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7088 | } |
| 7089 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7090 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7091 | /* |
| 7092 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7093 | * thus reset them as required. |
| 7094 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7095 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7096 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7097 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7098 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7099 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7100 | } |
| 7101 | /* END_CASE */ |
| 7102 | |
| 7103 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7104 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 7105 | data_t *key_data, |
| 7106 | int alg_arg, |
| 7107 | data_t *input_data, |
| 7108 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7109 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7110 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7111 | psa_key_type_t key_type = key_type_arg; |
| 7112 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7113 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7114 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7115 | size_t output_size; |
| 7116 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7117 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7118 | size_t output2_size; |
| 7119 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7120 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7121 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7122 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7123 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7124 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 7125 | psa_set_key_algorithm( &attributes, alg ); |
| 7126 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7127 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7128 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7129 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7130 | |
| 7131 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7132 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7133 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7134 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7135 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7136 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7137 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7138 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7139 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7140 | TEST_LE_U( output2_size, |
| 7141 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 7142 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7143 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7144 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7145 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7146 | * the original plaintext because of the non-optional random |
| 7147 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7148 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7149 | input_data->x, input_data->len, |
| 7150 | label->x, label->len, |
| 7151 | output, output_size, |
| 7152 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7153 | /* We don't know what ciphertext length to expect, but check that |
| 7154 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7155 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7156 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7157 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7158 | output, output_length, |
| 7159 | label->x, label->len, |
| 7160 | output2, output2_size, |
| 7161 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7162 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 7163 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7164 | |
| 7165 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7166 | /* |
| 7167 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7168 | * thus reset them as required. |
| 7169 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7170 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7171 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7172 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7173 | mbedtls_free( output ); |
| 7174 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7175 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7176 | } |
| 7177 | /* END_CASE */ |
| 7178 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7179 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7180 | void asymmetric_decrypt( int key_type_arg, |
| 7181 | data_t *key_data, |
| 7182 | int alg_arg, |
| 7183 | data_t *input_data, |
| 7184 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 7185 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7186 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7187 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7188 | psa_key_type_t key_type = key_type_arg; |
| 7189 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7190 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7191 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7192 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7193 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7194 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7195 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7196 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7197 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7198 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7199 | psa_set_key_algorithm( &attributes, alg ); |
| 7200 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7201 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7202 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7203 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7204 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7205 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 7206 | key_bits = psa_get_key_bits( &attributes ); |
| 7207 | |
| 7208 | /* Determine the maximum ciphertext length */ |
| 7209 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7210 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7211 | ASSERT_ALLOC( output, output_size ); |
| 7212 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7213 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7214 | input_data->x, input_data->len, |
| 7215 | label->x, label->len, |
| 7216 | output, |
| 7217 | output_size, |
| 7218 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7219 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7220 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7221 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7222 | /* If the label is empty, the test framework puts a non-null pointer |
| 7223 | * in label->x. Test that a null pointer works as well. */ |
| 7224 | if( label->len == 0 ) |
| 7225 | { |
| 7226 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7227 | if( output_size != 0 ) |
| 7228 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7229 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7230 | input_data->x, input_data->len, |
| 7231 | NULL, label->len, |
| 7232 | output, |
| 7233 | output_size, |
| 7234 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7235 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7236 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7237 | } |
| 7238 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7239 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7240 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7241 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7242 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7243 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7244 | } |
| 7245 | /* END_CASE */ |
| 7246 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7247 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7248 | void asymmetric_decrypt_fail( int key_type_arg, |
| 7249 | data_t *key_data, |
| 7250 | int alg_arg, |
| 7251 | data_t *input_data, |
| 7252 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7253 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7254 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7255 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7256 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7257 | psa_key_type_t key_type = key_type_arg; |
| 7258 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7259 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7260 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7261 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7262 | psa_status_t actual_status; |
| 7263 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7264 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7265 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7266 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7267 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7268 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7269 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7270 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7271 | psa_set_key_algorithm( &attributes, alg ); |
| 7272 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7273 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7274 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7275 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7276 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7277 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7278 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7279 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7280 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7281 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7282 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7283 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7284 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7285 | /* If the label is empty, the test framework puts a non-null pointer |
| 7286 | * in label->x. Test that a null pointer works as well. */ |
| 7287 | if( label->len == 0 ) |
| 7288 | { |
| 7289 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7290 | if( output_size != 0 ) |
| 7291 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7292 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7293 | input_data->x, input_data->len, |
| 7294 | NULL, label->len, |
| 7295 | output, output_size, |
| 7296 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7297 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7298 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7299 | } |
| 7300 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7301 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7302 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7303 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7304 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7305 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7306 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7307 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7308 | |
| 7309 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 7310 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7311 | { |
| 7312 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7313 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7314 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7315 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7316 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7317 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 7318 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7319 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7320 | |
| 7321 | memset( &zero, 0, sizeof( zero ) ); |
| 7322 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7323 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7324 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7325 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7326 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7327 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7328 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7329 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7330 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7331 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7332 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7333 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7334 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7335 | } |
| 7336 | /* END_CASE */ |
| 7337 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7338 | /* BEGIN_CASE */ |
| 7339 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7340 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7341 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7342 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7343 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7344 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7345 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7346 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7347 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7348 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7349 | |
| 7350 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7351 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7352 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7353 | } |
| 7354 | /* END_CASE */ |
| 7355 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7356 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7357 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7358 | int expected_status_arg ) |
| 7359 | { |
| 7360 | psa_algorithm_t alg = alg_arg; |
| 7361 | size_t capacity = capacity_arg; |
| 7362 | psa_status_t expected_status = expected_status_arg; |
| 7363 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7364 | |
| 7365 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7366 | |
| 7367 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7368 | |
| 7369 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7370 | expected_status ); |
| 7371 | |
| 7372 | exit: |
| 7373 | psa_key_derivation_abort( &operation ); |
| 7374 | PSA_DONE( ); |
| 7375 | } |
| 7376 | /* END_CASE */ |
| 7377 | |
| 7378 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7379 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7380 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7381 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7382 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7383 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7384 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7385 | int expected_status_arg3, |
| 7386 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7387 | { |
| 7388 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7389 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7390 | psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3}; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7391 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7392 | expected_status_arg2, |
| 7393 | expected_status_arg3}; |
| 7394 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7395 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7396 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7397 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7398 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7399 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7400 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7401 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7402 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7403 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7404 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7405 | |
| 7406 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7407 | |
| 7408 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7409 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7410 | |
| 7411 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7412 | |
| 7413 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7414 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7415 | mbedtls_test_set_step( i ); |
| 7416 | if( steps[i] == 0 ) |
| 7417 | { |
| 7418 | /* Skip this step */ |
| 7419 | } |
| 7420 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7421 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7422 | psa_set_key_type( &attributes, key_types[i] ); |
| 7423 | PSA_ASSERT( psa_import_key( &attributes, |
| 7424 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7425 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7426 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7427 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7428 | { |
| 7429 | // When taking a private key as secret input, use key agreement |
| 7430 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7431 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7432 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7433 | expected_statuses[i] ); |
| 7434 | } |
| 7435 | else |
| 7436 | { |
| 7437 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7438 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7439 | expected_statuses[i] ); |
| 7440 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7441 | } |
| 7442 | else |
| 7443 | { |
| 7444 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7445 | &operation, steps[i], |
| 7446 | inputs[i]->x, inputs[i]->len ), |
| 7447 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7448 | } |
| 7449 | } |
| 7450 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7451 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7452 | { |
| 7453 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7454 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7455 | psa_set_key_bits( &attributes, 8 ); |
| 7456 | actual_output_status = |
| 7457 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7458 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7459 | } |
| 7460 | else |
| 7461 | { |
| 7462 | uint8_t buffer[1]; |
| 7463 | actual_output_status = |
| 7464 | psa_key_derivation_output_bytes( &operation, |
| 7465 | buffer, sizeof( buffer ) ); |
| 7466 | } |
| 7467 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7468 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7469 | exit: |
| 7470 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7471 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7472 | psa_destroy_key( keys[i] ); |
| 7473 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7474 | PSA_DONE( ); |
| 7475 | } |
| 7476 | /* END_CASE */ |
| 7477 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7478 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7479 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7480 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7481 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7482 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7483 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7484 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7485 | unsigned char input1[] = "Input 1"; |
| 7486 | size_t input1_length = sizeof( input1 ); |
| 7487 | unsigned char input2[] = "Input 2"; |
| 7488 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7489 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7490 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7491 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7492 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7493 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7494 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7495 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7496 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7497 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7498 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7499 | psa_set_key_algorithm( &attributes, alg ); |
| 7500 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7501 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7502 | PSA_ASSERT( psa_import_key( &attributes, |
| 7503 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7504 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7505 | |
| 7506 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7507 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7508 | input1, input1_length, |
| 7509 | input2, input2_length, |
| 7510 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7511 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7512 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7513 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7514 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7515 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7516 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7517 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7518 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7519 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7520 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7521 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7522 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7523 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7524 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7525 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7526 | } |
| 7527 | /* END_CASE */ |
| 7528 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7529 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7530 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7531 | { |
| 7532 | uint8_t output_buffer[16]; |
| 7533 | size_t buffer_size = 16; |
| 7534 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7535 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7536 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7537 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7538 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7539 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7540 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7541 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7542 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7543 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7544 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7545 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7546 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7547 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7548 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7549 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7550 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7551 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7552 | |
| 7553 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7554 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7555 | } |
| 7556 | /* END_CASE */ |
| 7557 | |
| 7558 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7559 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7560 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7561 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7562 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7563 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7564 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7565 | int requested_capacity_arg, |
| 7566 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7567 | data_t *expected_output2, |
| 7568 | int other_key_input_type, |
| 7569 | int key_input_type, |
| 7570 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7571 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7572 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7573 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7574 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7575 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7576 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7577 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7578 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7579 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7580 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7581 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7582 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7583 | uint8_t *expected_outputs[2] = |
| 7584 | {expected_output1->x, expected_output2->x}; |
| 7585 | size_t output_sizes[2] = |
| 7586 | {expected_output1->len, expected_output2->len}; |
| 7587 | size_t output_buffer_size = 0; |
| 7588 | uint8_t *output_buffer = NULL; |
| 7589 | size_t expected_capacity; |
| 7590 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7591 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7592 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7593 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7594 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7595 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7596 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7597 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7598 | |
| 7599 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7600 | { |
| 7601 | if( output_sizes[i] > output_buffer_size ) |
| 7602 | output_buffer_size = output_sizes[i]; |
| 7603 | if( output_sizes[i] == 0 ) |
| 7604 | expected_outputs[i] = NULL; |
| 7605 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7606 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7607 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7608 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7609 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7610 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7611 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7612 | requested_capacity ) ); |
| 7613 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7614 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7615 | switch( steps[i] ) |
| 7616 | { |
| 7617 | case 0: |
| 7618 | break; |
| 7619 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7620 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7621 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7622 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7623 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7624 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7625 | inputs[i]->x, inputs[i]->len ), |
| 7626 | statuses[i] ); |
| 7627 | |
| 7628 | if( statuses[i] != PSA_SUCCESS ) |
| 7629 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7630 | break; |
| 7631 | case 1: // input key |
| 7632 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7633 | psa_set_key_algorithm( &attributes1, alg ); |
| 7634 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7635 | |
| 7636 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7637 | inputs[i]->x, inputs[i]->len, |
| 7638 | &keys[i] ) ); |
| 7639 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7640 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7641 | { |
| 7642 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7643 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7644 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7645 | } |
| 7646 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7647 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7648 | steps[i], |
| 7649 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7650 | break; |
| 7651 | default: |
| 7652 | TEST_ASSERT( ! "default case not supported" ); |
| 7653 | break; |
| 7654 | } |
| 7655 | break; |
| 7656 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7657 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7658 | { |
| 7659 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7660 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7661 | steps[i], |
| 7662 | inputs[i]->x, |
| 7663 | inputs[i]->len ), |
| 7664 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7665 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7666 | case 1: // input key, type DERIVE |
| 7667 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7668 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7669 | psa_set_key_algorithm( &attributes2, alg ); |
| 7670 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7671 | |
| 7672 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7673 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7674 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7675 | |
| 7676 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7677 | inputs[i]->x, inputs[i]->len, |
| 7678 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7679 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7680 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7681 | steps[i], |
| 7682 | keys[i] ), |
| 7683 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7684 | break; |
| 7685 | case 2: // key agreement |
| 7686 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7687 | psa_set_key_algorithm( &attributes3, alg ); |
| 7688 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7689 | |
| 7690 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7691 | inputs[i]->x, inputs[i]->len, |
| 7692 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7693 | |
| 7694 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7695 | &operation, |
| 7696 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7697 | keys[i], key_agreement_peer_key->x, |
| 7698 | key_agreement_peer_key->len ), statuses[i] ); |
| 7699 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7700 | default: |
| 7701 | TEST_ASSERT( ! "default case not supported" ); |
| 7702 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7703 | } |
| 7704 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7705 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7706 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7707 | break; |
| 7708 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7709 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7710 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7711 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7712 | |
| 7713 | if( statuses[i] != PSA_SUCCESS ) |
| 7714 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7715 | break; |
| 7716 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7717 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7718 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7719 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7720 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7721 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7722 | expected_capacity = requested_capacity; |
| 7723 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7724 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7725 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7726 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7727 | |
| 7728 | /* For output key derivation secret must be provided using |
| 7729 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7730 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7731 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7732 | |
| 7733 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7734 | psa_set_key_algorithm( &attributes4, alg ); |
| 7735 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7736 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7737 | |
| 7738 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7739 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7740 | } |
| 7741 | else // output bytes |
| 7742 | { |
| 7743 | /* Expansion phase. */ |
| 7744 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7745 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7746 | /* Read some bytes. */ |
| 7747 | status = psa_key_derivation_output_bytes( &operation, |
| 7748 | output_buffer, output_sizes[i] ); |
| 7749 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7750 | { |
| 7751 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7752 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7753 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7754 | continue; |
| 7755 | } |
| 7756 | else if( expected_capacity == 0 || |
| 7757 | output_sizes[i] > expected_capacity ) |
| 7758 | { |
| 7759 | /* Capacity exceeded. */ |
| 7760 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7761 | expected_capacity = 0; |
| 7762 | continue; |
| 7763 | } |
| 7764 | /* Success. Check the read data. */ |
| 7765 | PSA_ASSERT( status ); |
| 7766 | if( output_sizes[i] != 0 ) |
| 7767 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7768 | expected_outputs[i], output_sizes[i] ); |
| 7769 | /* Check the operation status. */ |
| 7770 | expected_capacity -= output_sizes[i]; |
| 7771 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7772 | ¤t_capacity ) ); |
| 7773 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7774 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7775 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7776 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7777 | |
| 7778 | exit: |
| 7779 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7780 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7781 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7782 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7783 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7784 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7785 | } |
| 7786 | /* END_CASE */ |
| 7787 | |
| 7788 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7789 | void derive_full( int alg_arg, |
| 7790 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7791 | data_t *input1, |
| 7792 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7793 | int requested_capacity_arg ) |
| 7794 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7795 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7796 | psa_algorithm_t alg = alg_arg; |
| 7797 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7798 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7799 | unsigned char output_buffer[16]; |
| 7800 | size_t expected_capacity = requested_capacity; |
| 7801 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7802 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7803 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7804 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7805 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7806 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7807 | psa_set_key_algorithm( &attributes, alg ); |
| 7808 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7809 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7810 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7811 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7812 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7813 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7814 | input1->x, input1->len, |
| 7815 | input2->x, input2->len, |
| 7816 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7817 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7818 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7819 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7820 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7821 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7822 | |
| 7823 | /* Expansion phase. */ |
| 7824 | while( current_capacity > 0 ) |
| 7825 | { |
| 7826 | size_t read_size = sizeof( output_buffer ); |
| 7827 | if( read_size > current_capacity ) |
| 7828 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7829 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7830 | output_buffer, |
| 7831 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7832 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7833 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7834 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7835 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7836 | } |
| 7837 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7838 | /* Check that the operation refuses to go over capacity. */ |
| 7839 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7840 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7841 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7842 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7843 | |
| 7844 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7845 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7846 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7847 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7848 | } |
| 7849 | /* END_CASE */ |
| 7850 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 7851 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7852 | void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg, |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7853 | int derivation_step, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7854 | int capacity, int expected_capacity_status_arg, |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7855 | data_t *expected_output, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7856 | int expected_output_status_arg ) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7857 | { |
| 7858 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7859 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 7860 | psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step; |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7861 | uint8_t *output_buffer = NULL; |
| 7862 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7863 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 7864 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 7865 | psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg; |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7866 | |
| 7867 | ASSERT_ALLOC( output_buffer, expected_output->len ); |
| 7868 | PSA_ASSERT( psa_crypto_init() ); |
| 7869 | |
| 7870 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7871 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7872 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7873 | |
| 7874 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7875 | step, input->x, input->len ), |
| 7876 | expected_input_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7877 | |
| 7878 | if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS ) |
| 7879 | goto exit; |
| 7880 | |
| 7881 | status = psa_key_derivation_output_bytes( &operation, output_buffer, |
| 7882 | expected_output->len ); |
| 7883 | |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7884 | TEST_EQUAL( status, expected_output_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7885 | if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS ) |
| 7886 | ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x, |
| 7887 | expected_output->len ); |
| 7888 | |
| 7889 | exit: |
| 7890 | mbedtls_free( output_buffer ); |
| 7891 | psa_key_derivation_abort( &operation ); |
| 7892 | PSA_DONE(); |
| 7893 | } |
| 7894 | /* END_CASE */ |
| 7895 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7896 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7897 | void derive_key_exercise( int alg_arg, |
| 7898 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7899 | data_t *input1, |
| 7900 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7901 | int derived_type_arg, |
| 7902 | int derived_bits_arg, |
| 7903 | int derived_usage_arg, |
| 7904 | int derived_alg_arg ) |
| 7905 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7906 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7907 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7908 | psa_algorithm_t alg = alg_arg; |
| 7909 | psa_key_type_t derived_type = derived_type_arg; |
| 7910 | size_t derived_bits = derived_bits_arg; |
| 7911 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7912 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7913 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7914 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7915 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7916 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7917 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7918 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7919 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7920 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7921 | psa_set_key_algorithm( &attributes, alg ); |
| 7922 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7923 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7924 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7925 | |
| 7926 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7927 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7928 | input1->x, input1->len, |
| 7929 | input2->x, input2->len, |
| 7930 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7931 | goto exit; |
| 7932 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7933 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7934 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7935 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7936 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7937 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7938 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7939 | |
| 7940 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7941 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7942 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7943 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7944 | |
| 7945 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7946 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7947 | goto exit; |
| 7948 | |
| 7949 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7950 | /* |
| 7951 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7952 | * thus reset them as required. |
| 7953 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7954 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7955 | |
| 7956 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7957 | psa_destroy_key( base_key ); |
| 7958 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7959 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7960 | } |
| 7961 | /* END_CASE */ |
| 7962 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7963 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7964 | void derive_key_export( int alg_arg, |
| 7965 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7966 | data_t *input1, |
| 7967 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7968 | int bytes1_arg, |
| 7969 | int bytes2_arg ) |
| 7970 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7971 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7972 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7973 | psa_algorithm_t alg = alg_arg; |
| 7974 | size_t bytes1 = bytes1_arg; |
| 7975 | size_t bytes2 = bytes2_arg; |
| 7976 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7977 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7978 | uint8_t *output_buffer = NULL; |
| 7979 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7980 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7981 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7982 | size_t length; |
| 7983 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7984 | ASSERT_ALLOC( output_buffer, capacity ); |
| 7985 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7986 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7987 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7988 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7989 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7990 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7991 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7992 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7993 | |
| 7994 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7995 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7996 | input1->x, input1->len, |
| 7997 | input2->x, input2->len, |
| 7998 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7999 | goto exit; |
| 8000 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8001 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8002 | output_buffer, |
| 8003 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8004 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8005 | |
| 8006 | /* Derive the same output again, but this time store it in key objects. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8007 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8008 | input1->x, input1->len, |
| 8009 | input2->x, input2->len, |
| 8010 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8011 | goto exit; |
| 8012 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8013 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8014 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8015 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8016 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8017 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8018 | &derived_key ) ); |
| 8019 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8020 | export_buffer, bytes1, |
| 8021 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8022 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8023 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8024 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8025 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8026 | &derived_key ) ); |
| 8027 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8028 | export_buffer + bytes1, bytes2, |
| 8029 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8030 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8031 | |
| 8032 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8033 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 8034 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8035 | |
| 8036 | exit: |
| 8037 | mbedtls_free( output_buffer ); |
| 8038 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8039 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8040 | psa_destroy_key( base_key ); |
| 8041 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8042 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8043 | } |
| 8044 | /* END_CASE */ |
| 8045 | |
| 8046 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8047 | void derive_key_type( int alg_arg, |
| 8048 | data_t *key_data, |
| 8049 | data_t *input1, |
| 8050 | data_t *input2, |
| 8051 | int key_type_arg, int bits_arg, |
| 8052 | data_t *expected_export ) |
| 8053 | { |
| 8054 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8055 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8056 | const psa_algorithm_t alg = alg_arg; |
| 8057 | const psa_key_type_t key_type = key_type_arg; |
| 8058 | const size_t bits = bits_arg; |
| 8059 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8060 | const size_t export_buffer_size = |
| 8061 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 8062 | uint8_t *export_buffer = NULL; |
| 8063 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8064 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8065 | size_t export_length; |
| 8066 | |
| 8067 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 8068 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8069 | |
| 8070 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8071 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8072 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8073 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 8074 | &base_key ) ); |
| 8075 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8076 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8077 | &operation, base_key, alg, |
| 8078 | input1->x, input1->len, |
| 8079 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8080 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8081 | goto exit; |
| 8082 | |
| 8083 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8084 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8085 | psa_set_key_type( &derived_attributes, key_type ); |
| 8086 | psa_set_key_bits( &derived_attributes, bits ); |
| 8087 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 8088 | &derived_key ) ); |
| 8089 | |
| 8090 | PSA_ASSERT( psa_export_key( derived_key, |
| 8091 | export_buffer, export_buffer_size, |
| 8092 | &export_length ) ); |
| 8093 | ASSERT_COMPARE( export_buffer, export_length, |
| 8094 | expected_export->x, expected_export->len ); |
| 8095 | |
| 8096 | exit: |
| 8097 | mbedtls_free( export_buffer ); |
| 8098 | psa_key_derivation_abort( &operation ); |
| 8099 | psa_destroy_key( base_key ); |
| 8100 | psa_destroy_key( derived_key ); |
| 8101 | PSA_DONE( ); |
| 8102 | } |
| 8103 | /* END_CASE */ |
| 8104 | |
| 8105 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8106 | void derive_key( int alg_arg, |
| 8107 | data_t *key_data, data_t *input1, data_t *input2, |
| 8108 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8109 | int expected_status_arg, |
| 8110 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8111 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8112 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8113 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8114 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8115 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8116 | size_t bits = bits_arg; |
| 8117 | psa_status_t expected_status = expected_status_arg; |
| 8118 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8119 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8120 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8121 | |
| 8122 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8123 | |
| 8124 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8125 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8126 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8127 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8128 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8129 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8130 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8131 | input1->x, input1->len, |
| 8132 | input2->x, input2->len, |
| 8133 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8134 | goto exit; |
| 8135 | |
| 8136 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8137 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8138 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8139 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8140 | |
| 8141 | psa_status_t status = |
| 8142 | psa_key_derivation_output_key( &derived_attributes, |
| 8143 | &operation, |
| 8144 | &derived_key ); |
| 8145 | if( is_large_output > 0 ) |
| 8146 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8147 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8148 | |
| 8149 | exit: |
| 8150 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8151 | psa_destroy_key( base_key ); |
| 8152 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8153 | PSA_DONE( ); |
| 8154 | } |
| 8155 | /* END_CASE */ |
| 8156 | |
| 8157 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8158 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 8159 | int our_key_type_arg, int our_key_alg_arg, |
| 8160 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8161 | int expected_status_arg ) |
| 8162 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8163 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8164 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8165 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8166 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8167 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8168 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8169 | psa_status_t expected_status = expected_status_arg; |
| 8170 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8171 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8172 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8173 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8174 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8175 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8176 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8177 | PSA_ASSERT( psa_import_key( &attributes, |
| 8178 | our_key_data->x, our_key_data->len, |
| 8179 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8180 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8181 | /* The tests currently include inputs that should fail at either step. |
| 8182 | * Test cases that fail at the setup step should be changed to call |
| 8183 | * key_derivation_setup instead, and this function should be renamed |
| 8184 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8185 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8186 | if( status == PSA_SUCCESS ) |
| 8187 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8188 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 8189 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8190 | our_key, |
| 8191 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8192 | expected_status ); |
| 8193 | } |
| 8194 | else |
| 8195 | { |
| 8196 | TEST_ASSERT( status == expected_status ); |
| 8197 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8198 | |
| 8199 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8200 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8201 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8202 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8203 | } |
| 8204 | /* END_CASE */ |
| 8205 | |
| 8206 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8207 | void raw_key_agreement( int alg_arg, |
| 8208 | int our_key_type_arg, data_t *our_key_data, |
| 8209 | data_t *peer_key_data, |
| 8210 | data_t *expected_output ) |
| 8211 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8212 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8213 | psa_algorithm_t alg = alg_arg; |
| 8214 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8215 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8216 | unsigned char *output = NULL; |
| 8217 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8218 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8219 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8220 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8221 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8222 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8223 | psa_set_key_algorithm( &attributes, alg ); |
| 8224 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8225 | PSA_ASSERT( psa_import_key( &attributes, |
| 8226 | our_key_data->x, our_key_data->len, |
| 8227 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8228 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8229 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 8230 | key_bits = psa_get_key_bits( &attributes ); |
| 8231 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8232 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8233 | TEST_LE_U( expected_output->len, |
| 8234 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 8235 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 8236 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8237 | |
| 8238 | /* Good case with exact output size */ |
| 8239 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 8240 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8241 | peer_key_data->x, peer_key_data->len, |
| 8242 | output, expected_output->len, |
| 8243 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8244 | ASSERT_COMPARE( output, output_length, |
| 8245 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8246 | mbedtls_free( output ); |
| 8247 | output = NULL; |
| 8248 | output_length = ~0; |
| 8249 | |
| 8250 | /* Larger buffer */ |
| 8251 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 8252 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8253 | peer_key_data->x, peer_key_data->len, |
| 8254 | output, expected_output->len + 1, |
| 8255 | &output_length ) ); |
| 8256 | ASSERT_COMPARE( output, output_length, |
| 8257 | expected_output->x, expected_output->len ); |
| 8258 | mbedtls_free( output ); |
| 8259 | output = NULL; |
| 8260 | output_length = ~0; |
| 8261 | |
| 8262 | /* Buffer too small */ |
| 8263 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 8264 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 8265 | peer_key_data->x, peer_key_data->len, |
| 8266 | output, expected_output->len - 1, |
| 8267 | &output_length ), |
| 8268 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8269 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8270 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8271 | mbedtls_free( output ); |
| 8272 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8273 | |
| 8274 | exit: |
| 8275 | mbedtls_free( output ); |
| 8276 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8277 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8278 | } |
| 8279 | /* END_CASE */ |
| 8280 | |
| 8281 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8282 | void key_agreement_capacity( int alg_arg, |
| 8283 | int our_key_type_arg, data_t *our_key_data, |
| 8284 | data_t *peer_key_data, |
| 8285 | int expected_capacity_arg ) |
| 8286 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8287 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8288 | psa_algorithm_t alg = alg_arg; |
| 8289 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8290 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8291 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8292 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8293 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8294 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8295 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8296 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8297 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8298 | psa_set_key_algorithm( &attributes, alg ); |
| 8299 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8300 | PSA_ASSERT( psa_import_key( &attributes, |
| 8301 | our_key_data->x, our_key_data->len, |
| 8302 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8303 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8304 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8305 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8306 | &operation, |
| 8307 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8308 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8309 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8310 | { |
| 8311 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8312 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8313 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8314 | NULL, 0 ) ); |
| 8315 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8316 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8317 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8318 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8319 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8320 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8321 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8322 | /* Test the actual capacity by reading the output. */ |
| 8323 | while( actual_capacity > sizeof( output ) ) |
| 8324 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8325 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8326 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8327 | actual_capacity -= sizeof( output ); |
| 8328 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8329 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8330 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8331 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 8332 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8333 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8334 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8335 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8336 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8337 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8338 | } |
| 8339 | /* END_CASE */ |
| 8340 | |
| 8341 | /* BEGIN_CASE */ |
| 8342 | void key_agreement_output( int alg_arg, |
| 8343 | int our_key_type_arg, data_t *our_key_data, |
| 8344 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8345 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8346 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8347 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8348 | psa_algorithm_t alg = alg_arg; |
| 8349 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8350 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8351 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8352 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8353 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8354 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 8355 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8356 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8357 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8358 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8359 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8360 | psa_set_key_algorithm( &attributes, alg ); |
| 8361 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8362 | PSA_ASSERT( psa_import_key( &attributes, |
| 8363 | our_key_data->x, our_key_data->len, |
| 8364 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8365 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8366 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8367 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8368 | &operation, |
| 8369 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8370 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8371 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8372 | { |
| 8373 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8374 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8375 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8376 | NULL, 0 ) ); |
| 8377 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8378 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8379 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8380 | actual_output, |
| 8381 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8382 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8383 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8384 | if( expected_output2->len != 0 ) |
| 8385 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8386 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8387 | actual_output, |
| 8388 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8389 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8390 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8391 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8392 | |
| 8393 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8394 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8395 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8396 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8397 | mbedtls_free( actual_output ); |
| 8398 | } |
| 8399 | /* END_CASE */ |
| 8400 | |
| 8401 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8402 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8403 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8404 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8405 | unsigned char *output = NULL; |
| 8406 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8407 | size_t i; |
| 8408 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8409 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8410 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8411 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8412 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8413 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8414 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8415 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8416 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8417 | /* Run several times, to ensure that every output byte will be |
| 8418 | * nonzero at least once with overwhelming probability |
| 8419 | * (2^(-8*number_of_runs)). */ |
| 8420 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8421 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8422 | if( bytes != 0 ) |
| 8423 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8424 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8425 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8426 | for( i = 0; i < bytes; i++ ) |
| 8427 | { |
| 8428 | if( output[i] != 0 ) |
| 8429 | ++changed[i]; |
| 8430 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8431 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8432 | |
| 8433 | /* Check that every byte was changed to nonzero at least once. This |
| 8434 | * validates that psa_generate_random is overwriting every byte of |
| 8435 | * the output buffer. */ |
| 8436 | for( i = 0; i < bytes; i++ ) |
| 8437 | { |
| 8438 | TEST_ASSERT( changed[i] != 0 ); |
| 8439 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8440 | |
| 8441 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8442 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8443 | mbedtls_free( output ); |
| 8444 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8445 | } |
| 8446 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8447 | |
| 8448 | /* BEGIN_CASE */ |
| 8449 | void generate_key( int type_arg, |
| 8450 | int bits_arg, |
| 8451 | int usage_arg, |
| 8452 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8453 | int expected_status_arg, |
| 8454 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8455 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8456 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8457 | psa_key_type_t type = type_arg; |
| 8458 | psa_key_usage_t usage = usage_arg; |
| 8459 | size_t bits = bits_arg; |
| 8460 | psa_algorithm_t alg = alg_arg; |
| 8461 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8462 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8463 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8464 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8465 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8466 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8467 | psa_set_key_usage_flags( &attributes, usage ); |
| 8468 | psa_set_key_algorithm( &attributes, alg ); |
| 8469 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8470 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8471 | |
| 8472 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8473 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8474 | |
| 8475 | if( is_large_key > 0 ) |
| 8476 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8477 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8478 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8479 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8480 | |
| 8481 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8482 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8483 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8484 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8485 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8486 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8487 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8488 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8489 | |
| 8490 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8491 | /* |
| 8492 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8493 | * thus reset them as required. |
| 8494 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8495 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8496 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8497 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8498 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8499 | } |
| 8500 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8501 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8502 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8503 | void generate_key_rsa( int bits_arg, |
| 8504 | data_t *e_arg, |
| 8505 | int expected_status_arg ) |
| 8506 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8507 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8508 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8509 | size_t bits = bits_arg; |
| 8510 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8511 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8512 | psa_status_t expected_status = expected_status_arg; |
| 8513 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8514 | uint8_t *exported = NULL; |
| 8515 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8516 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8517 | size_t exported_length = SIZE_MAX; |
| 8518 | uint8_t *e_read_buffer = NULL; |
| 8519 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8520 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8521 | size_t e_read_length = SIZE_MAX; |
| 8522 | |
| 8523 | if( e_arg->len == 0 || |
| 8524 | ( e_arg->len == 3 && |
| 8525 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8526 | { |
| 8527 | is_default_public_exponent = 1; |
| 8528 | e_read_size = 0; |
| 8529 | } |
| 8530 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8531 | ASSERT_ALLOC( exported, exported_size ); |
| 8532 | |
| 8533 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8534 | |
| 8535 | psa_set_key_usage_flags( &attributes, usage ); |
| 8536 | psa_set_key_algorithm( &attributes, alg ); |
| 8537 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8538 | e_arg->x, e_arg->len ) ); |
| 8539 | psa_set_key_bits( &attributes, bits ); |
| 8540 | |
| 8541 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8542 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8543 | if( expected_status != PSA_SUCCESS ) |
| 8544 | goto exit; |
| 8545 | |
| 8546 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8547 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8548 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8549 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8550 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8551 | e_read_buffer, e_read_size, |
| 8552 | &e_read_length ) ); |
| 8553 | if( is_default_public_exponent ) |
| 8554 | TEST_EQUAL( e_read_length, 0 ); |
| 8555 | else |
| 8556 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8557 | |
| 8558 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8559 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8560 | goto exit; |
| 8561 | |
| 8562 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8563 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8564 | exported, exported_size, |
| 8565 | &exported_length ) ); |
| 8566 | { |
| 8567 | uint8_t *p = exported; |
| 8568 | uint8_t *end = exported + exported_length; |
| 8569 | size_t len; |
| 8570 | /* RSAPublicKey ::= SEQUENCE { |
| 8571 | * modulus INTEGER, -- n |
| 8572 | * publicExponent INTEGER } -- e |
| 8573 | */ |
| 8574 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8575 | MBEDTLS_ASN1_SEQUENCE | |
| 8576 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8577 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8578 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8579 | MBEDTLS_ASN1_INTEGER ) ); |
| 8580 | if( len >= 1 && p[0] == 0 ) |
| 8581 | { |
| 8582 | ++p; |
| 8583 | --len; |
| 8584 | } |
| 8585 | if( e_arg->len == 0 ) |
| 8586 | { |
| 8587 | TEST_EQUAL( len, 3 ); |
| 8588 | TEST_EQUAL( p[0], 1 ); |
| 8589 | TEST_EQUAL( p[1], 0 ); |
| 8590 | TEST_EQUAL( p[2], 1 ); |
| 8591 | } |
| 8592 | else |
| 8593 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8594 | } |
| 8595 | |
| 8596 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8597 | /* |
| 8598 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8599 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8600 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8601 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8602 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8603 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8604 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8605 | mbedtls_free( e_read_buffer ); |
| 8606 | mbedtls_free( exported ); |
| 8607 | } |
| 8608 | /* END_CASE */ |
| 8609 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8610 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8611 | void persistent_key_load_key_from_storage( data_t *data, |
| 8612 | int type_arg, int bits_arg, |
| 8613 | int usage_flags_arg, int alg_arg, |
| 8614 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8615 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8616 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8617 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8618 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8619 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8620 | psa_key_type_t type = type_arg; |
| 8621 | size_t bits = bits_arg; |
| 8622 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8623 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8624 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8625 | unsigned char *first_export = NULL; |
| 8626 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8627 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8628 | size_t first_exported_length; |
| 8629 | size_t second_exported_length; |
| 8630 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8631 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8632 | { |
| 8633 | ASSERT_ALLOC( first_export, export_size ); |
| 8634 | ASSERT_ALLOC( second_export, export_size ); |
| 8635 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8636 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8637 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8638 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8639 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8640 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8641 | psa_set_key_algorithm( &attributes, alg ); |
| 8642 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8643 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8644 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8645 | switch( generation_method ) |
| 8646 | { |
| 8647 | case IMPORT_KEY: |
| 8648 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8649 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8650 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8651 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8652 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8653 | case GENERATE_KEY: |
| 8654 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8655 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8656 | break; |
| 8657 | |
| 8658 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8659 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8660 | { |
| 8661 | /* Create base key */ |
| 8662 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8663 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8664 | psa_set_key_usage_flags( &base_attributes, |
| 8665 | PSA_KEY_USAGE_DERIVE ); |
| 8666 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8667 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8668 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8669 | data->x, data->len, |
| 8670 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8671 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8672 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8673 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8674 | &operation, |
| 8675 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8676 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8677 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8678 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8679 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8680 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8681 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8682 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8683 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8684 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8685 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8686 | #else |
| 8687 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8688 | #endif |
| 8689 | break; |
| 8690 | |
| 8691 | default: |
| 8692 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8693 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8694 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8695 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8696 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8697 | /* Export the key if permitted by the key policy. */ |
| 8698 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8699 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8700 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8701 | first_export, export_size, |
| 8702 | &first_exported_length ) ); |
| 8703 | if( generation_method == IMPORT_KEY ) |
| 8704 | ASSERT_COMPARE( data->x, data->len, |
| 8705 | first_export, first_exported_length ); |
| 8706 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8707 | |
| 8708 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8709 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8710 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8711 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8712 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8713 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8714 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8715 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8716 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8717 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8718 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8719 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8720 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8721 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8722 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8723 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8724 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8725 | /* Export the key again if permitted by the key policy. */ |
| 8726 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8727 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8728 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8729 | second_export, export_size, |
| 8730 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8731 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8732 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8733 | } |
| 8734 | |
| 8735 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8736 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8737 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8738 | |
| 8739 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8740 | /* |
| 8741 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8742 | * thus reset them as required. |
| 8743 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8744 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8745 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8746 | mbedtls_free( first_export ); |
| 8747 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8748 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8749 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8750 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8751 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8752 | } |
| 8753 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8754 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8755 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8756 | void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 8757 | int primitive_arg, int hash_arg, int role_arg, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8758 | int input_first, data_t *pw_data, |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8759 | int expected_status_setup_arg, |
| 8760 | int expected_status_set_role_arg, |
| 8761 | int expected_status_set_password_key_arg, |
| 8762 | int expected_status_input_output_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8763 | { |
| 8764 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8765 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 8766 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8767 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8768 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 8769 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8770 | psa_algorithm_t hash_alg = hash_arg; |
| 8771 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8772 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8773 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8774 | psa_status_t expected_status_setup = expected_status_setup_arg; |
| 8775 | psa_status_t expected_status_set_role = expected_status_set_role_arg; |
| 8776 | psa_status_t expected_status_set_password_key = |
| 8777 | expected_status_set_password_key_arg; |
| 8778 | psa_status_t expected_status_input_output = |
| 8779 | expected_status_input_output_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8780 | unsigned char *output_buffer = NULL; |
| 8781 | size_t output_len = 0; |
| 8782 | |
| 8783 | PSA_INIT( ); |
| 8784 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8785 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8786 | PSA_PAKE_STEP_KEY_SHARE); |
| 8787 | ASSERT_ALLOC( output_buffer, buf_size ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8788 | |
| 8789 | if( pw_data->len > 0 ) |
| 8790 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8791 | psa_set_key_usage_flags( &attributes, key_usage_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8792 | psa_set_key_algorithm( &attributes, alg ); |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8793 | psa_set_key_type( &attributes, key_type_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8794 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8795 | &key ) ); |
| 8796 | } |
| 8797 | |
| 8798 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8799 | psa_pake_cs_set_primitive( &cipher_suite, primitive ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8800 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8801 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8802 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8803 | |
| 8804 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8805 | PSA_ERROR_BAD_STATE ); |
| 8806 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8807 | PSA_ERROR_BAD_STATE ); |
| 8808 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8809 | PSA_ERROR_BAD_STATE ); |
| 8810 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8811 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8812 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8813 | NULL, 0, NULL ), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8814 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8815 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8816 | PSA_ERROR_BAD_STATE ); |
| 8817 | |
| 8818 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8819 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8820 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8821 | expected_status_setup ); |
| 8822 | if( expected_status_setup != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8823 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8824 | |
Neil Armstrong | 50de0ae | 2022-06-08 17:46:24 +0200 | [diff] [blame] | 8825 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8826 | PSA_ERROR_BAD_STATE ); |
| 8827 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8828 | TEST_EQUAL( psa_pake_set_role( &operation, role), |
| 8829 | expected_status_set_role ); |
| 8830 | if( expected_status_set_role != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8831 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8832 | |
| 8833 | if( pw_data->len > 0 ) |
| 8834 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8835 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8836 | expected_status_set_password_key ); |
| 8837 | if( expected_status_set_password_key != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8838 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8839 | } |
| 8840 | |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8841 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8842 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8843 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8844 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8845 | |
| 8846 | const uint8_t unsupported_id[] = "abcd"; |
| 8847 | |
| 8848 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8849 | PSA_ERROR_NOT_SUPPORTED ); |
| 8850 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8851 | PSA_ERROR_NOT_SUPPORTED ); |
| 8852 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8853 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8854 | PSA_PAKE_STEP_KEY_SHARE ); |
| 8855 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8856 | PSA_PAKE_STEP_ZK_PUBLIC ); |
| 8857 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8858 | PSA_PAKE_STEP_ZK_PROOF ); |
| 8859 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8860 | /* First round */ |
| 8861 | if( input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8862 | { |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8863 | /* Invalid parameters (input) */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8864 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8865 | NULL, 0 ), |
| 8866 | PSA_ERROR_INVALID_ARGUMENT ); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8867 | /* Invalid parameters (step) */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8868 | ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8869 | key, pw_data->len ); |
| 8870 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8871 | output_buffer, size_zk_proof ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8872 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8873 | /* Invalid first step */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8874 | ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8875 | key, pw_data->len ); |
| 8876 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8877 | output_buffer, size_zk_proof ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8878 | PSA_ERROR_BAD_STATE ); |
| 8879 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8880 | /* Possibly valid */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8881 | ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8882 | key, pw_data->len ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8883 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8884 | output_buffer, size_key_share ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8885 | expected_status_input_output); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8886 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8887 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8888 | { |
| 8889 | /* Buffer too large */ |
| 8890 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8891 | output_buffer, size_zk_public + 1 ), |
| 8892 | PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8893 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8894 | /* The operation's state should be invalidated at this point */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8895 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8896 | output_buffer, size_zk_public ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8897 | PSA_ERROR_BAD_STATE ); |
| 8898 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8899 | } |
| 8900 | else |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8901 | { |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8902 | /* Invalid parameters (output) */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8903 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8904 | NULL, 0, NULL ), |
| 8905 | PSA_ERROR_INVALID_ARGUMENT ); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8906 | /* Invalid parameters (step) */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8907 | ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8908 | key, pw_data->len ); |
| 8909 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8910 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8911 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8912 | /* Invalid first step */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8913 | ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8914 | key, pw_data->len ); |
| 8915 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8916 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8917 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8918 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8919 | /* Possibly valid */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8920 | ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8921 | key, pw_data->len ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8922 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8923 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8924 | expected_status_input_output ); |
Neil Armstrong | 98506ab | 2022-06-08 17:43:20 +0200 | [diff] [blame] | 8925 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8926 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8927 | { |
| 8928 | TEST_ASSERT( output_len > 0 ); |
| 8929 | |
| 8930 | /* Buffer too small */ |
| 8931 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8932 | output_buffer, size_zk_public - 1, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8933 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8934 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8935 | /* The operation's state should be invalidated at this point */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8936 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8937 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8938 | PSA_ERROR_BAD_STATE ); |
| 8939 | } |
| 8940 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8941 | |
| 8942 | exit: |
| 8943 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 8944 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8945 | mbedtls_free( output_buffer ); |
| 8946 | PSA_DONE( ); |
| 8947 | } |
| 8948 | /* END_CASE */ |
| 8949 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8950 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8951 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 8952 | int client_input_first, int inject_error, |
| 8953 | data_t *pw_data ) |
| 8954 | { |
| 8955 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8956 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8957 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8958 | psa_algorithm_t alg = alg_arg; |
| 8959 | psa_algorithm_t hash_alg = hash_arg; |
| 8960 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8961 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8962 | |
| 8963 | PSA_INIT( ); |
| 8964 | |
| 8965 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8966 | psa_set_key_algorithm( &attributes, alg ); |
| 8967 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8968 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8969 | &key ) ); |
| 8970 | |
| 8971 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8972 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8973 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8974 | |
| 8975 | |
| 8976 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8977 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8978 | |
| 8979 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8980 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8981 | |
| 8982 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8983 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8984 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8985 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8986 | client_input_first, 1, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8987 | |
| 8988 | if( inject_error == 1 || inject_error == 2 ) |
| 8989 | goto exit; |
| 8990 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8991 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8992 | client_input_first, 2, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8993 | |
| 8994 | exit: |
| 8995 | psa_destroy_key( key ); |
| 8996 | psa_pake_abort( &server ); |
| 8997 | psa_pake_abort( &client ); |
| 8998 | PSA_DONE( ); |
| 8999 | } |
| 9000 | /* END_CASE */ |
| 9001 | |
| 9002 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9003 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9004 | int derive_alg_arg, data_t *pw_data, |
Przemek Stekiel | cd356c3 | 2022-11-20 19:05:20 +0100 | [diff] [blame] | 9005 | int client_input_first, int destroy_key ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9006 | { |
| 9007 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9008 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9009 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9010 | psa_algorithm_t alg = alg_arg; |
| 9011 | psa_algorithm_t hash_alg = hash_arg; |
| 9012 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 9013 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9014 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9015 | psa_key_derivation_operation_t server_derive = |
| 9016 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9017 | psa_key_derivation_operation_t client_derive = |
| 9018 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9019 | |
| 9020 | PSA_INIT( ); |
| 9021 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9022 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 9023 | psa_set_key_algorithm( &attributes, alg ); |
| 9024 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 9025 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 9026 | &key ) ); |
| 9027 | |
| 9028 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 9029 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 9030 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 9031 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9032 | /* Get shared key */ |
| 9033 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 9034 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 9035 | |
| 9036 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 9037 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 9038 | { |
| 9039 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 9040 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9041 | (const uint8_t*) "", 0) ); |
| 9042 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 9043 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9044 | (const uint8_t*) "", 0) ); |
| 9045 | } |
| 9046 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9047 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 9048 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 9049 | |
| 9050 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 9051 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 9052 | |
| 9053 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 9054 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 9055 | |
Przemek Stekiel | cd356c3 | 2022-11-20 19:05:20 +0100 | [diff] [blame] | 9056 | if( destroy_key == 1 ) |
| 9057 | psa_destroy_key( key ); |
| 9058 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9059 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 9060 | PSA_ERROR_BAD_STATE ); |
| 9061 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 9062 | PSA_ERROR_BAD_STATE ); |
| 9063 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9064 | /* First round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9065 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9066 | client_input_first, 1, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9067 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9068 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 9069 | PSA_ERROR_BAD_STATE ); |
| 9070 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 9071 | PSA_ERROR_BAD_STATE ); |
| 9072 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9073 | /* Second round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9074 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9075 | client_input_first, 2, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9076 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9077 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 9078 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 9079 | |
| 9080 | exit: |
| 9081 | psa_key_derivation_abort( &server_derive ); |
| 9082 | psa_key_derivation_abort( &client_derive ); |
| 9083 | psa_destroy_key( key ); |
| 9084 | psa_pake_abort( &server ); |
| 9085 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9086 | PSA_DONE( ); |
| 9087 | } |
| 9088 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9089 | |
| 9090 | /* BEGIN_CASE */ |
| 9091 | void ecjpake_size_macros( ) |
| 9092 | { |
| 9093 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 9094 | const size_t bits = 256; |
| 9095 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
| 9096 | PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits ); |
| 9097 | const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( |
| 9098 | PSA_ECC_FAMILY_SECP_R1 ); |
| 9099 | |
| 9100 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 9101 | /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */ |
| 9102 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9103 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9104 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9105 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9106 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
| 9107 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9108 | PSA_BITS_TO_BYTES( bits ) ); |
| 9109 | |
| 9110 | /* Input sizes are the same as output sizes */ |
| 9111 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9112 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) ); |
| 9113 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9114 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) ); |
| 9115 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9116 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) ); |
| 9117 | |
| 9118 | /* These inequalities will always hold even when other PAKEs are added */ |
| 9119 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9120 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9121 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9122 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9123 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9124 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9125 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9126 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9127 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9128 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9129 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9130 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9131 | } |
| 9132 | /* END_CASE */ |