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 | |
| 34 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 35 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 36 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 37 | /** Test if a buffer contains a constant byte value. |
| 38 | * |
| 39 | * `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] | 40 | * |
| 41 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 42 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 43 | * \param size Size of the buffer in bytes. |
| 44 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 45 | * \return 1 if the buffer is all-bits-zero. |
| 46 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 47 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 48 | 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] | 49 | { |
| 50 | size_t i; |
| 51 | for( i = 0; i < size; i++ ) |
| 52 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 53 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 54 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 55 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 56 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 57 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 58 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 59 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 60 | static int asn1_write_10x( unsigned char **p, |
| 61 | unsigned char *start, |
| 62 | size_t bits, |
| 63 | unsigned char x ) |
| 64 | { |
| 65 | int ret; |
| 66 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 67 | if( bits == 0 ) |
| 68 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 69 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 70 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 71 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 72 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 73 | *p -= len; |
| 74 | ( *p )[len-1] = x; |
| 75 | if( bits % 8 == 0 ) |
| 76 | ( *p )[1] |= 1; |
| 77 | else |
| 78 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 79 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 80 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 81 | MBEDTLS_ASN1_INTEGER ) ); |
| 82 | return( len ); |
| 83 | } |
| 84 | |
| 85 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 86 | size_t buffer_size, |
| 87 | unsigned char **p, |
| 88 | size_t bits, |
| 89 | int keypair ) |
| 90 | { |
| 91 | size_t half_bits = ( bits + 1 ) / 2; |
| 92 | int ret; |
| 93 | int len = 0; |
| 94 | /* Construct something that looks like a DER encoding of |
| 95 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 96 | * RSAPrivateKey ::= SEQUENCE { |
| 97 | * version Version, |
| 98 | * modulus INTEGER, -- n |
| 99 | * publicExponent INTEGER, -- e |
| 100 | * privateExponent INTEGER, -- d |
| 101 | * prime1 INTEGER, -- p |
| 102 | * prime2 INTEGER, -- q |
| 103 | * exponent1 INTEGER, -- d mod (p-1) |
| 104 | * exponent2 INTEGER, -- d mod (q-1) |
| 105 | * coefficient INTEGER, -- (inverse of q) mod p |
| 106 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 107 | * } |
| 108 | * Or, for a public key, the same structure with only |
| 109 | * version, modulus and publicExponent. |
| 110 | */ |
| 111 | *p = buffer + buffer_size; |
| 112 | if( keypair ) |
| 113 | { |
| 114 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 115 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 116 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 117 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 118 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 119 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 120 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 121 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 122 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 123 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 124 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 125 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 126 | } |
| 127 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 128 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 129 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 130 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 131 | if( keypair ) |
| 132 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 133 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 134 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 135 | { |
| 136 | const unsigned char tag = |
| 137 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 138 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 139 | } |
| 140 | return( len ); |
| 141 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 142 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 143 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 144 | int exercise_mac_setup( psa_key_type_t key_type, |
| 145 | const unsigned char *key_bytes, |
| 146 | size_t key_length, |
| 147 | psa_algorithm_t alg, |
| 148 | psa_mac_operation_t *operation, |
| 149 | psa_status_t *status ) |
| 150 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 151 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 152 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 153 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 154 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 155 | psa_set_key_algorithm( &attributes, alg ); |
| 156 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 157 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 158 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 159 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 160 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 161 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 162 | /* If setup failed, reproduce the failure, so that the caller can |
| 163 | * test the resulting state of the operation object. */ |
| 164 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 165 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 166 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 169 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 170 | return( 1 ); |
| 171 | |
| 172 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 173 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 174 | return( 0 ); |
| 175 | } |
| 176 | |
| 177 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 178 | const unsigned char *key_bytes, |
| 179 | size_t key_length, |
| 180 | psa_algorithm_t alg, |
| 181 | psa_cipher_operation_t *operation, |
| 182 | psa_status_t *status ) |
| 183 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 184 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 185 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 186 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 187 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 188 | psa_set_key_algorithm( &attributes, alg ); |
| 189 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 190 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 191 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 192 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 193 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 194 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 195 | /* If setup failed, reproduce the failure, so that the caller can |
| 196 | * test the resulting state of the operation object. */ |
| 197 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 198 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 199 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 200 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 203 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 204 | return( 1 ); |
| 205 | |
| 206 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 207 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 208 | return( 0 ); |
| 209 | } |
| 210 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 211 | 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] | 212 | { |
| 213 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 214 | 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] | 215 | uint8_t buffer[1]; |
| 216 | size_t length; |
| 217 | int ok = 0; |
| 218 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 219 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 220 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 221 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 222 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 223 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 224 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 225 | TEST_EQUAL( |
| 226 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 227 | TEST_EQUAL( |
| 228 | 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] | 229 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 230 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 231 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 232 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 233 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 234 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 235 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 236 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 237 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 238 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 239 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 240 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 241 | ok = 1; |
| 242 | |
| 243 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 244 | /* |
| 245 | * Key attributes may have been returned by psa_get_key_attributes() |
| 246 | * thus reset them as required. |
| 247 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 248 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 249 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 250 | return( ok ); |
| 251 | } |
| 252 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 253 | /* Assert that a key isn't reported as having a slot number. */ |
| 254 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 255 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 256 | do \ |
| 257 | { \ |
| 258 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 259 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 260 | attributes, \ |
| 261 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 262 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 263 | } \ |
| 264 | while( 0 ) |
| 265 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 266 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 267 | ( (void) 0 ) |
| 268 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 269 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 270 | /* An overapproximation of the amount of storage needed for a key of the |
| 271 | * given type and with the given content. The API doesn't make it easy |
| 272 | * to find a good value for the size. The current implementation doesn't |
| 273 | * care about the value anyway. */ |
| 274 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 275 | ( data )->len |
| 276 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 277 | typedef enum { |
| 278 | IMPORT_KEY = 0, |
| 279 | GENERATE_KEY = 1, |
| 280 | DERIVE_KEY = 2 |
| 281 | } generate_method; |
| 282 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 283 | typedef enum |
| 284 | { |
| 285 | DO_NOT_SET_LENGTHS = 0, |
| 286 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 287 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 288 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 289 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 290 | typedef enum |
| 291 | { |
| 292 | USE_NULL_TAG = 0, |
| 293 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 294 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 295 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 296 | /*! |
| 297 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 298 | * \param key_type_arg Type of key passed in |
| 299 | * \param key_data The encryption / decryption key data |
| 300 | * \param alg_arg The type of algorithm used |
| 301 | * \param nonce Nonce data |
| 302 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 303 | * \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] | 304 | * feed additional data in to be encrypted / |
| 305 | * decrypted. If -1, no chunking. |
| 306 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 307 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 308 | * the data in to be encrypted / decrypted. If |
| 309 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 310 | * \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] | 311 | * expected here, this controls whether or not |
| 312 | * to set lengths, and in what order with |
| 313 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 314 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 315 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 316 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 317 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 318 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 319 | */ |
| 320 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 321 | int alg_arg, |
| 322 | data_t *nonce, |
| 323 | data_t *additional_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 324 | int ad_part_len_arg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 325 | data_t *input_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 326 | int data_part_len_arg, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 327 | set_lengths_method_t set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 328 | data_t *expected_output, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 329 | int is_encrypt, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 330 | int do_zero_parts ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 331 | { |
| 332 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 333 | psa_key_type_t key_type = key_type_arg; |
| 334 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 335 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 336 | unsigned char *output_data = NULL; |
| 337 | unsigned char *part_data = NULL; |
| 338 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 339 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 340 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 341 | size_t output_size = 0; |
| 342 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 343 | size_t output_length = 0; |
| 344 | size_t key_bits = 0; |
| 345 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 346 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 347 | size_t part_length = 0; |
| 348 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 349 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 350 | size_t ad_part_len = 0; |
| 351 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 352 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 353 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 354 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 355 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 356 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 357 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 358 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 359 | PSA_ASSERT( psa_crypto_init( ) ); |
| 360 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 361 | if( is_encrypt ) |
| 362 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 363 | else |
| 364 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 365 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 366 | psa_set_key_algorithm( &attributes, alg ); |
| 367 | psa_set_key_type( &attributes, key_type ); |
| 368 | |
| 369 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 370 | &key ) ); |
| 371 | |
| 372 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 373 | key_bits = psa_get_key_bits( &attributes ); |
| 374 | |
| 375 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 376 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 377 | if( is_encrypt ) |
| 378 | { |
| 379 | /* Tag gets written at end of buffer. */ |
| 380 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 381 | ( input_data->len + |
| 382 | tag_length ) ); |
| 383 | data_true_size = input_data->len; |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 388 | ( input_data->len - |
| 389 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 390 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 391 | /* Do not want to attempt to decrypt tag. */ |
| 392 | data_true_size = input_data->len - tag_length; |
| 393 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 394 | |
| 395 | ASSERT_ALLOC( output_data, output_size ); |
| 396 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 397 | if( is_encrypt ) |
| 398 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 399 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 400 | TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 401 | } |
| 402 | else |
| 403 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 404 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 405 | TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 406 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 407 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 408 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 409 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 410 | if( is_encrypt ) |
| 411 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 412 | else |
| 413 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 414 | |
| 415 | /* If the operation is not supported, just skip and not fail in case the |
| 416 | * encryption involves a common limitation of cryptography hardwares and |
| 417 | * an alternative implementation. */ |
| 418 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 419 | { |
| 420 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 421 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 422 | } |
| 423 | |
| 424 | PSA_ASSERT( status ); |
| 425 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 426 | if( set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 427 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 428 | else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 429 | { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 430 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 431 | data_true_size ) ); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 432 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 433 | } |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 434 | else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 435 | { |
| 436 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 437 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 438 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 439 | data_true_size ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 440 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 441 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 442 | if( ad_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 443 | { |
| 444 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 445 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 446 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 447 | for( part_offset = 0, part_count = 0; |
| 448 | part_offset < additional_data->len; |
| 449 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 450 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 451 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 452 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 453 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 454 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 455 | else if( additional_data->len - part_offset < ad_part_len ) |
| 456 | { |
| 457 | part_length = additional_data->len - part_offset; |
| 458 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 459 | else |
| 460 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 461 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 465 | additional_data->x + part_offset, |
| 466 | part_length ) ); |
| 467 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | /* Pass additional data in one go. */ |
| 473 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 474 | additional_data->len ) ); |
| 475 | } |
| 476 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 477 | if( data_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 478 | { |
| 479 | /* Pass data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 480 | data_part_len = ( size_t ) data_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 481 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 482 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 483 | |
| 484 | ASSERT_ALLOC( part_data, part_data_size ); |
| 485 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 486 | for( part_offset = 0, part_count = 0; |
| 487 | part_offset < data_true_size; |
| 488 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 489 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 490 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 491 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 492 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 493 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 494 | else if( ( data_true_size - part_offset ) < data_part_len ) |
| 495 | { |
| 496 | part_length = ( data_true_size - part_offset ); |
| 497 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 498 | else |
| 499 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 500 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | PSA_ASSERT( psa_aead_update( &operation, |
| 504 | ( input_data->x + part_offset ), |
| 505 | part_length, part_data, |
| 506 | part_data_size, |
| 507 | &output_part_length ) ); |
| 508 | |
| 509 | if( output_data && output_part_length ) |
| 510 | { |
Mircea Udrea | 657ff4f | 2022-01-31 13:51:56 +0100 | [diff] [blame] | 511 | memcpy( ( output_data + output_length ), part_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 512 | output_part_length ); |
| 513 | } |
| 514 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 515 | output_length += output_part_length; |
| 516 | } |
| 517 | } |
| 518 | else |
| 519 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 520 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 521 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 522 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 523 | output_size, &output_length ) ); |
| 524 | } |
| 525 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 526 | if( is_encrypt ) |
| 527 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 528 | final_output_size, |
| 529 | &output_part_length, |
| 530 | tag_buffer, tag_length, |
| 531 | &tag_size ) ); |
| 532 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 533 | { |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 534 | PSA_ASSERT( psa_aead_verify( &operation, final_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 535 | final_output_size, |
| 536 | &output_part_length, |
| 537 | ( input_data->x + data_true_size ), |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 538 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 539 | } |
| 540 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 541 | if( output_data && output_part_length ) |
| 542 | memcpy( ( output_data + output_length ), final_data, |
| 543 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 544 | |
| 545 | output_length += output_part_length; |
| 546 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 547 | |
| 548 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 549 | * should be exact.*/ |
| 550 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 551 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 552 | TEST_EQUAL( tag_length, tag_size ); |
| 553 | |
| 554 | if( output_data && tag_length ) |
| 555 | memcpy( ( output_data + output_length ), tag_buffer, |
| 556 | tag_length ); |
| 557 | |
| 558 | output_length += tag_length; |
| 559 | |
| 560 | TEST_EQUAL( output_length, |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 561 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 562 | input_data->len ) ); |
| 563 | TEST_LE_U( output_length, |
| 564 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 565 | } |
| 566 | else |
| 567 | { |
| 568 | TEST_EQUAL( output_length, |
| 569 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 570 | input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 571 | TEST_LE_U( output_length, |
| 572 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 575 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 576 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 577 | output_data, output_length ); |
| 578 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 579 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 580 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 581 | |
| 582 | exit: |
| 583 | psa_destroy_key( key ); |
| 584 | psa_aead_abort( &operation ); |
| 585 | mbedtls_free( output_data ); |
| 586 | mbedtls_free( part_data ); |
| 587 | mbedtls_free( final_data ); |
| 588 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 589 | |
| 590 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 593 | /*! |
| 594 | * \brief Internal Function for MAC multipart tests. |
| 595 | * \param key_type_arg Type of key passed in |
| 596 | * \param key_data The encryption / decryption key data |
| 597 | * \param alg_arg The type of algorithm used |
| 598 | * \param input_data Data to encrypt / decrypt |
| 599 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 600 | * the data in to be encrypted / decrypted. If |
| 601 | * -1, no chunking |
| 602 | * \param expected_output Expected output |
| 603 | * \param is_verify If non-zero this is an verify operation. |
| 604 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 605 | * with normal length chunks. |
| 606 | * \return int Zero on failure, non-zero on success. |
| 607 | */ |
| 608 | static int mac_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 609 | int alg_arg, |
| 610 | data_t *input_data, |
| 611 | int data_part_len_arg, |
| 612 | data_t *expected_output, |
| 613 | int is_verify, |
| 614 | int do_zero_parts ) |
| 615 | { |
| 616 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 617 | psa_key_type_t key_type = key_type_arg; |
| 618 | psa_algorithm_t alg = alg_arg; |
| 619 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 620 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 621 | size_t part_offset = 0; |
| 622 | size_t part_length = 0; |
| 623 | size_t data_part_len = 0; |
| 624 | size_t mac_len = 0; |
| 625 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 626 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 627 | |
| 628 | int test_ok = 0; |
| 629 | size_t part_count = 0; |
| 630 | |
Neil Armstrong | fd4c259 | 2022-03-07 10:11:11 +0100 | [diff] [blame] | 631 | PSA_INIT( ); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 632 | |
| 633 | if( is_verify ) |
| 634 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 635 | else |
| 636 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 637 | |
| 638 | psa_set_key_algorithm( &attributes, alg ); |
| 639 | psa_set_key_type( &attributes, key_type ); |
| 640 | |
| 641 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 642 | &key ) ); |
| 643 | |
| 644 | if( is_verify ) |
| 645 | status = psa_mac_verify_setup( &operation, key, alg ); |
| 646 | else |
| 647 | status = psa_mac_sign_setup( &operation, key, alg ); |
| 648 | |
| 649 | PSA_ASSERT( status ); |
| 650 | |
| 651 | if( data_part_len_arg != -1 ) |
| 652 | { |
| 653 | /* Pass data in parts */ |
| 654 | data_part_len = ( size_t ) data_part_len_arg; |
| 655 | |
| 656 | for( part_offset = 0, part_count = 0; |
| 657 | part_offset < input_data->len; |
| 658 | part_offset += part_length, part_count++ ) |
| 659 | { |
| 660 | if( do_zero_parts && ( part_count & 0x01 ) ) |
| 661 | { |
| 662 | part_length = 0; |
| 663 | } |
| 664 | else if( ( input_data->len - part_offset ) < data_part_len ) |
| 665 | { |
| 666 | part_length = ( input_data->len - part_offset ); |
| 667 | } |
| 668 | else |
| 669 | { |
| 670 | part_length = data_part_len; |
| 671 | } |
| 672 | |
| 673 | PSA_ASSERT( psa_mac_update( &operation, |
| 674 | ( input_data->x + part_offset ), |
| 675 | part_length ) ); |
| 676 | } |
| 677 | } |
| 678 | else |
| 679 | { |
| 680 | /* Pass all data in one go. */ |
| 681 | PSA_ASSERT( psa_mac_update( &operation, input_data->x, |
| 682 | input_data->len ) ); |
| 683 | } |
| 684 | |
| 685 | if( is_verify ) |
| 686 | { |
| 687 | PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x, |
| 688 | expected_output->len ) ); |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | PSA_ASSERT( psa_mac_sign_finish( &operation, mac, |
| 693 | PSA_MAC_MAX_SIZE, &mac_len ) ); |
| 694 | |
| 695 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 696 | mac, mac_len ); |
| 697 | } |
| 698 | |
| 699 | test_ok = 1; |
| 700 | |
| 701 | exit: |
| 702 | psa_destroy_key( key ); |
| 703 | psa_mac_abort( &operation ); |
| 704 | PSA_DONE( ); |
| 705 | |
| 706 | return( test_ok ); |
| 707 | } |
| 708 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 709 | #if defined(PSA_WANT_ALG_JPAKE) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 710 | static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive, |
| 711 | psa_pake_operation_t *server, |
| 712 | psa_pake_operation_t *client, |
| 713 | int client_input_first, |
| 714 | int round, int inject_error ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 715 | { |
| 716 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 717 | size_t buffer_length = ( |
| 718 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 719 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 720 | 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] | 721 | /* The output should be exactly this size according to the spec */ |
| 722 | const size_t expected_size_key_share = |
| 723 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 724 | /* The output should be exactly this size according to the spec */ |
| 725 | const size_t expected_size_zk_public = |
| 726 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 727 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 728 | const size_t max_expected_size_zk_proof = |
| 729 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 730 | size_t buffer0_off = 0; |
| 731 | size_t buffer1_off = 0; |
| 732 | size_t s_g1_len, s_g2_len, s_a_len; |
| 733 | size_t s_g1_off, s_g2_off, s_a_off; |
| 734 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 735 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 736 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 737 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 738 | size_t c_g1_len, c_g2_len, c_a_len; |
| 739 | size_t c_g1_off, c_g2_off, c_a_off; |
| 740 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 741 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 742 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 743 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 744 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 745 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 746 | |
| 747 | ASSERT_ALLOC( buffer0, buffer_length ); |
| 748 | ASSERT_ALLOC( buffer1, buffer_length ); |
| 749 | |
| 750 | switch( round ) |
| 751 | { |
| 752 | case 1: |
| 753 | /* Server first round Output */ |
| 754 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 755 | buffer0 + buffer0_off, |
| 756 | 512 - buffer0_off, &s_g1_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 757 | TEST_EQUAL( s_g1_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 758 | s_g1_off = buffer0_off; |
| 759 | buffer0_off += s_g1_len; |
| 760 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 761 | buffer0 + buffer0_off, |
| 762 | 512 - buffer0_off, &s_x1_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 763 | TEST_EQUAL( s_x1_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 764 | s_x1_pk_off = buffer0_off; |
| 765 | buffer0_off += s_x1_pk_len; |
| 766 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 767 | buffer0 + buffer0_off, |
| 768 | 512 - buffer0_off, &s_x1_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 769 | TEST_LE_U( s_x1_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 770 | s_x1_pr_off = buffer0_off; |
| 771 | buffer0_off += s_x1_pr_len; |
| 772 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 773 | buffer0 + buffer0_off, |
| 774 | 512 - buffer0_off, &s_g2_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 775 | TEST_EQUAL( s_g2_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 776 | s_g2_off = buffer0_off; |
| 777 | buffer0_off += s_g2_len; |
| 778 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 779 | buffer0 + buffer0_off, |
| 780 | 512 - buffer0_off, &s_x2_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 781 | TEST_EQUAL( s_x2_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 782 | s_x2_pk_off = buffer0_off; |
| 783 | buffer0_off += s_x2_pk_len; |
| 784 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 785 | buffer0 + buffer0_off, |
| 786 | 512 - buffer0_off, &s_x2_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 787 | TEST_LE_U( s_x2_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 788 | s_x2_pr_off = buffer0_off; |
| 789 | buffer0_off += s_x2_pr_len; |
| 790 | |
| 791 | if( inject_error == 1 ) |
| 792 | { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 793 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 794 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 795 | expected_status = PSA_ERROR_DATA_INVALID; |
| 796 | } |
| 797 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 798 | /* |
| 799 | * When injecting errors in inputs, the implementation is |
| 800 | * free to detect it right away of with a delay. |
| 801 | * This permits delaying the error until the end of the input |
| 802 | * sequence, if no error appears then, this will be treated |
| 803 | * as an error. |
| 804 | */ |
| 805 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 806 | if( client_input_first == 1 ) |
| 807 | { |
| 808 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 809 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 810 | buffer0 + s_g1_off, s_g1_len ); |
| 811 | if( inject_error == 1 && status != PSA_SUCCESS ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 812 | { |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 813 | TEST_EQUAL( status, expected_status ); |
| 814 | break; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 815 | } |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 816 | else |
| 817 | { |
| 818 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 819 | } |
| 820 | |
| 821 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 822 | buffer0 + s_x1_pk_off, |
| 823 | s_x1_pk_len ); |
| 824 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 825 | { |
| 826 | TEST_EQUAL( status, expected_status ); |
| 827 | break; |
| 828 | } |
| 829 | else |
| 830 | { |
| 831 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 832 | } |
| 833 | |
| 834 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 835 | buffer0 + s_x1_pr_off, |
| 836 | s_x1_pr_len ); |
| 837 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 838 | { |
| 839 | TEST_EQUAL( status, expected_status ); |
| 840 | break; |
| 841 | } |
| 842 | else |
| 843 | { |
| 844 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 845 | } |
| 846 | |
| 847 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 848 | buffer0 + s_g2_off, |
| 849 | s_g2_len ); |
| 850 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 851 | { |
| 852 | TEST_EQUAL( status, expected_status ); |
| 853 | break; |
| 854 | } |
| 855 | else |
| 856 | { |
| 857 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 858 | } |
| 859 | |
| 860 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 861 | buffer0 + s_x2_pk_off, |
| 862 | s_x2_pk_len ); |
| 863 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 864 | { |
| 865 | TEST_EQUAL( status, expected_status ); |
| 866 | break; |
| 867 | } |
| 868 | else |
| 869 | { |
| 870 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 871 | } |
| 872 | |
| 873 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 874 | buffer0 + s_x2_pr_off, |
| 875 | s_x2_pr_len ); |
| 876 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 877 | { |
| 878 | TEST_EQUAL( status, expected_status ); |
| 879 | break; |
| 880 | } |
| 881 | else |
| 882 | { |
| 883 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 884 | } |
| 885 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 886 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 887 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 888 | 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] | 889 | } |
| 890 | |
| 891 | /* Client first round Output */ |
| 892 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 893 | buffer1 + buffer1_off, |
| 894 | 512 - buffer1_off, &c_g1_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 895 | TEST_EQUAL( c_g1_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 896 | c_g1_off = buffer1_off; |
| 897 | buffer1_off += c_g1_len; |
| 898 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 899 | buffer1 + buffer1_off, |
| 900 | 512 - buffer1_off, &c_x1_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 901 | TEST_EQUAL( c_x1_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 902 | c_x1_pk_off = buffer1_off; |
| 903 | buffer1_off += c_x1_pk_len; |
| 904 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 905 | buffer1 + buffer1_off, |
| 906 | 512 - buffer1_off, &c_x1_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 907 | TEST_LE_U( c_x1_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 908 | c_x1_pr_off = buffer1_off; |
| 909 | buffer1_off += c_x1_pr_len; |
| 910 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 911 | buffer1 + buffer1_off, |
| 912 | 512 - buffer1_off, &c_g2_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 913 | TEST_EQUAL( c_g2_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 914 | c_g2_off = buffer1_off; |
| 915 | buffer1_off += c_g2_len; |
| 916 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 917 | buffer1 + buffer1_off, |
| 918 | 512 - buffer1_off, &c_x2_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 919 | TEST_EQUAL( c_x2_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 920 | c_x2_pk_off = buffer1_off; |
| 921 | buffer1_off += c_x2_pk_len; |
| 922 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 923 | buffer1 + buffer1_off, |
| 924 | 512 - buffer1_off, &c_x2_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 925 | TEST_LE_U( c_x2_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 926 | c_x2_pr_off = buffer1_off; |
| 927 | buffer1_off += c_x2_pr_len; |
| 928 | |
| 929 | if( client_input_first == 0 ) |
| 930 | { |
| 931 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 932 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 933 | buffer0 + s_g1_off, s_g1_len ); |
| 934 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 935 | { |
| 936 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 937 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 938 | } |
| 939 | else |
| 940 | { |
| 941 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 942 | } |
| 943 | |
| 944 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 945 | buffer0 + s_x1_pk_off, |
| 946 | s_x1_pk_len ); |
| 947 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 948 | { |
| 949 | TEST_EQUAL( status, expected_status ); |
| 950 | break; |
| 951 | } |
| 952 | else |
| 953 | { |
| 954 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 955 | } |
| 956 | |
| 957 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 958 | buffer0 + s_x1_pr_off, |
| 959 | s_x1_pr_len ); |
| 960 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 961 | { |
| 962 | TEST_EQUAL( status, expected_status ); |
| 963 | break; |
| 964 | } |
| 965 | else |
| 966 | { |
| 967 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 968 | } |
| 969 | |
| 970 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 971 | buffer0 + s_g2_off, |
| 972 | s_g2_len ); |
| 973 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 974 | { |
| 975 | TEST_EQUAL( status, expected_status ); |
| 976 | break; |
| 977 | } |
| 978 | else |
| 979 | { |
| 980 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 981 | } |
| 982 | |
| 983 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 984 | buffer0 + s_x2_pk_off, |
| 985 | s_x2_pk_len ); |
| 986 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 987 | { |
| 988 | TEST_EQUAL( status, expected_status ); |
| 989 | break; |
| 990 | } |
| 991 | else |
| 992 | { |
| 993 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 994 | } |
| 995 | |
| 996 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 997 | buffer0 + s_x2_pr_off, |
| 998 | s_x2_pr_len ); |
| 999 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 1000 | { |
| 1001 | TEST_EQUAL( status, expected_status ); |
| 1002 | break; |
| 1003 | } |
| 1004 | else |
| 1005 | { |
| 1006 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1007 | } |
| 1008 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1009 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1010 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1011 | 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] | 1012 | } |
| 1013 | |
| 1014 | if( inject_error == 2 ) |
| 1015 | { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 1016 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 1017 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1018 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1019 | } |
| 1020 | |
| 1021 | /* Server first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1022 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1023 | buffer1 + c_g1_off, c_g1_len ); |
| 1024 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1025 | { |
| 1026 | TEST_EQUAL( status, expected_status ); |
| 1027 | break; |
| 1028 | } |
| 1029 | else |
| 1030 | { |
| 1031 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1032 | } |
| 1033 | |
| 1034 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1035 | buffer1 + c_x1_pk_off, c_x1_pk_len ); |
| 1036 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1037 | { |
| 1038 | TEST_EQUAL( status, expected_status ); |
| 1039 | break; |
| 1040 | } |
| 1041 | else |
| 1042 | { |
| 1043 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1044 | } |
| 1045 | |
| 1046 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1047 | buffer1 + c_x1_pr_off, c_x1_pr_len ); |
| 1048 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1049 | { |
| 1050 | TEST_EQUAL( status, expected_status ); |
| 1051 | break; |
| 1052 | } |
| 1053 | else |
| 1054 | { |
| 1055 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1056 | } |
| 1057 | |
| 1058 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1059 | buffer1 + c_g2_off, c_g2_len ); |
| 1060 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1061 | { |
| 1062 | TEST_EQUAL( status, expected_status ); |
| 1063 | break; |
| 1064 | } |
| 1065 | else |
| 1066 | { |
| 1067 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1068 | } |
| 1069 | |
| 1070 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1071 | buffer1 + c_x2_pk_off, c_x2_pk_len ); |
| 1072 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1073 | { |
| 1074 | TEST_EQUAL( status, expected_status ); |
| 1075 | break; |
| 1076 | } |
| 1077 | else |
| 1078 | { |
| 1079 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1080 | } |
| 1081 | |
| 1082 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1083 | buffer1 + c_x2_pr_off, c_x2_pr_len ); |
| 1084 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1085 | { |
| 1086 | TEST_EQUAL( status, expected_status ); |
| 1087 | break; |
| 1088 | } |
| 1089 | else |
| 1090 | { |
| 1091 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1092 | } |
| 1093 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1094 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1095 | if( inject_error == 2 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1096 | 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] | 1097 | |
| 1098 | break; |
| 1099 | |
| 1100 | case 2: |
| 1101 | /* Server second round Output */ |
| 1102 | buffer0_off = 0; |
| 1103 | |
| 1104 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1105 | buffer0 + buffer0_off, |
| 1106 | 512 - buffer0_off, &s_a_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1107 | TEST_EQUAL( s_a_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1108 | s_a_off = buffer0_off; |
| 1109 | buffer0_off += s_a_len; |
| 1110 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1111 | buffer0 + buffer0_off, |
| 1112 | 512 - buffer0_off, &s_x2s_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1113 | TEST_EQUAL( s_x2s_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1114 | s_x2s_pk_off = buffer0_off; |
| 1115 | buffer0_off += s_x2s_pk_len; |
| 1116 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1117 | buffer0 + buffer0_off, |
| 1118 | 512 - buffer0_off, &s_x2s_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1119 | TEST_LE_U( s_x2s_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1120 | s_x2s_pr_off = buffer0_off; |
| 1121 | buffer0_off += s_x2s_pr_len; |
| 1122 | |
| 1123 | if( inject_error == 3 ) |
| 1124 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1125 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1126 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1127 | } |
| 1128 | |
| 1129 | if( client_input_first == 1 ) |
| 1130 | { |
| 1131 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1132 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1133 | buffer0 + s_a_off, s_a_len ); |
| 1134 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1135 | { |
| 1136 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1137 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1138 | } |
| 1139 | else |
| 1140 | { |
| 1141 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1142 | } |
| 1143 | |
| 1144 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1145 | buffer0 + s_x2s_pk_off, |
| 1146 | s_x2s_pk_len ); |
| 1147 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1148 | { |
| 1149 | TEST_EQUAL( status, expected_status ); |
| 1150 | break; |
| 1151 | } |
| 1152 | else |
| 1153 | { |
| 1154 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1155 | } |
| 1156 | |
| 1157 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1158 | buffer0 + s_x2s_pr_off, |
| 1159 | s_x2s_pr_len ); |
| 1160 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1161 | { |
| 1162 | TEST_EQUAL( status, expected_status ); |
| 1163 | break; |
| 1164 | } |
| 1165 | else |
| 1166 | { |
| 1167 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1168 | } |
| 1169 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1170 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1171 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1172 | 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] | 1173 | } |
| 1174 | |
| 1175 | /* Client second round Output */ |
| 1176 | buffer1_off = 0; |
| 1177 | |
| 1178 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1179 | buffer1 + buffer1_off, |
| 1180 | 512 - buffer1_off, &c_a_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1181 | TEST_EQUAL( c_a_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1182 | c_a_off = buffer1_off; |
| 1183 | buffer1_off += c_a_len; |
| 1184 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1185 | buffer1 + buffer1_off, |
| 1186 | 512 - buffer1_off, &c_x2s_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1187 | TEST_EQUAL( c_x2s_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1188 | c_x2s_pk_off = buffer1_off; |
| 1189 | buffer1_off += c_x2s_pk_len; |
| 1190 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1191 | buffer1 + buffer1_off, |
| 1192 | 512 - buffer1_off, &c_x2s_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1193 | TEST_LE_U( c_x2s_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1194 | c_x2s_pr_off = buffer1_off; |
| 1195 | buffer1_off += c_x2s_pr_len; |
| 1196 | |
| 1197 | if( client_input_first == 0 ) |
| 1198 | { |
| 1199 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1200 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1201 | buffer0 + s_a_off, s_a_len ); |
| 1202 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1203 | { |
| 1204 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1205 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1206 | } |
| 1207 | else |
| 1208 | { |
| 1209 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1210 | } |
| 1211 | |
| 1212 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1213 | buffer0 + s_x2s_pk_off, |
| 1214 | s_x2s_pk_len ); |
| 1215 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1216 | { |
| 1217 | TEST_EQUAL( status, expected_status ); |
| 1218 | break; |
| 1219 | } |
| 1220 | else |
| 1221 | { |
| 1222 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1223 | } |
| 1224 | |
| 1225 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1226 | buffer0 + s_x2s_pr_off, |
| 1227 | s_x2s_pr_len ); |
| 1228 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1229 | { |
| 1230 | TEST_EQUAL( status, expected_status ); |
| 1231 | break; |
| 1232 | } |
| 1233 | else |
| 1234 | { |
| 1235 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1236 | } |
| 1237 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1238 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1239 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1240 | 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] | 1241 | } |
| 1242 | |
| 1243 | if( inject_error == 4 ) |
| 1244 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1245 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1246 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1247 | } |
| 1248 | |
| 1249 | /* Server second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1250 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1251 | buffer1 + c_a_off, c_a_len ); |
| 1252 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1253 | { |
| 1254 | TEST_EQUAL( status, expected_status ); |
| 1255 | break; |
| 1256 | } |
| 1257 | else |
| 1258 | { |
| 1259 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1260 | } |
| 1261 | |
| 1262 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1263 | buffer1 + c_x2s_pk_off, c_x2s_pk_len ); |
| 1264 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1265 | { |
| 1266 | TEST_EQUAL( status, expected_status ); |
| 1267 | break; |
| 1268 | } |
| 1269 | else |
| 1270 | { |
| 1271 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1272 | } |
| 1273 | |
| 1274 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1275 | buffer1 + c_x2s_pr_off, c_x2s_pr_len ); |
| 1276 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1277 | { |
| 1278 | TEST_EQUAL( status, expected_status ); |
| 1279 | break; |
| 1280 | } |
| 1281 | else |
| 1282 | { |
| 1283 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1284 | } |
| 1285 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1286 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1287 | if( inject_error == 4 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1288 | 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] | 1289 | |
| 1290 | break; |
| 1291 | |
| 1292 | } |
| 1293 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1294 | exit: |
| 1295 | mbedtls_free( buffer0 ); |
| 1296 | mbedtls_free( buffer1 ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1297 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1298 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1299 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1300 | /* END_HEADER */ |
| 1301 | |
| 1302 | /* BEGIN_DEPENDENCIES |
| 1303 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1304 | * END_DEPENDENCIES |
| 1305 | */ |
| 1306 | |
| 1307 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1308 | void static_checks( ) |
| 1309 | { |
| 1310 | size_t max_truncated_mac_size = |
| 1311 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1312 | |
| 1313 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1314 | * encoding. The shifted mask is the maximum truncated value. The |
| 1315 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1316 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1317 | } |
| 1318 | /* END_CASE */ |
| 1319 | |
| 1320 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1321 | void import_with_policy( int type_arg, |
| 1322 | int usage_arg, int alg_arg, |
| 1323 | int expected_status_arg ) |
| 1324 | { |
| 1325 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1326 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1327 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1328 | psa_key_type_t type = type_arg; |
| 1329 | psa_key_usage_t usage = usage_arg; |
| 1330 | psa_algorithm_t alg = alg_arg; |
| 1331 | psa_status_t expected_status = expected_status_arg; |
| 1332 | const uint8_t key_material[16] = {0}; |
| 1333 | psa_status_t status; |
| 1334 | |
| 1335 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1336 | |
| 1337 | psa_set_key_type( &attributes, type ); |
| 1338 | psa_set_key_usage_flags( &attributes, usage ); |
| 1339 | psa_set_key_algorithm( &attributes, alg ); |
| 1340 | |
| 1341 | status = psa_import_key( &attributes, |
| 1342 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1343 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1344 | TEST_EQUAL( status, expected_status ); |
| 1345 | if( status != PSA_SUCCESS ) |
| 1346 | goto exit; |
| 1347 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1348 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1349 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1350 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1351 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1352 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1353 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1354 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1355 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1356 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1357 | |
| 1358 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1359 | /* |
| 1360 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1361 | * thus reset them as required. |
| 1362 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1363 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1364 | |
| 1365 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1366 | PSA_DONE( ); |
| 1367 | } |
| 1368 | /* END_CASE */ |
| 1369 | |
| 1370 | /* BEGIN_CASE */ |
| 1371 | void import_with_data( data_t *data, int type_arg, |
| 1372 | int attr_bits_arg, |
| 1373 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1374 | { |
| 1375 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1376 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1377 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1378 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1379 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1380 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1381 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1382 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1383 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1384 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1385 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1386 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1387 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1388 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1389 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1390 | if( status != PSA_SUCCESS ) |
| 1391 | goto exit; |
| 1392 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1393 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1394 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1395 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1396 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1397 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1398 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1399 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1400 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1401 | |
| 1402 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1403 | /* |
| 1404 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1405 | * thus reset them as required. |
| 1406 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1407 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1408 | |
| 1409 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1410 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1411 | } |
| 1412 | /* END_CASE */ |
| 1413 | |
| 1414 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame^] | 1415 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1416 | void import_large_key( int type_arg, int byte_size_arg, |
| 1417 | int expected_status_arg ) |
| 1418 | { |
| 1419 | psa_key_type_t type = type_arg; |
| 1420 | size_t byte_size = byte_size_arg; |
| 1421 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1422 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1423 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1424 | psa_status_t status; |
| 1425 | uint8_t *buffer = NULL; |
| 1426 | size_t buffer_size = byte_size + 1; |
| 1427 | size_t n; |
| 1428 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1429 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1430 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1431 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1432 | memset( buffer, 'K', byte_size ); |
| 1433 | |
| 1434 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1435 | |
| 1436 | /* Try importing the key */ |
| 1437 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1438 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1439 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1440 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1441 | TEST_EQUAL( status, expected_status ); |
| 1442 | |
| 1443 | if( status == PSA_SUCCESS ) |
| 1444 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1445 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1446 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1447 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1448 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1449 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1450 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1451 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1452 | for( n = 0; n < byte_size; n++ ) |
| 1453 | TEST_EQUAL( buffer[n], 'K' ); |
| 1454 | for( n = byte_size; n < buffer_size; n++ ) |
| 1455 | TEST_EQUAL( buffer[n], 0 ); |
| 1456 | } |
| 1457 | |
| 1458 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1459 | /* |
| 1460 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1461 | * thus reset them as required. |
| 1462 | */ |
| 1463 | psa_reset_key_attributes( &attributes ); |
| 1464 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1465 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1466 | PSA_DONE( ); |
| 1467 | mbedtls_free( buffer ); |
| 1468 | } |
| 1469 | /* END_CASE */ |
| 1470 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1471 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame^] | 1472 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1473 | * inside, beyond having sensible size and parity). This is expected to |
| 1474 | * fail for large keys. */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1475 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1476 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1477 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1478 | size_t bits = bits_arg; |
| 1479 | psa_status_t expected_status = expected_status_arg; |
| 1480 | psa_status_t status; |
| 1481 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1482 | 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] | 1483 | size_t buffer_size = /* Slight overapproximations */ |
| 1484 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1485 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1486 | unsigned char *p; |
| 1487 | int ret; |
| 1488 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1489 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1490 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1491 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1492 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1493 | |
| 1494 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1495 | bits, keypair ) ) >= 0 ); |
| 1496 | length = ret; |
| 1497 | |
| 1498 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1499 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1500 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1501 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1502 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1503 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1504 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1505 | |
| 1506 | exit: |
| 1507 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1508 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1509 | } |
| 1510 | /* END_CASE */ |
| 1511 | |
| 1512 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1513 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1514 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1515 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1516 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1517 | int expected_bits, |
| 1518 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1519 | int expected_export_status_arg, |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame^] | 1520 | /*whether reexport must give the original input exactly*/ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1521 | int canonical_input ) |
| 1522 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1523 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1524 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1525 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1526 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1527 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1528 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1529 | unsigned char *exported = NULL; |
| 1530 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1531 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1532 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1533 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1534 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1535 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1536 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1537 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1538 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1539 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1540 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1541 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1542 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1543 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1544 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1545 | psa_set_key_algorithm( &attributes, alg ); |
| 1546 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1547 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1548 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1549 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1550 | |
| 1551 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1552 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1553 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1554 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1555 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1556 | |
| 1557 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1558 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1559 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1560 | |
| 1561 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1562 | * and export_size. On errors, the exported length must be 0. */ |
| 1563 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1564 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1565 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1566 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1567 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1568 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1569 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1570 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1571 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1572 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1573 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1574 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1575 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1576 | * this validates the canonical representations. For canonical inputs, |
| 1577 | * this doesn't directly validate the implementation, but it still helps |
| 1578 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1579 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1580 | { |
| 1581 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1582 | goto exit; |
| 1583 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1584 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1585 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1586 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1587 | else |
| 1588 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1589 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1590 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1591 | &key2 ) ); |
| 1592 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1593 | reexported, |
| 1594 | export_size, |
| 1595 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1596 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1597 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1598 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1599 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1600 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1601 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1602 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1603 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1604 | |
| 1605 | destroy: |
| 1606 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1607 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1608 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1609 | |
| 1610 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1611 | /* |
| 1612 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1613 | * thus reset them as required. |
| 1614 | */ |
| 1615 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1616 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1617 | mbedtls_free( exported ); |
| 1618 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1619 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1620 | } |
| 1621 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1622 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1623 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1624 | void import_export_public_key( data_t *data, |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame^] | 1625 | int type_arg, // key pair or public key |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1626 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1627 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1628 | int export_size_delta, |
| 1629 | int expected_export_status_arg, |
| 1630 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1631 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1632 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1633 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1634 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1635 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1636 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1637 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1638 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1639 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1640 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1641 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1642 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1643 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1644 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1645 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1646 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1647 | psa_set_key_algorithm( &attributes, alg ); |
| 1648 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1649 | |
| 1650 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1651 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1652 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1653 | /* Export the public key */ |
| 1654 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1655 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1656 | exported, export_size, |
| 1657 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1658 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1659 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1660 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1661 | 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] | 1662 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1663 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1664 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1665 | TEST_LE_U( expected_public_key->len, |
| 1666 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1667 | TEST_LE_U( expected_public_key->len, |
| 1668 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1669 | TEST_LE_U( expected_public_key->len, |
| 1670 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1671 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1672 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1673 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1674 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1675 | /* |
| 1676 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1677 | * thus reset them as required. |
| 1678 | */ |
| 1679 | psa_reset_key_attributes( &attributes ); |
| 1680 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1681 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1682 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1683 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1684 | } |
| 1685 | /* END_CASE */ |
| 1686 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1687 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1688 | void import_and_exercise_key( data_t *data, |
| 1689 | int type_arg, |
| 1690 | int bits_arg, |
| 1691 | int alg_arg ) |
| 1692 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1693 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1694 | psa_key_type_t type = type_arg; |
| 1695 | size_t bits = bits_arg; |
| 1696 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1697 | 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] | 1698 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1699 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1700 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1701 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1702 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1703 | psa_set_key_usage_flags( &attributes, usage ); |
| 1704 | psa_set_key_algorithm( &attributes, alg ); |
| 1705 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1706 | |
| 1707 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1708 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1709 | |
| 1710 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1711 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1712 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1713 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1714 | |
| 1715 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1716 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1717 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1718 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1719 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1720 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1721 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1722 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1723 | /* |
| 1724 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1725 | * thus reset them as required. |
| 1726 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1727 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1728 | |
| 1729 | psa_reset_key_attributes( &attributes ); |
| 1730 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1731 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1732 | } |
| 1733 | /* END_CASE */ |
| 1734 | |
| 1735 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1736 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1737 | int bits_arg, int expected_bits_arg, |
| 1738 | int usage_arg, int expected_usage_arg, |
| 1739 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1740 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1741 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1742 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1743 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1744 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1745 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1746 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1747 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1748 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1749 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1750 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1751 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1752 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1753 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1754 | psa_set_key_usage_flags( &attributes, usage ); |
| 1755 | psa_set_key_algorithm( &attributes, alg ); |
| 1756 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1757 | psa_set_key_bits( &attributes, bits ); |
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 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1760 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1761 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1762 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1763 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1764 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1765 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1766 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1767 | |
| 1768 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1769 | /* |
| 1770 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1771 | * thus reset them as required. |
| 1772 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1773 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1774 | |
| 1775 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1776 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1777 | } |
| 1778 | /* END_CASE */ |
| 1779 | |
| 1780 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1781 | void check_key_policy( int type_arg, int bits_arg, |
| 1782 | int usage_arg, int alg_arg ) |
| 1783 | { |
| 1784 | 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] | 1785 | usage_arg, |
| 1786 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1787 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1788 | goto exit; |
| 1789 | } |
| 1790 | /* END_CASE */ |
| 1791 | |
| 1792 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1793 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1794 | { |
| 1795 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1796 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1797 | * 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] | 1798 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1799 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1800 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1801 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1802 | |
| 1803 | memset( &zero, 0, sizeof( zero ) ); |
| 1804 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1805 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1806 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1807 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1808 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1809 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1810 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1811 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1812 | |
| 1813 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1814 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1815 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1816 | |
| 1817 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1818 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1819 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1820 | |
| 1821 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1822 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1823 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1824 | } |
| 1825 | /* END_CASE */ |
| 1826 | |
| 1827 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1828 | void mac_key_policy( int policy_usage_arg, |
| 1829 | int policy_alg_arg, |
| 1830 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1831 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1832 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1833 | int expected_status_sign_arg, |
| 1834 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1835 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1836 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1837 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1838 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1839 | psa_key_type_t key_type = key_type_arg; |
| 1840 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1841 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1842 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1843 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1844 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1845 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1846 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1847 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1848 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1849 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1850 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1851 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1852 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1853 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1854 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1855 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1856 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1857 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1858 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1859 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1860 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1861 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1862 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1863 | /* Calculate the MAC, one-shot case. */ |
| 1864 | uint8_t input[128] = {0}; |
| 1865 | size_t mac_len; |
| 1866 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1867 | input, 128, |
| 1868 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1869 | expected_status_sign ); |
| 1870 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1871 | /* Calculate the MAC, multi-part case. */ |
| 1872 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1873 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1874 | if( status == PSA_SUCCESS ) |
| 1875 | { |
| 1876 | status = psa_mac_update( &operation, input, 128 ); |
| 1877 | if( status == PSA_SUCCESS ) |
| 1878 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1879 | &mac_len ), |
| 1880 | expected_status_sign ); |
| 1881 | else |
| 1882 | TEST_EQUAL( status, expected_status_sign ); |
| 1883 | } |
| 1884 | else |
| 1885 | { |
| 1886 | TEST_EQUAL( status, expected_status_sign ); |
| 1887 | } |
| 1888 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1889 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1890 | /* Verify correct MAC, one-shot case. */ |
| 1891 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1892 | mac, mac_len ); |
| 1893 | |
| 1894 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1895 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1896 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1897 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1898 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1899 | /* Verify correct MAC, multi-part case. */ |
| 1900 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1901 | if( status == PSA_SUCCESS ) |
| 1902 | { |
| 1903 | status = psa_mac_update( &operation, input, 128 ); |
| 1904 | if( status == PSA_SUCCESS ) |
| 1905 | { |
| 1906 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1907 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1908 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1909 | else |
| 1910 | TEST_EQUAL( status, expected_status_verify ); |
| 1911 | } |
| 1912 | else |
| 1913 | { |
| 1914 | TEST_EQUAL( status, expected_status_verify ); |
| 1915 | } |
| 1916 | } |
| 1917 | else |
| 1918 | { |
| 1919 | TEST_EQUAL( status, expected_status_verify ); |
| 1920 | } |
| 1921 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1922 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1923 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1924 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1925 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1926 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1927 | |
| 1928 | exit: |
| 1929 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1930 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1931 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1932 | } |
| 1933 | /* END_CASE */ |
| 1934 | |
| 1935 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1936 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1937 | int policy_alg, |
| 1938 | int key_type, |
| 1939 | data_t *key_data, |
| 1940 | int exercise_alg ) |
| 1941 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1942 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1943 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1944 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1945 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1946 | size_t output_buffer_size = 0; |
| 1947 | size_t input_buffer_size = 0; |
| 1948 | size_t output_length = 0; |
| 1949 | uint8_t *output = NULL; |
| 1950 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1951 | psa_status_t status; |
| 1952 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1953 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1954 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1955 | input_buffer_size ); |
| 1956 | |
| 1957 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1958 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1959 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1960 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1961 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1962 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1963 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1964 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1965 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1966 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1967 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1968 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1969 | /* Check if no key usage flag implication is done */ |
| 1970 | TEST_EQUAL( policy_usage, |
| 1971 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1972 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1973 | /* Encrypt check, one-shot */ |
| 1974 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1975 | output, output_buffer_size, |
| 1976 | &output_length); |
| 1977 | if( policy_alg == exercise_alg && |
| 1978 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1979 | PSA_ASSERT( status ); |
| 1980 | else |
| 1981 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1982 | |
| 1983 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1984 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1985 | if( policy_alg == exercise_alg && |
| 1986 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1987 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1988 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1989 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1990 | psa_cipher_abort( &operation ); |
| 1991 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1992 | /* Decrypt check, one-shot */ |
| 1993 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 1994 | input, input_buffer_size, |
| 1995 | &output_length); |
| 1996 | if( policy_alg == exercise_alg && |
| 1997 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1998 | PSA_ASSERT( status ); |
| 1999 | else |
| 2000 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2001 | |
| 2002 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2003 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2004 | if( policy_alg == exercise_alg && |
| 2005 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2006 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2007 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2008 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2009 | |
| 2010 | exit: |
| 2011 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2012 | mbedtls_free( input ); |
| 2013 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2014 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2015 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2016 | } |
| 2017 | /* END_CASE */ |
| 2018 | |
| 2019 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2020 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2021 | int policy_alg, |
| 2022 | int key_type, |
| 2023 | data_t *key_data, |
| 2024 | int nonce_length_arg, |
| 2025 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2026 | int exercise_alg, |
| 2027 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2028 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2029 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2030 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2031 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2032 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2033 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2034 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2035 | unsigned char nonce[16] = {0}; |
| 2036 | size_t nonce_length = nonce_length_arg; |
| 2037 | unsigned char tag[16]; |
| 2038 | size_t tag_length = tag_length_arg; |
| 2039 | size_t output_length; |
| 2040 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2041 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 2042 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2043 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2044 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2045 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2046 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2047 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2048 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2049 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2050 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2051 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2052 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2053 | /* Check if no key usage implication is done */ |
| 2054 | TEST_EQUAL( policy_usage, |
| 2055 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2056 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2057 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2058 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2059 | nonce, nonce_length, |
| 2060 | NULL, 0, |
| 2061 | NULL, 0, |
| 2062 | tag, tag_length, |
| 2063 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2064 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2065 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2066 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2067 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2068 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2069 | /* Encrypt check, multi-part */ |
| 2070 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 2071 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2072 | TEST_EQUAL( status, expected_status ); |
| 2073 | else |
| 2074 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2075 | |
| 2076 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2077 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2078 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2079 | nonce, nonce_length, |
| 2080 | NULL, 0, |
| 2081 | tag, tag_length, |
| 2082 | NULL, 0, |
| 2083 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2084 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2085 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2086 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2087 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2088 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2089 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2090 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2091 | /* Decrypt check, multi-part */ |
| 2092 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 2093 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 2094 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2095 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2096 | else |
| 2097 | TEST_EQUAL( status, expected_status ); |
| 2098 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2099 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2100 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2101 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2102 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2103 | } |
| 2104 | /* END_CASE */ |
| 2105 | |
| 2106 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2107 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2108 | int policy_alg, |
| 2109 | int key_type, |
| 2110 | data_t *key_data, |
| 2111 | int exercise_alg ) |
| 2112 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2113 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2114 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2115 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2116 | psa_status_t status; |
| 2117 | size_t key_bits; |
| 2118 | size_t buffer_length; |
| 2119 | unsigned char *buffer = NULL; |
| 2120 | size_t output_length; |
| 2121 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2122 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2123 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2124 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2125 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2126 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2127 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2128 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2129 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2130 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2131 | /* Check if no key usage implication is done */ |
| 2132 | TEST_EQUAL( policy_usage, |
| 2133 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2134 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2135 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2136 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2137 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2138 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2139 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2140 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2141 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2142 | NULL, 0, |
| 2143 | NULL, 0, |
| 2144 | buffer, buffer_length, |
| 2145 | &output_length ); |
| 2146 | if( policy_alg == exercise_alg && |
| 2147 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2148 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2149 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2150 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2151 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2152 | if( buffer_length != 0 ) |
| 2153 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2154 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2155 | buffer, buffer_length, |
| 2156 | NULL, 0, |
| 2157 | buffer, buffer_length, |
| 2158 | &output_length ); |
| 2159 | if( policy_alg == exercise_alg && |
| 2160 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2161 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2162 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2163 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2164 | |
| 2165 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2166 | /* |
| 2167 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2168 | * thus reset them as required. |
| 2169 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2170 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2171 | |
| 2172 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2173 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2174 | mbedtls_free( buffer ); |
| 2175 | } |
| 2176 | /* END_CASE */ |
| 2177 | |
| 2178 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2179 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2180 | int policy_alg, |
| 2181 | int key_type, |
| 2182 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2183 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2184 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2185 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2186 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2187 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2188 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2189 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2190 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2191 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2192 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2193 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2194 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2195 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2196 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2197 | int compatible_alg = payload_length_arg > 0; |
| 2198 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2199 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2200 | size_t signature_length; |
| 2201 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2202 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2203 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2204 | TEST_EQUAL( expected_usage, |
| 2205 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2206 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2207 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2208 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2209 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2210 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2211 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2212 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2213 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2214 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2215 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2216 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 2217 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2218 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2219 | payload, payload_length, |
| 2220 | signature, sizeof( signature ), |
| 2221 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2222 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2223 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2224 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2225 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2226 | |
| 2227 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2228 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2229 | payload, payload_length, |
| 2230 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2231 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2232 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2233 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2234 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2235 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2236 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 2237 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2238 | { |
| 2239 | status = psa_sign_message( key, exercise_alg, |
| 2240 | payload, payload_length, |
| 2241 | signature, sizeof( signature ), |
| 2242 | &signature_length ); |
| 2243 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 2244 | PSA_ASSERT( status ); |
| 2245 | else |
| 2246 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2247 | |
| 2248 | memset( signature, 0, sizeof( signature ) ); |
| 2249 | status = psa_verify_message( key, exercise_alg, |
| 2250 | payload, payload_length, |
| 2251 | signature, sizeof( signature ) ); |
| 2252 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 2253 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 2254 | else |
| 2255 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2256 | } |
| 2257 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2258 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2259 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2260 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2261 | } |
| 2262 | /* END_CASE */ |
| 2263 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2264 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2265 | void derive_key_policy( int policy_usage, |
| 2266 | int policy_alg, |
| 2267 | int key_type, |
| 2268 | data_t *key_data, |
| 2269 | int exercise_alg ) |
| 2270 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2271 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2272 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2273 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2274 | psa_status_t status; |
| 2275 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2276 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2277 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2278 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2279 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2280 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2281 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2282 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2283 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2284 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2285 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2286 | |
| 2287 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2288 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2289 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2290 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2291 | &operation, |
| 2292 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2293 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2294 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2295 | |
| 2296 | status = psa_key_derivation_input_key( &operation, |
| 2297 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2298 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2299 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2300 | if( policy_alg == exercise_alg && |
| 2301 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2302 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2303 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2304 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2305 | |
| 2306 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2307 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2308 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2309 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2310 | } |
| 2311 | /* END_CASE */ |
| 2312 | |
| 2313 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2314 | void agreement_key_policy( int policy_usage, |
| 2315 | int policy_alg, |
| 2316 | int key_type_arg, |
| 2317 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2318 | int exercise_alg, |
| 2319 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2320 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2321 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2322 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2323 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2324 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2325 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2326 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2327 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2328 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2329 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2330 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2331 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2332 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2333 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2334 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2335 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2336 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2337 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2338 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2339 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2340 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2341 | |
| 2342 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2343 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2344 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2345 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2346 | } |
| 2347 | /* END_CASE */ |
| 2348 | |
| 2349 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2350 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2351 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2352 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2353 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2354 | psa_key_type_t key_type = key_type_arg; |
| 2355 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2356 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2357 | psa_key_usage_t usage = usage_arg; |
| 2358 | psa_algorithm_t alg = alg_arg; |
| 2359 | psa_algorithm_t alg2 = alg2_arg; |
| 2360 | |
| 2361 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2362 | |
| 2363 | psa_set_key_usage_flags( &attributes, usage ); |
| 2364 | psa_set_key_algorithm( &attributes, alg ); |
| 2365 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2366 | psa_set_key_type( &attributes, key_type ); |
| 2367 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2368 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2369 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2370 | /* Update the usage flags to obtain implicit usage flags */ |
| 2371 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2372 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2373 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2374 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2375 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2376 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2377 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2378 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2379 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2380 | goto exit; |
| 2381 | |
| 2382 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2383 | /* |
| 2384 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2385 | * thus reset them as required. |
| 2386 | */ |
| 2387 | psa_reset_key_attributes( &got_attributes ); |
| 2388 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2389 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2390 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2391 | } |
| 2392 | /* END_CASE */ |
| 2393 | |
| 2394 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2395 | void raw_agreement_key_policy( int policy_usage, |
| 2396 | int policy_alg, |
| 2397 | int key_type_arg, |
| 2398 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2399 | int exercise_alg, |
| 2400 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2401 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2402 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2403 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2404 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2405 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2406 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2407 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2408 | |
| 2409 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2410 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2411 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2412 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2413 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2414 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2415 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2416 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2417 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2418 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2419 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2420 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2421 | |
| 2422 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2423 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2424 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2425 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2426 | } |
| 2427 | /* END_CASE */ |
| 2428 | |
| 2429 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2430 | void copy_success( int source_usage_arg, |
| 2431 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2432 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2433 | int type_arg, data_t *material, |
| 2434 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2435 | int target_usage_arg, |
| 2436 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2437 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2438 | int expected_usage_arg, |
| 2439 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2440 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2441 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2442 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2443 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2444 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2445 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2446 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2447 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2448 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2449 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2450 | uint8_t *export_buffer = NULL; |
| 2451 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2452 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2453 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2454 | /* Prepare the source key. */ |
| 2455 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2456 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2457 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2458 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2459 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2460 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2461 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2462 | &source_key ) ); |
| 2463 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2464 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2465 | /* Prepare the target attributes. */ |
| 2466 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2467 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2468 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2469 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2470 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2471 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2472 | if( target_usage_arg != -1 ) |
| 2473 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2474 | if( target_alg_arg != -1 ) |
| 2475 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2476 | if( target_alg2_arg != -1 ) |
| 2477 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2478 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2479 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2480 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2481 | PSA_ASSERT( psa_copy_key( source_key, |
| 2482 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2483 | |
| 2484 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2485 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2486 | |
| 2487 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2488 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2489 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2490 | psa_get_key_type( &target_attributes ) ); |
| 2491 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2492 | psa_get_key_bits( &target_attributes ) ); |
| 2493 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2494 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2495 | TEST_EQUAL( expected_alg2, |
| 2496 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2497 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2498 | { |
| 2499 | size_t length; |
| 2500 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2501 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2502 | material->len, &length ) ); |
| 2503 | ASSERT_COMPARE( material->x, material->len, |
| 2504 | export_buffer, length ); |
| 2505 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2506 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2507 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2508 | { |
| 2509 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2510 | goto exit; |
| 2511 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2512 | goto exit; |
| 2513 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2514 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2515 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2516 | |
| 2517 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2518 | /* |
| 2519 | * Source and target key attributes may have been returned by |
| 2520 | * psa_get_key_attributes() thus reset them as required. |
| 2521 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2522 | psa_reset_key_attributes( &source_attributes ); |
| 2523 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2524 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2525 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2526 | mbedtls_free( export_buffer ); |
| 2527 | } |
| 2528 | /* END_CASE */ |
| 2529 | |
| 2530 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2531 | void copy_fail( int source_usage_arg, |
| 2532 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2533 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2534 | int type_arg, data_t *material, |
| 2535 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2536 | int target_usage_arg, |
| 2537 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2538 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2539 | int expected_status_arg ) |
| 2540 | { |
| 2541 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2542 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2543 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2544 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2545 | 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] | 2546 | |
| 2547 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2548 | |
| 2549 | /* Prepare the source key. */ |
| 2550 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2551 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2552 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2553 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2554 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2555 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2556 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2557 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2558 | |
| 2559 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2560 | psa_set_key_id( &target_attributes, key_id ); |
| 2561 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2562 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2563 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2564 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2565 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2566 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2567 | |
| 2568 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2569 | TEST_EQUAL( psa_copy_key( source_key, |
| 2570 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2571 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2572 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2573 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2574 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2575 | exit: |
| 2576 | psa_reset_key_attributes( &source_attributes ); |
| 2577 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2578 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2579 | } |
| 2580 | /* END_CASE */ |
| 2581 | |
| 2582 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2583 | void hash_operation_init( ) |
| 2584 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2585 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2586 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2587 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2588 | * 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] | 2589 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2590 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2591 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2592 | psa_hash_operation_t zero; |
| 2593 | |
| 2594 | memset( &zero, 0, sizeof( zero ) ); |
| 2595 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2596 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2597 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2598 | PSA_ERROR_BAD_STATE ); |
| 2599 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2600 | PSA_ERROR_BAD_STATE ); |
| 2601 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2602 | PSA_ERROR_BAD_STATE ); |
| 2603 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2604 | /* A default hash operation should be abortable without error. */ |
| 2605 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2606 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2607 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2608 | } |
| 2609 | /* END_CASE */ |
| 2610 | |
| 2611 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2612 | void hash_setup( int alg_arg, |
| 2613 | int expected_status_arg ) |
| 2614 | { |
| 2615 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2616 | uint8_t *output = NULL; |
| 2617 | size_t output_size = 0; |
| 2618 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2619 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2620 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2621 | psa_status_t status; |
| 2622 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2623 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2624 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2625 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2626 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2627 | ASSERT_ALLOC( output, output_size ); |
| 2628 | |
| 2629 | status = psa_hash_compute( alg, NULL, 0, |
| 2630 | output, output_size, &output_length ); |
| 2631 | TEST_EQUAL( status, expected_status ); |
| 2632 | |
| 2633 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2634 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2635 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2636 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2637 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2638 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2639 | |
| 2640 | /* If setup failed, reproduce the failure, so as to |
| 2641 | * test the resulting state of the operation object. */ |
| 2642 | if( status != PSA_SUCCESS ) |
| 2643 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2644 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2645 | /* Now the operation object should be reusable. */ |
| 2646 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2647 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2648 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2649 | #endif |
| 2650 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2651 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2652 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2653 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2654 | } |
| 2655 | /* END_CASE */ |
| 2656 | |
| 2657 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2658 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2659 | int output_size_arg, int expected_status_arg ) |
| 2660 | { |
| 2661 | psa_algorithm_t alg = alg_arg; |
| 2662 | uint8_t *output = NULL; |
| 2663 | size_t output_size = output_size_arg; |
| 2664 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2665 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2666 | psa_status_t expected_status = expected_status_arg; |
| 2667 | psa_status_t status; |
| 2668 | |
| 2669 | ASSERT_ALLOC( output, output_size ); |
| 2670 | |
| 2671 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2672 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2673 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2674 | status = psa_hash_compute( alg, input->x, input->len, |
| 2675 | output, output_size, &output_length ); |
| 2676 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2677 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2678 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2679 | /* Hash Compute, multi-part */ |
| 2680 | status = psa_hash_setup( &operation, alg ); |
| 2681 | if( status == PSA_SUCCESS ) |
| 2682 | { |
| 2683 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2684 | if( status == PSA_SUCCESS ) |
| 2685 | { |
| 2686 | status = psa_hash_finish( &operation, output, output_size, |
| 2687 | &output_length ); |
| 2688 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2689 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2690 | else |
| 2691 | TEST_EQUAL( status, expected_status ); |
| 2692 | } |
| 2693 | else |
| 2694 | { |
| 2695 | TEST_EQUAL( status, expected_status ); |
| 2696 | } |
| 2697 | } |
| 2698 | else |
| 2699 | { |
| 2700 | TEST_EQUAL( status, expected_status ); |
| 2701 | } |
| 2702 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2703 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2704 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2705 | mbedtls_free( output ); |
| 2706 | PSA_DONE( ); |
| 2707 | } |
| 2708 | /* END_CASE */ |
| 2709 | |
| 2710 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2711 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2712 | data_t *reference_hash, |
| 2713 | int expected_status_arg ) |
| 2714 | { |
| 2715 | psa_algorithm_t alg = alg_arg; |
| 2716 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2717 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2718 | psa_status_t status; |
| 2719 | |
| 2720 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2721 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2722 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2723 | status = psa_hash_compare( alg, input->x, input->len, |
| 2724 | reference_hash->x, reference_hash->len ); |
| 2725 | TEST_EQUAL( status, expected_status ); |
| 2726 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2727 | /* Hash Compare, multi-part */ |
| 2728 | status = psa_hash_setup( &operation, alg ); |
| 2729 | if( status == PSA_SUCCESS ) |
| 2730 | { |
| 2731 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2732 | if( status == PSA_SUCCESS ) |
| 2733 | { |
| 2734 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2735 | reference_hash->len ); |
| 2736 | TEST_EQUAL( status, expected_status ); |
| 2737 | } |
| 2738 | else |
| 2739 | { |
| 2740 | TEST_EQUAL( status, expected_status ); |
| 2741 | } |
| 2742 | } |
| 2743 | else |
| 2744 | { |
| 2745 | TEST_EQUAL( status, expected_status ); |
| 2746 | } |
| 2747 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2748 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2749 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2750 | PSA_DONE( ); |
| 2751 | } |
| 2752 | /* END_CASE */ |
| 2753 | |
| 2754 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2755 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2756 | data_t *expected_output ) |
| 2757 | { |
| 2758 | psa_algorithm_t alg = alg_arg; |
| 2759 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2760 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2761 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2762 | size_t i; |
| 2763 | |
| 2764 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2765 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2766 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2767 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2768 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2769 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2770 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2771 | ASSERT_COMPARE( output, output_length, |
| 2772 | expected_output->x, expected_output->len ); |
| 2773 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2774 | /* Compute with tight buffer, multi-part */ |
| 2775 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2776 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2777 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2778 | PSA_HASH_LENGTH( alg ), |
| 2779 | &output_length ) ); |
| 2780 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2781 | ASSERT_COMPARE( output, output_length, |
| 2782 | expected_output->x, expected_output->len ); |
| 2783 | |
| 2784 | /* Compute with larger 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, |
| 2786 | output, sizeof( output ), |
| 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 larger 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 | sizeof( output ), &output_length ) ); |
| 2797 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2798 | ASSERT_COMPARE( output, output_length, |
| 2799 | expected_output->x, expected_output->len ); |
| 2800 | |
| 2801 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2802 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2803 | output, output_length ) ); |
| 2804 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2805 | /* Compare with correct hash, multi-part */ |
| 2806 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2807 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2808 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2809 | output_length ) ); |
| 2810 | |
| 2811 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2812 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2813 | output, output_length + 1 ), |
| 2814 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2815 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2816 | /* Compare with trailing garbage, multi-part */ |
| 2817 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2818 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2819 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2820 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2821 | |
| 2822 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2823 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2824 | output, output_length - 1 ), |
| 2825 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2826 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2827 | /* Compare with truncated hash, multi-part */ |
| 2828 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2829 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2830 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2831 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2832 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2833 | /* Compare with corrupted value */ |
| 2834 | for( i = 0; i < output_length; i++ ) |
| 2835 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2836 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2837 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2838 | |
| 2839 | /* One-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2840 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2841 | output, output_length ), |
| 2842 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2843 | |
| 2844 | /* Multi-Part */ |
| 2845 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2846 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2847 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length ), |
| 2848 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2849 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2850 | output[i] ^= 1; |
| 2851 | } |
| 2852 | |
| 2853 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2854 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2855 | PSA_DONE( ); |
| 2856 | } |
| 2857 | /* END_CASE */ |
| 2858 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2859 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2860 | void hash_bad_order( ) |
| 2861 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2862 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2863 | unsigned char input[] = ""; |
| 2864 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2865 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2866 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2867 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2868 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2869 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2870 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2871 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2872 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2873 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2874 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2875 | /* Call setup twice in a row. */ |
| 2876 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2877 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2878 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2879 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2880 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2881 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2882 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2883 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2884 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2885 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2886 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2887 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2888 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2889 | /* Check that update calls abort on error. */ |
| 2890 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2891 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2892 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2893 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2894 | PSA_ERROR_BAD_STATE ); |
| 2895 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2896 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2897 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2898 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2899 | /* Call update after finish. */ |
| 2900 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2901 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2902 | hash, sizeof( hash ), &hash_len ) ); |
| 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 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2907 | /* Call verify without calling setup beforehand. */ |
| 2908 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2909 | valid_hash, sizeof( valid_hash ) ), |
| 2910 | PSA_ERROR_BAD_STATE ); |
| 2911 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2912 | |
| 2913 | /* Call verify after finish. */ |
| 2914 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2915 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2916 | hash, sizeof( hash ), &hash_len ) ); |
| 2917 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2918 | valid_hash, sizeof( valid_hash ) ), |
| 2919 | PSA_ERROR_BAD_STATE ); |
| 2920 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2921 | |
| 2922 | /* Call verify twice in a row. */ |
| 2923 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2924 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2925 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2926 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2927 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2928 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2929 | valid_hash, sizeof( valid_hash ) ), |
| 2930 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2931 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2932 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2933 | |
| 2934 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2935 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2936 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2937 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2938 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2939 | |
| 2940 | /* Call finish twice in a row. */ |
| 2941 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2942 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2943 | hash, sizeof( hash ), &hash_len ) ); |
| 2944 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2945 | hash, sizeof( hash ), &hash_len ), |
| 2946 | PSA_ERROR_BAD_STATE ); |
| 2947 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2948 | |
| 2949 | /* Call finish after calling verify. */ |
| 2950 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2951 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2952 | valid_hash, sizeof( valid_hash ) ) ); |
| 2953 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2954 | hash, sizeof( hash ), &hash_len ), |
| 2955 | PSA_ERROR_BAD_STATE ); |
| 2956 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2957 | |
| 2958 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2959 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2960 | } |
| 2961 | /* END_CASE */ |
| 2962 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2963 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2964 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2965 | { |
| 2966 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2967 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2968 | * appended to it */ |
| 2969 | unsigned char hash[] = { |
| 2970 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2971 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2972 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2973 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2974 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2975 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2976 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2977 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2978 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2979 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2980 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2981 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2982 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2983 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2984 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2985 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2986 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2987 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2988 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2989 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2990 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2991 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2992 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2993 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2994 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2995 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2996 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2997 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2998 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2999 | } |
| 3000 | /* END_CASE */ |
| 3001 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3002 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3003 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3004 | { |
| 3005 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3006 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3007 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3008 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3009 | size_t hash_len; |
| 3010 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3011 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3012 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3013 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3014 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3015 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3016 | hash, expected_size - 1, &hash_len ), |
| 3017 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3018 | |
| 3019 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3020 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3021 | } |
| 3022 | /* END_CASE */ |
| 3023 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3024 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3025 | void hash_clone_source_state( ) |
| 3026 | { |
| 3027 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3028 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3029 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3030 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3031 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3032 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3033 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3034 | size_t hash_len; |
| 3035 | |
| 3036 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3037 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 3038 | |
| 3039 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3040 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3041 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3042 | hash, sizeof( hash ), &hash_len ) ); |
| 3043 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3044 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3045 | |
| 3046 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 3047 | PSA_ERROR_BAD_STATE ); |
| 3048 | |
| 3049 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 3050 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 3051 | hash, sizeof( hash ), &hash_len ) ); |
| 3052 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 3053 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3054 | hash, sizeof( hash ), &hash_len ) ); |
| 3055 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 3056 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 3057 | hash, sizeof( hash ), &hash_len ) ); |
| 3058 | |
| 3059 | exit: |
| 3060 | psa_hash_abort( &op_source ); |
| 3061 | psa_hash_abort( &op_init ); |
| 3062 | psa_hash_abort( &op_setup ); |
| 3063 | psa_hash_abort( &op_finished ); |
| 3064 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3065 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3066 | } |
| 3067 | /* END_CASE */ |
| 3068 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3069 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3070 | void hash_clone_target_state( ) |
| 3071 | { |
| 3072 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3073 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3074 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3075 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3076 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3077 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3078 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3079 | size_t hash_len; |
| 3080 | |
| 3081 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3082 | |
| 3083 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3084 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3085 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3086 | hash, sizeof( hash ), &hash_len ) ); |
| 3087 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3088 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3089 | |
| 3090 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 3091 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 3092 | hash, sizeof( hash ), &hash_len ) ); |
| 3093 | |
| 3094 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 3095 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 3096 | PSA_ERROR_BAD_STATE ); |
| 3097 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 3098 | PSA_ERROR_BAD_STATE ); |
| 3099 | |
| 3100 | exit: |
| 3101 | psa_hash_abort( &op_target ); |
| 3102 | psa_hash_abort( &op_init ); |
| 3103 | psa_hash_abort( &op_setup ); |
| 3104 | psa_hash_abort( &op_finished ); |
| 3105 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3106 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3107 | } |
| 3108 | /* END_CASE */ |
| 3109 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3110 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3111 | void mac_operation_init( ) |
| 3112 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3113 | const uint8_t input[1] = { 0 }; |
| 3114 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3115 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3116 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3117 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3118 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3119 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 3120 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3121 | psa_mac_operation_t zero; |
| 3122 | |
| 3123 | memset( &zero, 0, sizeof( zero ) ); |
| 3124 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3125 | /* A freshly-initialized MAC operation should not be usable. */ |
| 3126 | TEST_EQUAL( psa_mac_update( &func, |
| 3127 | input, sizeof( input ) ), |
| 3128 | PSA_ERROR_BAD_STATE ); |
| 3129 | TEST_EQUAL( psa_mac_update( &init, |
| 3130 | input, sizeof( input ) ), |
| 3131 | PSA_ERROR_BAD_STATE ); |
| 3132 | TEST_EQUAL( psa_mac_update( &zero, |
| 3133 | input, sizeof( input ) ), |
| 3134 | PSA_ERROR_BAD_STATE ); |
| 3135 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3136 | /* A default MAC operation should be abortable without error. */ |
| 3137 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 3138 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 3139 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3140 | } |
| 3141 | /* END_CASE */ |
| 3142 | |
| 3143 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3144 | void mac_setup( int key_type_arg, |
| 3145 | data_t *key, |
| 3146 | int alg_arg, |
| 3147 | int expected_status_arg ) |
| 3148 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3149 | psa_key_type_t key_type = key_type_arg; |
| 3150 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3151 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3152 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3153 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3154 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3155 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3156 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3157 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3158 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3159 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3160 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 3161 | &operation, &status ) ) |
| 3162 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3163 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3164 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3165 | /* The operation object should be reusable. */ |
| 3166 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3167 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3168 | smoke_test_key_data, |
| 3169 | sizeof( smoke_test_key_data ), |
| 3170 | KNOWN_SUPPORTED_MAC_ALG, |
| 3171 | &operation, &status ) ) |
| 3172 | goto exit; |
| 3173 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3174 | #endif |
| 3175 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3176 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3177 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3178 | } |
| 3179 | /* END_CASE */ |
| 3180 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3181 | /* 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] | 3182 | void mac_bad_order( ) |
| 3183 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3184 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3185 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3186 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3187 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3188 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3189 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3190 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3191 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3192 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3193 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3194 | size_t sign_mac_length = 0; |
| 3195 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3196 | const uint8_t verify_mac[] = { |
| 3197 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3198 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 3199 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 3200 | |
| 3201 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3202 | 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] | 3203 | psa_set_key_algorithm( &attributes, alg ); |
| 3204 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3205 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3206 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3207 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3208 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3209 | /* Call update without calling setup beforehand. */ |
| 3210 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3211 | PSA_ERROR_BAD_STATE ); |
| 3212 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3213 | |
| 3214 | /* Call sign finish without calling setup beforehand. */ |
| 3215 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 3216 | &sign_mac_length), |
| 3217 | PSA_ERROR_BAD_STATE ); |
| 3218 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3219 | |
| 3220 | /* Call verify finish without calling setup beforehand. */ |
| 3221 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3222 | verify_mac, sizeof( verify_mac ) ), |
| 3223 | PSA_ERROR_BAD_STATE ); |
| 3224 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3225 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3226 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3227 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3228 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3229 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3230 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3231 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3232 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3233 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3234 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3235 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3236 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3237 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3238 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3239 | sign_mac, sizeof( sign_mac ), |
| 3240 | &sign_mac_length ) ); |
| 3241 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3242 | PSA_ERROR_BAD_STATE ); |
| 3243 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3244 | |
| 3245 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3246 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3247 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3248 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3249 | verify_mac, sizeof( verify_mac ) ) ); |
| 3250 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3251 | PSA_ERROR_BAD_STATE ); |
| 3252 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3253 | |
| 3254 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3255 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3256 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3257 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3258 | sign_mac, sizeof( sign_mac ), |
| 3259 | &sign_mac_length ) ); |
| 3260 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3261 | sign_mac, sizeof( sign_mac ), |
| 3262 | &sign_mac_length ), |
| 3263 | PSA_ERROR_BAD_STATE ); |
| 3264 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3265 | |
| 3266 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3267 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3268 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3269 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3270 | verify_mac, sizeof( verify_mac ) ) ); |
| 3271 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3272 | verify_mac, sizeof( verify_mac ) ), |
| 3273 | PSA_ERROR_BAD_STATE ); |
| 3274 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3275 | |
| 3276 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3277 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3278 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3279 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3280 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3281 | verify_mac, sizeof( verify_mac ) ), |
| 3282 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3283 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3284 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3285 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3286 | |
| 3287 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3288 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3289 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3290 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3291 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3292 | sign_mac, sizeof( sign_mac ), |
| 3293 | &sign_mac_length ), |
| 3294 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3295 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3296 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3297 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3298 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3299 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3300 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3301 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3302 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3303 | } |
| 3304 | /* END_CASE */ |
| 3305 | |
| 3306 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3307 | void mac_sign_verify_multi( int key_type_arg, |
| 3308 | data_t *key_data, |
| 3309 | int alg_arg, |
| 3310 | data_t *input, |
| 3311 | int is_verify, |
| 3312 | data_t *expected_mac ) |
| 3313 | { |
| 3314 | size_t data_part_len = 0; |
| 3315 | |
| 3316 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3317 | { |
| 3318 | /* Split data into length(data_part_len) parts. */ |
| 3319 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3320 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3321 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3322 | alg_arg, |
| 3323 | input, data_part_len, |
| 3324 | expected_mac, |
| 3325 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3326 | break; |
| 3327 | |
| 3328 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3329 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3330 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3331 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3332 | alg_arg, |
| 3333 | input, data_part_len, |
| 3334 | expected_mac, |
| 3335 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3336 | break; |
| 3337 | } |
| 3338 | |
| 3339 | /* Goto is required to silence warnings about unused labels, as we |
| 3340 | * don't actually do any test assertions in this function. */ |
| 3341 | goto exit; |
| 3342 | } |
| 3343 | /* END_CASE */ |
| 3344 | |
| 3345 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3346 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3347 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3348 | int alg_arg, |
| 3349 | data_t *input, |
| 3350 | data_t *expected_mac ) |
| 3351 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3352 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3353 | psa_key_type_t key_type = key_type_arg; |
| 3354 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3355 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3356 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3357 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3358 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3359 | 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] | 3360 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3361 | const size_t output_sizes_to_test[] = { |
| 3362 | 0, |
| 3363 | 1, |
| 3364 | expected_mac->len - 1, |
| 3365 | expected_mac->len, |
| 3366 | expected_mac->len + 1, |
| 3367 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3368 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3369 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3370 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3371 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3372 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3373 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3374 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3375 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3376 | psa_set_key_algorithm( &attributes, alg ); |
| 3377 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3378 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3379 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3380 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3381 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3382 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3383 | { |
| 3384 | const size_t output_size = output_sizes_to_test[i]; |
| 3385 | psa_status_t expected_status = |
| 3386 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3387 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3388 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3389 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3390 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3391 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3392 | /* Calculate the MAC, one-shot case. */ |
| 3393 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3394 | input->x, input->len, |
| 3395 | actual_mac, output_size, &mac_length ), |
| 3396 | expected_status ); |
| 3397 | if( expected_status == PSA_SUCCESS ) |
| 3398 | { |
| 3399 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3400 | actual_mac, mac_length ); |
| 3401 | } |
| 3402 | |
| 3403 | if( output_size > 0 ) |
| 3404 | memset( actual_mac, 0, output_size ); |
| 3405 | |
| 3406 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3407 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3408 | PSA_ASSERT( psa_mac_update( &operation, |
| 3409 | input->x, input->len ) ); |
| 3410 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3411 | actual_mac, output_size, |
| 3412 | &mac_length ), |
| 3413 | expected_status ); |
| 3414 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3415 | |
| 3416 | if( expected_status == PSA_SUCCESS ) |
| 3417 | { |
| 3418 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3419 | actual_mac, mac_length ); |
| 3420 | } |
| 3421 | mbedtls_free( actual_mac ); |
| 3422 | actual_mac = NULL; |
| 3423 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3424 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3425 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3426 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3427 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3428 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3429 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3430 | } |
| 3431 | /* END_CASE */ |
| 3432 | |
| 3433 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3434 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3435 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3436 | int alg_arg, |
| 3437 | data_t *input, |
| 3438 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3439 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3440 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3441 | psa_key_type_t key_type = key_type_arg; |
| 3442 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3443 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3444 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3445 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3446 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3447 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3448 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3449 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3450 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3451 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3452 | psa_set_key_algorithm( &attributes, alg ); |
| 3453 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3454 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3455 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3456 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3457 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3458 | /* Verify correct MAC, one-shot case. */ |
| 3459 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3460 | expected_mac->x, expected_mac->len ) ); |
| 3461 | |
| 3462 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3463 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3464 | PSA_ASSERT( psa_mac_update( &operation, |
| 3465 | input->x, input->len ) ); |
| 3466 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3467 | expected_mac->x, |
| 3468 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3469 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3470 | /* Test a MAC that's too short, one-shot case. */ |
| 3471 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3472 | input->x, input->len, |
| 3473 | expected_mac->x, |
| 3474 | expected_mac->len - 1 ), |
| 3475 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3476 | |
| 3477 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3478 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3479 | PSA_ASSERT( psa_mac_update( &operation, |
| 3480 | input->x, input->len ) ); |
| 3481 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3482 | expected_mac->x, |
| 3483 | expected_mac->len - 1 ), |
| 3484 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3485 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3486 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3487 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3488 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3489 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3490 | input->x, input->len, |
| 3491 | perturbed_mac, expected_mac->len + 1 ), |
| 3492 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3493 | |
| 3494 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3495 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3496 | PSA_ASSERT( psa_mac_update( &operation, |
| 3497 | input->x, input->len ) ); |
| 3498 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3499 | perturbed_mac, |
| 3500 | expected_mac->len + 1 ), |
| 3501 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3502 | |
| 3503 | /* Test changing one byte. */ |
| 3504 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3505 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3506 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3507 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3508 | |
| 3509 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3510 | input->x, input->len, |
| 3511 | perturbed_mac, expected_mac->len ), |
| 3512 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3513 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3514 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3515 | PSA_ASSERT( psa_mac_update( &operation, |
| 3516 | input->x, input->len ) ); |
| 3517 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3518 | perturbed_mac, |
| 3519 | expected_mac->len ), |
| 3520 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3521 | perturbed_mac[i] ^= 1; |
| 3522 | } |
| 3523 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3524 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3525 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3526 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3527 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3528 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3529 | } |
| 3530 | /* END_CASE */ |
| 3531 | |
| 3532 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3533 | void cipher_operation_init( ) |
| 3534 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3535 | const uint8_t input[1] = { 0 }; |
| 3536 | unsigned char output[1] = { 0 }; |
| 3537 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3538 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3539 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3540 | * 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] | 3541 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3542 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3543 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3544 | psa_cipher_operation_t zero; |
| 3545 | |
| 3546 | memset( &zero, 0, sizeof( zero ) ); |
| 3547 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3548 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3549 | TEST_EQUAL( psa_cipher_update( &func, |
| 3550 | input, sizeof( input ), |
| 3551 | output, sizeof( output ), |
| 3552 | &output_length ), |
| 3553 | PSA_ERROR_BAD_STATE ); |
| 3554 | TEST_EQUAL( psa_cipher_update( &init, |
| 3555 | input, sizeof( input ), |
| 3556 | output, sizeof( output ), |
| 3557 | &output_length ), |
| 3558 | PSA_ERROR_BAD_STATE ); |
| 3559 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3560 | input, sizeof( input ), |
| 3561 | output, sizeof( output ), |
| 3562 | &output_length ), |
| 3563 | PSA_ERROR_BAD_STATE ); |
| 3564 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3565 | /* A default cipher operation should be abortable without error. */ |
| 3566 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3567 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3568 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3569 | } |
| 3570 | /* END_CASE */ |
| 3571 | |
| 3572 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3573 | void cipher_setup( int key_type_arg, |
| 3574 | data_t *key, |
| 3575 | int alg_arg, |
| 3576 | int expected_status_arg ) |
| 3577 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3578 | psa_key_type_t key_type = key_type_arg; |
| 3579 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3580 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3581 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3582 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3583 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3584 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3585 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3586 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3587 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3588 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3589 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3590 | &operation, &status ) ) |
| 3591 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3592 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3593 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3594 | /* The operation object should be reusable. */ |
| 3595 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3596 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3597 | smoke_test_key_data, |
| 3598 | sizeof( smoke_test_key_data ), |
| 3599 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3600 | &operation, &status ) ) |
| 3601 | goto exit; |
| 3602 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3603 | #endif |
| 3604 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3605 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3606 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3607 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3608 | } |
| 3609 | /* END_CASE */ |
| 3610 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3611 | /* 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] | 3612 | void cipher_bad_order( ) |
| 3613 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3614 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3615 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3616 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3617 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3618 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3619 | 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] | 3620 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3621 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3622 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3623 | const uint8_t text[] = { |
| 3624 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3625 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3626 | 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] | 3627 | size_t length = 0; |
| 3628 | |
| 3629 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3630 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3631 | psa_set_key_algorithm( &attributes, alg ); |
| 3632 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3633 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3634 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3635 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3636 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3637 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3638 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3639 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3640 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3641 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3642 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3643 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3644 | |
| 3645 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3646 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3647 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3648 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3649 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3650 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3651 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3652 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3653 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3654 | /* Generate an IV without calling setup beforehand. */ |
| 3655 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3656 | buffer, sizeof( buffer ), |
| 3657 | &length ), |
| 3658 | PSA_ERROR_BAD_STATE ); |
| 3659 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3660 | |
| 3661 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3662 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3663 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3664 | buffer, sizeof( buffer ), |
| 3665 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3666 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3667 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3668 | buffer, sizeof( buffer ), |
| 3669 | &length ), |
| 3670 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3671 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3672 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3673 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3674 | |
| 3675 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3676 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3677 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3678 | iv, sizeof( iv ) ) ); |
| 3679 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3680 | buffer, sizeof( buffer ), |
| 3681 | &length ), |
| 3682 | PSA_ERROR_BAD_STATE ); |
| 3683 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3684 | |
| 3685 | /* Set an IV without calling setup beforehand. */ |
| 3686 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3687 | iv, sizeof( iv ) ), |
| 3688 | PSA_ERROR_BAD_STATE ); |
| 3689 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3690 | |
| 3691 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3692 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3693 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3694 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3695 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3696 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3697 | iv, sizeof( iv ) ), |
| 3698 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3699 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3700 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3701 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3702 | |
| 3703 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3704 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3705 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3706 | buffer, sizeof( buffer ), |
| 3707 | &length ) ); |
| 3708 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3709 | iv, sizeof( iv ) ), |
| 3710 | PSA_ERROR_BAD_STATE ); |
| 3711 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3712 | |
| 3713 | /* Call update without calling setup beforehand. */ |
| 3714 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3715 | text, sizeof( text ), |
| 3716 | buffer, sizeof( buffer ), |
| 3717 | &length ), |
| 3718 | PSA_ERROR_BAD_STATE ); |
| 3719 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3720 | |
| 3721 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3722 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3723 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3724 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3725 | text, sizeof( text ), |
| 3726 | buffer, sizeof( buffer ), |
| 3727 | &length ), |
| 3728 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3729 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3730 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3731 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3732 | |
| 3733 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3734 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3735 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3736 | iv, sizeof( iv ) ) ); |
| 3737 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3738 | buffer, sizeof( buffer ), &length ) ); |
| 3739 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3740 | text, sizeof( text ), |
| 3741 | buffer, sizeof( buffer ), |
| 3742 | &length ), |
| 3743 | PSA_ERROR_BAD_STATE ); |
| 3744 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3745 | |
| 3746 | /* Call finish without calling setup beforehand. */ |
| 3747 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3748 | buffer, sizeof( buffer ), &length ), |
| 3749 | PSA_ERROR_BAD_STATE ); |
| 3750 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3751 | |
| 3752 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3753 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3754 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3755 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3756 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3757 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3758 | buffer, sizeof( buffer ), &length ), |
| 3759 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3760 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3761 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3762 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3763 | |
| 3764 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3765 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3766 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3767 | iv, sizeof( iv ) ) ); |
| 3768 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3769 | buffer, sizeof( buffer ), &length ) ); |
| 3770 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3771 | buffer, sizeof( buffer ), &length ), |
| 3772 | PSA_ERROR_BAD_STATE ); |
| 3773 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3774 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3775 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3776 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3777 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3778 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3779 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3780 | } |
| 3781 | /* END_CASE */ |
| 3782 | |
| 3783 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3784 | void cipher_encrypt_fail( int alg_arg, |
| 3785 | int key_type_arg, |
| 3786 | data_t *key_data, |
| 3787 | data_t *input, |
| 3788 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3789 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3790 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3791 | psa_status_t status; |
| 3792 | psa_key_type_t key_type = key_type_arg; |
| 3793 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3794 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3795 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3796 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3797 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3798 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3799 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3800 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3801 | size_t function_output_length; |
| 3802 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3803 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3804 | |
| 3805 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3806 | { |
| 3807 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3808 | |
| 3809 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3810 | psa_set_key_algorithm( &attributes, alg ); |
| 3811 | psa_set_key_type( &attributes, key_type ); |
| 3812 | |
| 3813 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3814 | input->len ); |
| 3815 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3816 | |
| 3817 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3818 | &key ) ); |
| 3819 | } |
| 3820 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3821 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3822 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3823 | output_buffer_size, &output_length ); |
| 3824 | |
| 3825 | TEST_EQUAL( status, expected_status ); |
| 3826 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3827 | /* Encrypt, multi-part */ |
| 3828 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3829 | if( status == PSA_SUCCESS ) |
| 3830 | { |
| 3831 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3832 | { |
| 3833 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3834 | iv, iv_size, |
| 3835 | &iv_length ) ); |
| 3836 | } |
| 3837 | |
| 3838 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3839 | output, output_buffer_size, |
| 3840 | &function_output_length ); |
| 3841 | if( status == PSA_SUCCESS ) |
| 3842 | { |
| 3843 | output_length += function_output_length; |
| 3844 | |
| 3845 | status = psa_cipher_finish( &operation, output + output_length, |
| 3846 | output_buffer_size - output_length, |
| 3847 | &function_output_length ); |
| 3848 | |
| 3849 | TEST_EQUAL( status, expected_status ); |
| 3850 | } |
| 3851 | else |
| 3852 | { |
| 3853 | TEST_EQUAL( status, expected_status ); |
| 3854 | } |
| 3855 | } |
| 3856 | else |
| 3857 | { |
| 3858 | TEST_EQUAL( status, expected_status ); |
| 3859 | } |
| 3860 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3861 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3862 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3863 | mbedtls_free( output ); |
| 3864 | psa_destroy_key( key ); |
| 3865 | PSA_DONE( ); |
| 3866 | } |
| 3867 | /* END_CASE */ |
| 3868 | |
| 3869 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3870 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3871 | data_t *input, int iv_length, |
| 3872 | int expected_result ) |
| 3873 | { |
| 3874 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3875 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3876 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3877 | size_t output_buffer_size = 0; |
| 3878 | unsigned char *output = NULL; |
| 3879 | |
| 3880 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3881 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3882 | |
| 3883 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3884 | |
| 3885 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3886 | psa_set_key_algorithm( &attributes, alg ); |
| 3887 | psa_set_key_type( &attributes, key_type ); |
| 3888 | |
| 3889 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3890 | &key ) ); |
| 3891 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3892 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3893 | iv_length ) ); |
| 3894 | |
| 3895 | exit: |
| 3896 | psa_cipher_abort( &operation ); |
| 3897 | mbedtls_free( output ); |
| 3898 | psa_destroy_key( key ); |
| 3899 | PSA_DONE( ); |
| 3900 | } |
| 3901 | /* END_CASE */ |
| 3902 | |
| 3903 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3904 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3905 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3906 | { |
| 3907 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3908 | psa_key_type_t key_type = key_type_arg; |
| 3909 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3910 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3911 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3912 | unsigned char *output = NULL; |
| 3913 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3914 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3915 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3916 | |
| 3917 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3918 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3919 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3920 | TEST_LE_U( ciphertext->len, |
| 3921 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3922 | 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] | 3923 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3924 | TEST_LE_U( plaintext->len, |
| 3925 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3926 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3927 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3928 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3929 | |
| 3930 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3931 | psa_set_key_usage_flags( &attributes, |
| 3932 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3933 | psa_set_key_algorithm( &attributes, alg ); |
| 3934 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3935 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3936 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3937 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3938 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3939 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3940 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3941 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3942 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3943 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3944 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3945 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3946 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3947 | PSA_ERROR_BAD_STATE ); |
| 3948 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3949 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3950 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3951 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3952 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3953 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3954 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3955 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3956 | &length ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3957 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3958 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3959 | /* Multipart encryption */ |
| 3960 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3961 | output_length = 0; |
| 3962 | length = ~0; |
| 3963 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3964 | plaintext->x, plaintext->len, |
| 3965 | output, output_buffer_size, |
| 3966 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3967 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3968 | output_length += length; |
| 3969 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3970 | output + output_length, |
| 3971 | output_buffer_size - output_length, |
| 3972 | &length ) ); |
| 3973 | output_length += length; |
| 3974 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3975 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3976 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3977 | /* Multipart encryption */ |
| 3978 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3979 | output_length = 0; |
| 3980 | length = ~0; |
| 3981 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3982 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3983 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 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( plaintext->x, plaintext->len, |
| 3993 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3994 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3995 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3996 | output_length = ~0; |
| 3997 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 3998 | output, output_buffer_size, |
| 3999 | &output_length ) ); |
| 4000 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 4001 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4002 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4003 | /* One-shot decryption */ |
| 4004 | output_length = ~0; |
| 4005 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 4006 | output, output_buffer_size, |
| 4007 | &output_length ) ); |
| 4008 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4009 | output, output_length ); |
| 4010 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4011 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4012 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4013 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4014 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4015 | psa_destroy_key( key ); |
| 4016 | PSA_DONE( ); |
| 4017 | } |
| 4018 | /* END_CASE */ |
| 4019 | |
| 4020 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4021 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 4022 | { |
| 4023 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4024 | psa_algorithm_t alg = alg_arg; |
| 4025 | psa_key_type_t key_type = key_type_arg; |
| 4026 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4027 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4028 | psa_status_t status; |
| 4029 | |
| 4030 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4031 | |
| 4032 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4033 | psa_set_key_algorithm( &attributes, alg ); |
| 4034 | psa_set_key_type( &attributes, key_type ); |
| 4035 | |
| 4036 | /* Usage of either of these two size macros would cause divide by zero |
| 4037 | * with incorrect key types previously. Input length should be irrelevant |
| 4038 | * here. */ |
| 4039 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 4040 | 0 ); |
| 4041 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 4042 | |
| 4043 | |
| 4044 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4045 | &key ) ); |
| 4046 | |
| 4047 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4048 | * Encrypt or decrypt will end up in the same place. */ |
| 4049 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 4050 | |
| 4051 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 4052 | |
| 4053 | exit: |
| 4054 | psa_cipher_abort( &operation ); |
| 4055 | psa_destroy_key( key ); |
| 4056 | PSA_DONE( ); |
| 4057 | } |
| 4058 | /* END_CASE */ |
| 4059 | |
| 4060 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4061 | void cipher_encrypt_validation( int alg_arg, |
| 4062 | int key_type_arg, |
| 4063 | data_t *key_data, |
| 4064 | data_t *input ) |
| 4065 | { |
| 4066 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4067 | psa_key_type_t key_type = key_type_arg; |
| 4068 | psa_algorithm_t alg = alg_arg; |
| 4069 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 4070 | unsigned char *output1 = NULL; |
| 4071 | size_t output1_buffer_size = 0; |
| 4072 | size_t output1_length = 0; |
| 4073 | unsigned char *output2 = NULL; |
| 4074 | size_t output2_buffer_size = 0; |
| 4075 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4076 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4077 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4078 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4079 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4080 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4081 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4082 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4083 | psa_set_key_algorithm( &attributes, alg ); |
| 4084 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4085 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4086 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 4087 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4088 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4089 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 4090 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 4091 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4092 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4093 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4094 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4095 | /* The one-shot cipher encryption uses generated iv so validating |
| 4096 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4097 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 4098 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4099 | TEST_LE_U( output1_length, |
| 4100 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4101 | TEST_LE_U( output1_length, |
| 4102 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4103 | |
| 4104 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 4105 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4106 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4107 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4108 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4109 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4110 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4111 | TEST_LE_U( function_output_length, |
| 4112 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4113 | TEST_LE_U( function_output_length, |
| 4114 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4115 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4116 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4117 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4118 | output2 + output2_length, |
| 4119 | output2_buffer_size - output2_length, |
| 4120 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4121 | TEST_LE_U( function_output_length, |
| 4122 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4123 | TEST_LE_U( function_output_length, |
| 4124 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4125 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4126 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4127 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4128 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 4129 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4130 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4131 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4132 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4133 | mbedtls_free( output1 ); |
| 4134 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4135 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4136 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4137 | } |
| 4138 | /* END_CASE */ |
| 4139 | |
| 4140 | /* BEGIN_CASE */ |
| 4141 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4142 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4143 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4144 | int first_part_size_arg, |
| 4145 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4146 | data_t *expected_output, |
| 4147 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4148 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4149 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4150 | psa_key_type_t key_type = key_type_arg; |
| 4151 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4152 | psa_status_t status; |
| 4153 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4154 | size_t first_part_size = first_part_size_arg; |
| 4155 | size_t output1_length = output1_length_arg; |
| 4156 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4157 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4158 | size_t output_buffer_size = 0; |
| 4159 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4160 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4161 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4162 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4163 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4164 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4165 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4166 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4167 | psa_set_key_algorithm( &attributes, alg ); |
| 4168 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4169 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4170 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4171 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4172 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4173 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4174 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4175 | if( iv->len > 0 ) |
| 4176 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4177 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4178 | } |
| 4179 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4180 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4181 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4182 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4183 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4184 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4185 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 4186 | output, output_buffer_size, |
| 4187 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4188 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4189 | TEST_LE_U( function_output_length, |
| 4190 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4191 | TEST_LE_U( function_output_length, |
| 4192 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4193 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4194 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4195 | if( first_part_size < input->len ) |
| 4196 | { |
| 4197 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4198 | input->x + first_part_size, |
| 4199 | input->len - first_part_size, |
| 4200 | ( output_buffer_size == 0 ? NULL : |
| 4201 | output + total_output_length ), |
| 4202 | output_buffer_size - total_output_length, |
| 4203 | &function_output_length ) ); |
| 4204 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4205 | TEST_LE_U( function_output_length, |
| 4206 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4207 | alg, |
| 4208 | input->len - first_part_size ) ); |
| 4209 | TEST_LE_U( function_output_length, |
| 4210 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4211 | total_output_length += function_output_length; |
| 4212 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4213 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4214 | status = psa_cipher_finish( &operation, |
| 4215 | ( output_buffer_size == 0 ? NULL : |
| 4216 | output + total_output_length ), |
| 4217 | output_buffer_size - total_output_length, |
| 4218 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4219 | TEST_LE_U( function_output_length, |
| 4220 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4221 | TEST_LE_U( function_output_length, |
| 4222 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4223 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4224 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4225 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4226 | if( expected_status == PSA_SUCCESS ) |
| 4227 | { |
| 4228 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4229 | |
| 4230 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4231 | output, total_output_length ); |
| 4232 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4233 | |
| 4234 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4235 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4236 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4237 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4238 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4239 | } |
| 4240 | /* END_CASE */ |
| 4241 | |
| 4242 | /* BEGIN_CASE */ |
| 4243 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4244 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4245 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4246 | int first_part_size_arg, |
| 4247 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4248 | data_t *expected_output, |
| 4249 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4250 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4251 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4252 | psa_key_type_t key_type = key_type_arg; |
| 4253 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4254 | psa_status_t status; |
| 4255 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4256 | size_t first_part_size = first_part_size_arg; |
| 4257 | size_t output1_length = output1_length_arg; |
| 4258 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4259 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4260 | size_t output_buffer_size = 0; |
| 4261 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4262 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4263 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4264 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4265 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4266 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4267 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4268 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4269 | psa_set_key_algorithm( &attributes, alg ); |
| 4270 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4271 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4272 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4273 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4274 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4275 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4276 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4277 | if( iv->len > 0 ) |
| 4278 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4279 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4280 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4281 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4282 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4283 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4284 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4285 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4286 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4287 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4288 | input->x, first_part_size, |
| 4289 | output, output_buffer_size, |
| 4290 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4291 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4292 | TEST_LE_U( function_output_length, |
| 4293 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4294 | TEST_LE_U( function_output_length, |
| 4295 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4296 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4297 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4298 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4299 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4300 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4301 | input->x + first_part_size, |
| 4302 | input->len - first_part_size, |
| 4303 | ( output_buffer_size == 0 ? NULL : |
| 4304 | output + total_output_length ), |
| 4305 | output_buffer_size - total_output_length, |
| 4306 | &function_output_length ) ); |
| 4307 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4308 | TEST_LE_U( function_output_length, |
| 4309 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4310 | alg, |
| 4311 | input->len - first_part_size ) ); |
| 4312 | TEST_LE_U( function_output_length, |
| 4313 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4314 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4315 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4316 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4317 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4318 | ( output_buffer_size == 0 ? NULL : |
| 4319 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4320 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4321 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4322 | TEST_LE_U( function_output_length, |
| 4323 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4324 | TEST_LE_U( function_output_length, |
| 4325 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4326 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4327 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4328 | |
| 4329 | if( expected_status == PSA_SUCCESS ) |
| 4330 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4331 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4332 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4333 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4334 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4335 | } |
| 4336 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4337 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4338 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4339 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4340 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4341 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4342 | } |
| 4343 | /* END_CASE */ |
| 4344 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4345 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4346 | void cipher_decrypt_fail( int alg_arg, |
| 4347 | int key_type_arg, |
| 4348 | data_t *key_data, |
| 4349 | data_t *iv, |
| 4350 | data_t *input_arg, |
| 4351 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4352 | { |
| 4353 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4354 | psa_status_t status; |
| 4355 | psa_key_type_t key_type = key_type_arg; |
| 4356 | psa_algorithm_t alg = alg_arg; |
| 4357 | psa_status_t expected_status = expected_status_arg; |
| 4358 | unsigned char *input = NULL; |
| 4359 | size_t input_buffer_size = 0; |
| 4360 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4361 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4362 | size_t output_buffer_size = 0; |
| 4363 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4364 | size_t function_output_length; |
| 4365 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4366 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4367 | |
| 4368 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4369 | { |
| 4370 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4371 | |
| 4372 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4373 | psa_set_key_algorithm( &attributes, alg ); |
| 4374 | psa_set_key_type( &attributes, key_type ); |
| 4375 | |
| 4376 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4377 | &key ) ); |
| 4378 | } |
| 4379 | |
| 4380 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4381 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4382 | if ( input_buffer_size > 0 ) |
| 4383 | { |
| 4384 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4385 | memcpy( input, iv->x, iv->len ); |
| 4386 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4387 | } |
| 4388 | |
| 4389 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4390 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4391 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4392 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4393 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4394 | output_buffer_size, &output_length ); |
| 4395 | TEST_EQUAL( status, expected_status ); |
| 4396 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4397 | /* Decrypt, multi-part */ |
| 4398 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4399 | if( status == PSA_SUCCESS ) |
| 4400 | { |
| 4401 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4402 | input_arg->len ) + |
| 4403 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4404 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4405 | |
| 4406 | if( iv->len > 0 ) |
| 4407 | { |
| 4408 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4409 | |
| 4410 | if( status != PSA_SUCCESS ) |
| 4411 | TEST_EQUAL( status, expected_status ); |
| 4412 | } |
| 4413 | |
| 4414 | if( status == PSA_SUCCESS ) |
| 4415 | { |
| 4416 | status = psa_cipher_update( &operation, |
| 4417 | input_arg->x, input_arg->len, |
| 4418 | output_multi, output_buffer_size, |
| 4419 | &function_output_length ); |
| 4420 | if( status == PSA_SUCCESS ) |
| 4421 | { |
| 4422 | output_length = function_output_length; |
| 4423 | |
| 4424 | status = psa_cipher_finish( &operation, |
| 4425 | output_multi + output_length, |
| 4426 | output_buffer_size - output_length, |
| 4427 | &function_output_length ); |
| 4428 | |
| 4429 | TEST_EQUAL( status, expected_status ); |
| 4430 | } |
| 4431 | else |
| 4432 | { |
| 4433 | TEST_EQUAL( status, expected_status ); |
| 4434 | } |
| 4435 | } |
| 4436 | else |
| 4437 | { |
| 4438 | TEST_EQUAL( status, expected_status ); |
| 4439 | } |
| 4440 | } |
| 4441 | else |
| 4442 | { |
| 4443 | TEST_EQUAL( status, expected_status ); |
| 4444 | } |
| 4445 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4446 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4447 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4448 | mbedtls_free( input ); |
| 4449 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4450 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4451 | psa_destroy_key( key ); |
| 4452 | PSA_DONE( ); |
| 4453 | } |
| 4454 | /* END_CASE */ |
| 4455 | |
| 4456 | /* BEGIN_CASE */ |
| 4457 | void cipher_decrypt( int alg_arg, |
| 4458 | int key_type_arg, |
| 4459 | data_t *key_data, |
| 4460 | data_t *iv, |
| 4461 | data_t *input_arg, |
| 4462 | data_t *expected_output ) |
| 4463 | { |
| 4464 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4465 | psa_key_type_t key_type = key_type_arg; |
| 4466 | psa_algorithm_t alg = alg_arg; |
| 4467 | unsigned char *input = NULL; |
| 4468 | size_t input_buffer_size = 0; |
| 4469 | unsigned char *output = NULL; |
| 4470 | size_t output_buffer_size = 0; |
| 4471 | size_t output_length = 0; |
| 4472 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4473 | |
| 4474 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4475 | |
| 4476 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4477 | psa_set_key_algorithm( &attributes, alg ); |
| 4478 | psa_set_key_type( &attributes, key_type ); |
| 4479 | |
| 4480 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4481 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4482 | if ( input_buffer_size > 0 ) |
| 4483 | { |
| 4484 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4485 | memcpy( input, iv->x, iv->len ); |
| 4486 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4487 | } |
| 4488 | |
| 4489 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4490 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4491 | |
| 4492 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4493 | &key ) ); |
| 4494 | |
| 4495 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4496 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4497 | TEST_LE_U( output_length, |
| 4498 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4499 | TEST_LE_U( output_length, |
| 4500 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4501 | |
| 4502 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4503 | output, output_length ); |
| 4504 | exit: |
| 4505 | mbedtls_free( input ); |
| 4506 | mbedtls_free( output ); |
| 4507 | psa_destroy_key( key ); |
| 4508 | PSA_DONE( ); |
| 4509 | } |
| 4510 | /* END_CASE */ |
| 4511 | |
| 4512 | /* BEGIN_CASE */ |
| 4513 | void cipher_verify_output( int alg_arg, |
| 4514 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4515 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4516 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4517 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4518 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4519 | psa_key_type_t key_type = key_type_arg; |
| 4520 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4521 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4522 | size_t output1_size = 0; |
| 4523 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4524 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4525 | size_t output2_size = 0; |
| 4526 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4527 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4528 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4529 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4530 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4531 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4532 | psa_set_key_algorithm( &attributes, alg ); |
| 4533 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4534 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4535 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4536 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4537 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4538 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4539 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4540 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4541 | output1, output1_size, |
| 4542 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4543 | TEST_LE_U( output1_length, |
| 4544 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4545 | TEST_LE_U( output1_length, |
| 4546 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4547 | |
| 4548 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4549 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4550 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4551 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4552 | output2, output2_size, |
| 4553 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4554 | TEST_LE_U( output2_length, |
| 4555 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4556 | TEST_LE_U( output2_length, |
| 4557 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4558 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4559 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4560 | |
| 4561 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4562 | mbedtls_free( output1 ); |
| 4563 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4564 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4565 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4566 | } |
| 4567 | /* END_CASE */ |
| 4568 | |
| 4569 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4570 | void cipher_verify_output_multipart( int alg_arg, |
| 4571 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4572 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4573 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4574 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4575 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4576 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4577 | psa_key_type_t key_type = key_type_arg; |
| 4578 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4579 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4580 | unsigned char iv[16] = {0}; |
| 4581 | size_t iv_size = 16; |
| 4582 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4583 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4584 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4585 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4586 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4587 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4588 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4589 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4590 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4591 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4592 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4593 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4594 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4595 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4596 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4597 | psa_set_key_algorithm( &attributes, alg ); |
| 4598 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4599 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4600 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4601 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4602 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4603 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4604 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4605 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4606 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4607 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4608 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4609 | iv, iv_size, |
| 4610 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4611 | } |
| 4612 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4613 | 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] | 4614 | TEST_LE_U( output1_buffer_size, |
| 4615 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4616 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4617 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4618 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4619 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4620 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4621 | output1, output1_buffer_size, |
| 4622 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4623 | TEST_LE_U( function_output_length, |
| 4624 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4625 | TEST_LE_U( function_output_length, |
| 4626 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4627 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4628 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4629 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4630 | input->x + first_part_size, |
| 4631 | input->len - first_part_size, |
| 4632 | output1, output1_buffer_size, |
| 4633 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4634 | TEST_LE_U( function_output_length, |
| 4635 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4636 | alg, |
| 4637 | input->len - first_part_size ) ); |
| 4638 | TEST_LE_U( function_output_length, |
| 4639 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4640 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4641 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4642 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4643 | output1 + output1_length, |
| 4644 | output1_buffer_size - output1_length, |
| 4645 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4646 | TEST_LE_U( function_output_length, |
| 4647 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4648 | TEST_LE_U( function_output_length, |
| 4649 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4650 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4651 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4652 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4653 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4654 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4655 | TEST_LE_U( output2_buffer_size, |
| 4656 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4657 | TEST_LE_U( output2_buffer_size, |
| 4658 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4659 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4660 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4661 | if( iv_length > 0 ) |
| 4662 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4663 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4664 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4665 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4666 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4667 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4668 | output2, output2_buffer_size, |
| 4669 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4670 | TEST_LE_U( function_output_length, |
| 4671 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4672 | TEST_LE_U( function_output_length, |
| 4673 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4674 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4675 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4676 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4677 | output1 + first_part_size, |
| 4678 | output1_length - first_part_size, |
| 4679 | output2, output2_buffer_size, |
| 4680 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4681 | TEST_LE_U( function_output_length, |
| 4682 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4683 | alg, |
| 4684 | output1_length - first_part_size ) ); |
| 4685 | TEST_LE_U( function_output_length, |
| 4686 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4687 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4688 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4689 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4690 | output2 + output2_length, |
| 4691 | output2_buffer_size - output2_length, |
| 4692 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4693 | TEST_LE_U( function_output_length, |
| 4694 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4695 | TEST_LE_U( function_output_length, |
| 4696 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4697 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4698 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4699 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4700 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4701 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4702 | |
| 4703 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4704 | psa_cipher_abort( &operation1 ); |
| 4705 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4706 | mbedtls_free( output1 ); |
| 4707 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4708 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4709 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4710 | } |
| 4711 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4712 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4713 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4714 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4715 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4716 | data_t *nonce, |
| 4717 | data_t *additional_data, |
| 4718 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4719 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4720 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4721 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4722 | psa_key_type_t key_type = key_type_arg; |
| 4723 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4724 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4725 | unsigned char *output_data = NULL; |
| 4726 | size_t output_size = 0; |
| 4727 | size_t output_length = 0; |
| 4728 | unsigned char *output_data2 = NULL; |
| 4729 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4730 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4731 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4732 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4733 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4734 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4735 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4736 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4737 | psa_set_key_algorithm( &attributes, alg ); |
| 4738 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4739 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4740 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4741 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4742 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4743 | key_bits = psa_get_key_bits( &attributes ); |
| 4744 | |
| 4745 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4746 | alg ); |
| 4747 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4748 | * should be exact. */ |
| 4749 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4750 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4751 | { |
| 4752 | TEST_EQUAL( output_size, |
| 4753 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4754 | TEST_LE_U( output_size, |
| 4755 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4756 | } |
| 4757 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4758 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4759 | status = psa_aead_encrypt( key, alg, |
| 4760 | nonce->x, nonce->len, |
| 4761 | additional_data->x, |
| 4762 | additional_data->len, |
| 4763 | input_data->x, input_data->len, |
| 4764 | output_data, output_size, |
| 4765 | &output_length ); |
| 4766 | |
| 4767 | /* If the operation is not supported, just skip and not fail in case the |
| 4768 | * encryption involves a common limitation of cryptography hardwares and |
| 4769 | * an alternative implementation. */ |
| 4770 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4771 | { |
| 4772 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4773 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4774 | } |
| 4775 | |
| 4776 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4777 | |
| 4778 | if( PSA_SUCCESS == expected_result ) |
| 4779 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4780 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4781 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4782 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4783 | * should be exact. */ |
| 4784 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4785 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4786 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4787 | TEST_LE_U( input_data->len, |
| 4788 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4789 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4790 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4791 | nonce->x, nonce->len, |
| 4792 | additional_data->x, |
| 4793 | additional_data->len, |
| 4794 | output_data, output_length, |
| 4795 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4796 | &output_length2 ), |
| 4797 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4798 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4799 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4800 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4801 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4802 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4803 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4804 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4805 | mbedtls_free( output_data ); |
| 4806 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4807 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4808 | } |
| 4809 | /* END_CASE */ |
| 4810 | |
| 4811 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4812 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4813 | int alg_arg, |
| 4814 | data_t *nonce, |
| 4815 | data_t *additional_data, |
| 4816 | data_t *input_data, |
| 4817 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4818 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4819 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4820 | psa_key_type_t key_type = key_type_arg; |
| 4821 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4822 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4823 | unsigned char *output_data = NULL; |
| 4824 | size_t output_size = 0; |
| 4825 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4826 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4827 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4828 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4829 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4830 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4831 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4832 | psa_set_key_algorithm( &attributes, alg ); |
| 4833 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4834 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4835 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4836 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4837 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4838 | key_bits = psa_get_key_bits( &attributes ); |
| 4839 | |
| 4840 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4841 | alg ); |
| 4842 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4843 | * should be exact. */ |
| 4844 | TEST_EQUAL( output_size, |
| 4845 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4846 | TEST_LE_U( output_size, |
| 4847 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4848 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4849 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4850 | status = psa_aead_encrypt( key, alg, |
| 4851 | nonce->x, nonce->len, |
| 4852 | additional_data->x, additional_data->len, |
| 4853 | input_data->x, input_data->len, |
| 4854 | output_data, output_size, |
| 4855 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4856 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4857 | /* If the operation is not supported, just skip and not fail in case the |
| 4858 | * encryption involves a common limitation of cryptography hardwares and |
| 4859 | * an alternative implementation. */ |
| 4860 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4861 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4862 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4863 | 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] | 4864 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4865 | |
| 4866 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4867 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4868 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4869 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4870 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4871 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4872 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4873 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4874 | } |
| 4875 | /* END_CASE */ |
| 4876 | |
| 4877 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4878 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4879 | int alg_arg, |
| 4880 | data_t *nonce, |
| 4881 | data_t *additional_data, |
| 4882 | data_t *input_data, |
| 4883 | data_t *expected_data, |
| 4884 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4885 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4886 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4887 | psa_key_type_t key_type = key_type_arg; |
| 4888 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4889 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4890 | unsigned char *output_data = NULL; |
| 4891 | size_t output_size = 0; |
| 4892 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4893 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4894 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4895 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4896 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4897 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4898 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4899 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4900 | psa_set_key_algorithm( &attributes, alg ); |
| 4901 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4902 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4903 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4904 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4905 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4906 | key_bits = psa_get_key_bits( &attributes ); |
| 4907 | |
| 4908 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4909 | alg ); |
| 4910 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4911 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4912 | { |
| 4913 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4914 | * should be exact. */ |
| 4915 | TEST_EQUAL( output_size, |
| 4916 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4917 | TEST_LE_U( output_size, |
| 4918 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4919 | } |
| 4920 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4921 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4922 | status = psa_aead_decrypt( key, alg, |
| 4923 | nonce->x, nonce->len, |
| 4924 | additional_data->x, |
| 4925 | additional_data->len, |
| 4926 | input_data->x, input_data->len, |
| 4927 | output_data, output_size, |
| 4928 | &output_length ); |
| 4929 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4930 | /* If the operation is not supported, just skip and not fail in case the |
| 4931 | * decryption involves a common limitation of cryptography hardwares and |
| 4932 | * an alternative implementation. */ |
| 4933 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4934 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4935 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4936 | 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] | 4937 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4938 | |
| 4939 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4940 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4941 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4942 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4943 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4944 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4945 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4946 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4947 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4948 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4949 | } |
| 4950 | /* END_CASE */ |
| 4951 | |
| 4952 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4953 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4954 | int alg_arg, |
| 4955 | data_t *nonce, |
| 4956 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4957 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4958 | int do_set_lengths, |
| 4959 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4960 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4961 | size_t ad_part_len = 0; |
| 4962 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4963 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4964 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4965 | 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] | 4966 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4967 | mbedtls_test_set_step( ad_part_len ); |
| 4968 | |
| 4969 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4970 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4971 | if( ad_part_len & 0x01 ) |
| 4972 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4973 | else |
| 4974 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4975 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4976 | |
| 4977 | /* Split ad into length(ad_part_len) parts. */ |
| 4978 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4979 | alg_arg, nonce, |
| 4980 | additional_data, |
| 4981 | ad_part_len, |
| 4982 | input_data, -1, |
| 4983 | set_lengths_method, |
| 4984 | expected_output, |
| 4985 | 1, 0 ) ) |
| 4986 | break; |
| 4987 | |
| 4988 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4989 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4990 | |
| 4991 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4992 | alg_arg, nonce, |
| 4993 | additional_data, |
| 4994 | ad_part_len, |
| 4995 | input_data, -1, |
| 4996 | set_lengths_method, |
| 4997 | expected_output, |
| 4998 | 1, 1 ) ) |
| 4999 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5000 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5001 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5002 | 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] | 5003 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5004 | /* Split data into length(data_part_len) parts. */ |
| 5005 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5006 | |
| 5007 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5008 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5009 | if( data_part_len & 0x01 ) |
| 5010 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5011 | else |
| 5012 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5013 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5014 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5015 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5016 | alg_arg, nonce, |
| 5017 | additional_data, -1, |
| 5018 | input_data, data_part_len, |
| 5019 | set_lengths_method, |
| 5020 | expected_output, |
| 5021 | 1, 0 ) ) |
| 5022 | break; |
| 5023 | |
| 5024 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5025 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5026 | |
| 5027 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5028 | alg_arg, nonce, |
| 5029 | additional_data, -1, |
| 5030 | input_data, data_part_len, |
| 5031 | set_lengths_method, |
| 5032 | expected_output, |
| 5033 | 1, 1 ) ) |
| 5034 | break; |
| 5035 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5036 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5037 | /* Goto is required to silence warnings about unused labels, as we |
| 5038 | * don't actually do any test assertions in this function. */ |
| 5039 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5040 | } |
| 5041 | /* END_CASE */ |
| 5042 | |
| 5043 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5044 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 5045 | int alg_arg, |
| 5046 | data_t *nonce, |
| 5047 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5048 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 5049 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5050 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5051 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5052 | size_t ad_part_len = 0; |
| 5053 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5054 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5055 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5056 | 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] | 5057 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5058 | /* Split ad into length(ad_part_len) parts. */ |
| 5059 | mbedtls_test_set_step( ad_part_len ); |
| 5060 | |
| 5061 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5062 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5063 | if( ad_part_len & 0x01 ) |
| 5064 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5065 | else |
| 5066 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5067 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5068 | |
| 5069 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5070 | alg_arg, nonce, |
| 5071 | additional_data, |
| 5072 | ad_part_len, |
| 5073 | input_data, -1, |
| 5074 | set_lengths_method, |
| 5075 | expected_output, |
| 5076 | 0, 0 ) ) |
| 5077 | break; |
| 5078 | |
| 5079 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5080 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5081 | |
| 5082 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5083 | alg_arg, nonce, |
| 5084 | additional_data, |
| 5085 | ad_part_len, |
| 5086 | input_data, -1, |
| 5087 | set_lengths_method, |
| 5088 | expected_output, |
| 5089 | 0, 1 ) ) |
| 5090 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5091 | } |
| 5092 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5093 | 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] | 5094 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5095 | /* Split data into length(data_part_len) parts. */ |
| 5096 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5097 | |
| 5098 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5099 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5100 | if( data_part_len & 0x01 ) |
| 5101 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5102 | else |
| 5103 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5104 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5105 | |
| 5106 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5107 | alg_arg, nonce, |
| 5108 | additional_data, -1, |
| 5109 | input_data, data_part_len, |
| 5110 | set_lengths_method, |
| 5111 | expected_output, |
| 5112 | 0, 0 ) ) |
| 5113 | break; |
| 5114 | |
| 5115 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5116 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5117 | |
| 5118 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5119 | alg_arg, nonce, |
| 5120 | additional_data, -1, |
| 5121 | input_data, data_part_len, |
| 5122 | set_lengths_method, |
| 5123 | expected_output, |
| 5124 | 0, 1 ) ) |
| 5125 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5126 | } |
| 5127 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5128 | /* Goto is required to silence warnings about unused labels, as we |
| 5129 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5130 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5131 | } |
| 5132 | /* END_CASE */ |
| 5133 | |
| 5134 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5135 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 5136 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5137 | int nonce_length, |
| 5138 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5139 | data_t *additional_data, |
| 5140 | data_t *input_data, |
| 5141 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5142 | { |
| 5143 | |
| 5144 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5145 | psa_key_type_t key_type = key_type_arg; |
| 5146 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5147 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5148 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5149 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5150 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5151 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5152 | size_t actual_nonce_length = 0; |
| 5153 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5154 | unsigned char *output = NULL; |
| 5155 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5156 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5157 | size_t ciphertext_size = 0; |
| 5158 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5159 | size_t tag_length = 0; |
| 5160 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5161 | |
| 5162 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5163 | |
| 5164 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5165 | psa_set_key_algorithm( & attributes, alg ); |
| 5166 | psa_set_key_type( & attributes, key_type ); |
| 5167 | |
| 5168 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5169 | &key ) ); |
| 5170 | |
| 5171 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5172 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5173 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5174 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5175 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5176 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5177 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5178 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5179 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5180 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5181 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5182 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5183 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5184 | |
| 5185 | /* If the operation is not supported, just skip and not fail in case the |
| 5186 | * encryption involves a common limitation of cryptography hardwares and |
| 5187 | * an alternative implementation. */ |
| 5188 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5189 | { |
| 5190 | 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] | 5191 | 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] | 5192 | } |
| 5193 | |
| 5194 | PSA_ASSERT( status ); |
| 5195 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5196 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5197 | nonce_length, |
| 5198 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5199 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5200 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5201 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5202 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5203 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5204 | if( expected_status == PSA_SUCCESS ) |
| 5205 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 5206 | alg ) ); |
| 5207 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5208 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5209 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5210 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5211 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5212 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 5213 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5214 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5215 | |
| 5216 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5217 | additional_data->len ) ); |
| 5218 | |
| 5219 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5220 | output, output_size, |
| 5221 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5222 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5223 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5224 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5225 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5226 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5227 | |
| 5228 | exit: |
| 5229 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5230 | mbedtls_free( output ); |
| 5231 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5232 | psa_aead_abort( &operation ); |
| 5233 | PSA_DONE( ); |
| 5234 | } |
| 5235 | /* END_CASE */ |
| 5236 | |
| 5237 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5238 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 5239 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5240 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5241 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5242 | data_t *additional_data, |
| 5243 | data_t *input_data, |
| 5244 | int expected_status_arg ) |
| 5245 | { |
| 5246 | |
| 5247 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5248 | psa_key_type_t key_type = key_type_arg; |
| 5249 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5250 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5251 | uint8_t *nonce_buffer = NULL; |
| 5252 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5253 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5254 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5255 | unsigned char *output = NULL; |
| 5256 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5257 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5258 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5259 | size_t ciphertext_size = 0; |
| 5260 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5261 | size_t tag_length = 0; |
| 5262 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5263 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5264 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5265 | |
| 5266 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5267 | |
| 5268 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5269 | psa_set_key_algorithm( &attributes, alg ); |
| 5270 | psa_set_key_type( &attributes, key_type ); |
| 5271 | |
| 5272 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5273 | &key ) ); |
| 5274 | |
| 5275 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5276 | |
| 5277 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5278 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5279 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5280 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5281 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5282 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5283 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5284 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5285 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5286 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5287 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5288 | |
| 5289 | /* If the operation is not supported, just skip and not fail in case the |
| 5290 | * encryption involves a common limitation of cryptography hardwares and |
| 5291 | * an alternative implementation. */ |
| 5292 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5293 | { |
| 5294 | 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] | 5295 | 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] | 5296 | } |
| 5297 | |
| 5298 | PSA_ASSERT( status ); |
| 5299 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5300 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 5301 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5302 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 5303 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5304 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5305 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5306 | } |
| 5307 | else |
| 5308 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5309 | /* If length is zero, then this will return NULL. */ |
| 5310 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5311 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5312 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5313 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5314 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5315 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5316 | { |
| 5317 | nonce_buffer[index] = 'a' + index; |
| 5318 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5319 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5320 | } |
| 5321 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5322 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5323 | { |
| 5324 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5325 | input_data->len ) ); |
| 5326 | } |
| 5327 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5328 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5329 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5330 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5331 | |
| 5332 | if( expected_status == PSA_SUCCESS ) |
| 5333 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5334 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5335 | { |
| 5336 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5337 | input_data->len ) ); |
| 5338 | } |
| 5339 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5340 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5341 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5342 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5343 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5344 | additional_data->len ), |
| 5345 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5346 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5347 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5348 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5349 | &ciphertext_length ), |
| 5350 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5351 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5352 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5353 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5354 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5355 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5356 | } |
| 5357 | |
| 5358 | exit: |
| 5359 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5360 | mbedtls_free( output ); |
| 5361 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5362 | mbedtls_free( nonce_buffer ); |
| 5363 | psa_aead_abort( &operation ); |
| 5364 | PSA_DONE( ); |
| 5365 | } |
| 5366 | /* END_CASE */ |
| 5367 | |
| 5368 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5369 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5370 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5371 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5372 | data_t *nonce, |
| 5373 | data_t *additional_data, |
| 5374 | data_t *input_data, |
| 5375 | int expected_status_arg ) |
| 5376 | { |
| 5377 | |
| 5378 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5379 | psa_key_type_t key_type = key_type_arg; |
| 5380 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5381 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5382 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5383 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5384 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5385 | unsigned char *output = NULL; |
| 5386 | unsigned char *ciphertext = NULL; |
| 5387 | size_t output_size = output_size_arg; |
| 5388 | size_t ciphertext_size = 0; |
| 5389 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5390 | size_t tag_length = 0; |
| 5391 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5392 | |
| 5393 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5394 | |
| 5395 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5396 | psa_set_key_algorithm( &attributes, alg ); |
| 5397 | psa_set_key_type( &attributes, key_type ); |
| 5398 | |
| 5399 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5400 | &key ) ); |
| 5401 | |
| 5402 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5403 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5404 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5405 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5406 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5407 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5408 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5409 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5410 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5411 | |
| 5412 | /* If the operation is not supported, just skip and not fail in case the |
| 5413 | * encryption involves a common limitation of cryptography hardwares and |
| 5414 | * an alternative implementation. */ |
| 5415 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5416 | { |
| 5417 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5418 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5419 | } |
| 5420 | |
| 5421 | PSA_ASSERT( status ); |
| 5422 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5423 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5424 | input_data->len ) ); |
| 5425 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5426 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5427 | |
| 5428 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5429 | additional_data->len ) ); |
| 5430 | |
| 5431 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5432 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5433 | |
| 5434 | TEST_EQUAL( status, expected_status ); |
| 5435 | |
| 5436 | if( expected_status == PSA_SUCCESS ) |
| 5437 | { |
| 5438 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5439 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5440 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5441 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5442 | } |
| 5443 | |
| 5444 | exit: |
| 5445 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5446 | mbedtls_free( output ); |
| 5447 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5448 | psa_aead_abort( &operation ); |
| 5449 | PSA_DONE( ); |
| 5450 | } |
| 5451 | /* END_CASE */ |
| 5452 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5453 | /* BEGIN_CASE */ |
| 5454 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5455 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5456 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5457 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5458 | data_t *nonce, |
| 5459 | data_t *additional_data, |
| 5460 | data_t *input_data, |
| 5461 | int expected_status_arg ) |
| 5462 | { |
| 5463 | |
| 5464 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5465 | psa_key_type_t key_type = key_type_arg; |
| 5466 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5467 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5468 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5469 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5470 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5471 | unsigned char *ciphertext = NULL; |
| 5472 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5473 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5474 | size_t ciphertext_size = 0; |
| 5475 | size_t ciphertext_length = 0; |
| 5476 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5477 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5478 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5479 | |
| 5480 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5481 | |
| 5482 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5483 | psa_set_key_algorithm( &attributes, alg ); |
| 5484 | psa_set_key_type( &attributes, key_type ); |
| 5485 | |
| 5486 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5487 | &key ) ); |
| 5488 | |
| 5489 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5490 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5491 | 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] | 5492 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5493 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5494 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5495 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5496 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5497 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5498 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5499 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5500 | |
| 5501 | /* If the operation is not supported, just skip and not fail in case the |
| 5502 | * encryption involves a common limitation of cryptography hardwares and |
| 5503 | * an alternative implementation. */ |
| 5504 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5505 | { |
| 5506 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5507 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5508 | } |
| 5509 | |
| 5510 | PSA_ASSERT( status ); |
| 5511 | |
| 5512 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5513 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5514 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5515 | input_data->len ) ); |
| 5516 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5517 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5518 | additional_data->len ) ); |
| 5519 | |
| 5520 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5521 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5522 | |
| 5523 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5524 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5525 | finish_ciphertext_size, |
| 5526 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5527 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5528 | |
| 5529 | TEST_EQUAL( status, expected_status ); |
| 5530 | |
| 5531 | exit: |
| 5532 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5533 | mbedtls_free( ciphertext ); |
| 5534 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5535 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5536 | psa_aead_abort( &operation ); |
| 5537 | PSA_DONE( ); |
| 5538 | } |
| 5539 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5540 | |
| 5541 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5542 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5543 | int alg_arg, |
| 5544 | data_t *nonce, |
| 5545 | data_t *additional_data, |
| 5546 | data_t *input_data, |
| 5547 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5548 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5549 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5550 | int expected_status_arg ) |
| 5551 | { |
| 5552 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5553 | psa_key_type_t key_type = key_type_arg; |
| 5554 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5555 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5556 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5557 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5558 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5559 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5560 | unsigned char *plaintext = NULL; |
| 5561 | unsigned char *finish_plaintext = NULL; |
| 5562 | size_t plaintext_size = 0; |
| 5563 | size_t plaintext_length = 0; |
| 5564 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5565 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5566 | unsigned char *tag_buffer = NULL; |
| 5567 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5568 | |
| 5569 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5570 | |
| 5571 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5572 | psa_set_key_algorithm( &attributes, alg ); |
| 5573 | psa_set_key_type( &attributes, key_type ); |
| 5574 | |
| 5575 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5576 | &key ) ); |
| 5577 | |
| 5578 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5579 | |
| 5580 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5581 | input_data->len ); |
| 5582 | |
| 5583 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5584 | |
| 5585 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5586 | |
| 5587 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5588 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5589 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5590 | |
| 5591 | /* If the operation is not supported, just skip and not fail in case the |
| 5592 | * encryption involves a common limitation of cryptography hardwares and |
| 5593 | * an alternative implementation. */ |
| 5594 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5595 | { |
| 5596 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5597 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5598 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5599 | TEST_EQUAL( status, expected_setup_status ); |
| 5600 | |
| 5601 | if( status != PSA_SUCCESS ) |
| 5602 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5603 | |
| 5604 | PSA_ASSERT( status ); |
| 5605 | |
| 5606 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5607 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5608 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5609 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5610 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5611 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5612 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5613 | additional_data->len ) ); |
| 5614 | |
| 5615 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5616 | input_data->len, |
| 5617 | plaintext, plaintext_size, |
| 5618 | &plaintext_length ) ); |
| 5619 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5620 | if( tag_usage == USE_GIVEN_TAG ) |
| 5621 | { |
| 5622 | tag_buffer = tag->x; |
| 5623 | tag_size = tag->len; |
| 5624 | } |
| 5625 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5626 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5627 | verify_plaintext_size, |
| 5628 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5629 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5630 | |
| 5631 | TEST_EQUAL( status, expected_status ); |
| 5632 | |
| 5633 | exit: |
| 5634 | psa_destroy_key( key ); |
| 5635 | mbedtls_free( plaintext ); |
| 5636 | mbedtls_free( finish_plaintext ); |
| 5637 | psa_aead_abort( &operation ); |
| 5638 | PSA_DONE( ); |
| 5639 | } |
| 5640 | /* END_CASE */ |
| 5641 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5642 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5643 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5644 | int alg_arg, int expected_status_arg ) |
| 5645 | { |
| 5646 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5647 | psa_key_type_t key_type = key_type_arg; |
| 5648 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5649 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5650 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5651 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5652 | psa_status_t expected_status = expected_status_arg; |
| 5653 | |
| 5654 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5655 | |
| 5656 | psa_set_key_usage_flags( &attributes, |
| 5657 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5658 | psa_set_key_algorithm( &attributes, alg ); |
| 5659 | psa_set_key_type( &attributes, key_type ); |
| 5660 | |
| 5661 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5662 | &key ) ); |
| 5663 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5664 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5665 | |
| 5666 | TEST_EQUAL( status, expected_status ); |
| 5667 | |
| 5668 | psa_aead_abort( &operation ); |
| 5669 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5670 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5671 | |
| 5672 | TEST_EQUAL(status, expected_status ); |
| 5673 | |
| 5674 | exit: |
| 5675 | psa_destroy_key( key ); |
| 5676 | psa_aead_abort( &operation ); |
| 5677 | PSA_DONE( ); |
| 5678 | } |
| 5679 | /* END_CASE */ |
| 5680 | |
| 5681 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5682 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5683 | int alg_arg, |
| 5684 | data_t *nonce, |
| 5685 | data_t *additional_data, |
| 5686 | data_t *input_data ) |
| 5687 | { |
| 5688 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5689 | psa_key_type_t key_type = key_type_arg; |
| 5690 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5691 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5692 | unsigned char *output_data = NULL; |
| 5693 | unsigned char *final_data = NULL; |
| 5694 | size_t output_size = 0; |
| 5695 | size_t finish_output_size = 0; |
| 5696 | size_t output_length = 0; |
| 5697 | size_t key_bits = 0; |
| 5698 | size_t tag_length = 0; |
| 5699 | size_t tag_size = 0; |
| 5700 | size_t nonce_length = 0; |
| 5701 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5702 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5703 | size_t output_part_length = 0; |
| 5704 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5705 | |
| 5706 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5707 | |
| 5708 | psa_set_key_usage_flags( & attributes, |
| 5709 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5710 | psa_set_key_algorithm( & attributes, alg ); |
| 5711 | psa_set_key_type( & attributes, key_type ); |
| 5712 | |
| 5713 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5714 | &key ) ); |
| 5715 | |
| 5716 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5717 | key_bits = psa_get_key_bits( &attributes ); |
| 5718 | |
| 5719 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5720 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5721 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5722 | |
| 5723 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5724 | |
| 5725 | ASSERT_ALLOC( output_data, output_size ); |
| 5726 | |
| 5727 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5728 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5729 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5730 | |
| 5731 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5732 | |
| 5733 | /* Test all operations error without calling setup first. */ |
| 5734 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5735 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5736 | PSA_ERROR_BAD_STATE ); |
| 5737 | |
| 5738 | psa_aead_abort( &operation ); |
| 5739 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5740 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5741 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5742 | &nonce_length ), |
| 5743 | PSA_ERROR_BAD_STATE ); |
| 5744 | |
| 5745 | psa_aead_abort( &operation ); |
| 5746 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5747 | /* ------------------------------------------------------- */ |
| 5748 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5749 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5750 | input_data->len ), |
| 5751 | PSA_ERROR_BAD_STATE ); |
| 5752 | |
| 5753 | psa_aead_abort( &operation ); |
| 5754 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5755 | /* ------------------------------------------------------- */ |
| 5756 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5757 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5758 | additional_data->len ), |
| 5759 | PSA_ERROR_BAD_STATE ); |
| 5760 | |
| 5761 | psa_aead_abort( &operation ); |
| 5762 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5763 | /* ------------------------------------------------------- */ |
| 5764 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5765 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5766 | input_data->len, output_data, |
| 5767 | output_size, &output_length ), |
| 5768 | PSA_ERROR_BAD_STATE ); |
| 5769 | |
| 5770 | psa_aead_abort( &operation ); |
| 5771 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5772 | /* ------------------------------------------------------- */ |
| 5773 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5774 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5775 | finish_output_size, |
| 5776 | &output_part_length, |
| 5777 | tag_buffer, tag_length, |
| 5778 | &tag_size ), |
| 5779 | PSA_ERROR_BAD_STATE ); |
| 5780 | |
| 5781 | psa_aead_abort( &operation ); |
| 5782 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5783 | /* ------------------------------------------------------- */ |
| 5784 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5785 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5786 | finish_output_size, |
| 5787 | &output_part_length, |
| 5788 | tag_buffer, |
| 5789 | tag_length ), |
| 5790 | PSA_ERROR_BAD_STATE ); |
| 5791 | |
| 5792 | psa_aead_abort( &operation ); |
| 5793 | |
| 5794 | /* Test for double setups. */ |
| 5795 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5796 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5797 | |
| 5798 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5799 | PSA_ERROR_BAD_STATE ); |
| 5800 | |
| 5801 | psa_aead_abort( &operation ); |
| 5802 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5803 | /* ------------------------------------------------------- */ |
| 5804 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5805 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5806 | |
| 5807 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5808 | PSA_ERROR_BAD_STATE ); |
| 5809 | |
| 5810 | psa_aead_abort( &operation ); |
| 5811 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5812 | /* ------------------------------------------------------- */ |
| 5813 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5814 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5815 | |
| 5816 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5817 | PSA_ERROR_BAD_STATE ); |
| 5818 | |
| 5819 | psa_aead_abort( &operation ); |
| 5820 | |
| 5821 | /* ------------------------------------------------------- */ |
| 5822 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5823 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5824 | |
| 5825 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5826 | PSA_ERROR_BAD_STATE ); |
| 5827 | |
| 5828 | psa_aead_abort( &operation ); |
| 5829 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5830 | /* Test for not setting a nonce. */ |
| 5831 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5832 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5833 | |
| 5834 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5835 | additional_data->len ), |
| 5836 | PSA_ERROR_BAD_STATE ); |
| 5837 | |
| 5838 | psa_aead_abort( &operation ); |
| 5839 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5840 | /* ------------------------------------------------------- */ |
| 5841 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5842 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5843 | |
| 5844 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5845 | input_data->len, output_data, |
| 5846 | output_size, &output_length ), |
| 5847 | PSA_ERROR_BAD_STATE ); |
| 5848 | |
| 5849 | psa_aead_abort( &operation ); |
| 5850 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5851 | /* ------------------------------------------------------- */ |
| 5852 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5853 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5854 | |
| 5855 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5856 | finish_output_size, |
| 5857 | &output_part_length, |
| 5858 | tag_buffer, tag_length, |
| 5859 | &tag_size ), |
| 5860 | PSA_ERROR_BAD_STATE ); |
| 5861 | |
| 5862 | psa_aead_abort( &operation ); |
| 5863 | |
| 5864 | /* ------------------------------------------------------- */ |
| 5865 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5866 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5867 | |
| 5868 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5869 | finish_output_size, |
| 5870 | &output_part_length, |
| 5871 | tag_buffer, |
| 5872 | tag_length ), |
| 5873 | PSA_ERROR_BAD_STATE ); |
| 5874 | |
| 5875 | psa_aead_abort( &operation ); |
| 5876 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5877 | /* Test for double setting nonce. */ |
| 5878 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5879 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5880 | |
| 5881 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5882 | |
| 5883 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5884 | PSA_ERROR_BAD_STATE ); |
| 5885 | |
| 5886 | psa_aead_abort( &operation ); |
| 5887 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5888 | /* Test for double generating nonce. */ |
| 5889 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5890 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5891 | |
| 5892 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5893 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5894 | &nonce_length ) ); |
| 5895 | |
| 5896 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5897 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5898 | &nonce_length ), |
| 5899 | PSA_ERROR_BAD_STATE ); |
| 5900 | |
| 5901 | |
| 5902 | psa_aead_abort( &operation ); |
| 5903 | |
| 5904 | /* Test for generate nonce then set and vice versa */ |
| 5905 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5906 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5907 | |
| 5908 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5909 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5910 | &nonce_length ) ); |
| 5911 | |
| 5912 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5913 | PSA_ERROR_BAD_STATE ); |
| 5914 | |
| 5915 | psa_aead_abort( &operation ); |
| 5916 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5917 | /* Test for generating nonce after calling set lengths */ |
| 5918 | |
| 5919 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5920 | |
| 5921 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5922 | input_data->len ) ); |
| 5923 | |
| 5924 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5925 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5926 | &nonce_length ) ); |
| 5927 | |
| 5928 | psa_aead_abort( &operation ); |
| 5929 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5930 | /* 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] | 5931 | |
| 5932 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5933 | |
| 5934 | if( operation.alg == PSA_ALG_CCM ) |
| 5935 | { |
| 5936 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5937 | input_data->len ), |
| 5938 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5939 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5940 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5941 | &nonce_length ), |
| 5942 | PSA_ERROR_BAD_STATE ); |
| 5943 | } |
| 5944 | else |
| 5945 | { |
| 5946 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5947 | input_data->len ) ); |
| 5948 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5949 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5950 | &nonce_length ) ); |
| 5951 | } |
| 5952 | |
| 5953 | psa_aead_abort( &operation ); |
| 5954 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5955 | /* 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] | 5956 | #if SIZE_MAX > UINT32_MAX |
| 5957 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5958 | |
| 5959 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5960 | { |
| 5961 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5962 | input_data->len ), |
| 5963 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5964 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5965 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5966 | &nonce_length ), |
| 5967 | PSA_ERROR_BAD_STATE ); |
| 5968 | } |
| 5969 | else |
| 5970 | { |
| 5971 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5972 | input_data->len ) ); |
| 5973 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5974 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5975 | &nonce_length ) ); |
| 5976 | } |
| 5977 | |
| 5978 | psa_aead_abort( &operation ); |
| 5979 | #endif |
| 5980 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5981 | /* 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] | 5982 | |
| 5983 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5984 | |
| 5985 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5986 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5987 | &nonce_length ) ); |
| 5988 | |
| 5989 | if( operation.alg == PSA_ALG_CCM ) |
| 5990 | { |
| 5991 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5992 | input_data->len ), |
| 5993 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5994 | } |
| 5995 | else |
| 5996 | { |
| 5997 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5998 | input_data->len ) ); |
| 5999 | } |
| 6000 | |
| 6001 | psa_aead_abort( &operation ); |
| 6002 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6003 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6004 | /* Test for setting nonce after calling set lengths */ |
| 6005 | |
| 6006 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6007 | |
| 6008 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6009 | input_data->len ) ); |
| 6010 | |
| 6011 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6012 | |
| 6013 | psa_aead_abort( &operation ); |
| 6014 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6015 | /* 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] | 6016 | |
| 6017 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6018 | |
| 6019 | if( operation.alg == PSA_ALG_CCM ) |
| 6020 | { |
| 6021 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6022 | input_data->len ), |
| 6023 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6024 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6025 | PSA_ERROR_BAD_STATE ); |
| 6026 | } |
| 6027 | else |
| 6028 | { |
| 6029 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6030 | input_data->len ) ); |
| 6031 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6032 | } |
| 6033 | |
| 6034 | psa_aead_abort( &operation ); |
| 6035 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6036 | /* 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] | 6037 | #if SIZE_MAX > UINT32_MAX |
| 6038 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6039 | |
| 6040 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 6041 | { |
| 6042 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6043 | input_data->len ), |
| 6044 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6045 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6046 | PSA_ERROR_BAD_STATE ); |
| 6047 | } |
| 6048 | else |
| 6049 | { |
| 6050 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6051 | input_data->len ) ); |
| 6052 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6053 | } |
| 6054 | |
| 6055 | psa_aead_abort( &operation ); |
| 6056 | #endif |
| 6057 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6058 | /* 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] | 6059 | |
| 6060 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6061 | |
| 6062 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6063 | |
| 6064 | if( operation.alg == PSA_ALG_CCM ) |
| 6065 | { |
| 6066 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6067 | input_data->len ), |
| 6068 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6069 | } |
| 6070 | else |
| 6071 | { |
| 6072 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6073 | input_data->len ) ); |
| 6074 | } |
| 6075 | |
| 6076 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6077 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6078 | /* 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] | 6079 | #if SIZE_MAX > UINT32_MAX |
| 6080 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6081 | |
| 6082 | if( operation.alg == PSA_ALG_GCM ) |
| 6083 | { |
| 6084 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6085 | SIZE_MAX ), |
| 6086 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6087 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6088 | PSA_ERROR_BAD_STATE ); |
| 6089 | } |
| 6090 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6091 | { |
| 6092 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6093 | SIZE_MAX ) ); |
| 6094 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6095 | } |
| 6096 | |
| 6097 | psa_aead_abort( &operation ); |
| 6098 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6099 | /* 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] | 6100 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6101 | |
| 6102 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6103 | |
| 6104 | if( operation.alg == PSA_ALG_GCM ) |
| 6105 | { |
| 6106 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6107 | SIZE_MAX ), |
| 6108 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6109 | } |
| 6110 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6111 | { |
| 6112 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6113 | SIZE_MAX ) ); |
| 6114 | } |
| 6115 | |
| 6116 | psa_aead_abort( &operation ); |
| 6117 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6118 | |
| 6119 | /* ------------------------------------------------------- */ |
| 6120 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6121 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6122 | |
| 6123 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6124 | |
| 6125 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6126 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6127 | &nonce_length ), |
| 6128 | PSA_ERROR_BAD_STATE ); |
| 6129 | |
| 6130 | psa_aead_abort( &operation ); |
| 6131 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6132 | /* Test for generating nonce in decrypt setup. */ |
| 6133 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6134 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6135 | |
| 6136 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6137 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6138 | &nonce_length ), |
| 6139 | PSA_ERROR_BAD_STATE ); |
| 6140 | |
| 6141 | psa_aead_abort( &operation ); |
| 6142 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6143 | /* Test for setting lengths twice. */ |
| 6144 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6145 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6146 | |
| 6147 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6148 | |
| 6149 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6150 | input_data->len ) ); |
| 6151 | |
| 6152 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6153 | input_data->len ), |
| 6154 | PSA_ERROR_BAD_STATE ); |
| 6155 | |
| 6156 | psa_aead_abort( &operation ); |
| 6157 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6158 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6159 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6160 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6161 | |
| 6162 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6163 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6164 | if( operation.alg == PSA_ALG_CCM ) |
| 6165 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6166 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6167 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6168 | additional_data->len ), |
| 6169 | PSA_ERROR_BAD_STATE ); |
| 6170 | } |
| 6171 | else |
| 6172 | { |
| 6173 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6174 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6175 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6176 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6177 | input_data->len ), |
| 6178 | PSA_ERROR_BAD_STATE ); |
| 6179 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6180 | psa_aead_abort( &operation ); |
| 6181 | |
| 6182 | /* ------------------------------------------------------- */ |
| 6183 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6184 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6185 | |
| 6186 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6187 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6188 | if( operation.alg == PSA_ALG_CCM ) |
| 6189 | { |
| 6190 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6191 | input_data->len, output_data, |
| 6192 | output_size, &output_length ), |
| 6193 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6194 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6195 | } |
| 6196 | else |
| 6197 | { |
| 6198 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6199 | input_data->len, output_data, |
| 6200 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6201 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6202 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6203 | input_data->len ), |
| 6204 | PSA_ERROR_BAD_STATE ); |
| 6205 | } |
| 6206 | psa_aead_abort( &operation ); |
| 6207 | |
| 6208 | /* ------------------------------------------------------- */ |
| 6209 | |
| 6210 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6211 | |
| 6212 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6213 | |
| 6214 | if( operation.alg == PSA_ALG_CCM ) |
| 6215 | { |
| 6216 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6217 | finish_output_size, |
| 6218 | &output_part_length, |
| 6219 | tag_buffer, tag_length, |
| 6220 | &tag_size ) ); |
| 6221 | } |
| 6222 | else |
| 6223 | { |
| 6224 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6225 | finish_output_size, |
| 6226 | &output_part_length, |
| 6227 | tag_buffer, tag_length, |
| 6228 | &tag_size ) ); |
| 6229 | |
| 6230 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6231 | input_data->len ), |
| 6232 | PSA_ERROR_BAD_STATE ); |
| 6233 | } |
| 6234 | psa_aead_abort( &operation ); |
| 6235 | |
| 6236 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6237 | |
| 6238 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6239 | |
| 6240 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6241 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6242 | &nonce_length ) ); |
| 6243 | if( operation.alg == PSA_ALG_CCM ) |
| 6244 | { |
| 6245 | |
| 6246 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6247 | additional_data->len ), |
| 6248 | PSA_ERROR_BAD_STATE ); |
| 6249 | } |
| 6250 | else |
| 6251 | { |
| 6252 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6253 | additional_data->len ) ); |
| 6254 | |
| 6255 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6256 | input_data->len ), |
| 6257 | PSA_ERROR_BAD_STATE ); |
| 6258 | } |
| 6259 | psa_aead_abort( &operation ); |
| 6260 | |
| 6261 | /* ------------------------------------------------------- */ |
| 6262 | |
| 6263 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6264 | |
| 6265 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6266 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6267 | &nonce_length ) ); |
| 6268 | if( operation.alg == PSA_ALG_CCM ) |
| 6269 | { |
| 6270 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6271 | input_data->len, output_data, |
| 6272 | output_size, &output_length ), |
| 6273 | PSA_ERROR_BAD_STATE ); |
| 6274 | |
| 6275 | } |
| 6276 | else |
| 6277 | { |
| 6278 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6279 | input_data->len, output_data, |
| 6280 | output_size, &output_length ) ); |
| 6281 | |
| 6282 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6283 | input_data->len ), |
| 6284 | PSA_ERROR_BAD_STATE ); |
| 6285 | } |
| 6286 | psa_aead_abort( &operation ); |
| 6287 | |
| 6288 | /* ------------------------------------------------------- */ |
| 6289 | |
| 6290 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6291 | |
| 6292 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6293 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6294 | &nonce_length ) ); |
| 6295 | if( operation.alg == PSA_ALG_CCM ) |
| 6296 | { |
| 6297 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6298 | finish_output_size, |
| 6299 | &output_part_length, |
| 6300 | tag_buffer, tag_length, |
| 6301 | &tag_size ) ); |
| 6302 | } |
| 6303 | else |
| 6304 | { |
| 6305 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6306 | finish_output_size, |
| 6307 | &output_part_length, |
| 6308 | tag_buffer, tag_length, |
| 6309 | &tag_size ) ); |
| 6310 | |
| 6311 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6312 | input_data->len ), |
| 6313 | PSA_ERROR_BAD_STATE ); |
| 6314 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6315 | psa_aead_abort( &operation ); |
| 6316 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6317 | /* Test for not sending any additional data or data after setting non zero |
| 6318 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6319 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6320 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6321 | |
| 6322 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6323 | |
| 6324 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6325 | input_data->len ) ); |
| 6326 | |
| 6327 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6328 | finish_output_size, |
| 6329 | &output_part_length, |
| 6330 | tag_buffer, tag_length, |
| 6331 | &tag_size ), |
| 6332 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6333 | |
| 6334 | psa_aead_abort( &operation ); |
| 6335 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6336 | /* Test for not sending any additional data or data after setting non-zero |
| 6337 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6338 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6339 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6340 | |
| 6341 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6342 | |
| 6343 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6344 | input_data->len ) ); |
| 6345 | |
| 6346 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6347 | finish_output_size, |
| 6348 | &output_part_length, |
| 6349 | tag_buffer, |
| 6350 | tag_length ), |
| 6351 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6352 | |
| 6353 | psa_aead_abort( &operation ); |
| 6354 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6355 | /* Test for not sending any additional data after setting a non-zero length |
| 6356 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6357 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6358 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6359 | |
| 6360 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6361 | |
| 6362 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6363 | input_data->len ) ); |
| 6364 | |
| 6365 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6366 | input_data->len, output_data, |
| 6367 | output_size, &output_length ), |
| 6368 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6369 | |
| 6370 | psa_aead_abort( &operation ); |
| 6371 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6372 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6373 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6374 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6375 | |
| 6376 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6377 | |
| 6378 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6379 | input_data->len ) ); |
| 6380 | |
| 6381 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6382 | additional_data->len ) ); |
| 6383 | |
| 6384 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6385 | finish_output_size, |
| 6386 | &output_part_length, |
| 6387 | tag_buffer, tag_length, |
| 6388 | &tag_size ), |
| 6389 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6390 | |
| 6391 | psa_aead_abort( &operation ); |
| 6392 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6393 | /* Test for sending too much additional data after setting lengths. */ |
| 6394 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6395 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6396 | |
| 6397 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6398 | |
| 6399 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6400 | |
| 6401 | |
| 6402 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6403 | additional_data->len ), |
| 6404 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6405 | |
| 6406 | psa_aead_abort( &operation ); |
| 6407 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6408 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6409 | |
| 6410 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6411 | |
| 6412 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6413 | |
| 6414 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6415 | input_data->len ) ); |
| 6416 | |
| 6417 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6418 | additional_data->len ) ); |
| 6419 | |
| 6420 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6421 | 1 ), |
| 6422 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6423 | |
| 6424 | psa_aead_abort( &operation ); |
| 6425 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6426 | /* Test for sending too much data after setting lengths. */ |
| 6427 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 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, 0, 0 ) ); |
| 6433 | |
| 6434 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6435 | input_data->len, output_data, |
| 6436 | output_size, &output_length ), |
| 6437 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6438 | |
| 6439 | psa_aead_abort( &operation ); |
| 6440 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6441 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6442 | |
| 6443 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6444 | |
| 6445 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6446 | |
| 6447 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6448 | input_data->len ) ); |
| 6449 | |
| 6450 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6451 | additional_data->len ) ); |
| 6452 | |
| 6453 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6454 | input_data->len, output_data, |
| 6455 | output_size, &output_length ) ); |
| 6456 | |
| 6457 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6458 | 1, output_data, |
| 6459 | output_size, &output_length ), |
| 6460 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6461 | |
| 6462 | psa_aead_abort( &operation ); |
| 6463 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6464 | /* Test sending additional data after data. */ |
| 6465 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6466 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6467 | |
| 6468 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6469 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6470 | if( operation.alg != PSA_ALG_CCM ) |
| 6471 | { |
| 6472 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6473 | input_data->len, output_data, |
| 6474 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6475 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6476 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6477 | additional_data->len ), |
| 6478 | PSA_ERROR_BAD_STATE ); |
| 6479 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6480 | psa_aead_abort( &operation ); |
| 6481 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6482 | /* Test calling finish on decryption. */ |
| 6483 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6484 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6485 | |
| 6486 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6487 | |
| 6488 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6489 | finish_output_size, |
| 6490 | &output_part_length, |
| 6491 | tag_buffer, tag_length, |
| 6492 | &tag_size ), |
| 6493 | PSA_ERROR_BAD_STATE ); |
| 6494 | |
| 6495 | psa_aead_abort( &operation ); |
| 6496 | |
| 6497 | /* Test calling verify on encryption. */ |
| 6498 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6499 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6500 | |
| 6501 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6502 | |
| 6503 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6504 | finish_output_size, |
| 6505 | &output_part_length, |
| 6506 | tag_buffer, |
| 6507 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6508 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6509 | |
| 6510 | psa_aead_abort( &operation ); |
| 6511 | |
| 6512 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6513 | exit: |
| 6514 | psa_destroy_key( key ); |
| 6515 | psa_aead_abort( &operation ); |
| 6516 | mbedtls_free( output_data ); |
| 6517 | mbedtls_free( final_data ); |
| 6518 | PSA_DONE( ); |
| 6519 | } |
| 6520 | /* END_CASE */ |
| 6521 | |
| 6522 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6523 | void signature_size( int type_arg, |
| 6524 | int bits, |
| 6525 | int alg_arg, |
| 6526 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6527 | { |
| 6528 | psa_key_type_t type = type_arg; |
| 6529 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6530 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6531 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6532 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6533 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6534 | exit: |
| 6535 | ; |
| 6536 | } |
| 6537 | /* END_CASE */ |
| 6538 | |
| 6539 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6540 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6541 | int alg_arg, data_t *input_data, |
| 6542 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6543 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6544 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6545 | psa_key_type_t key_type = key_type_arg; |
| 6546 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6547 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6548 | unsigned char *signature = NULL; |
| 6549 | size_t signature_size; |
| 6550 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6551 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6552 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6553 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6554 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6555 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6556 | psa_set_key_algorithm( &attributes, alg ); |
| 6557 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6558 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6559 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6560 | &key ) ); |
| 6561 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6562 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6563 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6564 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6565 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6566 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6567 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6568 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6569 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6570 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6571 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6572 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6573 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6574 | input_data->x, input_data->len, |
| 6575 | signature, signature_size, |
| 6576 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6577 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6578 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6579 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6580 | |
| 6581 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6582 | /* |
| 6583 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6584 | * thus reset them as required. |
| 6585 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6586 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6587 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6588 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6589 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6590 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6591 | } |
| 6592 | /* END_CASE */ |
| 6593 | |
| 6594 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6595 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6596 | int alg_arg, data_t *input_data, |
| 6597 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6598 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6599 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6600 | psa_key_type_t key_type = key_type_arg; |
| 6601 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6602 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6603 | psa_status_t actual_status; |
| 6604 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6605 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6606 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6607 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6608 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6609 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6610 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6611 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6612 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6613 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6614 | psa_set_key_algorithm( &attributes, alg ); |
| 6615 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6616 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6617 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6618 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6619 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6620 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6621 | input_data->x, input_data->len, |
| 6622 | signature, signature_size, |
| 6623 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6624 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6625 | /* The value of *signature_length is unspecified on error, but |
| 6626 | * whatever it is, it should be less than signature_size, so that |
| 6627 | * if the caller tries to read *signature_length bytes without |
| 6628 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6629 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6630 | |
| 6631 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6632 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6633 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6634 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6635 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6636 | } |
| 6637 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6638 | |
| 6639 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6640 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6641 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6642 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6643 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6644 | psa_key_type_t key_type = key_type_arg; |
| 6645 | psa_algorithm_t alg = alg_arg; |
| 6646 | size_t key_bits; |
| 6647 | unsigned char *signature = NULL; |
| 6648 | size_t signature_size; |
| 6649 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6650 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6651 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6652 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6653 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6654 | 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] | 6655 | psa_set_key_algorithm( &attributes, alg ); |
| 6656 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6657 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6658 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6659 | &key ) ); |
| 6660 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6661 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6662 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6663 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6664 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6665 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6666 | key_bits, alg ); |
| 6667 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6668 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6669 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6670 | |
| 6671 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6672 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6673 | input_data->x, input_data->len, |
| 6674 | signature, signature_size, |
| 6675 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6676 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6677 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6678 | TEST_ASSERT( signature_length > 0 ); |
| 6679 | |
| 6680 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6681 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6682 | input_data->x, input_data->len, |
| 6683 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6684 | |
| 6685 | if( input_data->len != 0 ) |
| 6686 | { |
| 6687 | /* Flip a bit in the input and verify that the signature is now |
| 6688 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6689 | * because ECDSA may ignore the last few bits of the input. */ |
| 6690 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6691 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6692 | input_data->x, input_data->len, |
| 6693 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6694 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6695 | } |
| 6696 | |
| 6697 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6698 | /* |
| 6699 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6700 | * thus reset them as required. |
| 6701 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6702 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6703 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6704 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6705 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6706 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6707 | } |
| 6708 | /* END_CASE */ |
| 6709 | |
| 6710 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6711 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6712 | int alg_arg, data_t *hash_data, |
| 6713 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6714 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6715 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6716 | psa_key_type_t key_type = key_type_arg; |
| 6717 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6718 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6719 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6720 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6721 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6722 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6723 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6724 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6725 | psa_set_key_algorithm( &attributes, alg ); |
| 6726 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6727 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6728 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6729 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6730 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6731 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6732 | hash_data->x, hash_data->len, |
| 6733 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6734 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6735 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6736 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6737 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6738 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6739 | } |
| 6740 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6741 | |
| 6742 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6743 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6744 | int alg_arg, data_t *hash_data, |
| 6745 | data_t *signature_data, |
| 6746 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6747 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6748 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6749 | psa_key_type_t key_type = key_type_arg; |
| 6750 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6751 | psa_status_t actual_status; |
| 6752 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6753 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6754 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6755 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6756 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6757 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6758 | psa_set_key_algorithm( &attributes, alg ); |
| 6759 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6760 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6761 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6762 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6763 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6764 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6765 | hash_data->x, hash_data->len, |
| 6766 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6767 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6768 | |
| 6769 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6770 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6771 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6772 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6773 | } |
| 6774 | /* END_CASE */ |
| 6775 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6776 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6777 | void sign_message_deterministic( int key_type_arg, |
| 6778 | data_t *key_data, |
| 6779 | int alg_arg, |
| 6780 | data_t *input_data, |
| 6781 | data_t *output_data ) |
| 6782 | { |
| 6783 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6784 | psa_key_type_t key_type = key_type_arg; |
| 6785 | psa_algorithm_t alg = alg_arg; |
| 6786 | size_t key_bits; |
| 6787 | unsigned char *signature = NULL; |
| 6788 | size_t signature_size; |
| 6789 | size_t signature_length = 0xdeadbeef; |
| 6790 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6791 | |
| 6792 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6793 | |
| 6794 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6795 | psa_set_key_algorithm( &attributes, alg ); |
| 6796 | psa_set_key_type( &attributes, key_type ); |
| 6797 | |
| 6798 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6799 | &key ) ); |
| 6800 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6801 | key_bits = psa_get_key_bits( &attributes ); |
| 6802 | |
| 6803 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6804 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6805 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6806 | ASSERT_ALLOC( signature, signature_size ); |
| 6807 | |
| 6808 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6809 | input_data->x, input_data->len, |
| 6810 | signature, signature_size, |
| 6811 | &signature_length ) ); |
| 6812 | |
| 6813 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6814 | signature, signature_length ); |
| 6815 | |
| 6816 | exit: |
| 6817 | psa_reset_key_attributes( &attributes ); |
| 6818 | |
| 6819 | psa_destroy_key( key ); |
| 6820 | mbedtls_free( signature ); |
| 6821 | PSA_DONE( ); |
| 6822 | |
| 6823 | } |
| 6824 | /* END_CASE */ |
| 6825 | |
| 6826 | /* BEGIN_CASE */ |
| 6827 | void sign_message_fail( int key_type_arg, |
| 6828 | data_t *key_data, |
| 6829 | int alg_arg, |
| 6830 | data_t *input_data, |
| 6831 | int signature_size_arg, |
| 6832 | int expected_status_arg ) |
| 6833 | { |
| 6834 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6835 | psa_key_type_t key_type = key_type_arg; |
| 6836 | psa_algorithm_t alg = alg_arg; |
| 6837 | size_t signature_size = signature_size_arg; |
| 6838 | psa_status_t actual_status; |
| 6839 | psa_status_t expected_status = expected_status_arg; |
| 6840 | unsigned char *signature = NULL; |
| 6841 | size_t signature_length = 0xdeadbeef; |
| 6842 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6843 | |
| 6844 | ASSERT_ALLOC( signature, signature_size ); |
| 6845 | |
| 6846 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6847 | |
| 6848 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6849 | psa_set_key_algorithm( &attributes, alg ); |
| 6850 | psa_set_key_type( &attributes, key_type ); |
| 6851 | |
| 6852 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6853 | &key ) ); |
| 6854 | |
| 6855 | actual_status = psa_sign_message( key, alg, |
| 6856 | input_data->x, input_data->len, |
| 6857 | signature, signature_size, |
| 6858 | &signature_length ); |
| 6859 | TEST_EQUAL( actual_status, expected_status ); |
| 6860 | /* The value of *signature_length is unspecified on error, but |
| 6861 | * whatever it is, it should be less than signature_size, so that |
| 6862 | * if the caller tries to read *signature_length bytes without |
| 6863 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6864 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6865 | |
| 6866 | exit: |
| 6867 | psa_reset_key_attributes( &attributes ); |
| 6868 | psa_destroy_key( key ); |
| 6869 | mbedtls_free( signature ); |
| 6870 | PSA_DONE( ); |
| 6871 | } |
| 6872 | /* END_CASE */ |
| 6873 | |
| 6874 | /* BEGIN_CASE */ |
| 6875 | void sign_verify_message( int key_type_arg, |
| 6876 | data_t *key_data, |
| 6877 | int alg_arg, |
| 6878 | data_t *input_data ) |
| 6879 | { |
| 6880 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6881 | psa_key_type_t key_type = key_type_arg; |
| 6882 | psa_algorithm_t alg = alg_arg; |
| 6883 | size_t key_bits; |
| 6884 | unsigned char *signature = NULL; |
| 6885 | size_t signature_size; |
| 6886 | size_t signature_length = 0xdeadbeef; |
| 6887 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6888 | |
| 6889 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6890 | |
| 6891 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6892 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6893 | psa_set_key_algorithm( &attributes, alg ); |
| 6894 | psa_set_key_type( &attributes, key_type ); |
| 6895 | |
| 6896 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6897 | &key ) ); |
| 6898 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6899 | key_bits = psa_get_key_bits( &attributes ); |
| 6900 | |
| 6901 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6902 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6903 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6904 | ASSERT_ALLOC( signature, signature_size ); |
| 6905 | |
| 6906 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6907 | input_data->x, input_data->len, |
| 6908 | signature, signature_size, |
| 6909 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6910 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6911 | TEST_ASSERT( signature_length > 0 ); |
| 6912 | |
| 6913 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6914 | input_data->x, input_data->len, |
| 6915 | signature, signature_length ) ); |
| 6916 | |
| 6917 | if( input_data->len != 0 ) |
| 6918 | { |
| 6919 | /* Flip a bit in the input and verify that the signature is now |
| 6920 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6921 | * because ECDSA may ignore the last few bits of the input. */ |
| 6922 | input_data->x[0] ^= 1; |
| 6923 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6924 | input_data->x, input_data->len, |
| 6925 | signature, signature_length ), |
| 6926 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6927 | } |
| 6928 | |
| 6929 | exit: |
| 6930 | psa_reset_key_attributes( &attributes ); |
| 6931 | |
| 6932 | psa_destroy_key( key ); |
| 6933 | mbedtls_free( signature ); |
| 6934 | PSA_DONE( ); |
| 6935 | } |
| 6936 | /* END_CASE */ |
| 6937 | |
| 6938 | /* BEGIN_CASE */ |
| 6939 | void verify_message( int key_type_arg, |
| 6940 | data_t *key_data, |
| 6941 | int alg_arg, |
| 6942 | data_t *input_data, |
| 6943 | data_t *signature_data ) |
| 6944 | { |
| 6945 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6946 | psa_key_type_t key_type = key_type_arg; |
| 6947 | psa_algorithm_t alg = alg_arg; |
| 6948 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6949 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6950 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6951 | |
| 6952 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6953 | |
| 6954 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6955 | psa_set_key_algorithm( &attributes, alg ); |
| 6956 | psa_set_key_type( &attributes, key_type ); |
| 6957 | |
| 6958 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6959 | &key ) ); |
| 6960 | |
| 6961 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6962 | input_data->x, input_data->len, |
| 6963 | signature_data->x, signature_data->len ) ); |
| 6964 | |
| 6965 | exit: |
| 6966 | psa_reset_key_attributes( &attributes ); |
| 6967 | psa_destroy_key( key ); |
| 6968 | PSA_DONE( ); |
| 6969 | } |
| 6970 | /* END_CASE */ |
| 6971 | |
| 6972 | /* BEGIN_CASE */ |
| 6973 | void verify_message_fail( int key_type_arg, |
| 6974 | data_t *key_data, |
| 6975 | int alg_arg, |
| 6976 | data_t *hash_data, |
| 6977 | data_t *signature_data, |
| 6978 | int expected_status_arg ) |
| 6979 | { |
| 6980 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6981 | psa_key_type_t key_type = key_type_arg; |
| 6982 | psa_algorithm_t alg = alg_arg; |
| 6983 | psa_status_t actual_status; |
| 6984 | psa_status_t expected_status = expected_status_arg; |
| 6985 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6986 | |
| 6987 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6988 | |
| 6989 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6990 | psa_set_key_algorithm( &attributes, alg ); |
| 6991 | psa_set_key_type( &attributes, key_type ); |
| 6992 | |
| 6993 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6994 | &key ) ); |
| 6995 | |
| 6996 | actual_status = psa_verify_message( key, alg, |
| 6997 | hash_data->x, hash_data->len, |
| 6998 | signature_data->x, |
| 6999 | signature_data->len ); |
| 7000 | TEST_EQUAL( actual_status, expected_status ); |
| 7001 | |
| 7002 | exit: |
| 7003 | psa_reset_key_attributes( &attributes ); |
| 7004 | psa_destroy_key( key ); |
| 7005 | PSA_DONE( ); |
| 7006 | } |
| 7007 | /* END_CASE */ |
| 7008 | |
| 7009 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7010 | void asymmetric_encrypt( int key_type_arg, |
| 7011 | data_t *key_data, |
| 7012 | int alg_arg, |
| 7013 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7014 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7015 | int expected_output_length_arg, |
| 7016 | int expected_status_arg ) |
| 7017 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7018 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7019 | psa_key_type_t key_type = key_type_arg; |
| 7020 | psa_algorithm_t alg = alg_arg; |
| 7021 | size_t expected_output_length = expected_output_length_arg; |
| 7022 | size_t key_bits; |
| 7023 | unsigned char *output = NULL; |
| 7024 | size_t output_size; |
| 7025 | size_t output_length = ~0; |
| 7026 | psa_status_t actual_status; |
| 7027 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7028 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7029 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7030 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 7031 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7032 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7033 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7034 | psa_set_key_algorithm( &attributes, alg ); |
| 7035 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7036 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7037 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7038 | |
| 7039 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7040 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7041 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7042 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7043 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7044 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7045 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7046 | |
| 7047 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7048 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7049 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7050 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7051 | output, output_size, |
| 7052 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7053 | TEST_EQUAL( actual_status, expected_status ); |
| 7054 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7055 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7056 | /* If the label is empty, the test framework puts a non-null pointer |
| 7057 | * in label->x. Test that a null pointer works as well. */ |
| 7058 | if( label->len == 0 ) |
| 7059 | { |
| 7060 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7061 | if( output_size != 0 ) |
| 7062 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7063 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7064 | input_data->x, input_data->len, |
| 7065 | NULL, label->len, |
| 7066 | output, output_size, |
| 7067 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7068 | TEST_EQUAL( actual_status, expected_status ); |
| 7069 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7070 | } |
| 7071 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7072 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7073 | /* |
| 7074 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7075 | * thus reset them as required. |
| 7076 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7077 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7078 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7079 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7080 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7081 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7082 | } |
| 7083 | /* END_CASE */ |
| 7084 | |
| 7085 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7086 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 7087 | data_t *key_data, |
| 7088 | int alg_arg, |
| 7089 | data_t *input_data, |
| 7090 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7091 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7092 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7093 | psa_key_type_t key_type = key_type_arg; |
| 7094 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7095 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7096 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7097 | size_t output_size; |
| 7098 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7099 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7100 | size_t output2_size; |
| 7101 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7102 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7103 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7104 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7105 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7106 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 7107 | psa_set_key_algorithm( &attributes, alg ); |
| 7108 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7109 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7110 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7111 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7112 | |
| 7113 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7114 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7115 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7116 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7117 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7118 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7119 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7120 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7121 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7122 | TEST_LE_U( output2_size, |
| 7123 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 7124 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7125 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7126 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7127 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7128 | * the original plaintext because of the non-optional random |
| 7129 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7130 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7131 | input_data->x, input_data->len, |
| 7132 | label->x, label->len, |
| 7133 | output, output_size, |
| 7134 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7135 | /* We don't know what ciphertext length to expect, but check that |
| 7136 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7137 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7138 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7139 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7140 | output, output_length, |
| 7141 | label->x, label->len, |
| 7142 | output2, output2_size, |
| 7143 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7144 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 7145 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7146 | |
| 7147 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7148 | /* |
| 7149 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7150 | * thus reset them as required. |
| 7151 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7152 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7153 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7154 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7155 | mbedtls_free( output ); |
| 7156 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7157 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7158 | } |
| 7159 | /* END_CASE */ |
| 7160 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7161 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7162 | void asymmetric_decrypt( int key_type_arg, |
| 7163 | data_t *key_data, |
| 7164 | int alg_arg, |
| 7165 | data_t *input_data, |
| 7166 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 7167 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7168 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7169 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7170 | psa_key_type_t key_type = key_type_arg; |
| 7171 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7172 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7173 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7174 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7175 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7176 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7177 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7178 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7179 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7180 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7181 | psa_set_key_algorithm( &attributes, alg ); |
| 7182 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7183 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7184 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7185 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7186 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7187 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 7188 | key_bits = psa_get_key_bits( &attributes ); |
| 7189 | |
| 7190 | /* Determine the maximum ciphertext length */ |
| 7191 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7192 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7193 | ASSERT_ALLOC( output, output_size ); |
| 7194 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7195 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7196 | input_data->x, input_data->len, |
| 7197 | label->x, label->len, |
| 7198 | output, |
| 7199 | output_size, |
| 7200 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7201 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7202 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7203 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7204 | /* If the label is empty, the test framework puts a non-null pointer |
| 7205 | * in label->x. Test that a null pointer works as well. */ |
| 7206 | if( label->len == 0 ) |
| 7207 | { |
| 7208 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7209 | if( output_size != 0 ) |
| 7210 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7211 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7212 | input_data->x, input_data->len, |
| 7213 | NULL, label->len, |
| 7214 | output, |
| 7215 | output_size, |
| 7216 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7217 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7218 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7219 | } |
| 7220 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7221 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7222 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7223 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7224 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7225 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7226 | } |
| 7227 | /* END_CASE */ |
| 7228 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7229 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7230 | void asymmetric_decrypt_fail( int key_type_arg, |
| 7231 | data_t *key_data, |
| 7232 | int alg_arg, |
| 7233 | data_t *input_data, |
| 7234 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7235 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7236 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7237 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7238 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7239 | psa_key_type_t key_type = key_type_arg; |
| 7240 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7241 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7242 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7243 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7244 | psa_status_t actual_status; |
| 7245 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7246 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7247 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7248 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7249 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7250 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7251 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7252 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7253 | psa_set_key_algorithm( &attributes, alg ); |
| 7254 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7255 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7256 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7257 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7258 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7259 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7260 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7261 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7262 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7263 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7264 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7265 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7266 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7267 | /* If the label is empty, the test framework puts a non-null pointer |
| 7268 | * in label->x. Test that a null pointer works as well. */ |
| 7269 | if( label->len == 0 ) |
| 7270 | { |
| 7271 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7272 | if( output_size != 0 ) |
| 7273 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7274 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7275 | input_data->x, input_data->len, |
| 7276 | NULL, label->len, |
| 7277 | output, output_size, |
| 7278 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7279 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7280 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7281 | } |
| 7282 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7283 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7284 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7285 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7286 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7287 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7288 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7289 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7290 | |
| 7291 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 7292 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7293 | { |
| 7294 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7295 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7296 | * 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] | 7297 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7298 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7299 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 7300 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7301 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7302 | |
| 7303 | memset( &zero, 0, sizeof( zero ) ); |
| 7304 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7305 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7306 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7307 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7308 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7309 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7310 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7311 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7312 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7313 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7314 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7315 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7316 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7317 | } |
| 7318 | /* END_CASE */ |
| 7319 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7320 | /* BEGIN_CASE */ |
| 7321 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7322 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7323 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7324 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7325 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7326 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7327 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7328 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7329 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7330 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7331 | |
| 7332 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7333 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7334 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7335 | } |
| 7336 | /* END_CASE */ |
| 7337 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7338 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7339 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7340 | int expected_status_arg ) |
| 7341 | { |
| 7342 | psa_algorithm_t alg = alg_arg; |
| 7343 | size_t capacity = capacity_arg; |
| 7344 | psa_status_t expected_status = expected_status_arg; |
| 7345 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7346 | |
| 7347 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7348 | |
| 7349 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7350 | |
| 7351 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7352 | expected_status ); |
| 7353 | |
| 7354 | exit: |
| 7355 | psa_key_derivation_abort( &operation ); |
| 7356 | PSA_DONE( ); |
| 7357 | } |
| 7358 | /* END_CASE */ |
| 7359 | |
| 7360 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7361 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7362 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7363 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7364 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7365 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7366 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7367 | int expected_status_arg3, |
| 7368 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7369 | { |
| 7370 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7371 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7372 | 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] | 7373 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7374 | expected_status_arg2, |
| 7375 | expected_status_arg3}; |
| 7376 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7377 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7378 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7379 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7380 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7381 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7382 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7383 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7384 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7385 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7386 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7387 | |
| 7388 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7389 | |
| 7390 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7391 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7392 | |
| 7393 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7394 | |
| 7395 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7396 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7397 | mbedtls_test_set_step( i ); |
| 7398 | if( steps[i] == 0 ) |
| 7399 | { |
| 7400 | /* Skip this step */ |
| 7401 | } |
| 7402 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7403 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7404 | psa_set_key_type( &attributes, key_types[i] ); |
| 7405 | PSA_ASSERT( psa_import_key( &attributes, |
| 7406 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7407 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7408 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7409 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7410 | { |
| 7411 | // When taking a private key as secret input, use key agreement |
| 7412 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7413 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7414 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7415 | expected_statuses[i] ); |
| 7416 | } |
| 7417 | else |
| 7418 | { |
| 7419 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7420 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7421 | expected_statuses[i] ); |
| 7422 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7423 | } |
| 7424 | else |
| 7425 | { |
| 7426 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7427 | &operation, steps[i], |
| 7428 | inputs[i]->x, inputs[i]->len ), |
| 7429 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7430 | } |
| 7431 | } |
| 7432 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7433 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7434 | { |
| 7435 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7436 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7437 | psa_set_key_bits( &attributes, 8 ); |
| 7438 | actual_output_status = |
| 7439 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7440 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7441 | } |
| 7442 | else |
| 7443 | { |
| 7444 | uint8_t buffer[1]; |
| 7445 | actual_output_status = |
| 7446 | psa_key_derivation_output_bytes( &operation, |
| 7447 | buffer, sizeof( buffer ) ); |
| 7448 | } |
| 7449 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7450 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7451 | exit: |
| 7452 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7453 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7454 | psa_destroy_key( keys[i] ); |
| 7455 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7456 | PSA_DONE( ); |
| 7457 | } |
| 7458 | /* END_CASE */ |
| 7459 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7460 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7461 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7462 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7463 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7464 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7465 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7466 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7467 | unsigned char input1[] = "Input 1"; |
| 7468 | size_t input1_length = sizeof( input1 ); |
| 7469 | unsigned char input2[] = "Input 2"; |
| 7470 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7471 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7472 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7473 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7474 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7475 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7476 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7477 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7478 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7479 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7480 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7481 | psa_set_key_algorithm( &attributes, alg ); |
| 7482 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7483 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7484 | PSA_ASSERT( psa_import_key( &attributes, |
| 7485 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7486 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7487 | |
| 7488 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7489 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7490 | input1, input1_length, |
| 7491 | input2, input2_length, |
| 7492 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7493 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7494 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7495 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7496 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7497 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7498 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7499 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7500 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7501 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7502 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7503 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7504 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7505 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7506 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7507 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7508 | } |
| 7509 | /* END_CASE */ |
| 7510 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7511 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7512 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7513 | { |
| 7514 | uint8_t output_buffer[16]; |
| 7515 | size_t buffer_size = 16; |
| 7516 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7517 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7518 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7519 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7520 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7521 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7522 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7523 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7524 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7525 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7526 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7527 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7528 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7529 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7530 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7531 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7532 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7533 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7534 | |
| 7535 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7536 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7537 | } |
| 7538 | /* END_CASE */ |
| 7539 | |
| 7540 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7541 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7542 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7543 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7544 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7545 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7546 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7547 | int requested_capacity_arg, |
| 7548 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7549 | data_t *expected_output2, |
| 7550 | int other_key_input_type, |
| 7551 | int key_input_type, |
| 7552 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7553 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7554 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7555 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7556 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7557 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7558 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7559 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7560 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7561 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7562 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7563 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7564 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7565 | uint8_t *expected_outputs[2] = |
| 7566 | {expected_output1->x, expected_output2->x}; |
| 7567 | size_t output_sizes[2] = |
| 7568 | {expected_output1->len, expected_output2->len}; |
| 7569 | size_t output_buffer_size = 0; |
| 7570 | uint8_t *output_buffer = NULL; |
| 7571 | size_t expected_capacity; |
| 7572 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7573 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7574 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7575 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7576 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7577 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7578 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7579 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7580 | |
| 7581 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7582 | { |
| 7583 | if( output_sizes[i] > output_buffer_size ) |
| 7584 | output_buffer_size = output_sizes[i]; |
| 7585 | if( output_sizes[i] == 0 ) |
| 7586 | expected_outputs[i] = NULL; |
| 7587 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7588 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7589 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7590 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7591 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7592 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7593 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7594 | requested_capacity ) ); |
| 7595 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7596 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7597 | switch( steps[i] ) |
| 7598 | { |
| 7599 | case 0: |
| 7600 | break; |
| 7601 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7602 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7603 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7604 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7605 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7606 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7607 | inputs[i]->x, inputs[i]->len ), |
| 7608 | statuses[i] ); |
| 7609 | |
| 7610 | if( statuses[i] != PSA_SUCCESS ) |
| 7611 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7612 | break; |
| 7613 | case 1: // input key |
| 7614 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7615 | psa_set_key_algorithm( &attributes1, alg ); |
| 7616 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7617 | |
| 7618 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7619 | inputs[i]->x, inputs[i]->len, |
| 7620 | &keys[i] ) ); |
| 7621 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7622 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7623 | { |
| 7624 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7625 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7626 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7627 | } |
| 7628 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7629 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7630 | steps[i], |
| 7631 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7632 | break; |
| 7633 | default: |
| 7634 | TEST_ASSERT( ! "default case not supported" ); |
| 7635 | break; |
| 7636 | } |
| 7637 | break; |
| 7638 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7639 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7640 | { |
| 7641 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7642 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7643 | steps[i], |
| 7644 | inputs[i]->x, |
| 7645 | inputs[i]->len ), |
| 7646 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7647 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7648 | case 1: // input key, type DERIVE |
| 7649 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7650 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7651 | psa_set_key_algorithm( &attributes2, alg ); |
| 7652 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7653 | |
| 7654 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7655 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7656 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7657 | |
| 7658 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7659 | inputs[i]->x, inputs[i]->len, |
| 7660 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7661 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7662 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7663 | steps[i], |
| 7664 | keys[i] ), |
| 7665 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7666 | break; |
| 7667 | case 2: // key agreement |
| 7668 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7669 | psa_set_key_algorithm( &attributes3, alg ); |
| 7670 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7671 | |
| 7672 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7673 | inputs[i]->x, inputs[i]->len, |
| 7674 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7675 | |
| 7676 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7677 | &operation, |
| 7678 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7679 | keys[i], key_agreement_peer_key->x, |
| 7680 | key_agreement_peer_key->len ), statuses[i] ); |
| 7681 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7682 | default: |
| 7683 | TEST_ASSERT( ! "default case not supported" ); |
| 7684 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7685 | } |
| 7686 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7687 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7688 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7689 | break; |
| 7690 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7691 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7692 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7693 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7694 | |
| 7695 | if( statuses[i] != PSA_SUCCESS ) |
| 7696 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7697 | break; |
| 7698 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7699 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7700 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7701 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7702 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7703 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7704 | expected_capacity = requested_capacity; |
| 7705 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7706 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7707 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7708 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7709 | |
| 7710 | /* For output key derivation secret must be provided using |
| 7711 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7712 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7713 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7714 | |
| 7715 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7716 | psa_set_key_algorithm( &attributes4, alg ); |
| 7717 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7718 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7719 | |
| 7720 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7721 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7722 | } |
| 7723 | else // output bytes |
| 7724 | { |
| 7725 | /* Expansion phase. */ |
| 7726 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7727 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7728 | /* Read some bytes. */ |
| 7729 | status = psa_key_derivation_output_bytes( &operation, |
| 7730 | output_buffer, output_sizes[i] ); |
| 7731 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7732 | { |
| 7733 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7734 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7735 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7736 | continue; |
| 7737 | } |
| 7738 | else if( expected_capacity == 0 || |
| 7739 | output_sizes[i] > expected_capacity ) |
| 7740 | { |
| 7741 | /* Capacity exceeded. */ |
| 7742 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7743 | expected_capacity = 0; |
| 7744 | continue; |
| 7745 | } |
| 7746 | /* Success. Check the read data. */ |
| 7747 | PSA_ASSERT( status ); |
| 7748 | if( output_sizes[i] != 0 ) |
| 7749 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7750 | expected_outputs[i], output_sizes[i] ); |
| 7751 | /* Check the operation status. */ |
| 7752 | expected_capacity -= output_sizes[i]; |
| 7753 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7754 | ¤t_capacity ) ); |
| 7755 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7756 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7757 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7758 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7759 | |
| 7760 | exit: |
| 7761 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7762 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7763 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7764 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7765 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7766 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7767 | } |
| 7768 | /* END_CASE */ |
| 7769 | |
| 7770 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7771 | void derive_full( int alg_arg, |
| 7772 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7773 | data_t *input1, |
| 7774 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7775 | int requested_capacity_arg ) |
| 7776 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7777 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7778 | psa_algorithm_t alg = alg_arg; |
| 7779 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7780 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7781 | unsigned char output_buffer[16]; |
| 7782 | size_t expected_capacity = requested_capacity; |
| 7783 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7784 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7785 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7786 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7787 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7788 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7789 | psa_set_key_algorithm( &attributes, alg ); |
| 7790 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7791 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7792 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7793 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7794 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7795 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7796 | input1->x, input1->len, |
| 7797 | input2->x, input2->len, |
| 7798 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7799 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7800 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7801 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7802 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7803 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7804 | |
| 7805 | /* Expansion phase. */ |
| 7806 | while( current_capacity > 0 ) |
| 7807 | { |
| 7808 | size_t read_size = sizeof( output_buffer ); |
| 7809 | if( read_size > current_capacity ) |
| 7810 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7811 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7812 | output_buffer, |
| 7813 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7814 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7815 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7816 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7817 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7818 | } |
| 7819 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7820 | /* Check that the operation refuses to go over capacity. */ |
| 7821 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7822 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7823 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7824 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7825 | |
| 7826 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7827 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7828 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7829 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7830 | } |
| 7831 | /* END_CASE */ |
| 7832 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 7833 | /* 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] | 7834 | 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] | 7835 | int derivation_step, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7836 | int capacity, int expected_capacity_status_arg, |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7837 | data_t *expected_output, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7838 | int expected_output_status_arg ) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7839 | { |
| 7840 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7841 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 7842 | 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] | 7843 | uint8_t *output_buffer = NULL; |
| 7844 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7845 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 7846 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 7847 | 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] | 7848 | |
| 7849 | ASSERT_ALLOC( output_buffer, expected_output->len ); |
| 7850 | PSA_ASSERT( psa_crypto_init() ); |
| 7851 | |
| 7852 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7853 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7854 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7855 | |
| 7856 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7857 | step, input->x, input->len ), |
| 7858 | expected_input_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7859 | |
| 7860 | if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS ) |
| 7861 | goto exit; |
| 7862 | |
| 7863 | status = psa_key_derivation_output_bytes( &operation, output_buffer, |
| 7864 | expected_output->len ); |
| 7865 | |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7866 | TEST_EQUAL( status, expected_output_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7867 | if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS ) |
| 7868 | ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x, |
| 7869 | expected_output->len ); |
| 7870 | |
| 7871 | exit: |
| 7872 | mbedtls_free( output_buffer ); |
| 7873 | psa_key_derivation_abort( &operation ); |
| 7874 | PSA_DONE(); |
| 7875 | } |
| 7876 | /* END_CASE */ |
| 7877 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7878 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7879 | void derive_key_exercise( int alg_arg, |
| 7880 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7881 | data_t *input1, |
| 7882 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7883 | int derived_type_arg, |
| 7884 | int derived_bits_arg, |
| 7885 | int derived_usage_arg, |
| 7886 | int derived_alg_arg ) |
| 7887 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7888 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7889 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7890 | psa_algorithm_t alg = alg_arg; |
| 7891 | psa_key_type_t derived_type = derived_type_arg; |
| 7892 | size_t derived_bits = derived_bits_arg; |
| 7893 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7894 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7895 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7896 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7897 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7898 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7899 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7900 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7901 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7902 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7903 | psa_set_key_algorithm( &attributes, alg ); |
| 7904 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7905 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7906 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7907 | |
| 7908 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7909 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7910 | input1->x, input1->len, |
| 7911 | input2->x, input2->len, |
| 7912 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7913 | goto exit; |
| 7914 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7915 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7916 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7917 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7918 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7919 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7920 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7921 | |
| 7922 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7923 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7924 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7925 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7926 | |
| 7927 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7928 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7929 | goto exit; |
| 7930 | |
| 7931 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7932 | /* |
| 7933 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7934 | * thus reset them as required. |
| 7935 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7936 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7937 | |
| 7938 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7939 | psa_destroy_key( base_key ); |
| 7940 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7941 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7942 | } |
| 7943 | /* END_CASE */ |
| 7944 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7945 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7946 | void derive_key_export( int alg_arg, |
| 7947 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7948 | data_t *input1, |
| 7949 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7950 | int bytes1_arg, |
| 7951 | int bytes2_arg ) |
| 7952 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7953 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7954 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7955 | psa_algorithm_t alg = alg_arg; |
| 7956 | size_t bytes1 = bytes1_arg; |
| 7957 | size_t bytes2 = bytes2_arg; |
| 7958 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7959 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7960 | uint8_t *output_buffer = NULL; |
| 7961 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7962 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7963 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7964 | size_t length; |
| 7965 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7966 | ASSERT_ALLOC( output_buffer, capacity ); |
| 7967 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7968 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7969 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7970 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7971 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7972 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7973 | 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] | 7974 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7975 | |
| 7976 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7977 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7978 | input1->x, input1->len, |
| 7979 | input2->x, input2->len, |
| 7980 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7981 | goto exit; |
| 7982 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7983 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7984 | output_buffer, |
| 7985 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7986 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7987 | |
| 7988 | /* 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] | 7989 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7990 | input1->x, input1->len, |
| 7991 | input2->x, input2->len, |
| 7992 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7993 | goto exit; |
| 7994 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7995 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7996 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7997 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7998 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7999 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8000 | &derived_key ) ); |
| 8001 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8002 | export_buffer, bytes1, |
| 8003 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8004 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8005 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8006 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8007 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8008 | &derived_key ) ); |
| 8009 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8010 | export_buffer + bytes1, bytes2, |
| 8011 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8012 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8013 | |
| 8014 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8015 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 8016 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8017 | |
| 8018 | exit: |
| 8019 | mbedtls_free( output_buffer ); |
| 8020 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8021 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8022 | psa_destroy_key( base_key ); |
| 8023 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8024 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8025 | } |
| 8026 | /* END_CASE */ |
| 8027 | |
| 8028 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8029 | void derive_key_type( int alg_arg, |
| 8030 | data_t *key_data, |
| 8031 | data_t *input1, |
| 8032 | data_t *input2, |
| 8033 | int key_type_arg, int bits_arg, |
| 8034 | data_t *expected_export ) |
| 8035 | { |
| 8036 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8037 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8038 | const psa_algorithm_t alg = alg_arg; |
| 8039 | const psa_key_type_t key_type = key_type_arg; |
| 8040 | const size_t bits = bits_arg; |
| 8041 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8042 | const size_t export_buffer_size = |
| 8043 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 8044 | uint8_t *export_buffer = NULL; |
| 8045 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8046 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8047 | size_t export_length; |
| 8048 | |
| 8049 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 8050 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8051 | |
| 8052 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8053 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8054 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8055 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 8056 | &base_key ) ); |
| 8057 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8058 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8059 | &operation, base_key, alg, |
| 8060 | input1->x, input1->len, |
| 8061 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8062 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8063 | goto exit; |
| 8064 | |
| 8065 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8066 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8067 | psa_set_key_type( &derived_attributes, key_type ); |
| 8068 | psa_set_key_bits( &derived_attributes, bits ); |
| 8069 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 8070 | &derived_key ) ); |
| 8071 | |
| 8072 | PSA_ASSERT( psa_export_key( derived_key, |
| 8073 | export_buffer, export_buffer_size, |
| 8074 | &export_length ) ); |
| 8075 | ASSERT_COMPARE( export_buffer, export_length, |
| 8076 | expected_export->x, expected_export->len ); |
| 8077 | |
| 8078 | exit: |
| 8079 | mbedtls_free( export_buffer ); |
| 8080 | psa_key_derivation_abort( &operation ); |
| 8081 | psa_destroy_key( base_key ); |
| 8082 | psa_destroy_key( derived_key ); |
| 8083 | PSA_DONE( ); |
| 8084 | } |
| 8085 | /* END_CASE */ |
| 8086 | |
| 8087 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8088 | void derive_key( int alg_arg, |
| 8089 | data_t *key_data, data_t *input1, data_t *input2, |
| 8090 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8091 | int expected_status_arg, |
| 8092 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8093 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8094 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8095 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8096 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8097 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8098 | size_t bits = bits_arg; |
| 8099 | psa_status_t expected_status = expected_status_arg; |
| 8100 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8101 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8102 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8103 | |
| 8104 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8105 | |
| 8106 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8107 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8108 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8109 | 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] | 8110 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8111 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8112 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8113 | input1->x, input1->len, |
| 8114 | input2->x, input2->len, |
| 8115 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8116 | goto exit; |
| 8117 | |
| 8118 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8119 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8120 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8121 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8122 | |
| 8123 | psa_status_t status = |
| 8124 | psa_key_derivation_output_key( &derived_attributes, |
| 8125 | &operation, |
| 8126 | &derived_key ); |
| 8127 | if( is_large_output > 0 ) |
| 8128 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8129 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8130 | |
| 8131 | exit: |
| 8132 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8133 | psa_destroy_key( base_key ); |
| 8134 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8135 | PSA_DONE( ); |
| 8136 | } |
| 8137 | /* END_CASE */ |
| 8138 | |
| 8139 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8140 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 8141 | int our_key_type_arg, int our_key_alg_arg, |
| 8142 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8143 | int expected_status_arg ) |
| 8144 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8145 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8146 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8147 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8148 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8149 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8150 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8151 | psa_status_t expected_status = expected_status_arg; |
| 8152 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8153 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8154 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8155 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8156 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8157 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8158 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8159 | PSA_ASSERT( psa_import_key( &attributes, |
| 8160 | our_key_data->x, our_key_data->len, |
| 8161 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8162 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8163 | /* The tests currently include inputs that should fail at either step. |
| 8164 | * Test cases that fail at the setup step should be changed to call |
| 8165 | * key_derivation_setup instead, and this function should be renamed |
| 8166 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8167 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8168 | if( status == PSA_SUCCESS ) |
| 8169 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8170 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 8171 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8172 | our_key, |
| 8173 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8174 | expected_status ); |
| 8175 | } |
| 8176 | else |
| 8177 | { |
| 8178 | TEST_ASSERT( status == expected_status ); |
| 8179 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8180 | |
| 8181 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8182 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8183 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8184 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8185 | } |
| 8186 | /* END_CASE */ |
| 8187 | |
| 8188 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8189 | void raw_key_agreement( int alg_arg, |
| 8190 | int our_key_type_arg, data_t *our_key_data, |
| 8191 | data_t *peer_key_data, |
| 8192 | data_t *expected_output ) |
| 8193 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8194 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8195 | psa_algorithm_t alg = alg_arg; |
| 8196 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8197 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8198 | unsigned char *output = NULL; |
| 8199 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8200 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8201 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8202 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8203 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8204 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8205 | psa_set_key_algorithm( &attributes, alg ); |
| 8206 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8207 | PSA_ASSERT( psa_import_key( &attributes, |
| 8208 | our_key_data->x, our_key_data->len, |
| 8209 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8210 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8211 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 8212 | key_bits = psa_get_key_bits( &attributes ); |
| 8213 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8214 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8215 | TEST_LE_U( expected_output->len, |
| 8216 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 8217 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 8218 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8219 | |
| 8220 | /* Good case with exact output size */ |
| 8221 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 8222 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8223 | peer_key_data->x, peer_key_data->len, |
| 8224 | output, expected_output->len, |
| 8225 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8226 | ASSERT_COMPARE( output, output_length, |
| 8227 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8228 | mbedtls_free( output ); |
| 8229 | output = NULL; |
| 8230 | output_length = ~0; |
| 8231 | |
| 8232 | /* Larger buffer */ |
| 8233 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 8234 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8235 | peer_key_data->x, peer_key_data->len, |
| 8236 | output, expected_output->len + 1, |
| 8237 | &output_length ) ); |
| 8238 | ASSERT_COMPARE( output, output_length, |
| 8239 | expected_output->x, expected_output->len ); |
| 8240 | mbedtls_free( output ); |
| 8241 | output = NULL; |
| 8242 | output_length = ~0; |
| 8243 | |
| 8244 | /* Buffer too small */ |
| 8245 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 8246 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 8247 | peer_key_data->x, peer_key_data->len, |
| 8248 | output, expected_output->len - 1, |
| 8249 | &output_length ), |
| 8250 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8251 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8252 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8253 | mbedtls_free( output ); |
| 8254 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8255 | |
| 8256 | exit: |
| 8257 | mbedtls_free( output ); |
| 8258 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8259 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8260 | } |
| 8261 | /* END_CASE */ |
| 8262 | |
| 8263 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8264 | void key_agreement_capacity( int alg_arg, |
| 8265 | int our_key_type_arg, data_t *our_key_data, |
| 8266 | data_t *peer_key_data, |
| 8267 | int expected_capacity_arg ) |
| 8268 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8269 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8270 | psa_algorithm_t alg = alg_arg; |
| 8271 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8272 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8273 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8274 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8275 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8276 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8277 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8278 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8279 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8280 | psa_set_key_algorithm( &attributes, alg ); |
| 8281 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8282 | PSA_ASSERT( psa_import_key( &attributes, |
| 8283 | our_key_data->x, our_key_data->len, |
| 8284 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8285 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8286 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8287 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8288 | &operation, |
| 8289 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8290 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8291 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8292 | { |
| 8293 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8294 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8295 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8296 | NULL, 0 ) ); |
| 8297 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8298 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8299 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8300 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8301 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8302 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8303 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8304 | /* Test the actual capacity by reading the output. */ |
| 8305 | while( actual_capacity > sizeof( output ) ) |
| 8306 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8307 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8308 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8309 | actual_capacity -= sizeof( output ); |
| 8310 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8311 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8312 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8313 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 8314 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8315 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8316 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8317 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8318 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8319 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8320 | } |
| 8321 | /* END_CASE */ |
| 8322 | |
| 8323 | /* BEGIN_CASE */ |
| 8324 | void key_agreement_output( int alg_arg, |
| 8325 | int our_key_type_arg, data_t *our_key_data, |
| 8326 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8327 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8328 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8329 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8330 | psa_algorithm_t alg = alg_arg; |
| 8331 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8332 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8333 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8334 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8335 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8336 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 8337 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8338 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8339 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8340 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8341 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8342 | psa_set_key_algorithm( &attributes, alg ); |
| 8343 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8344 | PSA_ASSERT( psa_import_key( &attributes, |
| 8345 | our_key_data->x, our_key_data->len, |
| 8346 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8347 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8348 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8349 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8350 | &operation, |
| 8351 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8352 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8353 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8354 | { |
| 8355 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8356 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8357 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8358 | NULL, 0 ) ); |
| 8359 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8360 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8361 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8362 | actual_output, |
| 8363 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8364 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8365 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8366 | if( expected_output2->len != 0 ) |
| 8367 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8368 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8369 | actual_output, |
| 8370 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8371 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8372 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8373 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8374 | |
| 8375 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8376 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8377 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8378 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8379 | mbedtls_free( actual_output ); |
| 8380 | } |
| 8381 | /* END_CASE */ |
| 8382 | |
| 8383 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8384 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8385 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8386 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8387 | unsigned char *output = NULL; |
| 8388 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8389 | size_t i; |
| 8390 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8391 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8392 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8393 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8394 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8395 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8396 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8397 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8398 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8399 | /* Run several times, to ensure that every output byte will be |
| 8400 | * nonzero at least once with overwhelming probability |
| 8401 | * (2^(-8*number_of_runs)). */ |
| 8402 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8403 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8404 | if( bytes != 0 ) |
| 8405 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8406 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8407 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8408 | for( i = 0; i < bytes; i++ ) |
| 8409 | { |
| 8410 | if( output[i] != 0 ) |
| 8411 | ++changed[i]; |
| 8412 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8413 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8414 | |
| 8415 | /* Check that every byte was changed to nonzero at least once. This |
| 8416 | * validates that psa_generate_random is overwriting every byte of |
| 8417 | * the output buffer. */ |
| 8418 | for( i = 0; i < bytes; i++ ) |
| 8419 | { |
| 8420 | TEST_ASSERT( changed[i] != 0 ); |
| 8421 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8422 | |
| 8423 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8424 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8425 | mbedtls_free( output ); |
| 8426 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8427 | } |
| 8428 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8429 | |
| 8430 | /* BEGIN_CASE */ |
| 8431 | void generate_key( int type_arg, |
| 8432 | int bits_arg, |
| 8433 | int usage_arg, |
| 8434 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8435 | int expected_status_arg, |
| 8436 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8437 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8438 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8439 | psa_key_type_t type = type_arg; |
| 8440 | psa_key_usage_t usage = usage_arg; |
| 8441 | size_t bits = bits_arg; |
| 8442 | psa_algorithm_t alg = alg_arg; |
| 8443 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8444 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8445 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8446 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8447 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8448 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8449 | psa_set_key_usage_flags( &attributes, usage ); |
| 8450 | psa_set_key_algorithm( &attributes, alg ); |
| 8451 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8452 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8453 | |
| 8454 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8455 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8456 | |
| 8457 | if( is_large_key > 0 ) |
| 8458 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8459 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8460 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8461 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8462 | |
| 8463 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8464 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8465 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8466 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8467 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8468 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8469 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8470 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8471 | |
| 8472 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8473 | /* |
| 8474 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8475 | * thus reset them as required. |
| 8476 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8477 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8478 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8479 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8480 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8481 | } |
| 8482 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8483 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8484 | /* 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] | 8485 | void generate_key_rsa( int bits_arg, |
| 8486 | data_t *e_arg, |
| 8487 | int expected_status_arg ) |
| 8488 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8489 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8490 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8491 | size_t bits = bits_arg; |
| 8492 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8493 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8494 | psa_status_t expected_status = expected_status_arg; |
| 8495 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8496 | uint8_t *exported = NULL; |
| 8497 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8498 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8499 | size_t exported_length = SIZE_MAX; |
| 8500 | uint8_t *e_read_buffer = NULL; |
| 8501 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8502 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8503 | size_t e_read_length = SIZE_MAX; |
| 8504 | |
| 8505 | if( e_arg->len == 0 || |
| 8506 | ( e_arg->len == 3 && |
| 8507 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8508 | { |
| 8509 | is_default_public_exponent = 1; |
| 8510 | e_read_size = 0; |
| 8511 | } |
| 8512 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8513 | ASSERT_ALLOC( exported, exported_size ); |
| 8514 | |
| 8515 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8516 | |
| 8517 | psa_set_key_usage_flags( &attributes, usage ); |
| 8518 | psa_set_key_algorithm( &attributes, alg ); |
| 8519 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8520 | e_arg->x, e_arg->len ) ); |
| 8521 | psa_set_key_bits( &attributes, bits ); |
| 8522 | |
| 8523 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8524 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8525 | if( expected_status != PSA_SUCCESS ) |
| 8526 | goto exit; |
| 8527 | |
| 8528 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8529 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8530 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8531 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8532 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8533 | e_read_buffer, e_read_size, |
| 8534 | &e_read_length ) ); |
| 8535 | if( is_default_public_exponent ) |
| 8536 | TEST_EQUAL( e_read_length, 0 ); |
| 8537 | else |
| 8538 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8539 | |
| 8540 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8541 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8542 | goto exit; |
| 8543 | |
| 8544 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8545 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8546 | exported, exported_size, |
| 8547 | &exported_length ) ); |
| 8548 | { |
| 8549 | uint8_t *p = exported; |
| 8550 | uint8_t *end = exported + exported_length; |
| 8551 | size_t len; |
| 8552 | /* RSAPublicKey ::= SEQUENCE { |
| 8553 | * modulus INTEGER, -- n |
| 8554 | * publicExponent INTEGER } -- e |
| 8555 | */ |
| 8556 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8557 | MBEDTLS_ASN1_SEQUENCE | |
| 8558 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8559 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8560 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8561 | MBEDTLS_ASN1_INTEGER ) ); |
| 8562 | if( len >= 1 && p[0] == 0 ) |
| 8563 | { |
| 8564 | ++p; |
| 8565 | --len; |
| 8566 | } |
| 8567 | if( e_arg->len == 0 ) |
| 8568 | { |
| 8569 | TEST_EQUAL( len, 3 ); |
| 8570 | TEST_EQUAL( p[0], 1 ); |
| 8571 | TEST_EQUAL( p[1], 0 ); |
| 8572 | TEST_EQUAL( p[2], 1 ); |
| 8573 | } |
| 8574 | else |
| 8575 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8576 | } |
| 8577 | |
| 8578 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8579 | /* |
| 8580 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8581 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8582 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8583 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8584 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8585 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8586 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8587 | mbedtls_free( e_read_buffer ); |
| 8588 | mbedtls_free( exported ); |
| 8589 | } |
| 8590 | /* END_CASE */ |
| 8591 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8592 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8593 | void persistent_key_load_key_from_storage( data_t *data, |
| 8594 | int type_arg, int bits_arg, |
| 8595 | int usage_flags_arg, int alg_arg, |
| 8596 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8597 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8598 | 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] | 8599 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8600 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8601 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8602 | psa_key_type_t type = type_arg; |
| 8603 | size_t bits = bits_arg; |
| 8604 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8605 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8606 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8607 | unsigned char *first_export = NULL; |
| 8608 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8609 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8610 | size_t first_exported_length; |
| 8611 | size_t second_exported_length; |
| 8612 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8613 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8614 | { |
| 8615 | ASSERT_ALLOC( first_export, export_size ); |
| 8616 | ASSERT_ALLOC( second_export, export_size ); |
| 8617 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8618 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8619 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8620 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8621 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8622 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8623 | psa_set_key_algorithm( &attributes, alg ); |
| 8624 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8625 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8626 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8627 | switch( generation_method ) |
| 8628 | { |
| 8629 | case IMPORT_KEY: |
| 8630 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8631 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8632 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8633 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8634 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8635 | case GENERATE_KEY: |
| 8636 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8637 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8638 | break; |
| 8639 | |
| 8640 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8641 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8642 | { |
| 8643 | /* Create base key */ |
| 8644 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8645 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8646 | psa_set_key_usage_flags( &base_attributes, |
| 8647 | PSA_KEY_USAGE_DERIVE ); |
| 8648 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8649 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8650 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8651 | data->x, data->len, |
| 8652 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8653 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8654 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8655 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8656 | &operation, |
| 8657 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8658 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8659 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8660 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8661 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8662 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8663 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8664 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8665 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8666 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8667 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8668 | #else |
| 8669 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8670 | #endif |
| 8671 | break; |
| 8672 | |
| 8673 | default: |
| 8674 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8675 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8676 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8677 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8678 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8679 | /* Export the key if permitted by the key policy. */ |
| 8680 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8681 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8682 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8683 | first_export, export_size, |
| 8684 | &first_exported_length ) ); |
| 8685 | if( generation_method == IMPORT_KEY ) |
| 8686 | ASSERT_COMPARE( data->x, data->len, |
| 8687 | first_export, first_exported_length ); |
| 8688 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8689 | |
| 8690 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8691 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8692 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8693 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8694 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8695 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8696 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8697 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8698 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8699 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8700 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8701 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8702 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8703 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8704 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8705 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8706 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8707 | /* Export the key again if permitted by the key policy. */ |
| 8708 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8709 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8710 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8711 | second_export, export_size, |
| 8712 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8713 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8714 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8715 | } |
| 8716 | |
| 8717 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8718 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8719 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8720 | |
| 8721 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8722 | /* |
| 8723 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8724 | * thus reset them as required. |
| 8725 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8726 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8727 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8728 | mbedtls_free( first_export ); |
| 8729 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8730 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8731 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8732 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8733 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8734 | } |
| 8735 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8736 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8737 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8738 | void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 8739 | int primitive_arg, int hash_arg, int role_arg, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8740 | int input_first, data_t *pw_data, |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8741 | int expected_status_setup_arg, |
| 8742 | int expected_status_set_role_arg, |
| 8743 | int expected_status_set_password_key_arg, |
| 8744 | int expected_status_input_output_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8745 | { |
| 8746 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8747 | psa_pake_operation_t operation = psa_pake_operation_init(); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8748 | psa_pake_operation_t op_copy = psa_pake_operation_init(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8749 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8750 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8751 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 8752 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8753 | psa_algorithm_t hash_alg = hash_arg; |
| 8754 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8755 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8756 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8757 | psa_status_t expected_status_setup = expected_status_setup_arg; |
| 8758 | psa_status_t expected_status_set_role = expected_status_set_role_arg; |
| 8759 | psa_status_t expected_status_set_password_key = |
| 8760 | expected_status_set_password_key_arg; |
| 8761 | psa_status_t expected_status_input_output = |
| 8762 | expected_status_input_output_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8763 | unsigned char *output_buffer = NULL; |
| 8764 | size_t output_len = 0; |
| 8765 | |
| 8766 | PSA_INIT( ); |
| 8767 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8768 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8769 | PSA_PAKE_STEP_KEY_SHARE); |
| 8770 | ASSERT_ALLOC( output_buffer, buf_size ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8771 | |
| 8772 | if( pw_data->len > 0 ) |
| 8773 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8774 | psa_set_key_usage_flags( &attributes, key_usage_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8775 | psa_set_key_algorithm( &attributes, alg ); |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8776 | psa_set_key_type( &attributes, key_type_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8777 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8778 | &key ) ); |
| 8779 | } |
| 8780 | |
| 8781 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8782 | psa_pake_cs_set_primitive( &cipher_suite, primitive ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8783 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8784 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8785 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8786 | |
| 8787 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8788 | PSA_ERROR_BAD_STATE ); |
| 8789 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8790 | PSA_ERROR_BAD_STATE ); |
| 8791 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8792 | PSA_ERROR_BAD_STATE ); |
| 8793 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8794 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8795 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8796 | NULL, 0, NULL ), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8797 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8798 | 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] | 8799 | PSA_ERROR_BAD_STATE ); |
| 8800 | |
| 8801 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8802 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8803 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8804 | expected_status_setup ); |
| 8805 | if( expected_status_setup != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8806 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8807 | |
Neil Armstrong | 50de0ae | 2022-06-08 17:46:24 +0200 | [diff] [blame] | 8808 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8809 | PSA_ERROR_BAD_STATE ); |
| 8810 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8811 | TEST_EQUAL( psa_pake_set_role( &operation, role), |
| 8812 | expected_status_set_role ); |
| 8813 | if( expected_status_set_role != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8814 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8815 | |
| 8816 | if( pw_data->len > 0 ) |
| 8817 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8818 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8819 | expected_status_set_password_key ); |
| 8820 | if( expected_status_set_password_key != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8821 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8822 | } |
| 8823 | |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8824 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8825 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8826 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8827 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8828 | |
| 8829 | const uint8_t unsupported_id[] = "abcd"; |
| 8830 | |
| 8831 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8832 | PSA_ERROR_NOT_SUPPORTED ); |
| 8833 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8834 | PSA_ERROR_NOT_SUPPORTED ); |
| 8835 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8836 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8837 | PSA_PAKE_STEP_KEY_SHARE ); |
| 8838 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8839 | PSA_PAKE_STEP_ZK_PUBLIC ); |
| 8840 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8841 | PSA_PAKE_STEP_ZK_PROOF ); |
| 8842 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8843 | /* First round */ |
| 8844 | if( input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8845 | { |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8846 | /* Invalid parameters (input) */ |
| 8847 | op_copy = operation; |
| 8848 | TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8849 | NULL, 0 ), |
| 8850 | PSA_ERROR_INVALID_ARGUMENT ); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8851 | /* Invalid parameters (step) */ |
| 8852 | op_copy = operation; |
| 8853 | TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF + 10, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8854 | output_buffer, size_zk_proof ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8855 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8856 | /* Invalid first step */ |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8857 | op_copy = operation; |
| 8858 | TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8859 | output_buffer, size_zk_proof ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8860 | PSA_ERROR_BAD_STATE ); |
| 8861 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8862 | /* Possibly valid */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8863 | 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] | 8864 | output_buffer, size_key_share ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8865 | expected_status_input_output); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8866 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8867 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8868 | { |
| 8869 | /* Buffer too large */ |
| 8870 | 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] | 8871 | output_buffer, size_zk_public + 1 ), |
| 8872 | PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8873 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8874 | /* The operation's state should be invalidated at this point */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8875 | 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] | 8876 | output_buffer, size_zk_public ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8877 | PSA_ERROR_BAD_STATE ); |
| 8878 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8879 | } |
| 8880 | else |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8881 | { |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8882 | /* Invalid parameters (output) */ |
| 8883 | op_copy = operation; |
| 8884 | TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8885 | NULL, 0, NULL ), |
| 8886 | PSA_ERROR_INVALID_ARGUMENT ); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8887 | op_copy = operation; |
| 8888 | /* Invalid parameters (step) */ |
| 8889 | TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF + 10, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8890 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8891 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8892 | /* Invalid first step */ |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8893 | op_copy = operation; |
| 8894 | TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8895 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8896 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8897 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8898 | /* Possibly valid */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8899 | 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] | 8900 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8901 | expected_status_input_output ); |
Neil Armstrong | 98506ab | 2022-06-08 17:43:20 +0200 | [diff] [blame] | 8902 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8903 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8904 | { |
| 8905 | TEST_ASSERT( output_len > 0 ); |
| 8906 | |
| 8907 | /* Buffer too small */ |
| 8908 | 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] | 8909 | output_buffer, size_zk_public - 1, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8910 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8911 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8912 | /* The operation's state should be invalidated at this point */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8913 | 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] | 8914 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8915 | PSA_ERROR_BAD_STATE ); |
| 8916 | } |
| 8917 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8918 | |
| 8919 | exit: |
| 8920 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 8921 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8922 | mbedtls_free( output_buffer ); |
| 8923 | PSA_DONE( ); |
| 8924 | } |
| 8925 | /* END_CASE */ |
| 8926 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8927 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8928 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 8929 | int client_input_first, int inject_error, |
| 8930 | data_t *pw_data ) |
| 8931 | { |
| 8932 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8933 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8934 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8935 | psa_algorithm_t alg = alg_arg; |
| 8936 | psa_algorithm_t hash_alg = hash_arg; |
| 8937 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8938 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8939 | |
| 8940 | PSA_INIT( ); |
| 8941 | |
| 8942 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8943 | psa_set_key_algorithm( &attributes, alg ); |
| 8944 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8945 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8946 | &key ) ); |
| 8947 | |
| 8948 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8949 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8950 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8951 | |
| 8952 | |
| 8953 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8954 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8955 | |
| 8956 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8957 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8958 | |
| 8959 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8960 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8961 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8962 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8963 | client_input_first, 1, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8964 | |
| 8965 | if( inject_error == 1 || inject_error == 2 ) |
| 8966 | goto exit; |
| 8967 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8968 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8969 | client_input_first, 2, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8970 | |
| 8971 | exit: |
| 8972 | psa_destroy_key( key ); |
| 8973 | psa_pake_abort( &server ); |
| 8974 | psa_pake_abort( &client ); |
| 8975 | PSA_DONE( ); |
| 8976 | } |
| 8977 | /* END_CASE */ |
| 8978 | |
| 8979 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8980 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8981 | int derive_alg_arg, data_t *pw_data, |
| 8982 | int client_input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8983 | { |
| 8984 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8985 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8986 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8987 | psa_algorithm_t alg = alg_arg; |
| 8988 | psa_algorithm_t hash_alg = hash_arg; |
| 8989 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 8990 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8991 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8992 | psa_key_derivation_operation_t server_derive = |
| 8993 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8994 | psa_key_derivation_operation_t client_derive = |
| 8995 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8996 | |
| 8997 | PSA_INIT( ); |
| 8998 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8999 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 9000 | psa_set_key_algorithm( &attributes, alg ); |
| 9001 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 9002 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 9003 | &key ) ); |
| 9004 | |
| 9005 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 9006 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 9007 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 9008 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9009 | /* Get shared key */ |
| 9010 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 9011 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 9012 | |
| 9013 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 9014 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 9015 | { |
| 9016 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 9017 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9018 | (const uint8_t*) "", 0) ); |
| 9019 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 9020 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9021 | (const uint8_t*) "", 0) ); |
| 9022 | } |
| 9023 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9024 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 9025 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 9026 | |
| 9027 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 9028 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 9029 | |
| 9030 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 9031 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 9032 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9033 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 9034 | PSA_ERROR_BAD_STATE ); |
| 9035 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 9036 | PSA_ERROR_BAD_STATE ); |
| 9037 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9038 | /* First round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9039 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9040 | client_input_first, 1, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9041 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9042 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 9043 | PSA_ERROR_BAD_STATE ); |
| 9044 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 9045 | PSA_ERROR_BAD_STATE ); |
| 9046 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9047 | /* Second round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9048 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9049 | client_input_first, 2, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9050 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9051 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 9052 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 9053 | |
| 9054 | exit: |
| 9055 | psa_key_derivation_abort( &server_derive ); |
| 9056 | psa_key_derivation_abort( &client_derive ); |
| 9057 | psa_destroy_key( key ); |
| 9058 | psa_pake_abort( &server ); |
| 9059 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9060 | PSA_DONE( ); |
| 9061 | } |
| 9062 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9063 | |
| 9064 | /* BEGIN_CASE */ |
| 9065 | void ecjpake_size_macros( ) |
| 9066 | { |
| 9067 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 9068 | const size_t bits = 256; |
| 9069 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
| 9070 | PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits ); |
| 9071 | const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( |
| 9072 | PSA_ECC_FAMILY_SECP_R1 ); |
| 9073 | |
| 9074 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 9075 | /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */ |
| 9076 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9077 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9078 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9079 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9080 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
| 9081 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9082 | PSA_BITS_TO_BYTES( bits ) ); |
| 9083 | |
| 9084 | /* Input sizes are the same as output sizes */ |
| 9085 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9086 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) ); |
| 9087 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9088 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) ); |
| 9089 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9090 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) ); |
| 9091 | |
| 9092 | /* These inequalities will always hold even when other PAKEs are added */ |
| 9093 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9094 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9095 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9096 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9097 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9098 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9099 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9100 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9101 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9102 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9103 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9104 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9105 | } |
| 9106 | /* END_CASE */ |