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; |
| 721 | size_t buffer0_off = 0; |
| 722 | size_t buffer1_off = 0; |
| 723 | size_t s_g1_len, s_g2_len, s_a_len; |
| 724 | size_t s_g1_off, s_g2_off, s_a_off; |
| 725 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 726 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 727 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 728 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 729 | size_t c_g1_len, c_g2_len, c_a_len; |
| 730 | size_t c_g1_off, c_g2_off, c_a_off; |
| 731 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 732 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 733 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 734 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 735 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 736 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 737 | |
| 738 | ASSERT_ALLOC( buffer0, buffer_length ); |
| 739 | ASSERT_ALLOC( buffer1, buffer_length ); |
| 740 | |
| 741 | switch( round ) |
| 742 | { |
| 743 | case 1: |
| 744 | /* Server first round Output */ |
| 745 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 746 | buffer0 + buffer0_off, |
| 747 | 512 - buffer0_off, &s_g1_len ) ); |
| 748 | s_g1_off = buffer0_off; |
| 749 | buffer0_off += s_g1_len; |
| 750 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 751 | buffer0 + buffer0_off, |
| 752 | 512 - buffer0_off, &s_x1_pk_len ) ); |
| 753 | s_x1_pk_off = buffer0_off; |
| 754 | buffer0_off += s_x1_pk_len; |
| 755 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 756 | buffer0 + buffer0_off, |
| 757 | 512 - buffer0_off, &s_x1_pr_len ) ); |
| 758 | s_x1_pr_off = buffer0_off; |
| 759 | buffer0_off += s_x1_pr_len; |
| 760 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 761 | buffer0 + buffer0_off, |
| 762 | 512 - buffer0_off, &s_g2_len ) ); |
| 763 | s_g2_off = buffer0_off; |
| 764 | buffer0_off += s_g2_len; |
| 765 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 766 | buffer0 + buffer0_off, |
| 767 | 512 - buffer0_off, &s_x2_pk_len ) ); |
| 768 | s_x2_pk_off = buffer0_off; |
| 769 | buffer0_off += s_x2_pk_len; |
| 770 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 771 | buffer0 + buffer0_off, |
| 772 | 512 - buffer0_off, &s_x2_pr_len ) ); |
| 773 | s_x2_pr_off = buffer0_off; |
| 774 | buffer0_off += s_x2_pr_len; |
| 775 | |
| 776 | if( inject_error == 1 ) |
| 777 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 778 | buffer0[s_x1_pk_off + 8] >>= 4; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 779 | buffer0[s_x2_pk_off + 7] <<= 4; |
| 780 | expected_status = PSA_ERROR_DATA_INVALID; |
| 781 | } |
| 782 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 783 | /* |
| 784 | * When injecting errors in inputs, the implementation is |
| 785 | * free to detect it right away of with a delay. |
| 786 | * This permits delaying the error until the end of the input |
| 787 | * sequence, if no error appears then, this will be treated |
| 788 | * as an error. |
| 789 | */ |
| 790 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 791 | if( client_input_first == 1 ) |
| 792 | { |
| 793 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 794 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 795 | buffer0 + s_g1_off, s_g1_len ); |
| 796 | if( inject_error == 1 && status != PSA_SUCCESS ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 797 | { |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 798 | TEST_EQUAL( status, expected_status ); |
| 799 | break; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 800 | } |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 801 | else |
| 802 | { |
| 803 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 804 | } |
| 805 | |
| 806 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 807 | buffer0 + s_x1_pk_off, |
| 808 | s_x1_pk_len ); |
| 809 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 810 | { |
| 811 | TEST_EQUAL( status, expected_status ); |
| 812 | break; |
| 813 | } |
| 814 | else |
| 815 | { |
| 816 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 817 | } |
| 818 | |
| 819 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 820 | buffer0 + s_x1_pr_off, |
| 821 | s_x1_pr_len ); |
| 822 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 823 | { |
| 824 | TEST_EQUAL( status, expected_status ); |
| 825 | break; |
| 826 | } |
| 827 | else |
| 828 | { |
| 829 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 830 | } |
| 831 | |
| 832 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 833 | buffer0 + s_g2_off, |
| 834 | s_g2_len ); |
| 835 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 836 | { |
| 837 | TEST_EQUAL( status, expected_status ); |
| 838 | break; |
| 839 | } |
| 840 | else |
| 841 | { |
| 842 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 843 | } |
| 844 | |
| 845 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 846 | buffer0 + s_x2_pk_off, |
| 847 | s_x2_pk_len ); |
| 848 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 849 | { |
| 850 | TEST_EQUAL( status, expected_status ); |
| 851 | break; |
| 852 | } |
| 853 | else |
| 854 | { |
| 855 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 856 | } |
| 857 | |
| 858 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 859 | buffer0 + s_x2_pr_off, |
| 860 | s_x2_pr_len ); |
| 861 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 862 | { |
| 863 | TEST_EQUAL( status, expected_status ); |
| 864 | break; |
| 865 | } |
| 866 | else |
| 867 | { |
| 868 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 869 | } |
| 870 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 871 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 872 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 873 | 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] | 874 | } |
| 875 | |
| 876 | /* Client first round Output */ |
| 877 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 878 | buffer1 + buffer1_off, |
| 879 | 512 - buffer1_off, &c_g1_len ) ); |
| 880 | c_g1_off = buffer1_off; |
| 881 | buffer1_off += c_g1_len; |
| 882 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 883 | buffer1 + buffer1_off, |
| 884 | 512 - buffer1_off, &c_x1_pk_len ) ); |
| 885 | c_x1_pk_off = buffer1_off; |
| 886 | buffer1_off += c_x1_pk_len; |
| 887 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 888 | buffer1 + buffer1_off, |
| 889 | 512 - buffer1_off, &c_x1_pr_len ) ); |
| 890 | c_x1_pr_off = buffer1_off; |
| 891 | buffer1_off += c_x1_pr_len; |
| 892 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 893 | buffer1 + buffer1_off, |
| 894 | 512 - buffer1_off, &c_g2_len ) ); |
| 895 | c_g2_off = buffer1_off; |
| 896 | buffer1_off += c_g2_len; |
| 897 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 898 | buffer1 + buffer1_off, |
| 899 | 512 - buffer1_off, &c_x2_pk_len ) ); |
| 900 | c_x2_pk_off = buffer1_off; |
| 901 | buffer1_off += c_x2_pk_len; |
| 902 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 903 | buffer1 + buffer1_off, |
| 904 | 512 - buffer1_off, &c_x2_pr_len ) ); |
| 905 | c_x2_pr_off = buffer1_off; |
| 906 | buffer1_off += c_x2_pr_len; |
| 907 | |
| 908 | if( client_input_first == 0 ) |
| 909 | { |
| 910 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 911 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 912 | buffer0 + s_g1_off, s_g1_len ); |
| 913 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 914 | { |
| 915 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 916 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 917 | } |
| 918 | else |
| 919 | { |
| 920 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 921 | } |
| 922 | |
| 923 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 924 | buffer0 + s_x1_pk_off, |
| 925 | s_x1_pk_len ); |
| 926 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 927 | { |
| 928 | TEST_EQUAL( status, expected_status ); |
| 929 | break; |
| 930 | } |
| 931 | else |
| 932 | { |
| 933 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 934 | } |
| 935 | |
| 936 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 937 | buffer0 + s_x1_pr_off, |
| 938 | s_x1_pr_len ); |
| 939 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 940 | { |
| 941 | TEST_EQUAL( status, expected_status ); |
| 942 | break; |
| 943 | } |
| 944 | else |
| 945 | { |
| 946 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 947 | } |
| 948 | |
| 949 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 950 | buffer0 + s_g2_off, |
| 951 | s_g2_len ); |
| 952 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 953 | { |
| 954 | TEST_EQUAL( status, expected_status ); |
| 955 | break; |
| 956 | } |
| 957 | else |
| 958 | { |
| 959 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 960 | } |
| 961 | |
| 962 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 963 | buffer0 + s_x2_pk_off, |
| 964 | s_x2_pk_len ); |
| 965 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 966 | { |
| 967 | TEST_EQUAL( status, expected_status ); |
| 968 | break; |
| 969 | } |
| 970 | else |
| 971 | { |
| 972 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 973 | } |
| 974 | |
| 975 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 976 | buffer0 + s_x2_pr_off, |
| 977 | s_x2_pr_len ); |
| 978 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 979 | { |
| 980 | TEST_EQUAL( status, expected_status ); |
| 981 | break; |
| 982 | } |
| 983 | else |
| 984 | { |
| 985 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 986 | } |
| 987 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 988 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 989 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 990 | 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] | 991 | } |
| 992 | |
| 993 | if( inject_error == 2 ) |
| 994 | { |
| 995 | buffer1[c_x1_pk_off + 12] >>= 4; |
| 996 | buffer1[c_x2_pk_off + 7] <<= 4; |
| 997 | expected_status = PSA_ERROR_DATA_INVALID; |
| 998 | } |
| 999 | |
| 1000 | /* Server first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1001 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1002 | buffer1 + c_g1_off, c_g1_len ); |
| 1003 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1004 | { |
| 1005 | TEST_EQUAL( status, expected_status ); |
| 1006 | break; |
| 1007 | } |
| 1008 | else |
| 1009 | { |
| 1010 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1011 | } |
| 1012 | |
| 1013 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1014 | buffer1 + c_x1_pk_off, c_x1_pk_len ); |
| 1015 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1016 | { |
| 1017 | TEST_EQUAL( status, expected_status ); |
| 1018 | break; |
| 1019 | } |
| 1020 | else |
| 1021 | { |
| 1022 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1023 | } |
| 1024 | |
| 1025 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1026 | buffer1 + c_x1_pr_off, c_x1_pr_len ); |
| 1027 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1028 | { |
| 1029 | TEST_EQUAL( status, expected_status ); |
| 1030 | break; |
| 1031 | } |
| 1032 | else |
| 1033 | { |
| 1034 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1035 | } |
| 1036 | |
| 1037 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1038 | buffer1 + c_g2_off, c_g2_len ); |
| 1039 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1040 | { |
| 1041 | TEST_EQUAL( status, expected_status ); |
| 1042 | break; |
| 1043 | } |
| 1044 | else |
| 1045 | { |
| 1046 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1047 | } |
| 1048 | |
| 1049 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1050 | buffer1 + c_x2_pk_off, c_x2_pk_len ); |
| 1051 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1052 | { |
| 1053 | TEST_EQUAL( status, expected_status ); |
| 1054 | break; |
| 1055 | } |
| 1056 | else |
| 1057 | { |
| 1058 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1059 | } |
| 1060 | |
| 1061 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1062 | buffer1 + c_x2_pr_off, c_x2_pr_len ); |
| 1063 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1064 | { |
| 1065 | TEST_EQUAL( status, expected_status ); |
| 1066 | break; |
| 1067 | } |
| 1068 | else |
| 1069 | { |
| 1070 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1071 | } |
| 1072 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1073 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1074 | if( inject_error == 2 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1075 | 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] | 1076 | |
| 1077 | break; |
| 1078 | |
| 1079 | case 2: |
| 1080 | /* Server second round Output */ |
| 1081 | buffer0_off = 0; |
| 1082 | |
| 1083 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1084 | buffer0 + buffer0_off, |
| 1085 | 512 - buffer0_off, &s_a_len ) ); |
| 1086 | s_a_off = buffer0_off; |
| 1087 | buffer0_off += s_a_len; |
| 1088 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1089 | buffer0 + buffer0_off, |
| 1090 | 512 - buffer0_off, &s_x2s_pk_len ) ); |
| 1091 | s_x2s_pk_off = buffer0_off; |
| 1092 | buffer0_off += s_x2s_pk_len; |
| 1093 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1094 | buffer0 + buffer0_off, |
| 1095 | 512 - buffer0_off, &s_x2s_pr_len ) ); |
| 1096 | s_x2s_pr_off = buffer0_off; |
| 1097 | buffer0_off += s_x2s_pr_len; |
| 1098 | |
| 1099 | if( inject_error == 3 ) |
| 1100 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1101 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1102 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1103 | } |
| 1104 | |
| 1105 | if( client_input_first == 1 ) |
| 1106 | { |
| 1107 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1108 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1109 | buffer0 + s_a_off, s_a_len ); |
| 1110 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1111 | { |
| 1112 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1113 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1114 | } |
| 1115 | else |
| 1116 | { |
| 1117 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1118 | } |
| 1119 | |
| 1120 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1121 | buffer0 + s_x2s_pk_off, |
| 1122 | s_x2s_pk_len ); |
| 1123 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1124 | { |
| 1125 | TEST_EQUAL( status, expected_status ); |
| 1126 | break; |
| 1127 | } |
| 1128 | else |
| 1129 | { |
| 1130 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1131 | } |
| 1132 | |
| 1133 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1134 | buffer0 + s_x2s_pr_off, |
| 1135 | s_x2s_pr_len ); |
| 1136 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1137 | { |
| 1138 | TEST_EQUAL( status, expected_status ); |
| 1139 | break; |
| 1140 | } |
| 1141 | else |
| 1142 | { |
| 1143 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1144 | } |
| 1145 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1146 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1147 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1148 | 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] | 1149 | } |
| 1150 | |
| 1151 | /* Client second round Output */ |
| 1152 | buffer1_off = 0; |
| 1153 | |
| 1154 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1155 | buffer1 + buffer1_off, |
| 1156 | 512 - buffer1_off, &c_a_len ) ); |
| 1157 | c_a_off = buffer1_off; |
| 1158 | buffer1_off += c_a_len; |
| 1159 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1160 | buffer1 + buffer1_off, |
| 1161 | 512 - buffer1_off, &c_x2s_pk_len ) ); |
| 1162 | c_x2s_pk_off = buffer1_off; |
| 1163 | buffer1_off += c_x2s_pk_len; |
| 1164 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1165 | buffer1 + buffer1_off, |
| 1166 | 512 - buffer1_off, &c_x2s_pr_len ) ); |
| 1167 | c_x2s_pr_off = buffer1_off; |
| 1168 | buffer1_off += c_x2s_pr_len; |
| 1169 | |
| 1170 | if( client_input_first == 0 ) |
| 1171 | { |
| 1172 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1173 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1174 | buffer0 + s_a_off, s_a_len ); |
| 1175 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1176 | { |
| 1177 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1178 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1179 | } |
| 1180 | else |
| 1181 | { |
| 1182 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1183 | } |
| 1184 | |
| 1185 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1186 | buffer0 + s_x2s_pk_off, |
| 1187 | s_x2s_pk_len ); |
| 1188 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1189 | { |
| 1190 | TEST_EQUAL( status, expected_status ); |
| 1191 | break; |
| 1192 | } |
| 1193 | else |
| 1194 | { |
| 1195 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1196 | } |
| 1197 | |
| 1198 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1199 | buffer0 + s_x2s_pr_off, |
| 1200 | s_x2s_pr_len ); |
| 1201 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1202 | { |
| 1203 | TEST_EQUAL( status, expected_status ); |
| 1204 | break; |
| 1205 | } |
| 1206 | else |
| 1207 | { |
| 1208 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1209 | } |
| 1210 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1211 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1212 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1213 | 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] | 1214 | } |
| 1215 | |
| 1216 | if( inject_error == 4 ) |
| 1217 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1218 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1219 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1220 | } |
| 1221 | |
| 1222 | /* Server second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1223 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1224 | buffer1 + c_a_off, c_a_len ); |
| 1225 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1226 | { |
| 1227 | TEST_EQUAL( status, expected_status ); |
| 1228 | break; |
| 1229 | } |
| 1230 | else |
| 1231 | { |
| 1232 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1233 | } |
| 1234 | |
| 1235 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1236 | buffer1 + c_x2s_pk_off, c_x2s_pk_len ); |
| 1237 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1238 | { |
| 1239 | TEST_EQUAL( status, expected_status ); |
| 1240 | break; |
| 1241 | } |
| 1242 | else |
| 1243 | { |
| 1244 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1245 | } |
| 1246 | |
| 1247 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1248 | buffer1 + c_x2s_pr_off, c_x2s_pr_len ); |
| 1249 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1250 | { |
| 1251 | TEST_EQUAL( status, expected_status ); |
| 1252 | break; |
| 1253 | } |
| 1254 | else |
| 1255 | { |
| 1256 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1257 | } |
| 1258 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1259 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1260 | if( inject_error == 4 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1261 | 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] | 1262 | |
| 1263 | break; |
| 1264 | |
| 1265 | } |
| 1266 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1267 | exit: |
| 1268 | mbedtls_free( buffer0 ); |
| 1269 | mbedtls_free( buffer1 ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1270 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1271 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1272 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1273 | /* END_HEADER */ |
| 1274 | |
| 1275 | /* BEGIN_DEPENDENCIES |
| 1276 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1277 | * END_DEPENDENCIES |
| 1278 | */ |
| 1279 | |
| 1280 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1281 | void static_checks( ) |
| 1282 | { |
| 1283 | size_t max_truncated_mac_size = |
| 1284 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1285 | |
| 1286 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1287 | * encoding. The shifted mask is the maximum truncated value. The |
| 1288 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1289 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1290 | } |
| 1291 | /* END_CASE */ |
| 1292 | |
| 1293 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1294 | void import_with_policy( int type_arg, |
| 1295 | int usage_arg, int alg_arg, |
| 1296 | int expected_status_arg ) |
| 1297 | { |
| 1298 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1299 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1300 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1301 | psa_key_type_t type = type_arg; |
| 1302 | psa_key_usage_t usage = usage_arg; |
| 1303 | psa_algorithm_t alg = alg_arg; |
| 1304 | psa_status_t expected_status = expected_status_arg; |
| 1305 | const uint8_t key_material[16] = {0}; |
| 1306 | psa_status_t status; |
| 1307 | |
| 1308 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1309 | |
| 1310 | psa_set_key_type( &attributes, type ); |
| 1311 | psa_set_key_usage_flags( &attributes, usage ); |
| 1312 | psa_set_key_algorithm( &attributes, alg ); |
| 1313 | |
| 1314 | status = psa_import_key( &attributes, |
| 1315 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1316 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1317 | TEST_EQUAL( status, expected_status ); |
| 1318 | if( status != PSA_SUCCESS ) |
| 1319 | goto exit; |
| 1320 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1321 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1322 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1323 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1324 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1325 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1326 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1327 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1328 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1329 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1330 | |
| 1331 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1332 | /* |
| 1333 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1334 | * thus reset them as required. |
| 1335 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1336 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1337 | |
| 1338 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1339 | PSA_DONE( ); |
| 1340 | } |
| 1341 | /* END_CASE */ |
| 1342 | |
| 1343 | /* BEGIN_CASE */ |
| 1344 | void import_with_data( data_t *data, int type_arg, |
| 1345 | int attr_bits_arg, |
| 1346 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1347 | { |
| 1348 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1349 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1350 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1351 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1352 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1353 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1354 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1355 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1356 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1357 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1358 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1359 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1360 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1361 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1362 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1363 | if( status != PSA_SUCCESS ) |
| 1364 | goto exit; |
| 1365 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1366 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1367 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1368 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1369 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1370 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1371 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1372 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1373 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1374 | |
| 1375 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1376 | /* |
| 1377 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1378 | * thus reset them as required. |
| 1379 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1380 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1381 | |
| 1382 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1383 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1384 | } |
| 1385 | /* END_CASE */ |
| 1386 | |
| 1387 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1388 | void import_large_key( int type_arg, int byte_size_arg, |
| 1389 | int expected_status_arg ) |
| 1390 | { |
| 1391 | psa_key_type_t type = type_arg; |
| 1392 | size_t byte_size = byte_size_arg; |
| 1393 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1394 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1395 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1396 | psa_status_t status; |
| 1397 | uint8_t *buffer = NULL; |
| 1398 | size_t buffer_size = byte_size + 1; |
| 1399 | size_t n; |
| 1400 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1401 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1402 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1403 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1404 | memset( buffer, 'K', byte_size ); |
| 1405 | |
| 1406 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1407 | |
| 1408 | /* Try importing the key */ |
| 1409 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1410 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1411 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1412 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1413 | TEST_EQUAL( status, expected_status ); |
| 1414 | |
| 1415 | if( status == PSA_SUCCESS ) |
| 1416 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1417 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1418 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1419 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1420 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1421 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1422 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1423 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1424 | for( n = 0; n < byte_size; n++ ) |
| 1425 | TEST_EQUAL( buffer[n], 'K' ); |
| 1426 | for( n = byte_size; n < buffer_size; n++ ) |
| 1427 | TEST_EQUAL( buffer[n], 0 ); |
| 1428 | } |
| 1429 | |
| 1430 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1431 | /* |
| 1432 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1433 | * thus reset them as required. |
| 1434 | */ |
| 1435 | psa_reset_key_attributes( &attributes ); |
| 1436 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1437 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1438 | PSA_DONE( ); |
| 1439 | mbedtls_free( buffer ); |
| 1440 | } |
| 1441 | /* END_CASE */ |
| 1442 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1443 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1444 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1445 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1446 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1447 | size_t bits = bits_arg; |
| 1448 | psa_status_t expected_status = expected_status_arg; |
| 1449 | psa_status_t status; |
| 1450 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1451 | 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] | 1452 | size_t buffer_size = /* Slight overapproximations */ |
| 1453 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1454 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1455 | unsigned char *p; |
| 1456 | int ret; |
| 1457 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1458 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1459 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1460 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1461 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1462 | |
| 1463 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1464 | bits, keypair ) ) >= 0 ); |
| 1465 | length = ret; |
| 1466 | |
| 1467 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1468 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1469 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1470 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1471 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1472 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1473 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1474 | |
| 1475 | exit: |
| 1476 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1477 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1478 | } |
| 1479 | /* END_CASE */ |
| 1480 | |
| 1481 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1482 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1483 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1484 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1485 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1486 | int expected_bits, |
| 1487 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1488 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1489 | int canonical_input ) |
| 1490 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1491 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1492 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1493 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1494 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1495 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1496 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1497 | unsigned char *exported = NULL; |
| 1498 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1499 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1500 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1501 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1502 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1503 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1504 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1505 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1506 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1507 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1508 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1509 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1510 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1511 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1512 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1513 | psa_set_key_algorithm( &attributes, alg ); |
| 1514 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1515 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1516 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1517 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1518 | |
| 1519 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1520 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1521 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1522 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1523 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1524 | |
| 1525 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1526 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1527 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1528 | |
| 1529 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1530 | * and export_size. On errors, the exported length must be 0. */ |
| 1531 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1532 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1533 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1534 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1535 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1536 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1537 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1538 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1539 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1540 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1541 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1542 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1543 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1544 | * this validates the canonical representations. For canonical inputs, |
| 1545 | * this doesn't directly validate the implementation, but it still helps |
| 1546 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1547 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1548 | { |
| 1549 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1550 | goto exit; |
| 1551 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1552 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1553 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1554 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1555 | else |
| 1556 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1557 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1558 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1559 | &key2 ) ); |
| 1560 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1561 | reexported, |
| 1562 | export_size, |
| 1563 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1564 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1565 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1566 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1567 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1568 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1569 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1570 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1571 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1572 | |
| 1573 | destroy: |
| 1574 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1575 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1576 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1577 | |
| 1578 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1579 | /* |
| 1580 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1581 | * thus reset them as required. |
| 1582 | */ |
| 1583 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1584 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1585 | mbedtls_free( exported ); |
| 1586 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1587 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1588 | } |
| 1589 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1590 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1591 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1592 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1593 | int type_arg, |
| 1594 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1595 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1596 | int export_size_delta, |
| 1597 | int expected_export_status_arg, |
| 1598 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1599 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1600 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1601 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1602 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1603 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1604 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1605 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1606 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1607 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1608 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1609 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1610 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1611 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1612 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1613 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1614 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1615 | psa_set_key_algorithm( &attributes, alg ); |
| 1616 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1617 | |
| 1618 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1619 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1620 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1621 | /* Export the public key */ |
| 1622 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1623 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1624 | exported, export_size, |
| 1625 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1626 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1627 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1628 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1629 | 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] | 1630 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1631 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1632 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1633 | TEST_LE_U( expected_public_key->len, |
| 1634 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1635 | TEST_LE_U( expected_public_key->len, |
| 1636 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1637 | TEST_LE_U( expected_public_key->len, |
| 1638 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1639 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1640 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1641 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1642 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1643 | /* |
| 1644 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1645 | * thus reset them as required. |
| 1646 | */ |
| 1647 | psa_reset_key_attributes( &attributes ); |
| 1648 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1649 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1650 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1651 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1652 | } |
| 1653 | /* END_CASE */ |
| 1654 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1655 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1656 | void import_and_exercise_key( data_t *data, |
| 1657 | int type_arg, |
| 1658 | int bits_arg, |
| 1659 | int alg_arg ) |
| 1660 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1661 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1662 | psa_key_type_t type = type_arg; |
| 1663 | size_t bits = bits_arg; |
| 1664 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1665 | 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] | 1666 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1667 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1668 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1669 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1670 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1671 | psa_set_key_usage_flags( &attributes, usage ); |
| 1672 | psa_set_key_algorithm( &attributes, alg ); |
| 1673 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1674 | |
| 1675 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1676 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1677 | |
| 1678 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1679 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1680 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1681 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1682 | |
| 1683 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1684 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1685 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1686 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1687 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1688 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1689 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1690 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1691 | /* |
| 1692 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1693 | * thus reset them as required. |
| 1694 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1695 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1696 | |
| 1697 | psa_reset_key_attributes( &attributes ); |
| 1698 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1699 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1700 | } |
| 1701 | /* END_CASE */ |
| 1702 | |
| 1703 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1704 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1705 | int bits_arg, int expected_bits_arg, |
| 1706 | int usage_arg, int expected_usage_arg, |
| 1707 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1708 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1709 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1710 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1711 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1712 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1713 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1714 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1715 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1716 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1717 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1718 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1719 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1720 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1721 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1722 | psa_set_key_usage_flags( &attributes, usage ); |
| 1723 | psa_set_key_algorithm( &attributes, alg ); |
| 1724 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1725 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1726 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1727 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1728 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1729 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1730 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1731 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1732 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1733 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1734 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1735 | |
| 1736 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1737 | /* |
| 1738 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1739 | * thus reset them as required. |
| 1740 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1741 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1742 | |
| 1743 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1744 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1745 | } |
| 1746 | /* END_CASE */ |
| 1747 | |
| 1748 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1749 | void check_key_policy( int type_arg, int bits_arg, |
| 1750 | int usage_arg, int alg_arg ) |
| 1751 | { |
| 1752 | 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] | 1753 | usage_arg, |
| 1754 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1755 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1756 | goto exit; |
| 1757 | } |
| 1758 | /* END_CASE */ |
| 1759 | |
| 1760 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1761 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1762 | { |
| 1763 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1764 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1765 | * 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] | 1766 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1767 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1768 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1769 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1770 | |
| 1771 | memset( &zero, 0, sizeof( zero ) ); |
| 1772 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1773 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1774 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1775 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1776 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1777 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1778 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1779 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1780 | |
| 1781 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1782 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1783 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1784 | |
| 1785 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1786 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1787 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1788 | |
| 1789 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1790 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1791 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1792 | } |
| 1793 | /* END_CASE */ |
| 1794 | |
| 1795 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1796 | void mac_key_policy( int policy_usage_arg, |
| 1797 | int policy_alg_arg, |
| 1798 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1799 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1800 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1801 | int expected_status_sign_arg, |
| 1802 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1803 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1804 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1805 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1806 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1807 | psa_key_type_t key_type = key_type_arg; |
| 1808 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1809 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1810 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1811 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1812 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1813 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1814 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1815 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1816 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1817 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1818 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1819 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1820 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1821 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1822 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1823 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1824 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1825 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1826 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1827 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1828 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1829 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1830 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1831 | /* Calculate the MAC, one-shot case. */ |
| 1832 | uint8_t input[128] = {0}; |
| 1833 | size_t mac_len; |
| 1834 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1835 | input, 128, |
| 1836 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1837 | expected_status_sign ); |
| 1838 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1839 | /* Calculate the MAC, multi-part case. */ |
| 1840 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1841 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1842 | if( status == PSA_SUCCESS ) |
| 1843 | { |
| 1844 | status = psa_mac_update( &operation, input, 128 ); |
| 1845 | if( status == PSA_SUCCESS ) |
| 1846 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1847 | &mac_len ), |
| 1848 | expected_status_sign ); |
| 1849 | else |
| 1850 | TEST_EQUAL( status, expected_status_sign ); |
| 1851 | } |
| 1852 | else |
| 1853 | { |
| 1854 | TEST_EQUAL( status, expected_status_sign ); |
| 1855 | } |
| 1856 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1857 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1858 | /* Verify correct MAC, one-shot case. */ |
| 1859 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1860 | mac, mac_len ); |
| 1861 | |
| 1862 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1863 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1864 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1865 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1866 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1867 | /* Verify correct MAC, multi-part case. */ |
| 1868 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1869 | if( status == PSA_SUCCESS ) |
| 1870 | { |
| 1871 | status = psa_mac_update( &operation, input, 128 ); |
| 1872 | if( status == PSA_SUCCESS ) |
| 1873 | { |
| 1874 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1875 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1876 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1877 | else |
| 1878 | TEST_EQUAL( status, expected_status_verify ); |
| 1879 | } |
| 1880 | else |
| 1881 | { |
| 1882 | TEST_EQUAL( status, expected_status_verify ); |
| 1883 | } |
| 1884 | } |
| 1885 | else |
| 1886 | { |
| 1887 | TEST_EQUAL( status, expected_status_verify ); |
| 1888 | } |
| 1889 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1890 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1891 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1892 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1893 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1894 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1895 | |
| 1896 | exit: |
| 1897 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1898 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1899 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1900 | } |
| 1901 | /* END_CASE */ |
| 1902 | |
| 1903 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1904 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1905 | int policy_alg, |
| 1906 | int key_type, |
| 1907 | data_t *key_data, |
| 1908 | int exercise_alg ) |
| 1909 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1910 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1911 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1912 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1913 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1914 | size_t output_buffer_size = 0; |
| 1915 | size_t input_buffer_size = 0; |
| 1916 | size_t output_length = 0; |
| 1917 | uint8_t *output = NULL; |
| 1918 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1919 | psa_status_t status; |
| 1920 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1921 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1922 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1923 | input_buffer_size ); |
| 1924 | |
| 1925 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1926 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1927 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1928 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1929 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1930 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1931 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1932 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1933 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1934 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1935 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1936 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1937 | /* Check if no key usage flag implication is done */ |
| 1938 | TEST_EQUAL( policy_usage, |
| 1939 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1940 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1941 | /* Encrypt check, one-shot */ |
| 1942 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1943 | output, output_buffer_size, |
| 1944 | &output_length); |
| 1945 | if( policy_alg == exercise_alg && |
| 1946 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1947 | PSA_ASSERT( status ); |
| 1948 | else |
| 1949 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1950 | |
| 1951 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1952 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1953 | if( policy_alg == exercise_alg && |
| 1954 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1955 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1956 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1957 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1958 | psa_cipher_abort( &operation ); |
| 1959 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1960 | /* Decrypt check, one-shot */ |
| 1961 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 1962 | input, input_buffer_size, |
| 1963 | &output_length); |
| 1964 | if( policy_alg == exercise_alg && |
| 1965 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1966 | PSA_ASSERT( status ); |
| 1967 | else |
| 1968 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1969 | |
| 1970 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1971 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1972 | if( policy_alg == exercise_alg && |
| 1973 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1974 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1975 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1976 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1977 | |
| 1978 | exit: |
| 1979 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1980 | mbedtls_free( input ); |
| 1981 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1982 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1983 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1984 | } |
| 1985 | /* END_CASE */ |
| 1986 | |
| 1987 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1988 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1989 | int policy_alg, |
| 1990 | int key_type, |
| 1991 | data_t *key_data, |
| 1992 | int nonce_length_arg, |
| 1993 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1994 | int exercise_alg, |
| 1995 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1996 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1997 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1998 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1999 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2000 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2001 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2002 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2003 | unsigned char nonce[16] = {0}; |
| 2004 | size_t nonce_length = nonce_length_arg; |
| 2005 | unsigned char tag[16]; |
| 2006 | size_t tag_length = tag_length_arg; |
| 2007 | size_t output_length; |
| 2008 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2009 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 2010 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2011 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2012 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2013 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2014 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2015 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2016 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2017 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2018 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2019 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2020 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2021 | /* Check if no key usage implication is done */ |
| 2022 | TEST_EQUAL( policy_usage, |
| 2023 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2024 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2025 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2026 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2027 | nonce, nonce_length, |
| 2028 | NULL, 0, |
| 2029 | NULL, 0, |
| 2030 | tag, tag_length, |
| 2031 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2032 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2033 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2034 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2035 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2036 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2037 | /* Encrypt check, multi-part */ |
| 2038 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 2039 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2040 | TEST_EQUAL( status, expected_status ); |
| 2041 | else |
| 2042 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2043 | |
| 2044 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2045 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2046 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2047 | nonce, nonce_length, |
| 2048 | NULL, 0, |
| 2049 | tag, tag_length, |
| 2050 | NULL, 0, |
| 2051 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2052 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2053 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2054 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2055 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2056 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2057 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2058 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2059 | /* Decrypt check, multi-part */ |
| 2060 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 2061 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 2062 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2063 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2064 | else |
| 2065 | TEST_EQUAL( status, expected_status ); |
| 2066 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2067 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2068 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2069 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2070 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2071 | } |
| 2072 | /* END_CASE */ |
| 2073 | |
| 2074 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2075 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2076 | int policy_alg, |
| 2077 | int key_type, |
| 2078 | data_t *key_data, |
| 2079 | int exercise_alg ) |
| 2080 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2081 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2082 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2083 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2084 | psa_status_t status; |
| 2085 | size_t key_bits; |
| 2086 | size_t buffer_length; |
| 2087 | unsigned char *buffer = NULL; |
| 2088 | size_t output_length; |
| 2089 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2090 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2091 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2092 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2093 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2094 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2095 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2096 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2097 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2098 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2099 | /* Check if no key usage implication is done */ |
| 2100 | TEST_EQUAL( policy_usage, |
| 2101 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2102 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2103 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2104 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2105 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2106 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2107 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2108 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2109 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2110 | NULL, 0, |
| 2111 | NULL, 0, |
| 2112 | buffer, buffer_length, |
| 2113 | &output_length ); |
| 2114 | if( policy_alg == exercise_alg && |
| 2115 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2116 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2117 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2118 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2119 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2120 | if( buffer_length != 0 ) |
| 2121 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2122 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2123 | buffer, buffer_length, |
| 2124 | NULL, 0, |
| 2125 | buffer, buffer_length, |
| 2126 | &output_length ); |
| 2127 | if( policy_alg == exercise_alg && |
| 2128 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2129 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2130 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2131 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2132 | |
| 2133 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2134 | /* |
| 2135 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2136 | * thus reset them as required. |
| 2137 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2138 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2139 | |
| 2140 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2141 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2142 | mbedtls_free( buffer ); |
| 2143 | } |
| 2144 | /* END_CASE */ |
| 2145 | |
| 2146 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2147 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2148 | int policy_alg, |
| 2149 | int key_type, |
| 2150 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2151 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2152 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2153 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2154 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2155 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2156 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2157 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2158 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2159 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2160 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2161 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2162 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2163 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2164 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2165 | int compatible_alg = payload_length_arg > 0; |
| 2166 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2167 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2168 | size_t signature_length; |
| 2169 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2170 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2171 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2172 | TEST_EQUAL( expected_usage, |
| 2173 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2174 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2175 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2176 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2177 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2178 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2179 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2180 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2181 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2182 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2183 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2184 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 2185 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2186 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2187 | payload, payload_length, |
| 2188 | signature, sizeof( signature ), |
| 2189 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2190 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2191 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2192 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2193 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2194 | |
| 2195 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2196 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2197 | payload, payload_length, |
| 2198 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2199 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2200 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2201 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2202 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2203 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2204 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 2205 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2206 | { |
| 2207 | status = psa_sign_message( key, exercise_alg, |
| 2208 | payload, payload_length, |
| 2209 | signature, sizeof( signature ), |
| 2210 | &signature_length ); |
| 2211 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 2212 | PSA_ASSERT( status ); |
| 2213 | else |
| 2214 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2215 | |
| 2216 | memset( signature, 0, sizeof( signature ) ); |
| 2217 | status = psa_verify_message( key, exercise_alg, |
| 2218 | payload, payload_length, |
| 2219 | signature, sizeof( signature ) ); |
| 2220 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 2221 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 2222 | else |
| 2223 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2224 | } |
| 2225 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2226 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2227 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2228 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2229 | } |
| 2230 | /* END_CASE */ |
| 2231 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2232 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2233 | void derive_key_policy( int policy_usage, |
| 2234 | int policy_alg, |
| 2235 | int key_type, |
| 2236 | data_t *key_data, |
| 2237 | int exercise_alg ) |
| 2238 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2239 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2240 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2241 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2242 | psa_status_t status; |
| 2243 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2244 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2245 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2246 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2247 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2248 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2249 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2250 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2251 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2252 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2253 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2254 | |
| 2255 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2256 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2257 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2258 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2259 | &operation, |
| 2260 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2261 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2262 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2263 | |
| 2264 | status = psa_key_derivation_input_key( &operation, |
| 2265 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2266 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2267 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2268 | if( policy_alg == exercise_alg && |
| 2269 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2270 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2271 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2272 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2273 | |
| 2274 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2275 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2276 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2277 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2278 | } |
| 2279 | /* END_CASE */ |
| 2280 | |
| 2281 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2282 | void agreement_key_policy( int policy_usage, |
| 2283 | int policy_alg, |
| 2284 | int key_type_arg, |
| 2285 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2286 | int exercise_alg, |
| 2287 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2288 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2289 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2290 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2291 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2292 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2293 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2294 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2295 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2296 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2297 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2298 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2299 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2300 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2301 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2302 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2303 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2304 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2305 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2306 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2307 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2308 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2309 | |
| 2310 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2311 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2312 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2313 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2314 | } |
| 2315 | /* END_CASE */ |
| 2316 | |
| 2317 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2318 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2319 | int usage_arg, int alg_arg, int alg2_arg ) |
| 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 | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2322 | psa_key_type_t key_type = key_type_arg; |
| 2323 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2324 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2325 | psa_key_usage_t usage = usage_arg; |
| 2326 | psa_algorithm_t alg = alg_arg; |
| 2327 | psa_algorithm_t alg2 = alg2_arg; |
| 2328 | |
| 2329 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2330 | |
| 2331 | psa_set_key_usage_flags( &attributes, usage ); |
| 2332 | psa_set_key_algorithm( &attributes, alg ); |
| 2333 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2334 | psa_set_key_type( &attributes, key_type ); |
| 2335 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2336 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2337 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2338 | /* Update the usage flags to obtain implicit usage flags */ |
| 2339 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2340 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2341 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2342 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2343 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2344 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2345 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2346 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2347 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2348 | goto exit; |
| 2349 | |
| 2350 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2351 | /* |
| 2352 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2353 | * thus reset them as required. |
| 2354 | */ |
| 2355 | psa_reset_key_attributes( &got_attributes ); |
| 2356 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2357 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2358 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2359 | } |
| 2360 | /* END_CASE */ |
| 2361 | |
| 2362 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2363 | void raw_agreement_key_policy( int policy_usage, |
| 2364 | int policy_alg, |
| 2365 | int key_type_arg, |
| 2366 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2367 | int exercise_alg, |
| 2368 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2369 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2370 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2371 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2372 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2373 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2374 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2375 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2376 | |
| 2377 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2378 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2379 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2380 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2381 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2382 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2383 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2384 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2385 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2386 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2387 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2388 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2389 | |
| 2390 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2391 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2392 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2393 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2394 | } |
| 2395 | /* END_CASE */ |
| 2396 | |
| 2397 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2398 | void copy_success( int source_usage_arg, |
| 2399 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2400 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2401 | int type_arg, data_t *material, |
| 2402 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2403 | int target_usage_arg, |
| 2404 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2405 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2406 | int expected_usage_arg, |
| 2407 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2408 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2409 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2410 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2411 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2412 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2413 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2414 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2415 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2416 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2417 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2418 | uint8_t *export_buffer = NULL; |
| 2419 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2420 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2421 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2422 | /* Prepare the source key. */ |
| 2423 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2424 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2425 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2426 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2427 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2428 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2429 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2430 | &source_key ) ); |
| 2431 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2432 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2433 | /* Prepare the target attributes. */ |
| 2434 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2435 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2436 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2437 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2438 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2439 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2440 | if( target_usage_arg != -1 ) |
| 2441 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2442 | if( target_alg_arg != -1 ) |
| 2443 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2444 | if( target_alg2_arg != -1 ) |
| 2445 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2446 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2447 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2448 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2449 | PSA_ASSERT( psa_copy_key( source_key, |
| 2450 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2451 | |
| 2452 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2453 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2454 | |
| 2455 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2456 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2457 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2458 | psa_get_key_type( &target_attributes ) ); |
| 2459 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2460 | psa_get_key_bits( &target_attributes ) ); |
| 2461 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2462 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2463 | TEST_EQUAL( expected_alg2, |
| 2464 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2465 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2466 | { |
| 2467 | size_t length; |
| 2468 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2469 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2470 | material->len, &length ) ); |
| 2471 | ASSERT_COMPARE( material->x, material->len, |
| 2472 | export_buffer, length ); |
| 2473 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2474 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2475 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2476 | { |
| 2477 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2478 | goto exit; |
| 2479 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2480 | goto exit; |
| 2481 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2482 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2483 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2484 | |
| 2485 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2486 | /* |
| 2487 | * Source and target key attributes may have been returned by |
| 2488 | * psa_get_key_attributes() thus reset them as required. |
| 2489 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2490 | psa_reset_key_attributes( &source_attributes ); |
| 2491 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2492 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2493 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2494 | mbedtls_free( export_buffer ); |
| 2495 | } |
| 2496 | /* END_CASE */ |
| 2497 | |
| 2498 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2499 | void copy_fail( int source_usage_arg, |
| 2500 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2501 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2502 | int type_arg, data_t *material, |
| 2503 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2504 | int target_usage_arg, |
| 2505 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2506 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2507 | int expected_status_arg ) |
| 2508 | { |
| 2509 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2510 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2511 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2512 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2513 | 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] | 2514 | |
| 2515 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2516 | |
| 2517 | /* Prepare the source key. */ |
| 2518 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2519 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2520 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2521 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2522 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2523 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2524 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2525 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2526 | |
| 2527 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2528 | psa_set_key_id( &target_attributes, key_id ); |
| 2529 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2530 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2531 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2532 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2533 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2534 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2535 | |
| 2536 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2537 | TEST_EQUAL( psa_copy_key( source_key, |
| 2538 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2539 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2540 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2541 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2542 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2543 | exit: |
| 2544 | psa_reset_key_attributes( &source_attributes ); |
| 2545 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2546 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2547 | } |
| 2548 | /* END_CASE */ |
| 2549 | |
| 2550 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2551 | void hash_operation_init( ) |
| 2552 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2553 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2554 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2555 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2556 | * 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] | 2557 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2558 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2559 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2560 | psa_hash_operation_t zero; |
| 2561 | |
| 2562 | memset( &zero, 0, sizeof( zero ) ); |
| 2563 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2564 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2565 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2566 | PSA_ERROR_BAD_STATE ); |
| 2567 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2568 | PSA_ERROR_BAD_STATE ); |
| 2569 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2570 | PSA_ERROR_BAD_STATE ); |
| 2571 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2572 | /* A default hash operation should be abortable without error. */ |
| 2573 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2574 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2575 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2576 | } |
| 2577 | /* END_CASE */ |
| 2578 | |
| 2579 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2580 | void hash_setup( int alg_arg, |
| 2581 | int expected_status_arg ) |
| 2582 | { |
| 2583 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2584 | uint8_t *output = NULL; |
| 2585 | size_t output_size = 0; |
| 2586 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2587 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2588 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2589 | psa_status_t status; |
| 2590 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2591 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2592 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2593 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2594 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2595 | ASSERT_ALLOC( output, output_size ); |
| 2596 | |
| 2597 | status = psa_hash_compute( alg, NULL, 0, |
| 2598 | output, output_size, &output_length ); |
| 2599 | TEST_EQUAL( status, expected_status ); |
| 2600 | |
| 2601 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2602 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2603 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2604 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2605 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2606 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2607 | |
| 2608 | /* If setup failed, reproduce the failure, so as to |
| 2609 | * test the resulting state of the operation object. */ |
| 2610 | if( status != PSA_SUCCESS ) |
| 2611 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2612 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2613 | /* Now the operation object should be reusable. */ |
| 2614 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2615 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2616 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2617 | #endif |
| 2618 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2619 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2620 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2621 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2622 | } |
| 2623 | /* END_CASE */ |
| 2624 | |
| 2625 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2626 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2627 | int output_size_arg, int expected_status_arg ) |
| 2628 | { |
| 2629 | psa_algorithm_t alg = alg_arg; |
| 2630 | uint8_t *output = NULL; |
| 2631 | size_t output_size = output_size_arg; |
| 2632 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2633 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2634 | psa_status_t expected_status = expected_status_arg; |
| 2635 | psa_status_t status; |
| 2636 | |
| 2637 | ASSERT_ALLOC( output, output_size ); |
| 2638 | |
| 2639 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2640 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2641 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2642 | status = psa_hash_compute( alg, input->x, input->len, |
| 2643 | output, output_size, &output_length ); |
| 2644 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2645 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2646 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2647 | /* Hash Compute, multi-part */ |
| 2648 | status = psa_hash_setup( &operation, alg ); |
| 2649 | if( status == PSA_SUCCESS ) |
| 2650 | { |
| 2651 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2652 | if( status == PSA_SUCCESS ) |
| 2653 | { |
| 2654 | status = psa_hash_finish( &operation, output, output_size, |
| 2655 | &output_length ); |
| 2656 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2657 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2658 | else |
| 2659 | TEST_EQUAL( status, expected_status ); |
| 2660 | } |
| 2661 | else |
| 2662 | { |
| 2663 | TEST_EQUAL( status, expected_status ); |
| 2664 | } |
| 2665 | } |
| 2666 | else |
| 2667 | { |
| 2668 | TEST_EQUAL( status, expected_status ); |
| 2669 | } |
| 2670 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2671 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2672 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2673 | mbedtls_free( output ); |
| 2674 | PSA_DONE( ); |
| 2675 | } |
| 2676 | /* END_CASE */ |
| 2677 | |
| 2678 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2679 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2680 | data_t *reference_hash, |
| 2681 | int expected_status_arg ) |
| 2682 | { |
| 2683 | psa_algorithm_t alg = alg_arg; |
| 2684 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2685 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2686 | psa_status_t status; |
| 2687 | |
| 2688 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2689 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2690 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2691 | status = psa_hash_compare( alg, input->x, input->len, |
| 2692 | reference_hash->x, reference_hash->len ); |
| 2693 | TEST_EQUAL( status, expected_status ); |
| 2694 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2695 | /* Hash Compare, multi-part */ |
| 2696 | status = psa_hash_setup( &operation, alg ); |
| 2697 | if( status == PSA_SUCCESS ) |
| 2698 | { |
| 2699 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2700 | if( status == PSA_SUCCESS ) |
| 2701 | { |
| 2702 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2703 | reference_hash->len ); |
| 2704 | TEST_EQUAL( status, expected_status ); |
| 2705 | } |
| 2706 | else |
| 2707 | { |
| 2708 | TEST_EQUAL( status, expected_status ); |
| 2709 | } |
| 2710 | } |
| 2711 | else |
| 2712 | { |
| 2713 | TEST_EQUAL( status, expected_status ); |
| 2714 | } |
| 2715 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2716 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2717 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2718 | PSA_DONE( ); |
| 2719 | } |
| 2720 | /* END_CASE */ |
| 2721 | |
| 2722 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2723 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2724 | data_t *expected_output ) |
| 2725 | { |
| 2726 | psa_algorithm_t alg = alg_arg; |
| 2727 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2728 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2729 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2730 | size_t i; |
| 2731 | |
| 2732 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2733 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2734 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2735 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2736 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2737 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2738 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2739 | ASSERT_COMPARE( output, output_length, |
| 2740 | expected_output->x, expected_output->len ); |
| 2741 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2742 | /* Compute with tight buffer, multi-part */ |
| 2743 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2744 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2745 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2746 | PSA_HASH_LENGTH( alg ), |
| 2747 | &output_length ) ); |
| 2748 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2749 | ASSERT_COMPARE( output, output_length, |
| 2750 | expected_output->x, expected_output->len ); |
| 2751 | |
| 2752 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2753 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2754 | output, sizeof( output ), |
| 2755 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2756 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2757 | ASSERT_COMPARE( output, output_length, |
| 2758 | expected_output->x, expected_output->len ); |
| 2759 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2760 | /* Compute with larger buffer, multi-part */ |
| 2761 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2762 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2763 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2764 | sizeof( output ), &output_length ) ); |
| 2765 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2766 | ASSERT_COMPARE( output, output_length, |
| 2767 | expected_output->x, expected_output->len ); |
| 2768 | |
| 2769 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2770 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2771 | output, output_length ) ); |
| 2772 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2773 | /* Compare with correct hash, multi-part */ |
| 2774 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2775 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2776 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2777 | output_length ) ); |
| 2778 | |
| 2779 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2780 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2781 | output, output_length + 1 ), |
| 2782 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2783 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2784 | /* Compare with trailing garbage, multi-part */ |
| 2785 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2786 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2787 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2788 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2789 | |
| 2790 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2791 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2792 | output, output_length - 1 ), |
| 2793 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2794 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2795 | /* Compare with truncated hash, multi-part */ |
| 2796 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2797 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2798 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2799 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2800 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2801 | /* Compare with corrupted value */ |
| 2802 | for( i = 0; i < output_length; i++ ) |
| 2803 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2804 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2805 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2806 | |
| 2807 | /* One-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2808 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2809 | output, output_length ), |
| 2810 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2811 | |
| 2812 | /* Multi-Part */ |
| 2813 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2814 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2815 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length ), |
| 2816 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2817 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2818 | output[i] ^= 1; |
| 2819 | } |
| 2820 | |
| 2821 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2822 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2823 | PSA_DONE( ); |
| 2824 | } |
| 2825 | /* END_CASE */ |
| 2826 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2827 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2828 | void hash_bad_order( ) |
| 2829 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2830 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2831 | unsigned char input[] = ""; |
| 2832 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2833 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2834 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2835 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2836 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2837 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2838 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2839 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2840 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2841 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2842 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2843 | /* Call setup twice in a row. */ |
| 2844 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2845 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2846 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2847 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2848 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2849 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2850 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2851 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2852 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2853 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2854 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2855 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2856 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2857 | /* Check that update calls abort on error. */ |
| 2858 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2859 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2860 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2861 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2862 | PSA_ERROR_BAD_STATE ); |
| 2863 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2864 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2865 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2866 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2867 | /* Call update after finish. */ |
| 2868 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2869 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2870 | hash, sizeof( hash ), &hash_len ) ); |
| 2871 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2872 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2873 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2874 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2875 | /* Call verify without calling setup beforehand. */ |
| 2876 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2877 | valid_hash, sizeof( valid_hash ) ), |
| 2878 | PSA_ERROR_BAD_STATE ); |
| 2879 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2880 | |
| 2881 | /* Call verify after finish. */ |
| 2882 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2883 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2884 | hash, sizeof( hash ), &hash_len ) ); |
| 2885 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2886 | valid_hash, sizeof( valid_hash ) ), |
| 2887 | PSA_ERROR_BAD_STATE ); |
| 2888 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2889 | |
| 2890 | /* Call verify twice in a row. */ |
| 2891 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2892 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2893 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2894 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2895 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2896 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2897 | valid_hash, sizeof( valid_hash ) ), |
| 2898 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2899 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2900 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2901 | |
| 2902 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2903 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2904 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2905 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2906 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2907 | |
| 2908 | /* Call finish twice in a row. */ |
| 2909 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2910 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2911 | hash, sizeof( hash ), &hash_len ) ); |
| 2912 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2913 | hash, sizeof( hash ), &hash_len ), |
| 2914 | PSA_ERROR_BAD_STATE ); |
| 2915 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2916 | |
| 2917 | /* Call finish after calling verify. */ |
| 2918 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2919 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2920 | valid_hash, sizeof( valid_hash ) ) ); |
| 2921 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2922 | hash, sizeof( hash ), &hash_len ), |
| 2923 | PSA_ERROR_BAD_STATE ); |
| 2924 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2925 | |
| 2926 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2927 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2928 | } |
| 2929 | /* END_CASE */ |
| 2930 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2931 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2932 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2933 | { |
| 2934 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2935 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2936 | * appended to it */ |
| 2937 | unsigned char hash[] = { |
| 2938 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2939 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2940 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2941 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2942 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2943 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2944 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2945 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2946 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2947 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2948 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2949 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2950 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2951 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2952 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2953 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2954 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2955 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2956 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2957 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2958 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2959 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2960 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2961 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2962 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2963 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2964 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2965 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2966 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2967 | } |
| 2968 | /* END_CASE */ |
| 2969 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2970 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2971 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2972 | { |
| 2973 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2974 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2975 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2976 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2977 | size_t hash_len; |
| 2978 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2979 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2980 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2981 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2982 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2983 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2984 | hash, expected_size - 1, &hash_len ), |
| 2985 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2986 | |
| 2987 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2988 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2989 | } |
| 2990 | /* END_CASE */ |
| 2991 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2992 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2993 | void hash_clone_source_state( ) |
| 2994 | { |
| 2995 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2996 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2997 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2998 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2999 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3000 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3001 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3002 | size_t hash_len; |
| 3003 | |
| 3004 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3005 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 3006 | |
| 3007 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3008 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3009 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3010 | hash, sizeof( hash ), &hash_len ) ); |
| 3011 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3012 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3013 | |
| 3014 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 3015 | PSA_ERROR_BAD_STATE ); |
| 3016 | |
| 3017 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 3018 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 3019 | hash, sizeof( hash ), &hash_len ) ); |
| 3020 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 3021 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3022 | hash, sizeof( hash ), &hash_len ) ); |
| 3023 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 3024 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 3025 | hash, sizeof( hash ), &hash_len ) ); |
| 3026 | |
| 3027 | exit: |
| 3028 | psa_hash_abort( &op_source ); |
| 3029 | psa_hash_abort( &op_init ); |
| 3030 | psa_hash_abort( &op_setup ); |
| 3031 | psa_hash_abort( &op_finished ); |
| 3032 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3033 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3034 | } |
| 3035 | /* END_CASE */ |
| 3036 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3037 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3038 | void hash_clone_target_state( ) |
| 3039 | { |
| 3040 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3041 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3042 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3043 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3044 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3045 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3046 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3047 | size_t hash_len; |
| 3048 | |
| 3049 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3050 | |
| 3051 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3052 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3053 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3054 | hash, sizeof( hash ), &hash_len ) ); |
| 3055 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3056 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3057 | |
| 3058 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 3059 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 3060 | hash, sizeof( hash ), &hash_len ) ); |
| 3061 | |
| 3062 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 3063 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 3064 | PSA_ERROR_BAD_STATE ); |
| 3065 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 3066 | PSA_ERROR_BAD_STATE ); |
| 3067 | |
| 3068 | exit: |
| 3069 | psa_hash_abort( &op_target ); |
| 3070 | psa_hash_abort( &op_init ); |
| 3071 | psa_hash_abort( &op_setup ); |
| 3072 | psa_hash_abort( &op_finished ); |
| 3073 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3074 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3075 | } |
| 3076 | /* END_CASE */ |
| 3077 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3078 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3079 | void mac_operation_init( ) |
| 3080 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3081 | const uint8_t input[1] = { 0 }; |
| 3082 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3083 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3084 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3085 | * 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] | 3086 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3087 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 3088 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3089 | psa_mac_operation_t zero; |
| 3090 | |
| 3091 | memset( &zero, 0, sizeof( zero ) ); |
| 3092 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3093 | /* A freshly-initialized MAC operation should not be usable. */ |
| 3094 | TEST_EQUAL( psa_mac_update( &func, |
| 3095 | input, sizeof( input ) ), |
| 3096 | PSA_ERROR_BAD_STATE ); |
| 3097 | TEST_EQUAL( psa_mac_update( &init, |
| 3098 | input, sizeof( input ) ), |
| 3099 | PSA_ERROR_BAD_STATE ); |
| 3100 | TEST_EQUAL( psa_mac_update( &zero, |
| 3101 | input, sizeof( input ) ), |
| 3102 | PSA_ERROR_BAD_STATE ); |
| 3103 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3104 | /* A default MAC operation should be abortable without error. */ |
| 3105 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 3106 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 3107 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3108 | } |
| 3109 | /* END_CASE */ |
| 3110 | |
| 3111 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3112 | void mac_setup( int key_type_arg, |
| 3113 | data_t *key, |
| 3114 | int alg_arg, |
| 3115 | int expected_status_arg ) |
| 3116 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3117 | psa_key_type_t key_type = key_type_arg; |
| 3118 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3119 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3120 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3121 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3122 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3123 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3124 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3125 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3126 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3127 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3128 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 3129 | &operation, &status ) ) |
| 3130 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3131 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3132 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3133 | /* The operation object should be reusable. */ |
| 3134 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3135 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3136 | smoke_test_key_data, |
| 3137 | sizeof( smoke_test_key_data ), |
| 3138 | KNOWN_SUPPORTED_MAC_ALG, |
| 3139 | &operation, &status ) ) |
| 3140 | goto exit; |
| 3141 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3142 | #endif |
| 3143 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3144 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3145 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3146 | } |
| 3147 | /* END_CASE */ |
| 3148 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3149 | /* 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] | 3150 | void mac_bad_order( ) |
| 3151 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3152 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3153 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3154 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3155 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3156 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3157 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3158 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3159 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3160 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3161 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3162 | size_t sign_mac_length = 0; |
| 3163 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3164 | const uint8_t verify_mac[] = { |
| 3165 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3166 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 3167 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 3168 | |
| 3169 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3170 | 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] | 3171 | psa_set_key_algorithm( &attributes, alg ); |
| 3172 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3173 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3174 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3175 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3176 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3177 | /* Call update without calling setup beforehand. */ |
| 3178 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3179 | PSA_ERROR_BAD_STATE ); |
| 3180 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3181 | |
| 3182 | /* Call sign finish without calling setup beforehand. */ |
| 3183 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 3184 | &sign_mac_length), |
| 3185 | PSA_ERROR_BAD_STATE ); |
| 3186 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3187 | |
| 3188 | /* Call verify finish without calling setup beforehand. */ |
| 3189 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3190 | verify_mac, sizeof( verify_mac ) ), |
| 3191 | PSA_ERROR_BAD_STATE ); |
| 3192 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3193 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3194 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3195 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3196 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3197 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3198 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3199 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3200 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3201 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3202 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3203 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3204 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3205 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3206 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3207 | sign_mac, sizeof( sign_mac ), |
| 3208 | &sign_mac_length ) ); |
| 3209 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3210 | PSA_ERROR_BAD_STATE ); |
| 3211 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3212 | |
| 3213 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3214 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3215 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3216 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3217 | verify_mac, sizeof( verify_mac ) ) ); |
| 3218 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3219 | PSA_ERROR_BAD_STATE ); |
| 3220 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3221 | |
| 3222 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3223 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3224 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3225 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3226 | sign_mac, sizeof( sign_mac ), |
| 3227 | &sign_mac_length ) ); |
| 3228 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3229 | sign_mac, sizeof( sign_mac ), |
| 3230 | &sign_mac_length ), |
| 3231 | PSA_ERROR_BAD_STATE ); |
| 3232 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3233 | |
| 3234 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3235 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3236 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3237 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3238 | verify_mac, sizeof( verify_mac ) ) ); |
| 3239 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3240 | verify_mac, sizeof( verify_mac ) ), |
| 3241 | PSA_ERROR_BAD_STATE ); |
| 3242 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3243 | |
| 3244 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3245 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3246 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3247 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3248 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3249 | verify_mac, sizeof( verify_mac ) ), |
| 3250 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3251 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3252 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3253 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3254 | |
| 3255 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3256 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3257 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3258 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3259 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3260 | sign_mac, sizeof( sign_mac ), |
| 3261 | &sign_mac_length ), |
| 3262 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3263 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3264 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3265 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3266 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3267 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3268 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3269 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3270 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3271 | } |
| 3272 | /* END_CASE */ |
| 3273 | |
| 3274 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3275 | void mac_sign_verify_multi( int key_type_arg, |
| 3276 | data_t *key_data, |
| 3277 | int alg_arg, |
| 3278 | data_t *input, |
| 3279 | int is_verify, |
| 3280 | data_t *expected_mac ) |
| 3281 | { |
| 3282 | size_t data_part_len = 0; |
| 3283 | |
| 3284 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3285 | { |
| 3286 | /* Split data into length(data_part_len) parts. */ |
| 3287 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3288 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3289 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3290 | alg_arg, |
| 3291 | input, data_part_len, |
| 3292 | expected_mac, |
| 3293 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3294 | break; |
| 3295 | |
| 3296 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3297 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3298 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3299 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3300 | alg_arg, |
| 3301 | input, data_part_len, |
| 3302 | expected_mac, |
| 3303 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3304 | break; |
| 3305 | } |
| 3306 | |
| 3307 | /* Goto is required to silence warnings about unused labels, as we |
| 3308 | * don't actually do any test assertions in this function. */ |
| 3309 | goto exit; |
| 3310 | } |
| 3311 | /* END_CASE */ |
| 3312 | |
| 3313 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3314 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3315 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3316 | int alg_arg, |
| 3317 | data_t *input, |
| 3318 | data_t *expected_mac ) |
| 3319 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3320 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3321 | psa_key_type_t key_type = key_type_arg; |
| 3322 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3323 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3324 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3325 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3326 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3327 | 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] | 3328 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3329 | const size_t output_sizes_to_test[] = { |
| 3330 | 0, |
| 3331 | 1, |
| 3332 | expected_mac->len - 1, |
| 3333 | expected_mac->len, |
| 3334 | expected_mac->len + 1, |
| 3335 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3336 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3337 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3338 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3339 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3340 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3341 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3342 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3343 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3344 | psa_set_key_algorithm( &attributes, alg ); |
| 3345 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3346 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3347 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3348 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3349 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3350 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3351 | { |
| 3352 | const size_t output_size = output_sizes_to_test[i]; |
| 3353 | psa_status_t expected_status = |
| 3354 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3355 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3356 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3357 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3358 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3359 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3360 | /* Calculate the MAC, one-shot case. */ |
| 3361 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3362 | input->x, input->len, |
| 3363 | actual_mac, output_size, &mac_length ), |
| 3364 | expected_status ); |
| 3365 | if( expected_status == PSA_SUCCESS ) |
| 3366 | { |
| 3367 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3368 | actual_mac, mac_length ); |
| 3369 | } |
| 3370 | |
| 3371 | if( output_size > 0 ) |
| 3372 | memset( actual_mac, 0, output_size ); |
| 3373 | |
| 3374 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3375 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3376 | PSA_ASSERT( psa_mac_update( &operation, |
| 3377 | input->x, input->len ) ); |
| 3378 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3379 | actual_mac, output_size, |
| 3380 | &mac_length ), |
| 3381 | expected_status ); |
| 3382 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3383 | |
| 3384 | if( expected_status == PSA_SUCCESS ) |
| 3385 | { |
| 3386 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3387 | actual_mac, mac_length ); |
| 3388 | } |
| 3389 | mbedtls_free( actual_mac ); |
| 3390 | actual_mac = NULL; |
| 3391 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3392 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3393 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3394 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3395 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3396 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3397 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3398 | } |
| 3399 | /* END_CASE */ |
| 3400 | |
| 3401 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3402 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3403 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3404 | int alg_arg, |
| 3405 | data_t *input, |
| 3406 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3407 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3408 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3409 | psa_key_type_t key_type = key_type_arg; |
| 3410 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3411 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3412 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3413 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3414 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3415 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3416 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3417 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3418 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3419 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3420 | psa_set_key_algorithm( &attributes, alg ); |
| 3421 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3422 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3423 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3424 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3425 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3426 | /* Verify correct MAC, one-shot case. */ |
| 3427 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3428 | expected_mac->x, expected_mac->len ) ); |
| 3429 | |
| 3430 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3431 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3432 | PSA_ASSERT( psa_mac_update( &operation, |
| 3433 | input->x, input->len ) ); |
| 3434 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3435 | expected_mac->x, |
| 3436 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3437 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3438 | /* Test a MAC that's too short, one-shot case. */ |
| 3439 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3440 | input->x, input->len, |
| 3441 | expected_mac->x, |
| 3442 | expected_mac->len - 1 ), |
| 3443 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3444 | |
| 3445 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3446 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3447 | PSA_ASSERT( psa_mac_update( &operation, |
| 3448 | input->x, input->len ) ); |
| 3449 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3450 | expected_mac->x, |
| 3451 | expected_mac->len - 1 ), |
| 3452 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3453 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3454 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3455 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3456 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3457 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3458 | input->x, input->len, |
| 3459 | perturbed_mac, expected_mac->len + 1 ), |
| 3460 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3461 | |
| 3462 | /* Test a MAC that's too long, 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 | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3464 | PSA_ASSERT( psa_mac_update( &operation, |
| 3465 | input->x, input->len ) ); |
| 3466 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3467 | perturbed_mac, |
| 3468 | expected_mac->len + 1 ), |
| 3469 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3470 | |
| 3471 | /* Test changing one byte. */ |
| 3472 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3473 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3474 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3475 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3476 | |
| 3477 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3478 | input->x, input->len, |
| 3479 | perturbed_mac, expected_mac->len ), |
| 3480 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3481 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3482 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3483 | PSA_ASSERT( psa_mac_update( &operation, |
| 3484 | input->x, input->len ) ); |
| 3485 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3486 | perturbed_mac, |
| 3487 | expected_mac->len ), |
| 3488 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3489 | perturbed_mac[i] ^= 1; |
| 3490 | } |
| 3491 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3492 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3493 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3494 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3495 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3496 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3497 | } |
| 3498 | /* END_CASE */ |
| 3499 | |
| 3500 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3501 | void cipher_operation_init( ) |
| 3502 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3503 | const uint8_t input[1] = { 0 }; |
| 3504 | unsigned char output[1] = { 0 }; |
| 3505 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3506 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3507 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3508 | * 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] | 3509 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3510 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3511 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3512 | psa_cipher_operation_t zero; |
| 3513 | |
| 3514 | memset( &zero, 0, sizeof( zero ) ); |
| 3515 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3516 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3517 | TEST_EQUAL( psa_cipher_update( &func, |
| 3518 | input, sizeof( input ), |
| 3519 | output, sizeof( output ), |
| 3520 | &output_length ), |
| 3521 | PSA_ERROR_BAD_STATE ); |
| 3522 | TEST_EQUAL( psa_cipher_update( &init, |
| 3523 | input, sizeof( input ), |
| 3524 | output, sizeof( output ), |
| 3525 | &output_length ), |
| 3526 | PSA_ERROR_BAD_STATE ); |
| 3527 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3528 | input, sizeof( input ), |
| 3529 | output, sizeof( output ), |
| 3530 | &output_length ), |
| 3531 | PSA_ERROR_BAD_STATE ); |
| 3532 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3533 | /* A default cipher operation should be abortable without error. */ |
| 3534 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3535 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3536 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3537 | } |
| 3538 | /* END_CASE */ |
| 3539 | |
| 3540 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3541 | void cipher_setup( int key_type_arg, |
| 3542 | data_t *key, |
| 3543 | int alg_arg, |
| 3544 | int expected_status_arg ) |
| 3545 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3546 | psa_key_type_t key_type = key_type_arg; |
| 3547 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3548 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3549 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3550 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3551 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3552 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3553 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3554 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3555 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3556 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3557 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3558 | &operation, &status ) ) |
| 3559 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3560 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3561 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3562 | /* The operation object should be reusable. */ |
| 3563 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3564 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3565 | smoke_test_key_data, |
| 3566 | sizeof( smoke_test_key_data ), |
| 3567 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3568 | &operation, &status ) ) |
| 3569 | goto exit; |
| 3570 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3571 | #endif |
| 3572 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3573 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3574 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3575 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3576 | } |
| 3577 | /* END_CASE */ |
| 3578 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3579 | /* 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] | 3580 | void cipher_bad_order( ) |
| 3581 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3582 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3583 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3584 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3585 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3586 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3587 | 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] | 3588 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3589 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3590 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3591 | const uint8_t text[] = { |
| 3592 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3593 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3594 | 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] | 3595 | size_t length = 0; |
| 3596 | |
| 3597 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3598 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3599 | psa_set_key_algorithm( &attributes, alg ); |
| 3600 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3601 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3602 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3603 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3604 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3605 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3606 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3607 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3608 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3609 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3610 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3611 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3612 | |
| 3613 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3614 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3615 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3616 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3617 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3618 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3619 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3620 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3621 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3622 | /* Generate an IV without calling setup beforehand. */ |
| 3623 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3624 | buffer, sizeof( buffer ), |
| 3625 | &length ), |
| 3626 | PSA_ERROR_BAD_STATE ); |
| 3627 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3628 | |
| 3629 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3630 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3631 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3632 | buffer, sizeof( buffer ), |
| 3633 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3634 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3635 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3636 | buffer, sizeof( buffer ), |
| 3637 | &length ), |
| 3638 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3639 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3640 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3641 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3642 | |
| 3643 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3644 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3645 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3646 | iv, sizeof( iv ) ) ); |
| 3647 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3648 | buffer, sizeof( buffer ), |
| 3649 | &length ), |
| 3650 | PSA_ERROR_BAD_STATE ); |
| 3651 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3652 | |
| 3653 | /* Set an IV without calling setup beforehand. */ |
| 3654 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3655 | iv, sizeof( iv ) ), |
| 3656 | PSA_ERROR_BAD_STATE ); |
| 3657 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3658 | |
| 3659 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3660 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3661 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3662 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3663 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3664 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3665 | iv, sizeof( iv ) ), |
| 3666 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3667 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3668 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3669 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3670 | |
| 3671 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3672 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3673 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3674 | buffer, sizeof( buffer ), |
| 3675 | &length ) ); |
| 3676 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3677 | iv, sizeof( iv ) ), |
| 3678 | PSA_ERROR_BAD_STATE ); |
| 3679 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3680 | |
| 3681 | /* Call update without calling setup beforehand. */ |
| 3682 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3683 | text, sizeof( text ), |
| 3684 | buffer, sizeof( buffer ), |
| 3685 | &length ), |
| 3686 | PSA_ERROR_BAD_STATE ); |
| 3687 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3688 | |
| 3689 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3690 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3691 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3692 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3693 | text, sizeof( text ), |
| 3694 | buffer, sizeof( buffer ), |
| 3695 | &length ), |
| 3696 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3697 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3698 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
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 | |
| 3701 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3702 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3703 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3704 | iv, sizeof( iv ) ) ); |
| 3705 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3706 | buffer, sizeof( buffer ), &length ) ); |
| 3707 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3708 | text, sizeof( text ), |
| 3709 | buffer, sizeof( buffer ), |
| 3710 | &length ), |
| 3711 | PSA_ERROR_BAD_STATE ); |
| 3712 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3713 | |
| 3714 | /* Call finish without calling setup beforehand. */ |
| 3715 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3716 | buffer, sizeof( buffer ), &length ), |
| 3717 | PSA_ERROR_BAD_STATE ); |
| 3718 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3719 | |
| 3720 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3721 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3722 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3723 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3724 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3725 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3726 | buffer, sizeof( buffer ), &length ), |
| 3727 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3728 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3729 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3730 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3731 | |
| 3732 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3733 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3734 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3735 | iv, sizeof( iv ) ) ); |
| 3736 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3737 | buffer, sizeof( buffer ), &length ) ); |
| 3738 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3739 | buffer, sizeof( buffer ), &length ), |
| 3740 | PSA_ERROR_BAD_STATE ); |
| 3741 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3742 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3743 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3744 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3745 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3746 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3747 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3748 | } |
| 3749 | /* END_CASE */ |
| 3750 | |
| 3751 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3752 | void cipher_encrypt_fail( int alg_arg, |
| 3753 | int key_type_arg, |
| 3754 | data_t *key_data, |
| 3755 | data_t *input, |
| 3756 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3757 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3758 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3759 | psa_status_t status; |
| 3760 | psa_key_type_t key_type = key_type_arg; |
| 3761 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3762 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3763 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3764 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3765 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3766 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3767 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3768 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3769 | size_t function_output_length; |
| 3770 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3771 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3772 | |
| 3773 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3774 | { |
| 3775 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3776 | |
| 3777 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3778 | psa_set_key_algorithm( &attributes, alg ); |
| 3779 | psa_set_key_type( &attributes, key_type ); |
| 3780 | |
| 3781 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3782 | input->len ); |
| 3783 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3784 | |
| 3785 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3786 | &key ) ); |
| 3787 | } |
| 3788 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3789 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3790 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3791 | output_buffer_size, &output_length ); |
| 3792 | |
| 3793 | TEST_EQUAL( status, expected_status ); |
| 3794 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3795 | /* Encrypt, multi-part */ |
| 3796 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3797 | if( status == PSA_SUCCESS ) |
| 3798 | { |
| 3799 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3800 | { |
| 3801 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3802 | iv, iv_size, |
| 3803 | &iv_length ) ); |
| 3804 | } |
| 3805 | |
| 3806 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3807 | output, output_buffer_size, |
| 3808 | &function_output_length ); |
| 3809 | if( status == PSA_SUCCESS ) |
| 3810 | { |
| 3811 | output_length += function_output_length; |
| 3812 | |
| 3813 | status = psa_cipher_finish( &operation, output + output_length, |
| 3814 | output_buffer_size - output_length, |
| 3815 | &function_output_length ); |
| 3816 | |
| 3817 | TEST_EQUAL( status, expected_status ); |
| 3818 | } |
| 3819 | else |
| 3820 | { |
| 3821 | TEST_EQUAL( status, expected_status ); |
| 3822 | } |
| 3823 | } |
| 3824 | else |
| 3825 | { |
| 3826 | TEST_EQUAL( status, expected_status ); |
| 3827 | } |
| 3828 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3829 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3830 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3831 | mbedtls_free( output ); |
| 3832 | psa_destroy_key( key ); |
| 3833 | PSA_DONE( ); |
| 3834 | } |
| 3835 | /* END_CASE */ |
| 3836 | |
| 3837 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3838 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3839 | data_t *input, int iv_length, |
| 3840 | int expected_result ) |
| 3841 | { |
| 3842 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3843 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3844 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3845 | size_t output_buffer_size = 0; |
| 3846 | unsigned char *output = NULL; |
| 3847 | |
| 3848 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3849 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3850 | |
| 3851 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3852 | |
| 3853 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3854 | psa_set_key_algorithm( &attributes, alg ); |
| 3855 | psa_set_key_type( &attributes, key_type ); |
| 3856 | |
| 3857 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3858 | &key ) ); |
| 3859 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3860 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3861 | iv_length ) ); |
| 3862 | |
| 3863 | exit: |
| 3864 | psa_cipher_abort( &operation ); |
| 3865 | mbedtls_free( output ); |
| 3866 | psa_destroy_key( key ); |
| 3867 | PSA_DONE( ); |
| 3868 | } |
| 3869 | /* END_CASE */ |
| 3870 | |
| 3871 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3872 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3873 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3874 | { |
| 3875 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3876 | psa_key_type_t key_type = key_type_arg; |
| 3877 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3878 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3879 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3880 | unsigned char *output = NULL; |
| 3881 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3882 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3883 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3884 | |
| 3885 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3886 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3887 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3888 | TEST_LE_U( ciphertext->len, |
| 3889 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3890 | 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] | 3891 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3892 | TEST_LE_U( plaintext->len, |
| 3893 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3894 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3895 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3896 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3897 | |
| 3898 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3899 | psa_set_key_usage_flags( &attributes, |
| 3900 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3901 | psa_set_key_algorithm( &attributes, alg ); |
| 3902 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3903 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3904 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3905 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3906 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3907 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3908 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3909 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3910 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3911 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3912 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3913 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3914 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3915 | PSA_ERROR_BAD_STATE ); |
| 3916 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3917 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3918 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3919 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3920 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3921 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3922 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3923 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3924 | &length ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3925 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3926 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3927 | /* Multipart encryption */ |
| 3928 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3929 | output_length = 0; |
| 3930 | length = ~0; |
| 3931 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3932 | plaintext->x, plaintext->len, |
| 3933 | output, output_buffer_size, |
| 3934 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3935 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3936 | output_length += length; |
| 3937 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3938 | output + output_length, |
| 3939 | output_buffer_size - output_length, |
| 3940 | &length ) ); |
| 3941 | output_length += length; |
| 3942 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3943 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3944 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3945 | /* Multipart encryption */ |
| 3946 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3947 | output_length = 0; |
| 3948 | length = ~0; |
| 3949 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3950 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3951 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3952 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3953 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3954 | output_length += length; |
| 3955 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3956 | output + output_length, |
| 3957 | output_buffer_size - output_length, |
| 3958 | &length ) ); |
| 3959 | output_length += length; |
| 3960 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 3961 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3962 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3963 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3964 | output_length = ~0; |
| 3965 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 3966 | output, output_buffer_size, |
| 3967 | &output_length ) ); |
| 3968 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 3969 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3970 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3971 | /* One-shot decryption */ |
| 3972 | output_length = ~0; |
| 3973 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 3974 | output, output_buffer_size, |
| 3975 | &output_length ) ); |
| 3976 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3977 | output, output_length ); |
| 3978 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3979 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3980 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3981 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3982 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3983 | psa_destroy_key( key ); |
| 3984 | PSA_DONE( ); |
| 3985 | } |
| 3986 | /* END_CASE */ |
| 3987 | |
| 3988 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3989 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 3990 | { |
| 3991 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3992 | psa_algorithm_t alg = alg_arg; |
| 3993 | psa_key_type_t key_type = key_type_arg; |
| 3994 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3995 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3996 | psa_status_t status; |
| 3997 | |
| 3998 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3999 | |
| 4000 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4001 | psa_set_key_algorithm( &attributes, alg ); |
| 4002 | psa_set_key_type( &attributes, key_type ); |
| 4003 | |
| 4004 | /* Usage of either of these two size macros would cause divide by zero |
| 4005 | * with incorrect key types previously. Input length should be irrelevant |
| 4006 | * here. */ |
| 4007 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 4008 | 0 ); |
| 4009 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 4010 | |
| 4011 | |
| 4012 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4013 | &key ) ); |
| 4014 | |
| 4015 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4016 | * Encrypt or decrypt will end up in the same place. */ |
| 4017 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 4018 | |
| 4019 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 4020 | |
| 4021 | exit: |
| 4022 | psa_cipher_abort( &operation ); |
| 4023 | psa_destroy_key( key ); |
| 4024 | PSA_DONE( ); |
| 4025 | } |
| 4026 | /* END_CASE */ |
| 4027 | |
| 4028 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4029 | void cipher_encrypt_validation( int alg_arg, |
| 4030 | int key_type_arg, |
| 4031 | data_t *key_data, |
| 4032 | data_t *input ) |
| 4033 | { |
| 4034 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4035 | psa_key_type_t key_type = key_type_arg; |
| 4036 | psa_algorithm_t alg = alg_arg; |
| 4037 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 4038 | unsigned char *output1 = NULL; |
| 4039 | size_t output1_buffer_size = 0; |
| 4040 | size_t output1_length = 0; |
| 4041 | unsigned char *output2 = NULL; |
| 4042 | size_t output2_buffer_size = 0; |
| 4043 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4044 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4045 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4046 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4047 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4048 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4049 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4050 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4051 | psa_set_key_algorithm( &attributes, alg ); |
| 4052 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4053 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4054 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 4055 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4056 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4057 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 4058 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 4059 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4060 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4061 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4062 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4063 | /* The one-shot cipher encryption uses generated iv so validating |
| 4064 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4065 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 4066 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4067 | TEST_LE_U( output1_length, |
| 4068 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4069 | TEST_LE_U( output1_length, |
| 4070 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4071 | |
| 4072 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 4073 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4074 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4075 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4076 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4077 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4078 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4079 | TEST_LE_U( function_output_length, |
| 4080 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4081 | TEST_LE_U( function_output_length, |
| 4082 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4083 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4084 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4085 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4086 | output2 + output2_length, |
| 4087 | output2_buffer_size - output2_length, |
| 4088 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4089 | TEST_LE_U( function_output_length, |
| 4090 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4091 | TEST_LE_U( function_output_length, |
| 4092 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4093 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4094 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4095 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4096 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 4097 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4098 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4099 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4100 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4101 | mbedtls_free( output1 ); |
| 4102 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4103 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4104 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4105 | } |
| 4106 | /* END_CASE */ |
| 4107 | |
| 4108 | /* BEGIN_CASE */ |
| 4109 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4110 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4111 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4112 | int first_part_size_arg, |
| 4113 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4114 | data_t *expected_output, |
| 4115 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4116 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4117 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4118 | psa_key_type_t key_type = key_type_arg; |
| 4119 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4120 | psa_status_t status; |
| 4121 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4122 | size_t first_part_size = first_part_size_arg; |
| 4123 | size_t output1_length = output1_length_arg; |
| 4124 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4125 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4126 | size_t output_buffer_size = 0; |
| 4127 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4128 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4129 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4130 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4131 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4132 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4133 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4134 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4135 | psa_set_key_algorithm( &attributes, alg ); |
| 4136 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4137 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4138 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4139 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4140 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4141 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4142 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4143 | if( iv->len > 0 ) |
| 4144 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4145 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4146 | } |
| 4147 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4148 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4149 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4150 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4151 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4152 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4153 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 4154 | output, output_buffer_size, |
| 4155 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4156 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4157 | TEST_LE_U( function_output_length, |
| 4158 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4159 | TEST_LE_U( function_output_length, |
| 4160 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4161 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4162 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4163 | if( first_part_size < input->len ) |
| 4164 | { |
| 4165 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4166 | input->x + first_part_size, |
| 4167 | input->len - first_part_size, |
| 4168 | ( output_buffer_size == 0 ? NULL : |
| 4169 | output + total_output_length ), |
| 4170 | output_buffer_size - total_output_length, |
| 4171 | &function_output_length ) ); |
| 4172 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4173 | TEST_LE_U( function_output_length, |
| 4174 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4175 | alg, |
| 4176 | input->len - first_part_size ) ); |
| 4177 | TEST_LE_U( function_output_length, |
| 4178 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4179 | total_output_length += function_output_length; |
| 4180 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4181 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4182 | status = psa_cipher_finish( &operation, |
| 4183 | ( output_buffer_size == 0 ? NULL : |
| 4184 | output + total_output_length ), |
| 4185 | output_buffer_size - total_output_length, |
| 4186 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4187 | TEST_LE_U( function_output_length, |
| 4188 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4189 | TEST_LE_U( function_output_length, |
| 4190 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4191 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4192 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4193 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4194 | if( expected_status == PSA_SUCCESS ) |
| 4195 | { |
| 4196 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4197 | |
| 4198 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4199 | output, total_output_length ); |
| 4200 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4201 | |
| 4202 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4203 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4204 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4205 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4206 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4207 | } |
| 4208 | /* END_CASE */ |
| 4209 | |
| 4210 | /* BEGIN_CASE */ |
| 4211 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4212 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4213 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4214 | int first_part_size_arg, |
| 4215 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4216 | data_t *expected_output, |
| 4217 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4218 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4219 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4220 | psa_key_type_t key_type = key_type_arg; |
| 4221 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4222 | psa_status_t status; |
| 4223 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4224 | size_t first_part_size = first_part_size_arg; |
| 4225 | size_t output1_length = output1_length_arg; |
| 4226 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4227 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4228 | size_t output_buffer_size = 0; |
| 4229 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4230 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4231 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4232 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4233 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4234 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4235 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4236 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4237 | psa_set_key_algorithm( &attributes, alg ); |
| 4238 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4239 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4240 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4241 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4242 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4243 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4244 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4245 | if( iv->len > 0 ) |
| 4246 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4247 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4248 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4249 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4250 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4251 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4252 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4253 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4254 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4255 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4256 | input->x, first_part_size, |
| 4257 | output, output_buffer_size, |
| 4258 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4259 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4260 | TEST_LE_U( function_output_length, |
| 4261 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4262 | TEST_LE_U( function_output_length, |
| 4263 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4264 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4265 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4266 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4267 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4268 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4269 | input->x + first_part_size, |
| 4270 | input->len - first_part_size, |
| 4271 | ( output_buffer_size == 0 ? NULL : |
| 4272 | output + total_output_length ), |
| 4273 | output_buffer_size - total_output_length, |
| 4274 | &function_output_length ) ); |
| 4275 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4276 | TEST_LE_U( function_output_length, |
| 4277 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4278 | alg, |
| 4279 | input->len - first_part_size ) ); |
| 4280 | TEST_LE_U( function_output_length, |
| 4281 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4282 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4283 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4284 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4285 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4286 | ( output_buffer_size == 0 ? NULL : |
| 4287 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4288 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4289 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4290 | TEST_LE_U( function_output_length, |
| 4291 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4292 | TEST_LE_U( function_output_length, |
| 4293 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4294 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4295 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4296 | |
| 4297 | if( expected_status == PSA_SUCCESS ) |
| 4298 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4299 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4300 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4301 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4302 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4303 | } |
| 4304 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4305 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4306 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4307 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4308 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4309 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4310 | } |
| 4311 | /* END_CASE */ |
| 4312 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4313 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4314 | void cipher_decrypt_fail( int alg_arg, |
| 4315 | int key_type_arg, |
| 4316 | data_t *key_data, |
| 4317 | data_t *iv, |
| 4318 | data_t *input_arg, |
| 4319 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4320 | { |
| 4321 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4322 | psa_status_t status; |
| 4323 | psa_key_type_t key_type = key_type_arg; |
| 4324 | psa_algorithm_t alg = alg_arg; |
| 4325 | psa_status_t expected_status = expected_status_arg; |
| 4326 | unsigned char *input = NULL; |
| 4327 | size_t input_buffer_size = 0; |
| 4328 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4329 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4330 | size_t output_buffer_size = 0; |
| 4331 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4332 | size_t function_output_length; |
| 4333 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4334 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4335 | |
| 4336 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4337 | { |
| 4338 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4339 | |
| 4340 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4341 | psa_set_key_algorithm( &attributes, alg ); |
| 4342 | psa_set_key_type( &attributes, key_type ); |
| 4343 | |
| 4344 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4345 | &key ) ); |
| 4346 | } |
| 4347 | |
| 4348 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4349 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4350 | if ( input_buffer_size > 0 ) |
| 4351 | { |
| 4352 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4353 | memcpy( input, iv->x, iv->len ); |
| 4354 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4355 | } |
| 4356 | |
| 4357 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4358 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4359 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4360 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4361 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4362 | output_buffer_size, &output_length ); |
| 4363 | TEST_EQUAL( status, expected_status ); |
| 4364 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4365 | /* Decrypt, multi-part */ |
| 4366 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4367 | if( status == PSA_SUCCESS ) |
| 4368 | { |
| 4369 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4370 | input_arg->len ) + |
| 4371 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4372 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4373 | |
| 4374 | if( iv->len > 0 ) |
| 4375 | { |
| 4376 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4377 | |
| 4378 | if( status != PSA_SUCCESS ) |
| 4379 | TEST_EQUAL( status, expected_status ); |
| 4380 | } |
| 4381 | |
| 4382 | if( status == PSA_SUCCESS ) |
| 4383 | { |
| 4384 | status = psa_cipher_update( &operation, |
| 4385 | input_arg->x, input_arg->len, |
| 4386 | output_multi, output_buffer_size, |
| 4387 | &function_output_length ); |
| 4388 | if( status == PSA_SUCCESS ) |
| 4389 | { |
| 4390 | output_length = function_output_length; |
| 4391 | |
| 4392 | status = psa_cipher_finish( &operation, |
| 4393 | output_multi + output_length, |
| 4394 | output_buffer_size - output_length, |
| 4395 | &function_output_length ); |
| 4396 | |
| 4397 | TEST_EQUAL( status, expected_status ); |
| 4398 | } |
| 4399 | else |
| 4400 | { |
| 4401 | TEST_EQUAL( status, expected_status ); |
| 4402 | } |
| 4403 | } |
| 4404 | else |
| 4405 | { |
| 4406 | TEST_EQUAL( status, expected_status ); |
| 4407 | } |
| 4408 | } |
| 4409 | else |
| 4410 | { |
| 4411 | TEST_EQUAL( status, expected_status ); |
| 4412 | } |
| 4413 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4414 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4415 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4416 | mbedtls_free( input ); |
| 4417 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4418 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4419 | psa_destroy_key( key ); |
| 4420 | PSA_DONE( ); |
| 4421 | } |
| 4422 | /* END_CASE */ |
| 4423 | |
| 4424 | /* BEGIN_CASE */ |
| 4425 | void cipher_decrypt( int alg_arg, |
| 4426 | int key_type_arg, |
| 4427 | data_t *key_data, |
| 4428 | data_t *iv, |
| 4429 | data_t *input_arg, |
| 4430 | data_t *expected_output ) |
| 4431 | { |
| 4432 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4433 | psa_key_type_t key_type = key_type_arg; |
| 4434 | psa_algorithm_t alg = alg_arg; |
| 4435 | unsigned char *input = NULL; |
| 4436 | size_t input_buffer_size = 0; |
| 4437 | unsigned char *output = NULL; |
| 4438 | size_t output_buffer_size = 0; |
| 4439 | size_t output_length = 0; |
| 4440 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4441 | |
| 4442 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4443 | |
| 4444 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4445 | psa_set_key_algorithm( &attributes, alg ); |
| 4446 | psa_set_key_type( &attributes, key_type ); |
| 4447 | |
| 4448 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4449 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4450 | if ( input_buffer_size > 0 ) |
| 4451 | { |
| 4452 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4453 | memcpy( input, iv->x, iv->len ); |
| 4454 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4455 | } |
| 4456 | |
| 4457 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4458 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4459 | |
| 4460 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4461 | &key ) ); |
| 4462 | |
| 4463 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4464 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4465 | TEST_LE_U( output_length, |
| 4466 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4467 | TEST_LE_U( output_length, |
| 4468 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4469 | |
| 4470 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4471 | output, output_length ); |
| 4472 | exit: |
| 4473 | mbedtls_free( input ); |
| 4474 | mbedtls_free( output ); |
| 4475 | psa_destroy_key( key ); |
| 4476 | PSA_DONE( ); |
| 4477 | } |
| 4478 | /* END_CASE */ |
| 4479 | |
| 4480 | /* BEGIN_CASE */ |
| 4481 | void cipher_verify_output( int alg_arg, |
| 4482 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4483 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4484 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4485 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4486 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4487 | psa_key_type_t key_type = key_type_arg; |
| 4488 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4489 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4490 | size_t output1_size = 0; |
| 4491 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4492 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4493 | size_t output2_size = 0; |
| 4494 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4495 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4496 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4497 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4498 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4499 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4500 | psa_set_key_algorithm( &attributes, alg ); |
| 4501 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4502 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4503 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4504 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4505 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4506 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4507 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4508 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4509 | output1, output1_size, |
| 4510 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4511 | TEST_LE_U( output1_length, |
| 4512 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4513 | TEST_LE_U( output1_length, |
| 4514 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4515 | |
| 4516 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4517 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4518 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4519 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4520 | output2, output2_size, |
| 4521 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4522 | TEST_LE_U( output2_length, |
| 4523 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4524 | TEST_LE_U( output2_length, |
| 4525 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4526 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4527 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4528 | |
| 4529 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4530 | mbedtls_free( output1 ); |
| 4531 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4532 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4533 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4534 | } |
| 4535 | /* END_CASE */ |
| 4536 | |
| 4537 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4538 | void cipher_verify_output_multipart( int alg_arg, |
| 4539 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4540 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4541 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4542 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4543 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4544 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4545 | psa_key_type_t key_type = key_type_arg; |
| 4546 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4547 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4548 | unsigned char iv[16] = {0}; |
| 4549 | size_t iv_size = 16; |
| 4550 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4551 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4552 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4553 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4554 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4555 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4556 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4557 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4558 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4559 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4560 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4561 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4562 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4563 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4564 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4565 | psa_set_key_algorithm( &attributes, alg ); |
| 4566 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4567 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4568 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4569 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4570 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4571 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4572 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4573 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4574 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4575 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4576 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4577 | iv, iv_size, |
| 4578 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4579 | } |
| 4580 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4581 | 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] | 4582 | TEST_LE_U( output1_buffer_size, |
| 4583 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4584 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4585 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4586 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4587 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4588 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4589 | output1, output1_buffer_size, |
| 4590 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4591 | TEST_LE_U( function_output_length, |
| 4592 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4593 | TEST_LE_U( function_output_length, |
| 4594 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4595 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4596 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4597 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4598 | input->x + first_part_size, |
| 4599 | input->len - first_part_size, |
| 4600 | output1, output1_buffer_size, |
| 4601 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4602 | TEST_LE_U( function_output_length, |
| 4603 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4604 | alg, |
| 4605 | input->len - first_part_size ) ); |
| 4606 | TEST_LE_U( function_output_length, |
| 4607 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4608 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4609 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4610 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4611 | output1 + output1_length, |
| 4612 | output1_buffer_size - output1_length, |
| 4613 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4614 | TEST_LE_U( function_output_length, |
| 4615 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4616 | TEST_LE_U( function_output_length, |
| 4617 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4618 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4619 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4620 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4621 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4622 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4623 | TEST_LE_U( output2_buffer_size, |
| 4624 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4625 | TEST_LE_U( output2_buffer_size, |
| 4626 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4627 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4628 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4629 | if( iv_length > 0 ) |
| 4630 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4631 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4632 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4633 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4634 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4635 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4636 | output2, output2_buffer_size, |
| 4637 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4638 | TEST_LE_U( function_output_length, |
| 4639 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4640 | TEST_LE_U( function_output_length, |
| 4641 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4642 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4643 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4644 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4645 | output1 + first_part_size, |
| 4646 | output1_length - first_part_size, |
| 4647 | output2, output2_buffer_size, |
| 4648 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4649 | TEST_LE_U( function_output_length, |
| 4650 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4651 | alg, |
| 4652 | output1_length - first_part_size ) ); |
| 4653 | TEST_LE_U( function_output_length, |
| 4654 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4655 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4656 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4657 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4658 | output2 + output2_length, |
| 4659 | output2_buffer_size - output2_length, |
| 4660 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4661 | TEST_LE_U( function_output_length, |
| 4662 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4663 | TEST_LE_U( function_output_length, |
| 4664 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4665 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4666 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4667 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4668 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4669 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4670 | |
| 4671 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4672 | psa_cipher_abort( &operation1 ); |
| 4673 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4674 | mbedtls_free( output1 ); |
| 4675 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4676 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4677 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4678 | } |
| 4679 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4680 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4681 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4682 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4683 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4684 | data_t *nonce, |
| 4685 | data_t *additional_data, |
| 4686 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4687 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4688 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4689 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4690 | psa_key_type_t key_type = key_type_arg; |
| 4691 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4692 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4693 | unsigned char *output_data = NULL; |
| 4694 | size_t output_size = 0; |
| 4695 | size_t output_length = 0; |
| 4696 | unsigned char *output_data2 = NULL; |
| 4697 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4698 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4699 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4700 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4701 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4702 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4703 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4704 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4705 | psa_set_key_algorithm( &attributes, alg ); |
| 4706 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4707 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4708 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4709 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4710 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4711 | key_bits = psa_get_key_bits( &attributes ); |
| 4712 | |
| 4713 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4714 | alg ); |
| 4715 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4716 | * should be exact. */ |
| 4717 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4718 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4719 | { |
| 4720 | TEST_EQUAL( output_size, |
| 4721 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4722 | TEST_LE_U( output_size, |
| 4723 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4724 | } |
| 4725 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4726 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4727 | status = psa_aead_encrypt( key, alg, |
| 4728 | nonce->x, nonce->len, |
| 4729 | additional_data->x, |
| 4730 | additional_data->len, |
| 4731 | input_data->x, input_data->len, |
| 4732 | output_data, output_size, |
| 4733 | &output_length ); |
| 4734 | |
| 4735 | /* If the operation is not supported, just skip and not fail in case the |
| 4736 | * encryption involves a common limitation of cryptography hardwares and |
| 4737 | * an alternative implementation. */ |
| 4738 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4739 | { |
| 4740 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4741 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4742 | } |
| 4743 | |
| 4744 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4745 | |
| 4746 | if( PSA_SUCCESS == expected_result ) |
| 4747 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4748 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4749 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4750 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4751 | * should be exact. */ |
| 4752 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4753 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4754 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4755 | TEST_LE_U( input_data->len, |
| 4756 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4757 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4758 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4759 | nonce->x, nonce->len, |
| 4760 | additional_data->x, |
| 4761 | additional_data->len, |
| 4762 | output_data, output_length, |
| 4763 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4764 | &output_length2 ), |
| 4765 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4766 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4767 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4768 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4769 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4770 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4771 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4772 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4773 | mbedtls_free( output_data ); |
| 4774 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4775 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4776 | } |
| 4777 | /* END_CASE */ |
| 4778 | |
| 4779 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4780 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4781 | int alg_arg, |
| 4782 | data_t *nonce, |
| 4783 | data_t *additional_data, |
| 4784 | data_t *input_data, |
| 4785 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4786 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4787 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4788 | psa_key_type_t key_type = key_type_arg; |
| 4789 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4790 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4791 | unsigned char *output_data = NULL; |
| 4792 | size_t output_size = 0; |
| 4793 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4794 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4795 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4796 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4797 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4798 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4799 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4800 | psa_set_key_algorithm( &attributes, alg ); |
| 4801 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4802 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4803 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4804 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4805 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4806 | key_bits = psa_get_key_bits( &attributes ); |
| 4807 | |
| 4808 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4809 | alg ); |
| 4810 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4811 | * should be exact. */ |
| 4812 | TEST_EQUAL( output_size, |
| 4813 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4814 | TEST_LE_U( output_size, |
| 4815 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4816 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4817 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4818 | status = psa_aead_encrypt( key, alg, |
| 4819 | nonce->x, nonce->len, |
| 4820 | additional_data->x, additional_data->len, |
| 4821 | input_data->x, input_data->len, |
| 4822 | output_data, output_size, |
| 4823 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4824 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4825 | /* If the operation is not supported, just skip and not fail in case the |
| 4826 | * encryption involves a common limitation of cryptography hardwares and |
| 4827 | * an alternative implementation. */ |
| 4828 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4829 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4830 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4831 | 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] | 4832 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4833 | |
| 4834 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4835 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4836 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4837 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4838 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4839 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4840 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4841 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4842 | } |
| 4843 | /* END_CASE */ |
| 4844 | |
| 4845 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4846 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4847 | int alg_arg, |
| 4848 | data_t *nonce, |
| 4849 | data_t *additional_data, |
| 4850 | data_t *input_data, |
| 4851 | data_t *expected_data, |
| 4852 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4853 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4854 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4855 | psa_key_type_t key_type = key_type_arg; |
| 4856 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4857 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4858 | unsigned char *output_data = NULL; |
| 4859 | size_t output_size = 0; |
| 4860 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4861 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4862 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4863 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4864 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4865 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4866 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4867 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4868 | psa_set_key_algorithm( &attributes, alg ); |
| 4869 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4870 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4871 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4872 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4873 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4874 | key_bits = psa_get_key_bits( &attributes ); |
| 4875 | |
| 4876 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4877 | alg ); |
| 4878 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4879 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4880 | { |
| 4881 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4882 | * should be exact. */ |
| 4883 | TEST_EQUAL( output_size, |
| 4884 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4885 | TEST_LE_U( output_size, |
| 4886 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4887 | } |
| 4888 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4889 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4890 | status = psa_aead_decrypt( key, alg, |
| 4891 | nonce->x, nonce->len, |
| 4892 | additional_data->x, |
| 4893 | additional_data->len, |
| 4894 | input_data->x, input_data->len, |
| 4895 | output_data, output_size, |
| 4896 | &output_length ); |
| 4897 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4898 | /* If the operation is not supported, just skip and not fail in case the |
| 4899 | * decryption involves a common limitation of cryptography hardwares and |
| 4900 | * an alternative implementation. */ |
| 4901 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4902 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4903 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4904 | 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] | 4905 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4906 | |
| 4907 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4908 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4909 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4910 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4911 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4912 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4913 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4914 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4915 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4916 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4917 | } |
| 4918 | /* END_CASE */ |
| 4919 | |
| 4920 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4921 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4922 | int alg_arg, |
| 4923 | data_t *nonce, |
| 4924 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4925 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4926 | int do_set_lengths, |
| 4927 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4928 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4929 | size_t ad_part_len = 0; |
| 4930 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4931 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4932 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4933 | 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] | 4934 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4935 | mbedtls_test_set_step( ad_part_len ); |
| 4936 | |
| 4937 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4938 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4939 | if( ad_part_len & 0x01 ) |
| 4940 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4941 | else |
| 4942 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4943 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4944 | |
| 4945 | /* Split ad into length(ad_part_len) parts. */ |
| 4946 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4947 | alg_arg, nonce, |
| 4948 | additional_data, |
| 4949 | ad_part_len, |
| 4950 | input_data, -1, |
| 4951 | set_lengths_method, |
| 4952 | expected_output, |
| 4953 | 1, 0 ) ) |
| 4954 | break; |
| 4955 | |
| 4956 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4957 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4958 | |
| 4959 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4960 | alg_arg, nonce, |
| 4961 | additional_data, |
| 4962 | ad_part_len, |
| 4963 | input_data, -1, |
| 4964 | set_lengths_method, |
| 4965 | expected_output, |
| 4966 | 1, 1 ) ) |
| 4967 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4968 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4969 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4970 | 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] | 4971 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4972 | /* Split data into length(data_part_len) parts. */ |
| 4973 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 4974 | |
| 4975 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4976 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4977 | if( data_part_len & 0x01 ) |
| 4978 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4979 | else |
| 4980 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4981 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4982 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4983 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4984 | alg_arg, nonce, |
| 4985 | additional_data, -1, |
| 4986 | input_data, data_part_len, |
| 4987 | set_lengths_method, |
| 4988 | expected_output, |
| 4989 | 1, 0 ) ) |
| 4990 | break; |
| 4991 | |
| 4992 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 4993 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 4994 | |
| 4995 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4996 | alg_arg, nonce, |
| 4997 | additional_data, -1, |
| 4998 | input_data, data_part_len, |
| 4999 | set_lengths_method, |
| 5000 | expected_output, |
| 5001 | 1, 1 ) ) |
| 5002 | break; |
| 5003 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5004 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5005 | /* Goto is required to silence warnings about unused labels, as we |
| 5006 | * don't actually do any test assertions in this function. */ |
| 5007 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5008 | } |
| 5009 | /* END_CASE */ |
| 5010 | |
| 5011 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5012 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 5013 | int alg_arg, |
| 5014 | data_t *nonce, |
| 5015 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5016 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 5017 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5018 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5019 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5020 | size_t ad_part_len = 0; |
| 5021 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5022 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5023 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5024 | 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] | 5025 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5026 | /* Split ad into length(ad_part_len) parts. */ |
| 5027 | mbedtls_test_set_step( ad_part_len ); |
| 5028 | |
| 5029 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5030 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5031 | if( ad_part_len & 0x01 ) |
| 5032 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5033 | else |
| 5034 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5035 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5036 | |
| 5037 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5038 | alg_arg, nonce, |
| 5039 | additional_data, |
| 5040 | ad_part_len, |
| 5041 | input_data, -1, |
| 5042 | set_lengths_method, |
| 5043 | expected_output, |
| 5044 | 0, 0 ) ) |
| 5045 | break; |
| 5046 | |
| 5047 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5048 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5049 | |
| 5050 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5051 | alg_arg, nonce, |
| 5052 | additional_data, |
| 5053 | ad_part_len, |
| 5054 | input_data, -1, |
| 5055 | set_lengths_method, |
| 5056 | expected_output, |
| 5057 | 0, 1 ) ) |
| 5058 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5059 | } |
| 5060 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5061 | 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] | 5062 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5063 | /* Split data into length(data_part_len) parts. */ |
| 5064 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5065 | |
| 5066 | if( do_set_lengths ) |
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 | if( data_part_len & 0x01 ) |
| 5069 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5070 | else |
| 5071 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5072 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5073 | |
| 5074 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5075 | alg_arg, nonce, |
| 5076 | additional_data, -1, |
| 5077 | input_data, data_part_len, |
| 5078 | set_lengths_method, |
| 5079 | expected_output, |
| 5080 | 0, 0 ) ) |
| 5081 | break; |
| 5082 | |
| 5083 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5084 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5085 | |
| 5086 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5087 | alg_arg, nonce, |
| 5088 | additional_data, -1, |
| 5089 | input_data, data_part_len, |
| 5090 | set_lengths_method, |
| 5091 | expected_output, |
| 5092 | 0, 1 ) ) |
| 5093 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5094 | } |
| 5095 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5096 | /* Goto is required to silence warnings about unused labels, as we |
| 5097 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5098 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5099 | } |
| 5100 | /* END_CASE */ |
| 5101 | |
| 5102 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5103 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 5104 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5105 | int nonce_length, |
| 5106 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5107 | data_t *additional_data, |
| 5108 | data_t *input_data, |
| 5109 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5110 | { |
| 5111 | |
| 5112 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5113 | psa_key_type_t key_type = key_type_arg; |
| 5114 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5115 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5116 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5117 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5118 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5119 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5120 | size_t actual_nonce_length = 0; |
| 5121 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5122 | unsigned char *output = NULL; |
| 5123 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5124 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5125 | size_t ciphertext_size = 0; |
| 5126 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5127 | size_t tag_length = 0; |
| 5128 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5129 | |
| 5130 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5131 | |
| 5132 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5133 | psa_set_key_algorithm( & attributes, alg ); |
| 5134 | psa_set_key_type( & attributes, key_type ); |
| 5135 | |
| 5136 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5137 | &key ) ); |
| 5138 | |
| 5139 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5140 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5141 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5142 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5143 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5144 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5145 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5146 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5147 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5148 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5149 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5150 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5151 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5152 | |
| 5153 | /* If the operation is not supported, just skip and not fail in case the |
| 5154 | * encryption involves a common limitation of cryptography hardwares and |
| 5155 | * an alternative implementation. */ |
| 5156 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5157 | { |
| 5158 | 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] | 5159 | 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] | 5160 | } |
| 5161 | |
| 5162 | PSA_ASSERT( status ); |
| 5163 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5164 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5165 | nonce_length, |
| 5166 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5167 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5168 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5169 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5170 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5171 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5172 | if( expected_status == PSA_SUCCESS ) |
| 5173 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 5174 | alg ) ); |
| 5175 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5176 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5177 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5178 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5179 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5180 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 5181 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5182 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5183 | |
| 5184 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5185 | additional_data->len ) ); |
| 5186 | |
| 5187 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5188 | output, output_size, |
| 5189 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5190 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5191 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5192 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5193 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5194 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5195 | |
| 5196 | exit: |
| 5197 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5198 | mbedtls_free( output ); |
| 5199 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5200 | psa_aead_abort( &operation ); |
| 5201 | PSA_DONE( ); |
| 5202 | } |
| 5203 | /* END_CASE */ |
| 5204 | |
| 5205 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5206 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 5207 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5208 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5209 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5210 | data_t *additional_data, |
| 5211 | data_t *input_data, |
| 5212 | int expected_status_arg ) |
| 5213 | { |
| 5214 | |
| 5215 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5216 | psa_key_type_t key_type = key_type_arg; |
| 5217 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5218 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5219 | uint8_t *nonce_buffer = NULL; |
| 5220 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5221 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5222 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5223 | unsigned char *output = NULL; |
| 5224 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5225 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5226 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5227 | size_t ciphertext_size = 0; |
| 5228 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5229 | size_t tag_length = 0; |
| 5230 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5231 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5232 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5233 | |
| 5234 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5235 | |
| 5236 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5237 | psa_set_key_algorithm( &attributes, alg ); |
| 5238 | psa_set_key_type( &attributes, key_type ); |
| 5239 | |
| 5240 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5241 | &key ) ); |
| 5242 | |
| 5243 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5244 | |
| 5245 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5246 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5247 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5248 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5249 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5250 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5251 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5252 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5253 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5254 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5255 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5256 | |
| 5257 | /* If the operation is not supported, just skip and not fail in case the |
| 5258 | * encryption involves a common limitation of cryptography hardwares and |
| 5259 | * an alternative implementation. */ |
| 5260 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5261 | { |
| 5262 | 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] | 5263 | 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] | 5264 | } |
| 5265 | |
| 5266 | PSA_ASSERT( status ); |
| 5267 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5268 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 5269 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5270 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 5271 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5272 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5273 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5274 | } |
| 5275 | else |
| 5276 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5277 | /* If length is zero, then this will return NULL. */ |
| 5278 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5279 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5280 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5281 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5282 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5283 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5284 | { |
| 5285 | nonce_buffer[index] = 'a' + index; |
| 5286 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5287 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5288 | } |
| 5289 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5290 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5291 | { |
| 5292 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5293 | input_data->len ) ); |
| 5294 | } |
| 5295 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5296 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5297 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5298 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5299 | |
| 5300 | if( expected_status == PSA_SUCCESS ) |
| 5301 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5302 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5303 | { |
| 5304 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5305 | input_data->len ) ); |
| 5306 | } |
| 5307 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5308 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5309 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5310 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5311 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5312 | additional_data->len ), |
| 5313 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5314 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5315 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5316 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5317 | &ciphertext_length ), |
| 5318 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5319 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5320 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5321 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5322 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5323 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | exit: |
| 5327 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5328 | mbedtls_free( output ); |
| 5329 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5330 | mbedtls_free( nonce_buffer ); |
| 5331 | psa_aead_abort( &operation ); |
| 5332 | PSA_DONE( ); |
| 5333 | } |
| 5334 | /* END_CASE */ |
| 5335 | |
| 5336 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5337 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5338 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5339 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5340 | data_t *nonce, |
| 5341 | data_t *additional_data, |
| 5342 | data_t *input_data, |
| 5343 | int expected_status_arg ) |
| 5344 | { |
| 5345 | |
| 5346 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5347 | psa_key_type_t key_type = key_type_arg; |
| 5348 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5349 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5350 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5351 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5352 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5353 | unsigned char *output = NULL; |
| 5354 | unsigned char *ciphertext = NULL; |
| 5355 | size_t output_size = output_size_arg; |
| 5356 | size_t ciphertext_size = 0; |
| 5357 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5358 | size_t tag_length = 0; |
| 5359 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5360 | |
| 5361 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5362 | |
| 5363 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5364 | psa_set_key_algorithm( &attributes, alg ); |
| 5365 | psa_set_key_type( &attributes, key_type ); |
| 5366 | |
| 5367 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5368 | &key ) ); |
| 5369 | |
| 5370 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5371 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5372 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5373 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5374 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5375 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5376 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5377 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5378 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5379 | |
| 5380 | /* If the operation is not supported, just skip and not fail in case the |
| 5381 | * encryption involves a common limitation of cryptography hardwares and |
| 5382 | * an alternative implementation. */ |
| 5383 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5384 | { |
| 5385 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5386 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5387 | } |
| 5388 | |
| 5389 | PSA_ASSERT( status ); |
| 5390 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5391 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5392 | input_data->len ) ); |
| 5393 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5394 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5395 | |
| 5396 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5397 | additional_data->len ) ); |
| 5398 | |
| 5399 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5400 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5401 | |
| 5402 | TEST_EQUAL( status, expected_status ); |
| 5403 | |
| 5404 | if( expected_status == PSA_SUCCESS ) |
| 5405 | { |
| 5406 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5407 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5408 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5409 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5410 | } |
| 5411 | |
| 5412 | exit: |
| 5413 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5414 | mbedtls_free( output ); |
| 5415 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5416 | psa_aead_abort( &operation ); |
| 5417 | PSA_DONE( ); |
| 5418 | } |
| 5419 | /* END_CASE */ |
| 5420 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5421 | /* BEGIN_CASE */ |
| 5422 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5423 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5424 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5425 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5426 | data_t *nonce, |
| 5427 | data_t *additional_data, |
| 5428 | data_t *input_data, |
| 5429 | int expected_status_arg ) |
| 5430 | { |
| 5431 | |
| 5432 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5433 | psa_key_type_t key_type = key_type_arg; |
| 5434 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5435 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5436 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5437 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5438 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5439 | unsigned char *ciphertext = NULL; |
| 5440 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5441 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5442 | size_t ciphertext_size = 0; |
| 5443 | size_t ciphertext_length = 0; |
| 5444 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5445 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5446 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5447 | |
| 5448 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5449 | |
| 5450 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5451 | psa_set_key_algorithm( &attributes, alg ); |
| 5452 | psa_set_key_type( &attributes, key_type ); |
| 5453 | |
| 5454 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5455 | &key ) ); |
| 5456 | |
| 5457 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5458 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5459 | 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] | 5460 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5461 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5462 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5463 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5464 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5465 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5466 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5467 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5468 | |
| 5469 | /* If the operation is not supported, just skip and not fail in case the |
| 5470 | * encryption involves a common limitation of cryptography hardwares and |
| 5471 | * an alternative implementation. */ |
| 5472 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5473 | { |
| 5474 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5475 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5476 | } |
| 5477 | |
| 5478 | PSA_ASSERT( status ); |
| 5479 | |
| 5480 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5481 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5482 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5483 | input_data->len ) ); |
| 5484 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5485 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5486 | additional_data->len ) ); |
| 5487 | |
| 5488 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5489 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5490 | |
| 5491 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5492 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5493 | finish_ciphertext_size, |
| 5494 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5495 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5496 | |
| 5497 | TEST_EQUAL( status, expected_status ); |
| 5498 | |
| 5499 | exit: |
| 5500 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5501 | mbedtls_free( ciphertext ); |
| 5502 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5503 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5504 | psa_aead_abort( &operation ); |
| 5505 | PSA_DONE( ); |
| 5506 | } |
| 5507 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5508 | |
| 5509 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5510 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5511 | int alg_arg, |
| 5512 | data_t *nonce, |
| 5513 | data_t *additional_data, |
| 5514 | data_t *input_data, |
| 5515 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5516 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5517 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5518 | int expected_status_arg ) |
| 5519 | { |
| 5520 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5521 | psa_key_type_t key_type = key_type_arg; |
| 5522 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5523 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5524 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5525 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5526 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5527 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5528 | unsigned char *plaintext = NULL; |
| 5529 | unsigned char *finish_plaintext = NULL; |
| 5530 | size_t plaintext_size = 0; |
| 5531 | size_t plaintext_length = 0; |
| 5532 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5533 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5534 | unsigned char *tag_buffer = NULL; |
| 5535 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5536 | |
| 5537 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5538 | |
| 5539 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5540 | psa_set_key_algorithm( &attributes, alg ); |
| 5541 | psa_set_key_type( &attributes, key_type ); |
| 5542 | |
| 5543 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5544 | &key ) ); |
| 5545 | |
| 5546 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5547 | |
| 5548 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5549 | input_data->len ); |
| 5550 | |
| 5551 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5552 | |
| 5553 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5554 | |
| 5555 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5556 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5557 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5558 | |
| 5559 | /* If the operation is not supported, just skip and not fail in case the |
| 5560 | * encryption involves a common limitation of cryptography hardwares and |
| 5561 | * an alternative implementation. */ |
| 5562 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5563 | { |
| 5564 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5565 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5566 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5567 | TEST_EQUAL( status, expected_setup_status ); |
| 5568 | |
| 5569 | if( status != PSA_SUCCESS ) |
| 5570 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5571 | |
| 5572 | PSA_ASSERT( status ); |
| 5573 | |
| 5574 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5575 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5576 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5577 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5578 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5579 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5580 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5581 | additional_data->len ) ); |
| 5582 | |
| 5583 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5584 | input_data->len, |
| 5585 | plaintext, plaintext_size, |
| 5586 | &plaintext_length ) ); |
| 5587 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5588 | if( tag_usage == USE_GIVEN_TAG ) |
| 5589 | { |
| 5590 | tag_buffer = tag->x; |
| 5591 | tag_size = tag->len; |
| 5592 | } |
| 5593 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5594 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5595 | verify_plaintext_size, |
| 5596 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5597 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5598 | |
| 5599 | TEST_EQUAL( status, expected_status ); |
| 5600 | |
| 5601 | exit: |
| 5602 | psa_destroy_key( key ); |
| 5603 | mbedtls_free( plaintext ); |
| 5604 | mbedtls_free( finish_plaintext ); |
| 5605 | psa_aead_abort( &operation ); |
| 5606 | PSA_DONE( ); |
| 5607 | } |
| 5608 | /* END_CASE */ |
| 5609 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5610 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5611 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5612 | int alg_arg, int expected_status_arg ) |
| 5613 | { |
| 5614 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5615 | psa_key_type_t key_type = key_type_arg; |
| 5616 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5617 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5618 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5619 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5620 | psa_status_t expected_status = expected_status_arg; |
| 5621 | |
| 5622 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5623 | |
| 5624 | psa_set_key_usage_flags( &attributes, |
| 5625 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5626 | psa_set_key_algorithm( &attributes, alg ); |
| 5627 | psa_set_key_type( &attributes, key_type ); |
| 5628 | |
| 5629 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5630 | &key ) ); |
| 5631 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5632 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5633 | |
| 5634 | TEST_EQUAL( status, expected_status ); |
| 5635 | |
| 5636 | psa_aead_abort( &operation ); |
| 5637 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5638 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5639 | |
| 5640 | TEST_EQUAL(status, expected_status ); |
| 5641 | |
| 5642 | exit: |
| 5643 | psa_destroy_key( key ); |
| 5644 | psa_aead_abort( &operation ); |
| 5645 | PSA_DONE( ); |
| 5646 | } |
| 5647 | /* END_CASE */ |
| 5648 | |
| 5649 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5650 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5651 | int alg_arg, |
| 5652 | data_t *nonce, |
| 5653 | data_t *additional_data, |
| 5654 | data_t *input_data ) |
| 5655 | { |
| 5656 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5657 | psa_key_type_t key_type = key_type_arg; |
| 5658 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5659 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5660 | unsigned char *output_data = NULL; |
| 5661 | unsigned char *final_data = NULL; |
| 5662 | size_t output_size = 0; |
| 5663 | size_t finish_output_size = 0; |
| 5664 | size_t output_length = 0; |
| 5665 | size_t key_bits = 0; |
| 5666 | size_t tag_length = 0; |
| 5667 | size_t tag_size = 0; |
| 5668 | size_t nonce_length = 0; |
| 5669 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5670 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5671 | size_t output_part_length = 0; |
| 5672 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5673 | |
| 5674 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5675 | |
| 5676 | psa_set_key_usage_flags( & attributes, |
| 5677 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5678 | psa_set_key_algorithm( & attributes, alg ); |
| 5679 | psa_set_key_type( & attributes, key_type ); |
| 5680 | |
| 5681 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5682 | &key ) ); |
| 5683 | |
| 5684 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5685 | key_bits = psa_get_key_bits( &attributes ); |
| 5686 | |
| 5687 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5688 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5689 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5690 | |
| 5691 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5692 | |
| 5693 | ASSERT_ALLOC( output_data, output_size ); |
| 5694 | |
| 5695 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5696 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5697 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5698 | |
| 5699 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5700 | |
| 5701 | /* Test all operations error without calling setup first. */ |
| 5702 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5703 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5704 | PSA_ERROR_BAD_STATE ); |
| 5705 | |
| 5706 | psa_aead_abort( &operation ); |
| 5707 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5708 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5709 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5710 | &nonce_length ), |
| 5711 | PSA_ERROR_BAD_STATE ); |
| 5712 | |
| 5713 | psa_aead_abort( &operation ); |
| 5714 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5715 | /* ------------------------------------------------------- */ |
| 5716 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5717 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5718 | input_data->len ), |
| 5719 | PSA_ERROR_BAD_STATE ); |
| 5720 | |
| 5721 | psa_aead_abort( &operation ); |
| 5722 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5723 | /* ------------------------------------------------------- */ |
| 5724 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5725 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5726 | additional_data->len ), |
| 5727 | PSA_ERROR_BAD_STATE ); |
| 5728 | |
| 5729 | psa_aead_abort( &operation ); |
| 5730 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5731 | /* ------------------------------------------------------- */ |
| 5732 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5733 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5734 | input_data->len, output_data, |
| 5735 | output_size, &output_length ), |
| 5736 | PSA_ERROR_BAD_STATE ); |
| 5737 | |
| 5738 | psa_aead_abort( &operation ); |
| 5739 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5740 | /* ------------------------------------------------------- */ |
| 5741 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5742 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5743 | finish_output_size, |
| 5744 | &output_part_length, |
| 5745 | tag_buffer, tag_length, |
| 5746 | &tag_size ), |
| 5747 | PSA_ERROR_BAD_STATE ); |
| 5748 | |
| 5749 | psa_aead_abort( &operation ); |
| 5750 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5751 | /* ------------------------------------------------------- */ |
| 5752 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5753 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5754 | finish_output_size, |
| 5755 | &output_part_length, |
| 5756 | tag_buffer, |
| 5757 | tag_length ), |
| 5758 | PSA_ERROR_BAD_STATE ); |
| 5759 | |
| 5760 | psa_aead_abort( &operation ); |
| 5761 | |
| 5762 | /* Test for double setups. */ |
| 5763 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5764 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5765 | |
| 5766 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5767 | PSA_ERROR_BAD_STATE ); |
| 5768 | |
| 5769 | psa_aead_abort( &operation ); |
| 5770 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5771 | /* ------------------------------------------------------- */ |
| 5772 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5773 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5774 | |
| 5775 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5776 | PSA_ERROR_BAD_STATE ); |
| 5777 | |
| 5778 | psa_aead_abort( &operation ); |
| 5779 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5780 | /* ------------------------------------------------------- */ |
| 5781 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5782 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5783 | |
| 5784 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5785 | PSA_ERROR_BAD_STATE ); |
| 5786 | |
| 5787 | psa_aead_abort( &operation ); |
| 5788 | |
| 5789 | /* ------------------------------------------------------- */ |
| 5790 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5791 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5792 | |
| 5793 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5794 | PSA_ERROR_BAD_STATE ); |
| 5795 | |
| 5796 | psa_aead_abort( &operation ); |
| 5797 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5798 | /* Test for not setting a nonce. */ |
| 5799 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5800 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5801 | |
| 5802 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5803 | additional_data->len ), |
| 5804 | PSA_ERROR_BAD_STATE ); |
| 5805 | |
| 5806 | psa_aead_abort( &operation ); |
| 5807 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5808 | /* ------------------------------------------------------- */ |
| 5809 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5810 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5811 | |
| 5812 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5813 | input_data->len, output_data, |
| 5814 | output_size, &output_length ), |
| 5815 | PSA_ERROR_BAD_STATE ); |
| 5816 | |
| 5817 | psa_aead_abort( &operation ); |
| 5818 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5819 | /* ------------------------------------------------------- */ |
| 5820 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5821 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5822 | |
| 5823 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5824 | finish_output_size, |
| 5825 | &output_part_length, |
| 5826 | tag_buffer, tag_length, |
| 5827 | &tag_size ), |
| 5828 | PSA_ERROR_BAD_STATE ); |
| 5829 | |
| 5830 | psa_aead_abort( &operation ); |
| 5831 | |
| 5832 | /* ------------------------------------------------------- */ |
| 5833 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5834 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5835 | |
| 5836 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5837 | finish_output_size, |
| 5838 | &output_part_length, |
| 5839 | tag_buffer, |
| 5840 | tag_length ), |
| 5841 | PSA_ERROR_BAD_STATE ); |
| 5842 | |
| 5843 | psa_aead_abort( &operation ); |
| 5844 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5845 | /* Test for double setting nonce. */ |
| 5846 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5847 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5848 | |
| 5849 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5850 | |
| 5851 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5852 | PSA_ERROR_BAD_STATE ); |
| 5853 | |
| 5854 | psa_aead_abort( &operation ); |
| 5855 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5856 | /* Test for double generating nonce. */ |
| 5857 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5858 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5859 | |
| 5860 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5861 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5862 | &nonce_length ) ); |
| 5863 | |
| 5864 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5865 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5866 | &nonce_length ), |
| 5867 | PSA_ERROR_BAD_STATE ); |
| 5868 | |
| 5869 | |
| 5870 | psa_aead_abort( &operation ); |
| 5871 | |
| 5872 | /* Test for generate nonce then set and vice versa */ |
| 5873 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5874 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5875 | |
| 5876 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5877 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5878 | &nonce_length ) ); |
| 5879 | |
| 5880 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5881 | PSA_ERROR_BAD_STATE ); |
| 5882 | |
| 5883 | psa_aead_abort( &operation ); |
| 5884 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5885 | /* Test for generating nonce after calling set lengths */ |
| 5886 | |
| 5887 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5888 | |
| 5889 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5890 | input_data->len ) ); |
| 5891 | |
| 5892 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5893 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5894 | &nonce_length ) ); |
| 5895 | |
| 5896 | psa_aead_abort( &operation ); |
| 5897 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5898 | /* 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] | 5899 | |
| 5900 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5901 | |
| 5902 | if( operation.alg == PSA_ALG_CCM ) |
| 5903 | { |
| 5904 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5905 | input_data->len ), |
| 5906 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5907 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5908 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5909 | &nonce_length ), |
| 5910 | PSA_ERROR_BAD_STATE ); |
| 5911 | } |
| 5912 | else |
| 5913 | { |
| 5914 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5915 | input_data->len ) ); |
| 5916 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5917 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5918 | &nonce_length ) ); |
| 5919 | } |
| 5920 | |
| 5921 | psa_aead_abort( &operation ); |
| 5922 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5923 | /* 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] | 5924 | #if SIZE_MAX > UINT32_MAX |
| 5925 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5926 | |
| 5927 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5928 | { |
| 5929 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5930 | input_data->len ), |
| 5931 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5932 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5933 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5934 | &nonce_length ), |
| 5935 | PSA_ERROR_BAD_STATE ); |
| 5936 | } |
| 5937 | else |
| 5938 | { |
| 5939 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5940 | input_data->len ) ); |
| 5941 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5942 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5943 | &nonce_length ) ); |
| 5944 | } |
| 5945 | |
| 5946 | psa_aead_abort( &operation ); |
| 5947 | #endif |
| 5948 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5949 | /* 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] | 5950 | |
| 5951 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5952 | |
| 5953 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5954 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5955 | &nonce_length ) ); |
| 5956 | |
| 5957 | if( operation.alg == PSA_ALG_CCM ) |
| 5958 | { |
| 5959 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5960 | input_data->len ), |
| 5961 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5962 | } |
| 5963 | else |
| 5964 | { |
| 5965 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5966 | input_data->len ) ); |
| 5967 | } |
| 5968 | |
| 5969 | psa_aead_abort( &operation ); |
| 5970 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5971 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5972 | /* Test for setting nonce after calling set lengths */ |
| 5973 | |
| 5974 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5975 | |
| 5976 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5977 | input_data->len ) ); |
| 5978 | |
| 5979 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5980 | |
| 5981 | psa_aead_abort( &operation ); |
| 5982 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5983 | /* 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] | 5984 | |
| 5985 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5986 | |
| 5987 | if( operation.alg == PSA_ALG_CCM ) |
| 5988 | { |
| 5989 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5990 | input_data->len ), |
| 5991 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5992 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5993 | PSA_ERROR_BAD_STATE ); |
| 5994 | } |
| 5995 | else |
| 5996 | { |
| 5997 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5998 | input_data->len ) ); |
| 5999 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6000 | } |
| 6001 | |
| 6002 | psa_aead_abort( &operation ); |
| 6003 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6004 | /* 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] | 6005 | #if SIZE_MAX > UINT32_MAX |
| 6006 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6007 | |
| 6008 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 6009 | { |
| 6010 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6011 | input_data->len ), |
| 6012 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6013 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6014 | PSA_ERROR_BAD_STATE ); |
| 6015 | } |
| 6016 | else |
| 6017 | { |
| 6018 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6019 | input_data->len ) ); |
| 6020 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6021 | } |
| 6022 | |
| 6023 | psa_aead_abort( &operation ); |
| 6024 | #endif |
| 6025 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6026 | /* 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] | 6027 | |
| 6028 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6029 | |
| 6030 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6031 | |
| 6032 | if( operation.alg == PSA_ALG_CCM ) |
| 6033 | { |
| 6034 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6035 | input_data->len ), |
| 6036 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6037 | } |
| 6038 | else |
| 6039 | { |
| 6040 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6041 | input_data->len ) ); |
| 6042 | } |
| 6043 | |
| 6044 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6045 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6046 | /* 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] | 6047 | #if SIZE_MAX > UINT32_MAX |
| 6048 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6049 | |
| 6050 | if( operation.alg == PSA_ALG_GCM ) |
| 6051 | { |
| 6052 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6053 | SIZE_MAX ), |
| 6054 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6055 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6056 | PSA_ERROR_BAD_STATE ); |
| 6057 | } |
| 6058 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6059 | { |
| 6060 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6061 | SIZE_MAX ) ); |
| 6062 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6063 | } |
| 6064 | |
| 6065 | psa_aead_abort( &operation ); |
| 6066 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6067 | /* 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] | 6068 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6069 | |
| 6070 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6071 | |
| 6072 | if( operation.alg == PSA_ALG_GCM ) |
| 6073 | { |
| 6074 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6075 | SIZE_MAX ), |
| 6076 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6077 | } |
| 6078 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6079 | { |
| 6080 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6081 | SIZE_MAX ) ); |
| 6082 | } |
| 6083 | |
| 6084 | psa_aead_abort( &operation ); |
| 6085 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6086 | |
| 6087 | /* ------------------------------------------------------- */ |
| 6088 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6089 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6090 | |
| 6091 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6092 | |
| 6093 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6094 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6095 | &nonce_length ), |
| 6096 | PSA_ERROR_BAD_STATE ); |
| 6097 | |
| 6098 | psa_aead_abort( &operation ); |
| 6099 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6100 | /* Test for generating nonce in decrypt setup. */ |
| 6101 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6102 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6103 | |
| 6104 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6105 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6106 | &nonce_length ), |
| 6107 | PSA_ERROR_BAD_STATE ); |
| 6108 | |
| 6109 | psa_aead_abort( &operation ); |
| 6110 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6111 | /* Test for setting lengths twice. */ |
| 6112 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6113 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6114 | |
| 6115 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6116 | |
| 6117 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6118 | input_data->len ) ); |
| 6119 | |
| 6120 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6121 | input_data->len ), |
| 6122 | PSA_ERROR_BAD_STATE ); |
| 6123 | |
| 6124 | psa_aead_abort( &operation ); |
| 6125 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6126 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6127 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6128 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6129 | |
| 6130 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6131 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6132 | if( operation.alg == PSA_ALG_CCM ) |
| 6133 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6134 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6135 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6136 | additional_data->len ), |
| 6137 | PSA_ERROR_BAD_STATE ); |
| 6138 | } |
| 6139 | else |
| 6140 | { |
| 6141 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6142 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6143 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6144 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6145 | input_data->len ), |
| 6146 | PSA_ERROR_BAD_STATE ); |
| 6147 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6148 | psa_aead_abort( &operation ); |
| 6149 | |
| 6150 | /* ------------------------------------------------------- */ |
| 6151 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6152 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6153 | |
| 6154 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6155 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6156 | if( operation.alg == PSA_ALG_CCM ) |
| 6157 | { |
| 6158 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6159 | input_data->len, output_data, |
| 6160 | output_size, &output_length ), |
| 6161 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6162 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6163 | } |
| 6164 | else |
| 6165 | { |
| 6166 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6167 | input_data->len, output_data, |
| 6168 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6169 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6170 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6171 | input_data->len ), |
| 6172 | PSA_ERROR_BAD_STATE ); |
| 6173 | } |
| 6174 | psa_aead_abort( &operation ); |
| 6175 | |
| 6176 | /* ------------------------------------------------------- */ |
| 6177 | |
| 6178 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6179 | |
| 6180 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6181 | |
| 6182 | if( operation.alg == PSA_ALG_CCM ) |
| 6183 | { |
| 6184 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6185 | finish_output_size, |
| 6186 | &output_part_length, |
| 6187 | tag_buffer, tag_length, |
| 6188 | &tag_size ) ); |
| 6189 | } |
| 6190 | else |
| 6191 | { |
| 6192 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6193 | finish_output_size, |
| 6194 | &output_part_length, |
| 6195 | tag_buffer, tag_length, |
| 6196 | &tag_size ) ); |
| 6197 | |
| 6198 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6199 | input_data->len ), |
| 6200 | PSA_ERROR_BAD_STATE ); |
| 6201 | } |
| 6202 | psa_aead_abort( &operation ); |
| 6203 | |
| 6204 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6205 | |
| 6206 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6207 | |
| 6208 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6209 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6210 | &nonce_length ) ); |
| 6211 | if( operation.alg == PSA_ALG_CCM ) |
| 6212 | { |
| 6213 | |
| 6214 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6215 | additional_data->len ), |
| 6216 | PSA_ERROR_BAD_STATE ); |
| 6217 | } |
| 6218 | else |
| 6219 | { |
| 6220 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6221 | additional_data->len ) ); |
| 6222 | |
| 6223 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6224 | input_data->len ), |
| 6225 | PSA_ERROR_BAD_STATE ); |
| 6226 | } |
| 6227 | psa_aead_abort( &operation ); |
| 6228 | |
| 6229 | /* ------------------------------------------------------- */ |
| 6230 | |
| 6231 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6232 | |
| 6233 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6234 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6235 | &nonce_length ) ); |
| 6236 | if( operation.alg == PSA_ALG_CCM ) |
| 6237 | { |
| 6238 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6239 | input_data->len, output_data, |
| 6240 | output_size, &output_length ), |
| 6241 | PSA_ERROR_BAD_STATE ); |
| 6242 | |
| 6243 | } |
| 6244 | else |
| 6245 | { |
| 6246 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6247 | input_data->len, output_data, |
| 6248 | output_size, &output_length ) ); |
| 6249 | |
| 6250 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6251 | input_data->len ), |
| 6252 | PSA_ERROR_BAD_STATE ); |
| 6253 | } |
| 6254 | psa_aead_abort( &operation ); |
| 6255 | |
| 6256 | /* ------------------------------------------------------- */ |
| 6257 | |
| 6258 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6259 | |
| 6260 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6261 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6262 | &nonce_length ) ); |
| 6263 | if( operation.alg == PSA_ALG_CCM ) |
| 6264 | { |
| 6265 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6266 | finish_output_size, |
| 6267 | &output_part_length, |
| 6268 | tag_buffer, tag_length, |
| 6269 | &tag_size ) ); |
| 6270 | } |
| 6271 | else |
| 6272 | { |
| 6273 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6274 | finish_output_size, |
| 6275 | &output_part_length, |
| 6276 | tag_buffer, tag_length, |
| 6277 | &tag_size ) ); |
| 6278 | |
| 6279 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6280 | input_data->len ), |
| 6281 | PSA_ERROR_BAD_STATE ); |
| 6282 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6283 | psa_aead_abort( &operation ); |
| 6284 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6285 | /* Test for not sending any additional data or data after setting non zero |
| 6286 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6287 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6288 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6289 | |
| 6290 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6291 | |
| 6292 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6293 | input_data->len ) ); |
| 6294 | |
| 6295 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6296 | finish_output_size, |
| 6297 | &output_part_length, |
| 6298 | tag_buffer, tag_length, |
| 6299 | &tag_size ), |
| 6300 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6301 | |
| 6302 | psa_aead_abort( &operation ); |
| 6303 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6304 | /* Test for not sending any additional data or data after setting non-zero |
| 6305 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6306 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6307 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6308 | |
| 6309 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6310 | |
| 6311 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6312 | input_data->len ) ); |
| 6313 | |
| 6314 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6315 | finish_output_size, |
| 6316 | &output_part_length, |
| 6317 | tag_buffer, |
| 6318 | tag_length ), |
| 6319 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6320 | |
| 6321 | psa_aead_abort( &operation ); |
| 6322 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6323 | /* Test for not sending any additional data after setting a non-zero length |
| 6324 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6325 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6326 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6327 | |
| 6328 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6329 | |
| 6330 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6331 | input_data->len ) ); |
| 6332 | |
| 6333 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6334 | input_data->len, output_data, |
| 6335 | output_size, &output_length ), |
| 6336 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6337 | |
| 6338 | psa_aead_abort( &operation ); |
| 6339 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6340 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6341 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6342 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6343 | |
| 6344 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6345 | |
| 6346 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6347 | input_data->len ) ); |
| 6348 | |
| 6349 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6350 | additional_data->len ) ); |
| 6351 | |
| 6352 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6353 | finish_output_size, |
| 6354 | &output_part_length, |
| 6355 | tag_buffer, tag_length, |
| 6356 | &tag_size ), |
| 6357 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6358 | |
| 6359 | psa_aead_abort( &operation ); |
| 6360 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6361 | /* Test for sending too much additional data after setting lengths. */ |
| 6362 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6363 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6364 | |
| 6365 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6366 | |
| 6367 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6368 | |
| 6369 | |
| 6370 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6371 | additional_data->len ), |
| 6372 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6373 | |
| 6374 | psa_aead_abort( &operation ); |
| 6375 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6376 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6377 | |
| 6378 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6379 | |
| 6380 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6381 | |
| 6382 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6383 | input_data->len ) ); |
| 6384 | |
| 6385 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6386 | additional_data->len ) ); |
| 6387 | |
| 6388 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6389 | 1 ), |
| 6390 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6391 | |
| 6392 | psa_aead_abort( &operation ); |
| 6393 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6394 | /* Test for sending too much data after setting lengths. */ |
| 6395 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6396 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6397 | |
| 6398 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6399 | |
| 6400 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6401 | |
| 6402 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6403 | input_data->len, output_data, |
| 6404 | output_size, &output_length ), |
| 6405 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6406 | |
| 6407 | psa_aead_abort( &operation ); |
| 6408 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6409 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6410 | |
| 6411 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6412 | |
| 6413 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6414 | |
| 6415 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6416 | input_data->len ) ); |
| 6417 | |
| 6418 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6419 | additional_data->len ) ); |
| 6420 | |
| 6421 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6422 | input_data->len, output_data, |
| 6423 | output_size, &output_length ) ); |
| 6424 | |
| 6425 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6426 | 1, output_data, |
| 6427 | output_size, &output_length ), |
| 6428 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6429 | |
| 6430 | psa_aead_abort( &operation ); |
| 6431 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6432 | /* Test sending additional data after data. */ |
| 6433 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6434 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6435 | |
| 6436 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6437 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6438 | if( operation.alg != PSA_ALG_CCM ) |
| 6439 | { |
| 6440 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6441 | input_data->len, output_data, |
| 6442 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6443 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6444 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6445 | additional_data->len ), |
| 6446 | PSA_ERROR_BAD_STATE ); |
| 6447 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6448 | psa_aead_abort( &operation ); |
| 6449 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6450 | /* Test calling finish on decryption. */ |
| 6451 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6452 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6453 | |
| 6454 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6455 | |
| 6456 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6457 | finish_output_size, |
| 6458 | &output_part_length, |
| 6459 | tag_buffer, tag_length, |
| 6460 | &tag_size ), |
| 6461 | PSA_ERROR_BAD_STATE ); |
| 6462 | |
| 6463 | psa_aead_abort( &operation ); |
| 6464 | |
| 6465 | /* Test calling verify on encryption. */ |
| 6466 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6467 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6468 | |
| 6469 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6470 | |
| 6471 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6472 | finish_output_size, |
| 6473 | &output_part_length, |
| 6474 | tag_buffer, |
| 6475 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6476 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6477 | |
| 6478 | psa_aead_abort( &operation ); |
| 6479 | |
| 6480 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6481 | exit: |
| 6482 | psa_destroy_key( key ); |
| 6483 | psa_aead_abort( &operation ); |
| 6484 | mbedtls_free( output_data ); |
| 6485 | mbedtls_free( final_data ); |
| 6486 | PSA_DONE( ); |
| 6487 | } |
| 6488 | /* END_CASE */ |
| 6489 | |
| 6490 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6491 | void signature_size( int type_arg, |
| 6492 | int bits, |
| 6493 | int alg_arg, |
| 6494 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6495 | { |
| 6496 | psa_key_type_t type = type_arg; |
| 6497 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6498 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6499 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6500 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6501 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6502 | exit: |
| 6503 | ; |
| 6504 | } |
| 6505 | /* END_CASE */ |
| 6506 | |
| 6507 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6508 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6509 | int alg_arg, data_t *input_data, |
| 6510 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6511 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6512 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6513 | psa_key_type_t key_type = key_type_arg; |
| 6514 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6515 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6516 | unsigned char *signature = NULL; |
| 6517 | size_t signature_size; |
| 6518 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6519 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6520 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6521 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6522 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6523 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6524 | psa_set_key_algorithm( &attributes, alg ); |
| 6525 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6526 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6527 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6528 | &key ) ); |
| 6529 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6530 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6531 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6532 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6533 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6534 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6535 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6536 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6537 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6538 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6539 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6540 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6541 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6542 | input_data->x, input_data->len, |
| 6543 | signature, signature_size, |
| 6544 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6545 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6546 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6547 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6548 | |
| 6549 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6550 | /* |
| 6551 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6552 | * thus reset them as required. |
| 6553 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6554 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6555 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6556 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6557 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6558 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6559 | } |
| 6560 | /* END_CASE */ |
| 6561 | |
| 6562 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6563 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6564 | int alg_arg, data_t *input_data, |
| 6565 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6566 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6567 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6568 | psa_key_type_t key_type = key_type_arg; |
| 6569 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6570 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6571 | psa_status_t actual_status; |
| 6572 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6573 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6574 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6575 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6576 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6577 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6578 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6579 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6580 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6581 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6582 | psa_set_key_algorithm( &attributes, alg ); |
| 6583 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6584 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6585 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6586 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6587 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6588 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6589 | input_data->x, input_data->len, |
| 6590 | signature, signature_size, |
| 6591 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6592 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6593 | /* The value of *signature_length is unspecified on error, but |
| 6594 | * whatever it is, it should be less than signature_size, so that |
| 6595 | * if the caller tries to read *signature_length bytes without |
| 6596 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6597 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6598 | |
| 6599 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6600 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6601 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6602 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6603 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6604 | } |
| 6605 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6606 | |
| 6607 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6608 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6609 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6610 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6611 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6612 | psa_key_type_t key_type = key_type_arg; |
| 6613 | psa_algorithm_t alg = alg_arg; |
| 6614 | size_t key_bits; |
| 6615 | unsigned char *signature = NULL; |
| 6616 | size_t signature_size; |
| 6617 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6618 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6619 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6620 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6621 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6622 | 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] | 6623 | psa_set_key_algorithm( &attributes, alg ); |
| 6624 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6625 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6626 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6627 | &key ) ); |
| 6628 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6629 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6630 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6631 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6632 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6633 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6634 | key_bits, alg ); |
| 6635 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6636 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6637 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6638 | |
| 6639 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6640 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6641 | input_data->x, input_data->len, |
| 6642 | signature, signature_size, |
| 6643 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6644 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6645 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6646 | TEST_ASSERT( signature_length > 0 ); |
| 6647 | |
| 6648 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6649 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6650 | input_data->x, input_data->len, |
| 6651 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6652 | |
| 6653 | if( input_data->len != 0 ) |
| 6654 | { |
| 6655 | /* Flip a bit in the input and verify that the signature is now |
| 6656 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6657 | * because ECDSA may ignore the last few bits of the input. */ |
| 6658 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6659 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6660 | input_data->x, input_data->len, |
| 6661 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6662 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6663 | } |
| 6664 | |
| 6665 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6666 | /* |
| 6667 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6668 | * thus reset them as required. |
| 6669 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6670 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6671 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6672 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6673 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6674 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6675 | } |
| 6676 | /* END_CASE */ |
| 6677 | |
| 6678 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6679 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6680 | int alg_arg, data_t *hash_data, |
| 6681 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6682 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6683 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6684 | psa_key_type_t key_type = key_type_arg; |
| 6685 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6686 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6687 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6688 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6689 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6690 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6691 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6692 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6693 | psa_set_key_algorithm( &attributes, alg ); |
| 6694 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6695 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6696 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6697 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6698 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6699 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6700 | hash_data->x, hash_data->len, |
| 6701 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6702 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6703 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6704 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6705 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6706 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6707 | } |
| 6708 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6709 | |
| 6710 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6711 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6712 | int alg_arg, data_t *hash_data, |
| 6713 | data_t *signature_data, |
| 6714 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6715 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6716 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6717 | psa_key_type_t key_type = key_type_arg; |
| 6718 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6719 | psa_status_t actual_status; |
| 6720 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6721 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6722 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6723 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6724 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6725 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6726 | psa_set_key_algorithm( &attributes, alg ); |
| 6727 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6728 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6729 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6730 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6731 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6732 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6733 | hash_data->x, hash_data->len, |
| 6734 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6735 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6736 | |
| 6737 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6738 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6739 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6740 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6741 | } |
| 6742 | /* END_CASE */ |
| 6743 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6744 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6745 | void sign_message_deterministic( int key_type_arg, |
| 6746 | data_t *key_data, |
| 6747 | int alg_arg, |
| 6748 | data_t *input_data, |
| 6749 | data_t *output_data ) |
| 6750 | { |
| 6751 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6752 | psa_key_type_t key_type = key_type_arg; |
| 6753 | psa_algorithm_t alg = alg_arg; |
| 6754 | size_t key_bits; |
| 6755 | unsigned char *signature = NULL; |
| 6756 | size_t signature_size; |
| 6757 | size_t signature_length = 0xdeadbeef; |
| 6758 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6759 | |
| 6760 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6761 | |
| 6762 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6763 | psa_set_key_algorithm( &attributes, alg ); |
| 6764 | psa_set_key_type( &attributes, key_type ); |
| 6765 | |
| 6766 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6767 | &key ) ); |
| 6768 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6769 | key_bits = psa_get_key_bits( &attributes ); |
| 6770 | |
| 6771 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6772 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6773 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6774 | ASSERT_ALLOC( signature, signature_size ); |
| 6775 | |
| 6776 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6777 | input_data->x, input_data->len, |
| 6778 | signature, signature_size, |
| 6779 | &signature_length ) ); |
| 6780 | |
| 6781 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6782 | signature, signature_length ); |
| 6783 | |
| 6784 | exit: |
| 6785 | psa_reset_key_attributes( &attributes ); |
| 6786 | |
| 6787 | psa_destroy_key( key ); |
| 6788 | mbedtls_free( signature ); |
| 6789 | PSA_DONE( ); |
| 6790 | |
| 6791 | } |
| 6792 | /* END_CASE */ |
| 6793 | |
| 6794 | /* BEGIN_CASE */ |
| 6795 | void sign_message_fail( int key_type_arg, |
| 6796 | data_t *key_data, |
| 6797 | int alg_arg, |
| 6798 | data_t *input_data, |
| 6799 | int signature_size_arg, |
| 6800 | int expected_status_arg ) |
| 6801 | { |
| 6802 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6803 | psa_key_type_t key_type = key_type_arg; |
| 6804 | psa_algorithm_t alg = alg_arg; |
| 6805 | size_t signature_size = signature_size_arg; |
| 6806 | psa_status_t actual_status; |
| 6807 | psa_status_t expected_status = expected_status_arg; |
| 6808 | unsigned char *signature = NULL; |
| 6809 | size_t signature_length = 0xdeadbeef; |
| 6810 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6811 | |
| 6812 | ASSERT_ALLOC( signature, signature_size ); |
| 6813 | |
| 6814 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6815 | |
| 6816 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6817 | psa_set_key_algorithm( &attributes, alg ); |
| 6818 | psa_set_key_type( &attributes, key_type ); |
| 6819 | |
| 6820 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6821 | &key ) ); |
| 6822 | |
| 6823 | actual_status = psa_sign_message( key, alg, |
| 6824 | input_data->x, input_data->len, |
| 6825 | signature, signature_size, |
| 6826 | &signature_length ); |
| 6827 | TEST_EQUAL( actual_status, expected_status ); |
| 6828 | /* The value of *signature_length is unspecified on error, but |
| 6829 | * whatever it is, it should be less than signature_size, so that |
| 6830 | * if the caller tries to read *signature_length bytes without |
| 6831 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6832 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6833 | |
| 6834 | exit: |
| 6835 | psa_reset_key_attributes( &attributes ); |
| 6836 | psa_destroy_key( key ); |
| 6837 | mbedtls_free( signature ); |
| 6838 | PSA_DONE( ); |
| 6839 | } |
| 6840 | /* END_CASE */ |
| 6841 | |
| 6842 | /* BEGIN_CASE */ |
| 6843 | void sign_verify_message( int key_type_arg, |
| 6844 | data_t *key_data, |
| 6845 | int alg_arg, |
| 6846 | data_t *input_data ) |
| 6847 | { |
| 6848 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6849 | psa_key_type_t key_type = key_type_arg; |
| 6850 | psa_algorithm_t alg = alg_arg; |
| 6851 | size_t key_bits; |
| 6852 | unsigned char *signature = NULL; |
| 6853 | size_t signature_size; |
| 6854 | size_t signature_length = 0xdeadbeef; |
| 6855 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6856 | |
| 6857 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6858 | |
| 6859 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6860 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6861 | psa_set_key_algorithm( &attributes, alg ); |
| 6862 | psa_set_key_type( &attributes, key_type ); |
| 6863 | |
| 6864 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6865 | &key ) ); |
| 6866 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6867 | key_bits = psa_get_key_bits( &attributes ); |
| 6868 | |
| 6869 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6870 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6871 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6872 | ASSERT_ALLOC( signature, signature_size ); |
| 6873 | |
| 6874 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6875 | input_data->x, input_data->len, |
| 6876 | signature, signature_size, |
| 6877 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6878 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6879 | TEST_ASSERT( signature_length > 0 ); |
| 6880 | |
| 6881 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6882 | input_data->x, input_data->len, |
| 6883 | signature, signature_length ) ); |
| 6884 | |
| 6885 | if( input_data->len != 0 ) |
| 6886 | { |
| 6887 | /* Flip a bit in the input and verify that the signature is now |
| 6888 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6889 | * because ECDSA may ignore the last few bits of the input. */ |
| 6890 | input_data->x[0] ^= 1; |
| 6891 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6892 | input_data->x, input_data->len, |
| 6893 | signature, signature_length ), |
| 6894 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6895 | } |
| 6896 | |
| 6897 | exit: |
| 6898 | psa_reset_key_attributes( &attributes ); |
| 6899 | |
| 6900 | psa_destroy_key( key ); |
| 6901 | mbedtls_free( signature ); |
| 6902 | PSA_DONE( ); |
| 6903 | } |
| 6904 | /* END_CASE */ |
| 6905 | |
| 6906 | /* BEGIN_CASE */ |
| 6907 | void verify_message( int key_type_arg, |
| 6908 | data_t *key_data, |
| 6909 | int alg_arg, |
| 6910 | data_t *input_data, |
| 6911 | data_t *signature_data ) |
| 6912 | { |
| 6913 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6914 | psa_key_type_t key_type = key_type_arg; |
| 6915 | psa_algorithm_t alg = alg_arg; |
| 6916 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6917 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6918 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6919 | |
| 6920 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6921 | |
| 6922 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6923 | psa_set_key_algorithm( &attributes, alg ); |
| 6924 | psa_set_key_type( &attributes, key_type ); |
| 6925 | |
| 6926 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6927 | &key ) ); |
| 6928 | |
| 6929 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6930 | input_data->x, input_data->len, |
| 6931 | signature_data->x, signature_data->len ) ); |
| 6932 | |
| 6933 | exit: |
| 6934 | psa_reset_key_attributes( &attributes ); |
| 6935 | psa_destroy_key( key ); |
| 6936 | PSA_DONE( ); |
| 6937 | } |
| 6938 | /* END_CASE */ |
| 6939 | |
| 6940 | /* BEGIN_CASE */ |
| 6941 | void verify_message_fail( int key_type_arg, |
| 6942 | data_t *key_data, |
| 6943 | int alg_arg, |
| 6944 | data_t *hash_data, |
| 6945 | data_t *signature_data, |
| 6946 | int expected_status_arg ) |
| 6947 | { |
| 6948 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6949 | psa_key_type_t key_type = key_type_arg; |
| 6950 | psa_algorithm_t alg = alg_arg; |
| 6951 | psa_status_t actual_status; |
| 6952 | psa_status_t expected_status = expected_status_arg; |
| 6953 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6954 | |
| 6955 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6956 | |
| 6957 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6958 | psa_set_key_algorithm( &attributes, alg ); |
| 6959 | psa_set_key_type( &attributes, key_type ); |
| 6960 | |
| 6961 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6962 | &key ) ); |
| 6963 | |
| 6964 | actual_status = psa_verify_message( key, alg, |
| 6965 | hash_data->x, hash_data->len, |
| 6966 | signature_data->x, |
| 6967 | signature_data->len ); |
| 6968 | TEST_EQUAL( actual_status, expected_status ); |
| 6969 | |
| 6970 | exit: |
| 6971 | psa_reset_key_attributes( &attributes ); |
| 6972 | psa_destroy_key( key ); |
| 6973 | PSA_DONE( ); |
| 6974 | } |
| 6975 | /* END_CASE */ |
| 6976 | |
| 6977 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6978 | void asymmetric_encrypt( int key_type_arg, |
| 6979 | data_t *key_data, |
| 6980 | int alg_arg, |
| 6981 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6982 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6983 | int expected_output_length_arg, |
| 6984 | int expected_status_arg ) |
| 6985 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6986 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6987 | psa_key_type_t key_type = key_type_arg; |
| 6988 | psa_algorithm_t alg = alg_arg; |
| 6989 | size_t expected_output_length = expected_output_length_arg; |
| 6990 | size_t key_bits; |
| 6991 | unsigned char *output = NULL; |
| 6992 | size_t output_size; |
| 6993 | size_t output_length = ~0; |
| 6994 | psa_status_t actual_status; |
| 6995 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6996 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6997 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6998 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 6999 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7000 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7001 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7002 | psa_set_key_algorithm( &attributes, alg ); |
| 7003 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7004 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7005 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7006 | |
| 7007 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7008 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7009 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7010 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7011 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7012 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7013 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7014 | |
| 7015 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7016 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7017 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7018 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7019 | output, output_size, |
| 7020 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7021 | TEST_EQUAL( actual_status, expected_status ); |
| 7022 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7023 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7024 | /* If the label is empty, the test framework puts a non-null pointer |
| 7025 | * in label->x. Test that a null pointer works as well. */ |
| 7026 | if( label->len == 0 ) |
| 7027 | { |
| 7028 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7029 | if( output_size != 0 ) |
| 7030 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7031 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7032 | input_data->x, input_data->len, |
| 7033 | NULL, label->len, |
| 7034 | output, output_size, |
| 7035 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7036 | TEST_EQUAL( actual_status, expected_status ); |
| 7037 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7038 | } |
| 7039 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7040 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7041 | /* |
| 7042 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7043 | * thus reset them as required. |
| 7044 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7045 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7046 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7047 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7048 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7049 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7050 | } |
| 7051 | /* END_CASE */ |
| 7052 | |
| 7053 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7054 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 7055 | data_t *key_data, |
| 7056 | int alg_arg, |
| 7057 | data_t *input_data, |
| 7058 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7059 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7060 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7061 | psa_key_type_t key_type = key_type_arg; |
| 7062 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7063 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7064 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7065 | size_t output_size; |
| 7066 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7067 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7068 | size_t output2_size; |
| 7069 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7070 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7071 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7072 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7073 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7074 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 7075 | psa_set_key_algorithm( &attributes, alg ); |
| 7076 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7077 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7078 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7079 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7080 | |
| 7081 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7082 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7083 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7084 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7085 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7086 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7087 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7088 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7089 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7090 | TEST_LE_U( output2_size, |
| 7091 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 7092 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7093 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7094 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7095 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7096 | * the original plaintext because of the non-optional random |
| 7097 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7098 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7099 | input_data->x, input_data->len, |
| 7100 | label->x, label->len, |
| 7101 | output, output_size, |
| 7102 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7103 | /* We don't know what ciphertext length to expect, but check that |
| 7104 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7105 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7106 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7107 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7108 | output, output_length, |
| 7109 | label->x, label->len, |
| 7110 | output2, output2_size, |
| 7111 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7112 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 7113 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7114 | |
| 7115 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7116 | /* |
| 7117 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7118 | * thus reset them as required. |
| 7119 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7120 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7121 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7122 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7123 | mbedtls_free( output ); |
| 7124 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7125 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7126 | } |
| 7127 | /* END_CASE */ |
| 7128 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7129 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7130 | void asymmetric_decrypt( int key_type_arg, |
| 7131 | data_t *key_data, |
| 7132 | int alg_arg, |
| 7133 | data_t *input_data, |
| 7134 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 7135 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7136 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7137 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7138 | psa_key_type_t key_type = key_type_arg; |
| 7139 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7140 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7141 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7142 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7143 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7144 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7145 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7146 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7147 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7148 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7149 | psa_set_key_algorithm( &attributes, alg ); |
| 7150 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7151 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7152 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7153 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7154 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7155 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 7156 | key_bits = psa_get_key_bits( &attributes ); |
| 7157 | |
| 7158 | /* Determine the maximum ciphertext length */ |
| 7159 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7160 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7161 | ASSERT_ALLOC( output, output_size ); |
| 7162 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7163 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7164 | input_data->x, input_data->len, |
| 7165 | label->x, label->len, |
| 7166 | output, |
| 7167 | output_size, |
| 7168 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7169 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7170 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7171 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7172 | /* If the label is empty, the test framework puts a non-null pointer |
| 7173 | * in label->x. Test that a null pointer works as well. */ |
| 7174 | if( label->len == 0 ) |
| 7175 | { |
| 7176 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7177 | if( output_size != 0 ) |
| 7178 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7179 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7180 | input_data->x, input_data->len, |
| 7181 | NULL, label->len, |
| 7182 | output, |
| 7183 | output_size, |
| 7184 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7185 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7186 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7187 | } |
| 7188 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7189 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7190 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7191 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7192 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7193 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7194 | } |
| 7195 | /* END_CASE */ |
| 7196 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7197 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7198 | void asymmetric_decrypt_fail( int key_type_arg, |
| 7199 | data_t *key_data, |
| 7200 | int alg_arg, |
| 7201 | data_t *input_data, |
| 7202 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7203 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7204 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7205 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7206 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7207 | psa_key_type_t key_type = key_type_arg; |
| 7208 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7209 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7210 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7211 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7212 | psa_status_t actual_status; |
| 7213 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7214 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7215 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7216 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7217 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7218 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7219 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7220 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7221 | psa_set_key_algorithm( &attributes, alg ); |
| 7222 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7223 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7224 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7225 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7226 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7227 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7228 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7229 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7230 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7231 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7232 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7233 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7234 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7235 | /* If the label is empty, the test framework puts a non-null pointer |
| 7236 | * in label->x. Test that a null pointer works as well. */ |
| 7237 | if( label->len == 0 ) |
| 7238 | { |
| 7239 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7240 | if( output_size != 0 ) |
| 7241 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7242 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7243 | input_data->x, input_data->len, |
| 7244 | NULL, label->len, |
| 7245 | output, output_size, |
| 7246 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7247 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7248 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7249 | } |
| 7250 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7251 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7252 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7253 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7254 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7255 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7256 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7257 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7258 | |
| 7259 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 7260 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7261 | { |
| 7262 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7263 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7264 | * 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] | 7265 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7266 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7267 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 7268 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7269 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7270 | |
| 7271 | memset( &zero, 0, sizeof( zero ) ); |
| 7272 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7273 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7274 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7275 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7276 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7277 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7278 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7279 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7280 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7281 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7282 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7283 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7284 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7285 | } |
| 7286 | /* END_CASE */ |
| 7287 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7288 | /* BEGIN_CASE */ |
| 7289 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7290 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7291 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7292 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7293 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7294 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7295 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7296 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7297 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7298 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7299 | |
| 7300 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7301 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7302 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7303 | } |
| 7304 | /* END_CASE */ |
| 7305 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7306 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7307 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7308 | int expected_status_arg ) |
| 7309 | { |
| 7310 | psa_algorithm_t alg = alg_arg; |
| 7311 | size_t capacity = capacity_arg; |
| 7312 | psa_status_t expected_status = expected_status_arg; |
| 7313 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7314 | |
| 7315 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7316 | |
| 7317 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7318 | |
| 7319 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7320 | expected_status ); |
| 7321 | |
| 7322 | exit: |
| 7323 | psa_key_derivation_abort( &operation ); |
| 7324 | PSA_DONE( ); |
| 7325 | } |
| 7326 | /* END_CASE */ |
| 7327 | |
| 7328 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7329 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7330 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7331 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7332 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7333 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7334 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7335 | int expected_status_arg3, |
| 7336 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7337 | { |
| 7338 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7339 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7340 | 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] | 7341 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7342 | expected_status_arg2, |
| 7343 | expected_status_arg3}; |
| 7344 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7345 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7346 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7347 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7348 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7349 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7350 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7351 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7352 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7353 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7354 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7355 | |
| 7356 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7357 | |
| 7358 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7359 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7360 | |
| 7361 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7362 | |
| 7363 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7364 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7365 | mbedtls_test_set_step( i ); |
| 7366 | if( steps[i] == 0 ) |
| 7367 | { |
| 7368 | /* Skip this step */ |
| 7369 | } |
| 7370 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7371 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7372 | psa_set_key_type( &attributes, key_types[i] ); |
| 7373 | PSA_ASSERT( psa_import_key( &attributes, |
| 7374 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7375 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7376 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7377 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7378 | { |
| 7379 | // When taking a private key as secret input, use key agreement |
| 7380 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7381 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7382 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7383 | expected_statuses[i] ); |
| 7384 | } |
| 7385 | else |
| 7386 | { |
| 7387 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7388 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7389 | expected_statuses[i] ); |
| 7390 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7391 | } |
| 7392 | else |
| 7393 | { |
| 7394 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7395 | &operation, steps[i], |
| 7396 | inputs[i]->x, inputs[i]->len ), |
| 7397 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7398 | } |
| 7399 | } |
| 7400 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7401 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7402 | { |
| 7403 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7404 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7405 | psa_set_key_bits( &attributes, 8 ); |
| 7406 | actual_output_status = |
| 7407 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7408 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7409 | } |
| 7410 | else |
| 7411 | { |
| 7412 | uint8_t buffer[1]; |
| 7413 | actual_output_status = |
| 7414 | psa_key_derivation_output_bytes( &operation, |
| 7415 | buffer, sizeof( buffer ) ); |
| 7416 | } |
| 7417 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7418 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7419 | exit: |
| 7420 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7421 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7422 | psa_destroy_key( keys[i] ); |
| 7423 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7424 | PSA_DONE( ); |
| 7425 | } |
| 7426 | /* END_CASE */ |
| 7427 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7428 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7429 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7430 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7431 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7432 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7433 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7434 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7435 | unsigned char input1[] = "Input 1"; |
| 7436 | size_t input1_length = sizeof( input1 ); |
| 7437 | unsigned char input2[] = "Input 2"; |
| 7438 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7439 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7440 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7441 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7442 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7443 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7444 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7445 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7446 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7447 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7448 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7449 | psa_set_key_algorithm( &attributes, alg ); |
| 7450 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7451 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7452 | PSA_ASSERT( psa_import_key( &attributes, |
| 7453 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7454 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7455 | |
| 7456 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7457 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7458 | input1, input1_length, |
| 7459 | input2, input2_length, |
| 7460 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7461 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7462 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7463 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7464 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7465 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7466 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7467 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7468 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7469 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7470 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7471 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7472 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7473 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7474 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7475 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7476 | } |
| 7477 | /* END_CASE */ |
| 7478 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7479 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7480 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7481 | { |
| 7482 | uint8_t output_buffer[16]; |
| 7483 | size_t buffer_size = 16; |
| 7484 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7485 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7486 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7487 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7488 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7489 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7490 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7491 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7492 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7493 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7494 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7495 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7496 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7497 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7498 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7499 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7500 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7501 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7502 | |
| 7503 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7504 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7505 | } |
| 7506 | /* END_CASE */ |
| 7507 | |
| 7508 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7509 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7510 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7511 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7512 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7513 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7514 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7515 | int requested_capacity_arg, |
| 7516 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7517 | data_t *expected_output2, |
| 7518 | int other_key_input_type, |
| 7519 | int key_input_type, |
| 7520 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7521 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7522 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7523 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7524 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7525 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7526 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7527 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7528 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7529 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7530 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7531 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7532 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7533 | uint8_t *expected_outputs[2] = |
| 7534 | {expected_output1->x, expected_output2->x}; |
| 7535 | size_t output_sizes[2] = |
| 7536 | {expected_output1->len, expected_output2->len}; |
| 7537 | size_t output_buffer_size = 0; |
| 7538 | uint8_t *output_buffer = NULL; |
| 7539 | size_t expected_capacity; |
| 7540 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7541 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7542 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7543 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7544 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7545 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7546 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7547 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7548 | |
| 7549 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7550 | { |
| 7551 | if( output_sizes[i] > output_buffer_size ) |
| 7552 | output_buffer_size = output_sizes[i]; |
| 7553 | if( output_sizes[i] == 0 ) |
| 7554 | expected_outputs[i] = NULL; |
| 7555 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7556 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7557 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7558 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7559 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7560 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7561 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7562 | requested_capacity ) ); |
| 7563 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7564 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7565 | switch( steps[i] ) |
| 7566 | { |
| 7567 | case 0: |
| 7568 | break; |
| 7569 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7570 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7571 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7572 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7573 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7574 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7575 | inputs[i]->x, inputs[i]->len ), |
| 7576 | statuses[i] ); |
| 7577 | |
| 7578 | if( statuses[i] != PSA_SUCCESS ) |
| 7579 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7580 | break; |
| 7581 | case 1: // input key |
| 7582 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7583 | psa_set_key_algorithm( &attributes1, alg ); |
| 7584 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7585 | |
| 7586 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7587 | inputs[i]->x, inputs[i]->len, |
| 7588 | &keys[i] ) ); |
| 7589 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7590 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7591 | { |
| 7592 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7593 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7594 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7595 | } |
| 7596 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7597 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7598 | steps[i], |
| 7599 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7600 | break; |
| 7601 | default: |
| 7602 | TEST_ASSERT( ! "default case not supported" ); |
| 7603 | break; |
| 7604 | } |
| 7605 | break; |
| 7606 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7607 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7608 | { |
| 7609 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7610 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7611 | steps[i], |
| 7612 | inputs[i]->x, |
| 7613 | inputs[i]->len ), |
| 7614 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7615 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7616 | case 1: // input key, type DERIVE |
| 7617 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7618 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7619 | psa_set_key_algorithm( &attributes2, alg ); |
| 7620 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7621 | |
| 7622 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7623 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7624 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7625 | |
| 7626 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7627 | inputs[i]->x, inputs[i]->len, |
| 7628 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7629 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7630 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7631 | steps[i], |
| 7632 | keys[i] ), |
| 7633 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7634 | break; |
| 7635 | case 2: // key agreement |
| 7636 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7637 | psa_set_key_algorithm( &attributes3, alg ); |
| 7638 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7639 | |
| 7640 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7641 | inputs[i]->x, inputs[i]->len, |
| 7642 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7643 | |
| 7644 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7645 | &operation, |
| 7646 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7647 | keys[i], key_agreement_peer_key->x, |
| 7648 | key_agreement_peer_key->len ), statuses[i] ); |
| 7649 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7650 | default: |
| 7651 | TEST_ASSERT( ! "default case not supported" ); |
| 7652 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7653 | } |
| 7654 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7655 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7656 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7657 | break; |
| 7658 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7659 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7660 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7661 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7662 | |
| 7663 | if( statuses[i] != PSA_SUCCESS ) |
| 7664 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7665 | break; |
| 7666 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7667 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7668 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7669 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7670 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7671 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7672 | expected_capacity = requested_capacity; |
| 7673 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7674 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7675 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7676 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7677 | |
| 7678 | /* For output key derivation secret must be provided using |
| 7679 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7680 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7681 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7682 | |
| 7683 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7684 | psa_set_key_algorithm( &attributes4, alg ); |
| 7685 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7686 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7687 | |
| 7688 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7689 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7690 | } |
| 7691 | else // output bytes |
| 7692 | { |
| 7693 | /* Expansion phase. */ |
| 7694 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7695 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7696 | /* Read some bytes. */ |
| 7697 | status = psa_key_derivation_output_bytes( &operation, |
| 7698 | output_buffer, output_sizes[i] ); |
| 7699 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7700 | { |
| 7701 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7702 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7703 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7704 | continue; |
| 7705 | } |
| 7706 | else if( expected_capacity == 0 || |
| 7707 | output_sizes[i] > expected_capacity ) |
| 7708 | { |
| 7709 | /* Capacity exceeded. */ |
| 7710 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7711 | expected_capacity = 0; |
| 7712 | continue; |
| 7713 | } |
| 7714 | /* Success. Check the read data. */ |
| 7715 | PSA_ASSERT( status ); |
| 7716 | if( output_sizes[i] != 0 ) |
| 7717 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7718 | expected_outputs[i], output_sizes[i] ); |
| 7719 | /* Check the operation status. */ |
| 7720 | expected_capacity -= output_sizes[i]; |
| 7721 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7722 | ¤t_capacity ) ); |
| 7723 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7724 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7725 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7726 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7727 | |
| 7728 | exit: |
| 7729 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7730 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7731 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7732 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7733 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7734 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7735 | } |
| 7736 | /* END_CASE */ |
| 7737 | |
| 7738 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7739 | void derive_full( int alg_arg, |
| 7740 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7741 | data_t *input1, |
| 7742 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7743 | int requested_capacity_arg ) |
| 7744 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7745 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7746 | psa_algorithm_t alg = alg_arg; |
| 7747 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7748 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7749 | unsigned char output_buffer[16]; |
| 7750 | size_t expected_capacity = requested_capacity; |
| 7751 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7752 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7753 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7754 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7755 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7756 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7757 | psa_set_key_algorithm( &attributes, alg ); |
| 7758 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7759 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7760 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7761 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7762 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7763 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7764 | input1->x, input1->len, |
| 7765 | input2->x, input2->len, |
| 7766 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7767 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7768 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7769 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7770 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7771 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7772 | |
| 7773 | /* Expansion phase. */ |
| 7774 | while( current_capacity > 0 ) |
| 7775 | { |
| 7776 | size_t read_size = sizeof( output_buffer ); |
| 7777 | if( read_size > current_capacity ) |
| 7778 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7779 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7780 | output_buffer, |
| 7781 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7782 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7783 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7784 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7785 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7786 | } |
| 7787 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7788 | /* Check that the operation refuses to go over capacity. */ |
| 7789 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7790 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7791 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7792 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7793 | |
| 7794 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7795 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7796 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7797 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7798 | } |
| 7799 | /* END_CASE */ |
| 7800 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame^] | 7801 | /* 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] | 7802 | 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] | 7803 | int derivation_step, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7804 | int capacity, int expected_capacity_status_arg, |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7805 | data_t *expected_output, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7806 | int expected_output_status_arg ) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7807 | { |
| 7808 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7809 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 7810 | 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] | 7811 | uint8_t *output_buffer = NULL; |
| 7812 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7813 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 7814 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 7815 | 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] | 7816 | |
| 7817 | ASSERT_ALLOC( output_buffer, expected_output->len ); |
| 7818 | PSA_ASSERT( psa_crypto_init() ); |
| 7819 | |
| 7820 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7821 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7822 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7823 | |
| 7824 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7825 | step, input->x, input->len ), |
| 7826 | expected_input_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7827 | |
| 7828 | if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS ) |
| 7829 | goto exit; |
| 7830 | |
| 7831 | status = psa_key_derivation_output_bytes( &operation, output_buffer, |
| 7832 | expected_output->len ); |
| 7833 | |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7834 | TEST_EQUAL( status, expected_output_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7835 | if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS ) |
| 7836 | ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x, |
| 7837 | expected_output->len ); |
| 7838 | |
| 7839 | exit: |
| 7840 | mbedtls_free( output_buffer ); |
| 7841 | psa_key_derivation_abort( &operation ); |
| 7842 | PSA_DONE(); |
| 7843 | } |
| 7844 | /* END_CASE */ |
| 7845 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7846 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7847 | void derive_key_exercise( int alg_arg, |
| 7848 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7849 | data_t *input1, |
| 7850 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7851 | int derived_type_arg, |
| 7852 | int derived_bits_arg, |
| 7853 | int derived_usage_arg, |
| 7854 | int derived_alg_arg ) |
| 7855 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7856 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7857 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7858 | psa_algorithm_t alg = alg_arg; |
| 7859 | psa_key_type_t derived_type = derived_type_arg; |
| 7860 | size_t derived_bits = derived_bits_arg; |
| 7861 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7862 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7863 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7864 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7865 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7866 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7867 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7868 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7869 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7870 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7871 | psa_set_key_algorithm( &attributes, alg ); |
| 7872 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7873 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7874 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7875 | |
| 7876 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7877 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7878 | input1->x, input1->len, |
| 7879 | input2->x, input2->len, |
| 7880 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7881 | goto exit; |
| 7882 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7883 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7884 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7885 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7886 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7887 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7888 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7889 | |
| 7890 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7891 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7892 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7893 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7894 | |
| 7895 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7896 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7897 | goto exit; |
| 7898 | |
| 7899 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7900 | /* |
| 7901 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7902 | * thus reset them as required. |
| 7903 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7904 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7905 | |
| 7906 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7907 | psa_destroy_key( base_key ); |
| 7908 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7909 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7910 | } |
| 7911 | /* END_CASE */ |
| 7912 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7913 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7914 | void derive_key_export( int alg_arg, |
| 7915 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7916 | data_t *input1, |
| 7917 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7918 | int bytes1_arg, |
| 7919 | int bytes2_arg ) |
| 7920 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7921 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7922 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7923 | psa_algorithm_t alg = alg_arg; |
| 7924 | size_t bytes1 = bytes1_arg; |
| 7925 | size_t bytes2 = bytes2_arg; |
| 7926 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7927 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7928 | uint8_t *output_buffer = NULL; |
| 7929 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7930 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7931 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7932 | size_t length; |
| 7933 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7934 | ASSERT_ALLOC( output_buffer, capacity ); |
| 7935 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7936 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7937 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7938 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7939 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7940 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7941 | 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] | 7942 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7943 | |
| 7944 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7945 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7946 | input1->x, input1->len, |
| 7947 | input2->x, input2->len, |
| 7948 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7949 | goto exit; |
| 7950 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7951 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7952 | output_buffer, |
| 7953 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7954 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7955 | |
| 7956 | /* 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] | 7957 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7958 | input1->x, input1->len, |
| 7959 | input2->x, input2->len, |
| 7960 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7961 | goto exit; |
| 7962 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7963 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7964 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7965 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7966 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7967 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7968 | &derived_key ) ); |
| 7969 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7970 | export_buffer, bytes1, |
| 7971 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7972 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7973 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7974 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7975 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7976 | &derived_key ) ); |
| 7977 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7978 | export_buffer + bytes1, bytes2, |
| 7979 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7980 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7981 | |
| 7982 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 7983 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 7984 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7985 | |
| 7986 | exit: |
| 7987 | mbedtls_free( output_buffer ); |
| 7988 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7989 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7990 | psa_destroy_key( base_key ); |
| 7991 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7992 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7993 | } |
| 7994 | /* END_CASE */ |
| 7995 | |
| 7996 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7997 | void derive_key_type( int alg_arg, |
| 7998 | data_t *key_data, |
| 7999 | data_t *input1, |
| 8000 | data_t *input2, |
| 8001 | int key_type_arg, int bits_arg, |
| 8002 | data_t *expected_export ) |
| 8003 | { |
| 8004 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8005 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8006 | const psa_algorithm_t alg = alg_arg; |
| 8007 | const psa_key_type_t key_type = key_type_arg; |
| 8008 | const size_t bits = bits_arg; |
| 8009 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8010 | const size_t export_buffer_size = |
| 8011 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 8012 | uint8_t *export_buffer = NULL; |
| 8013 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8014 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8015 | size_t export_length; |
| 8016 | |
| 8017 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 8018 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8019 | |
| 8020 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8021 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8022 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8023 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 8024 | &base_key ) ); |
| 8025 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8026 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8027 | &operation, base_key, alg, |
| 8028 | input1->x, input1->len, |
| 8029 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8030 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8031 | goto exit; |
| 8032 | |
| 8033 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8034 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8035 | psa_set_key_type( &derived_attributes, key_type ); |
| 8036 | psa_set_key_bits( &derived_attributes, bits ); |
| 8037 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 8038 | &derived_key ) ); |
| 8039 | |
| 8040 | PSA_ASSERT( psa_export_key( derived_key, |
| 8041 | export_buffer, export_buffer_size, |
| 8042 | &export_length ) ); |
| 8043 | ASSERT_COMPARE( export_buffer, export_length, |
| 8044 | expected_export->x, expected_export->len ); |
| 8045 | |
| 8046 | exit: |
| 8047 | mbedtls_free( export_buffer ); |
| 8048 | psa_key_derivation_abort( &operation ); |
| 8049 | psa_destroy_key( base_key ); |
| 8050 | psa_destroy_key( derived_key ); |
| 8051 | PSA_DONE( ); |
| 8052 | } |
| 8053 | /* END_CASE */ |
| 8054 | |
| 8055 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8056 | void derive_key( int alg_arg, |
| 8057 | data_t *key_data, data_t *input1, data_t *input2, |
| 8058 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8059 | int expected_status_arg, |
| 8060 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8061 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8062 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8063 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8064 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8065 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8066 | size_t bits = bits_arg; |
| 8067 | psa_status_t expected_status = expected_status_arg; |
| 8068 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8069 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8070 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8071 | |
| 8072 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8073 | |
| 8074 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8075 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8076 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8077 | 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] | 8078 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8079 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8080 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8081 | input1->x, input1->len, |
| 8082 | input2->x, input2->len, |
| 8083 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8084 | goto exit; |
| 8085 | |
| 8086 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8087 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8088 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8089 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8090 | |
| 8091 | psa_status_t status = |
| 8092 | psa_key_derivation_output_key( &derived_attributes, |
| 8093 | &operation, |
| 8094 | &derived_key ); |
| 8095 | if( is_large_output > 0 ) |
| 8096 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8097 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8098 | |
| 8099 | exit: |
| 8100 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8101 | psa_destroy_key( base_key ); |
| 8102 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8103 | PSA_DONE( ); |
| 8104 | } |
| 8105 | /* END_CASE */ |
| 8106 | |
| 8107 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8108 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 8109 | int our_key_type_arg, int our_key_alg_arg, |
| 8110 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8111 | int expected_status_arg ) |
| 8112 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8113 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8114 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8115 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8116 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8117 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8118 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8119 | psa_status_t expected_status = expected_status_arg; |
| 8120 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8121 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8122 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8123 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8124 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8125 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8126 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8127 | PSA_ASSERT( psa_import_key( &attributes, |
| 8128 | our_key_data->x, our_key_data->len, |
| 8129 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8130 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8131 | /* The tests currently include inputs that should fail at either step. |
| 8132 | * Test cases that fail at the setup step should be changed to call |
| 8133 | * key_derivation_setup instead, and this function should be renamed |
| 8134 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8135 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8136 | if( status == PSA_SUCCESS ) |
| 8137 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8138 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 8139 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8140 | our_key, |
| 8141 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8142 | expected_status ); |
| 8143 | } |
| 8144 | else |
| 8145 | { |
| 8146 | TEST_ASSERT( status == expected_status ); |
| 8147 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8148 | |
| 8149 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8150 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8151 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8152 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8153 | } |
| 8154 | /* END_CASE */ |
| 8155 | |
| 8156 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8157 | void raw_key_agreement( int alg_arg, |
| 8158 | int our_key_type_arg, data_t *our_key_data, |
| 8159 | data_t *peer_key_data, |
| 8160 | data_t *expected_output ) |
| 8161 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8162 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8163 | psa_algorithm_t alg = alg_arg; |
| 8164 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8165 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8166 | unsigned char *output = NULL; |
| 8167 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8168 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8169 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8170 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8171 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8172 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8173 | psa_set_key_algorithm( &attributes, alg ); |
| 8174 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8175 | PSA_ASSERT( psa_import_key( &attributes, |
| 8176 | our_key_data->x, our_key_data->len, |
| 8177 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8178 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8179 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 8180 | key_bits = psa_get_key_bits( &attributes ); |
| 8181 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8182 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8183 | TEST_LE_U( expected_output->len, |
| 8184 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 8185 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 8186 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8187 | |
| 8188 | /* Good case with exact output size */ |
| 8189 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 8190 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8191 | peer_key_data->x, peer_key_data->len, |
| 8192 | output, expected_output->len, |
| 8193 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8194 | ASSERT_COMPARE( output, output_length, |
| 8195 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8196 | mbedtls_free( output ); |
| 8197 | output = NULL; |
| 8198 | output_length = ~0; |
| 8199 | |
| 8200 | /* Larger buffer */ |
| 8201 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 8202 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8203 | peer_key_data->x, peer_key_data->len, |
| 8204 | output, expected_output->len + 1, |
| 8205 | &output_length ) ); |
| 8206 | ASSERT_COMPARE( output, output_length, |
| 8207 | expected_output->x, expected_output->len ); |
| 8208 | mbedtls_free( output ); |
| 8209 | output = NULL; |
| 8210 | output_length = ~0; |
| 8211 | |
| 8212 | /* Buffer too small */ |
| 8213 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 8214 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 8215 | peer_key_data->x, peer_key_data->len, |
| 8216 | output, expected_output->len - 1, |
| 8217 | &output_length ), |
| 8218 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8219 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8220 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8221 | mbedtls_free( output ); |
| 8222 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8223 | |
| 8224 | exit: |
| 8225 | mbedtls_free( output ); |
| 8226 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8227 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8228 | } |
| 8229 | /* END_CASE */ |
| 8230 | |
| 8231 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8232 | void key_agreement_capacity( int alg_arg, |
| 8233 | int our_key_type_arg, data_t *our_key_data, |
| 8234 | data_t *peer_key_data, |
| 8235 | int expected_capacity_arg ) |
| 8236 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8237 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8238 | psa_algorithm_t alg = alg_arg; |
| 8239 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8240 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8241 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8242 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8243 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8244 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8245 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8246 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8247 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8248 | psa_set_key_algorithm( &attributes, alg ); |
| 8249 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8250 | PSA_ASSERT( psa_import_key( &attributes, |
| 8251 | our_key_data->x, our_key_data->len, |
| 8252 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8253 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8254 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8255 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8256 | &operation, |
| 8257 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8258 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8259 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8260 | { |
| 8261 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8262 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8263 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8264 | NULL, 0 ) ); |
| 8265 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8266 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8267 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8268 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8269 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8270 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8271 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8272 | /* Test the actual capacity by reading the output. */ |
| 8273 | while( actual_capacity > sizeof( output ) ) |
| 8274 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8275 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8276 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8277 | actual_capacity -= sizeof( output ); |
| 8278 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8279 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8280 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8281 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 8282 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8283 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8284 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8285 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8286 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8287 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8288 | } |
| 8289 | /* END_CASE */ |
| 8290 | |
| 8291 | /* BEGIN_CASE */ |
| 8292 | void key_agreement_output( int alg_arg, |
| 8293 | int our_key_type_arg, data_t *our_key_data, |
| 8294 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8295 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8296 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8297 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8298 | psa_algorithm_t alg = alg_arg; |
| 8299 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8300 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8301 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8302 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8303 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8304 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 8305 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8306 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8307 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8308 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8309 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8310 | psa_set_key_algorithm( &attributes, alg ); |
| 8311 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8312 | PSA_ASSERT( psa_import_key( &attributes, |
| 8313 | our_key_data->x, our_key_data->len, |
| 8314 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8315 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8316 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8317 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8318 | &operation, |
| 8319 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8320 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8321 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8322 | { |
| 8323 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8324 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8325 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8326 | NULL, 0 ) ); |
| 8327 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8328 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8329 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8330 | actual_output, |
| 8331 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8332 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8333 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8334 | if( expected_output2->len != 0 ) |
| 8335 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8336 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8337 | actual_output, |
| 8338 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8339 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8340 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8341 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8342 | |
| 8343 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8344 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8345 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8346 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8347 | mbedtls_free( actual_output ); |
| 8348 | } |
| 8349 | /* END_CASE */ |
| 8350 | |
| 8351 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8352 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8353 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8354 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8355 | unsigned char *output = NULL; |
| 8356 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8357 | size_t i; |
| 8358 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8359 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8360 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8361 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8362 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8363 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8364 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8365 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8366 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8367 | /* Run several times, to ensure that every output byte will be |
| 8368 | * nonzero at least once with overwhelming probability |
| 8369 | * (2^(-8*number_of_runs)). */ |
| 8370 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8371 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8372 | if( bytes != 0 ) |
| 8373 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8374 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8375 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8376 | for( i = 0; i < bytes; i++ ) |
| 8377 | { |
| 8378 | if( output[i] != 0 ) |
| 8379 | ++changed[i]; |
| 8380 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8381 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8382 | |
| 8383 | /* Check that every byte was changed to nonzero at least once. This |
| 8384 | * validates that psa_generate_random is overwriting every byte of |
| 8385 | * the output buffer. */ |
| 8386 | for( i = 0; i < bytes; i++ ) |
| 8387 | { |
| 8388 | TEST_ASSERT( changed[i] != 0 ); |
| 8389 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8390 | |
| 8391 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8392 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8393 | mbedtls_free( output ); |
| 8394 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8395 | } |
| 8396 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8397 | |
| 8398 | /* BEGIN_CASE */ |
| 8399 | void generate_key( int type_arg, |
| 8400 | int bits_arg, |
| 8401 | int usage_arg, |
| 8402 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8403 | int expected_status_arg, |
| 8404 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8405 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8406 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8407 | psa_key_type_t type = type_arg; |
| 8408 | psa_key_usage_t usage = usage_arg; |
| 8409 | size_t bits = bits_arg; |
| 8410 | psa_algorithm_t alg = alg_arg; |
| 8411 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8412 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8413 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8414 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8415 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8416 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8417 | psa_set_key_usage_flags( &attributes, usage ); |
| 8418 | psa_set_key_algorithm( &attributes, alg ); |
| 8419 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8420 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8421 | |
| 8422 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8423 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8424 | |
| 8425 | if( is_large_key > 0 ) |
| 8426 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8427 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8428 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8429 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8430 | |
| 8431 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8432 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8433 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8434 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8435 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8436 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8437 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8438 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8439 | |
| 8440 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8441 | /* |
| 8442 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8443 | * thus reset them as required. |
| 8444 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8445 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8446 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8447 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8448 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8449 | } |
| 8450 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8451 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8452 | /* 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] | 8453 | void generate_key_rsa( int bits_arg, |
| 8454 | data_t *e_arg, |
| 8455 | int expected_status_arg ) |
| 8456 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8457 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8458 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8459 | size_t bits = bits_arg; |
| 8460 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8461 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8462 | psa_status_t expected_status = expected_status_arg; |
| 8463 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8464 | uint8_t *exported = NULL; |
| 8465 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8466 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8467 | size_t exported_length = SIZE_MAX; |
| 8468 | uint8_t *e_read_buffer = NULL; |
| 8469 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8470 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8471 | size_t e_read_length = SIZE_MAX; |
| 8472 | |
| 8473 | if( e_arg->len == 0 || |
| 8474 | ( e_arg->len == 3 && |
| 8475 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8476 | { |
| 8477 | is_default_public_exponent = 1; |
| 8478 | e_read_size = 0; |
| 8479 | } |
| 8480 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8481 | ASSERT_ALLOC( exported, exported_size ); |
| 8482 | |
| 8483 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8484 | |
| 8485 | psa_set_key_usage_flags( &attributes, usage ); |
| 8486 | psa_set_key_algorithm( &attributes, alg ); |
| 8487 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8488 | e_arg->x, e_arg->len ) ); |
| 8489 | psa_set_key_bits( &attributes, bits ); |
| 8490 | |
| 8491 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8492 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8493 | if( expected_status != PSA_SUCCESS ) |
| 8494 | goto exit; |
| 8495 | |
| 8496 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8497 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8498 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8499 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8500 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8501 | e_read_buffer, e_read_size, |
| 8502 | &e_read_length ) ); |
| 8503 | if( is_default_public_exponent ) |
| 8504 | TEST_EQUAL( e_read_length, 0 ); |
| 8505 | else |
| 8506 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8507 | |
| 8508 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8509 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8510 | goto exit; |
| 8511 | |
| 8512 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8513 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8514 | exported, exported_size, |
| 8515 | &exported_length ) ); |
| 8516 | { |
| 8517 | uint8_t *p = exported; |
| 8518 | uint8_t *end = exported + exported_length; |
| 8519 | size_t len; |
| 8520 | /* RSAPublicKey ::= SEQUENCE { |
| 8521 | * modulus INTEGER, -- n |
| 8522 | * publicExponent INTEGER } -- e |
| 8523 | */ |
| 8524 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8525 | MBEDTLS_ASN1_SEQUENCE | |
| 8526 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8527 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8528 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8529 | MBEDTLS_ASN1_INTEGER ) ); |
| 8530 | if( len >= 1 && p[0] == 0 ) |
| 8531 | { |
| 8532 | ++p; |
| 8533 | --len; |
| 8534 | } |
| 8535 | if( e_arg->len == 0 ) |
| 8536 | { |
| 8537 | TEST_EQUAL( len, 3 ); |
| 8538 | TEST_EQUAL( p[0], 1 ); |
| 8539 | TEST_EQUAL( p[1], 0 ); |
| 8540 | TEST_EQUAL( p[2], 1 ); |
| 8541 | } |
| 8542 | else |
| 8543 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8544 | } |
| 8545 | |
| 8546 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8547 | /* |
| 8548 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8549 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8550 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8551 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8552 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8553 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8554 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8555 | mbedtls_free( e_read_buffer ); |
| 8556 | mbedtls_free( exported ); |
| 8557 | } |
| 8558 | /* END_CASE */ |
| 8559 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8560 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8561 | void persistent_key_load_key_from_storage( data_t *data, |
| 8562 | int type_arg, int bits_arg, |
| 8563 | int usage_flags_arg, int alg_arg, |
| 8564 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8565 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8566 | 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] | 8567 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8568 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8569 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8570 | psa_key_type_t type = type_arg; |
| 8571 | size_t bits = bits_arg; |
| 8572 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8573 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8574 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8575 | unsigned char *first_export = NULL; |
| 8576 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8577 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8578 | size_t first_exported_length; |
| 8579 | size_t second_exported_length; |
| 8580 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8581 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8582 | { |
| 8583 | ASSERT_ALLOC( first_export, export_size ); |
| 8584 | ASSERT_ALLOC( second_export, export_size ); |
| 8585 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8586 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8587 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8588 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8589 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8590 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8591 | psa_set_key_algorithm( &attributes, alg ); |
| 8592 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8593 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8594 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8595 | switch( generation_method ) |
| 8596 | { |
| 8597 | case IMPORT_KEY: |
| 8598 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8599 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8600 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8601 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8602 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8603 | case GENERATE_KEY: |
| 8604 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8605 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8606 | break; |
| 8607 | |
| 8608 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8609 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8610 | { |
| 8611 | /* Create base key */ |
| 8612 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8613 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8614 | psa_set_key_usage_flags( &base_attributes, |
| 8615 | PSA_KEY_USAGE_DERIVE ); |
| 8616 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8617 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8618 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8619 | data->x, data->len, |
| 8620 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8621 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8622 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8623 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8624 | &operation, |
| 8625 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8626 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8627 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8628 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8629 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8630 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8631 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8632 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8633 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8634 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8635 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8636 | #else |
| 8637 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8638 | #endif |
| 8639 | break; |
| 8640 | |
| 8641 | default: |
| 8642 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8643 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8644 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8645 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8646 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8647 | /* Export the key if permitted by the key policy. */ |
| 8648 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8649 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8650 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8651 | first_export, export_size, |
| 8652 | &first_exported_length ) ); |
| 8653 | if( generation_method == IMPORT_KEY ) |
| 8654 | ASSERT_COMPARE( data->x, data->len, |
| 8655 | first_export, first_exported_length ); |
| 8656 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8657 | |
| 8658 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8659 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8660 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8661 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8662 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8663 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8664 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8665 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8666 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8667 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8668 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8669 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8670 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8671 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8672 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8673 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8674 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8675 | /* Export the key again if permitted by the key policy. */ |
| 8676 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8677 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8678 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8679 | second_export, export_size, |
| 8680 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8681 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8682 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8683 | } |
| 8684 | |
| 8685 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8686 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8687 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8688 | |
| 8689 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8690 | /* |
| 8691 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8692 | * thus reset them as required. |
| 8693 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8694 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8695 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8696 | mbedtls_free( first_export ); |
| 8697 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8698 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8699 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8700 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8701 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8702 | } |
| 8703 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8704 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8705 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8706 | void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 8707 | int primitive_arg, int hash_arg, int role_arg, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8708 | int input_first, data_t *pw_data, |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8709 | int expected_status_setup_arg, |
| 8710 | int expected_status_set_role_arg, |
| 8711 | int expected_status_set_password_key_arg, |
| 8712 | int expected_status_input_output_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8713 | { |
| 8714 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8715 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 8716 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8717 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 8718 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8719 | psa_algorithm_t hash_alg = hash_arg; |
| 8720 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8721 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8722 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8723 | psa_status_t expected_status_setup = expected_status_setup_arg; |
| 8724 | psa_status_t expected_status_set_role = expected_status_set_role_arg; |
| 8725 | psa_status_t expected_status_set_password_key = |
| 8726 | expected_status_set_password_key_arg; |
| 8727 | psa_status_t expected_status_input_output = |
| 8728 | expected_status_input_output_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8729 | unsigned char *output_buffer = NULL; |
| 8730 | size_t output_len = 0; |
| 8731 | |
| 8732 | PSA_INIT( ); |
| 8733 | |
| 8734 | ASSERT_ALLOC( output_buffer, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8735 | PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8736 | PSA_PAKE_STEP_KEY_SHARE) ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8737 | |
| 8738 | if( pw_data->len > 0 ) |
| 8739 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8740 | psa_set_key_usage_flags( &attributes, key_usage_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8741 | psa_set_key_algorithm( &attributes, alg ); |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8742 | psa_set_key_type( &attributes, key_type_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8743 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8744 | &key ) ); |
| 8745 | } |
| 8746 | |
| 8747 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8748 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8749 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8750 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8751 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8752 | |
| 8753 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8754 | PSA_ERROR_BAD_STATE ); |
| 8755 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8756 | PSA_ERROR_BAD_STATE ); |
| 8757 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8758 | PSA_ERROR_BAD_STATE ); |
| 8759 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8760 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8761 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8762 | NULL, 0, NULL ), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8763 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8764 | 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] | 8765 | PSA_ERROR_BAD_STATE ); |
| 8766 | |
| 8767 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8768 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8769 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8770 | expected_status_setup ); |
| 8771 | if( expected_status_setup != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8772 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8773 | |
Neil Armstrong | 50de0ae | 2022-06-08 17:46:24 +0200 | [diff] [blame] | 8774 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8775 | PSA_ERROR_BAD_STATE ); |
| 8776 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8777 | TEST_EQUAL( psa_pake_set_role( &operation, role), |
| 8778 | expected_status_set_role ); |
| 8779 | if( expected_status_set_role != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8780 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8781 | |
| 8782 | if( pw_data->len > 0 ) |
| 8783 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8784 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8785 | expected_status_set_password_key ); |
| 8786 | if( expected_status_set_password_key != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8787 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8788 | } |
| 8789 | |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8790 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8791 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8792 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8793 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8794 | |
| 8795 | const uint8_t unsupported_id[] = "abcd"; |
| 8796 | |
| 8797 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8798 | PSA_ERROR_NOT_SUPPORTED ); |
| 8799 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8800 | PSA_ERROR_NOT_SUPPORTED ); |
| 8801 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8802 | /* First round */ |
| 8803 | if( input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8804 | { |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8805 | /* Invalid parameters */ |
| 8806 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8807 | NULL, 0 ), |
| 8808 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8809 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8810 | output_buffer, 66 ), |
| 8811 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8812 | /* Invalid first step */ |
| 8813 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8814 | output_buffer, 66 ), |
| 8815 | PSA_ERROR_BAD_STATE ); |
| 8816 | |
| 8817 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8818 | output_buffer, 66 ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8819 | expected_status_input_output); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8820 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8821 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8822 | { |
| 8823 | /* Buffer too large */ |
| 8824 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8825 | output_buffer, 512 ), |
| 8826 | PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8827 | |
| 8828 | /* The operation should be aborted at this point */ |
| 8829 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8830 | output_buffer, 66 ), |
| 8831 | PSA_ERROR_BAD_STATE ); |
| 8832 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8833 | } |
| 8834 | else |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8835 | { |
| 8836 | /* Invalid parameters */ |
| 8837 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8838 | NULL, 0, NULL ), |
| 8839 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8840 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8841 | output_buffer, 512, &output_len ), |
| 8842 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8843 | /* Invalid first step */ |
| 8844 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8845 | output_buffer, 512, &output_len ), |
| 8846 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8847 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8848 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8849 | output_buffer, 512, &output_len ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8850 | expected_status_input_output ); |
Neil Armstrong | 98506ab | 2022-06-08 17:43:20 +0200 | [diff] [blame] | 8851 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8852 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8853 | { |
| 8854 | TEST_ASSERT( output_len > 0 ); |
| 8855 | |
| 8856 | /* Buffer too small */ |
| 8857 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8858 | output_buffer, 5, &output_len ), |
| 8859 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8860 | |
| 8861 | /* The operation should be aborted at this point */ |
| 8862 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8863 | output_buffer, 512, &output_len ), |
| 8864 | PSA_ERROR_BAD_STATE ); |
| 8865 | } |
| 8866 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8867 | |
| 8868 | exit: |
| 8869 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 8870 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8871 | mbedtls_free( output_buffer ); |
| 8872 | PSA_DONE( ); |
| 8873 | } |
| 8874 | /* END_CASE */ |
| 8875 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8876 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8877 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 8878 | int client_input_first, int inject_error, |
| 8879 | data_t *pw_data ) |
| 8880 | { |
| 8881 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8882 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8883 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8884 | psa_algorithm_t alg = alg_arg; |
| 8885 | psa_algorithm_t hash_alg = hash_arg; |
| 8886 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8887 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8888 | |
| 8889 | PSA_INIT( ); |
| 8890 | |
| 8891 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8892 | psa_set_key_algorithm( &attributes, alg ); |
| 8893 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8894 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8895 | &key ) ); |
| 8896 | |
| 8897 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8898 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8899 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8900 | |
| 8901 | |
| 8902 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8903 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8904 | |
| 8905 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8906 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8907 | |
| 8908 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8909 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8910 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8911 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8912 | client_input_first, 1, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8913 | |
| 8914 | if( inject_error == 1 || inject_error == 2 ) |
| 8915 | goto exit; |
| 8916 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8917 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8918 | client_input_first, 2, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8919 | |
| 8920 | exit: |
| 8921 | psa_destroy_key( key ); |
| 8922 | psa_pake_abort( &server ); |
| 8923 | psa_pake_abort( &client ); |
| 8924 | PSA_DONE( ); |
| 8925 | } |
| 8926 | /* END_CASE */ |
| 8927 | |
| 8928 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8929 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8930 | int derive_alg_arg, data_t *pw_data, |
| 8931 | int client_input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8932 | { |
| 8933 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8934 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8935 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8936 | psa_algorithm_t alg = alg_arg; |
| 8937 | psa_algorithm_t hash_alg = hash_arg; |
| 8938 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 8939 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8940 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8941 | psa_key_derivation_operation_t server_derive = |
| 8942 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8943 | psa_key_derivation_operation_t client_derive = |
| 8944 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8945 | |
| 8946 | PSA_INIT( ); |
| 8947 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8948 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8949 | psa_set_key_algorithm( &attributes, alg ); |
| 8950 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8951 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8952 | &key ) ); |
| 8953 | |
| 8954 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8955 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8956 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8957 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8958 | /* Get shared key */ |
| 8959 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 8960 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 8961 | |
| 8962 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 8963 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 8964 | { |
| 8965 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 8966 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8967 | (const uint8_t*) "", 0) ); |
| 8968 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 8969 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8970 | (const uint8_t*) "", 0) ); |
| 8971 | } |
| 8972 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8973 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8974 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8975 | |
| 8976 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8977 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8978 | |
| 8979 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8980 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8981 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8982 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8983 | PSA_ERROR_BAD_STATE ); |
| 8984 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8985 | PSA_ERROR_BAD_STATE ); |
| 8986 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8987 | /* First round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8988 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8989 | client_input_first, 1, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8990 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8991 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8992 | PSA_ERROR_BAD_STATE ); |
| 8993 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8994 | PSA_ERROR_BAD_STATE ); |
| 8995 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8996 | /* Second round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8997 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8998 | client_input_first, 2, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8999 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9000 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 9001 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 9002 | |
| 9003 | exit: |
| 9004 | psa_key_derivation_abort( &server_derive ); |
| 9005 | psa_key_derivation_abort( &client_derive ); |
| 9006 | psa_destroy_key( key ); |
| 9007 | psa_pake_abort( &server ); |
| 9008 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9009 | PSA_DONE( ); |
| 9010 | } |
| 9011 | /* END_CASE */ |