Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
| 7 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 8 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 9 | * uses mbedtls_ctr_drbg internally. */ |
| 10 | #include "mbedtls/ctr_drbg.h" |
| 11 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 12 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 13 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 14 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 15 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 16 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 17 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 18 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 19 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 20 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 21 | #else |
| 22 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 23 | #endif |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 24 | #include "mbedtls/legacy_or_psa.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 25 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 26 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 27 | #define UNUSED 0xdeadbeef |
| 28 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 29 | /* Assert that an operation is (not) active. |
| 30 | * This serves as a proxy for checking if the operation is aborted. */ |
| 31 | #define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 ) |
| 32 | #define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 33 | |
| 34 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 35 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 36 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 37 | /** Test if a buffer contains a constant byte value. |
| 38 | * |
| 39 | * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 40 | * |
| 41 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 42 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 43 | * \param size Size of the buffer in bytes. |
| 44 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 45 | * \return 1 if the buffer is all-bits-zero. |
| 46 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 47 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 48 | static int mem_is_char( void *buffer, unsigned char c, size_t size ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 49 | { |
| 50 | size_t i; |
| 51 | for( i = 0; i < size; i++ ) |
| 52 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 53 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 54 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 55 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 56 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 57 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 58 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 59 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 60 | static int asn1_write_10x( unsigned char **p, |
| 61 | unsigned char *start, |
| 62 | size_t bits, |
| 63 | unsigned char x ) |
| 64 | { |
| 65 | int ret; |
| 66 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 67 | if( bits == 0 ) |
| 68 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 69 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 70 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 71 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 72 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 73 | *p -= len; |
| 74 | ( *p )[len-1] = x; |
| 75 | if( bits % 8 == 0 ) |
| 76 | ( *p )[1] |= 1; |
| 77 | else |
| 78 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 79 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 80 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 81 | MBEDTLS_ASN1_INTEGER ) ); |
| 82 | return( len ); |
| 83 | } |
| 84 | |
| 85 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 86 | size_t buffer_size, |
| 87 | unsigned char **p, |
| 88 | size_t bits, |
| 89 | int keypair ) |
| 90 | { |
| 91 | size_t half_bits = ( bits + 1 ) / 2; |
| 92 | int ret; |
| 93 | int len = 0; |
| 94 | /* Construct something that looks like a DER encoding of |
| 95 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 96 | * RSAPrivateKey ::= SEQUENCE { |
| 97 | * version Version, |
| 98 | * modulus INTEGER, -- n |
| 99 | * publicExponent INTEGER, -- e |
| 100 | * privateExponent INTEGER, -- d |
| 101 | * prime1 INTEGER, -- p |
| 102 | * prime2 INTEGER, -- q |
| 103 | * exponent1 INTEGER, -- d mod (p-1) |
| 104 | * exponent2 INTEGER, -- d mod (q-1) |
| 105 | * coefficient INTEGER, -- (inverse of q) mod p |
| 106 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 107 | * } |
| 108 | * Or, for a public key, the same structure with only |
| 109 | * version, modulus and publicExponent. |
| 110 | */ |
| 111 | *p = buffer + buffer_size; |
| 112 | if( keypair ) |
| 113 | { |
| 114 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 115 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 116 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 117 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 118 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 119 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 120 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 121 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 122 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 123 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 124 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 125 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 126 | } |
| 127 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 128 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 129 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 130 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 131 | if( keypair ) |
| 132 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 133 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 134 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 135 | { |
| 136 | const unsigned char tag = |
| 137 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 138 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 139 | } |
| 140 | return( len ); |
| 141 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 142 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 143 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 144 | int exercise_mac_setup( psa_key_type_t key_type, |
| 145 | const unsigned char *key_bytes, |
| 146 | size_t key_length, |
| 147 | psa_algorithm_t alg, |
| 148 | psa_mac_operation_t *operation, |
| 149 | psa_status_t *status ) |
| 150 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 151 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 152 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 153 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 154 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 155 | psa_set_key_algorithm( &attributes, alg ); |
| 156 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 157 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 158 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 159 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 160 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 161 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 162 | /* If setup failed, reproduce the failure, so that the caller can |
| 163 | * test the resulting state of the operation object. */ |
| 164 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 165 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 166 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 169 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 170 | return( 1 ); |
| 171 | |
| 172 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 173 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 174 | return( 0 ); |
| 175 | } |
| 176 | |
| 177 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 178 | const unsigned char *key_bytes, |
| 179 | size_t key_length, |
| 180 | psa_algorithm_t alg, |
| 181 | psa_cipher_operation_t *operation, |
| 182 | psa_status_t *status ) |
| 183 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 184 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 185 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 186 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 187 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 188 | psa_set_key_algorithm( &attributes, alg ); |
| 189 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 190 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 191 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 192 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 193 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 194 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 195 | /* If setup failed, reproduce the failure, so that the caller can |
| 196 | * test the resulting state of the operation object. */ |
| 197 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 198 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 199 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 200 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 203 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 204 | return( 1 ); |
| 205 | |
| 206 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 207 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 208 | return( 0 ); |
| 209 | } |
| 210 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 211 | static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 212 | { |
| 213 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 214 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 215 | uint8_t buffer[1]; |
| 216 | size_t length; |
| 217 | int ok = 0; |
| 218 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 219 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 220 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 221 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 222 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 223 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 224 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 225 | TEST_EQUAL( |
| 226 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 227 | TEST_EQUAL( |
| 228 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 229 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 230 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 231 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 232 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 233 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 234 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 235 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 236 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 237 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 238 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 239 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 240 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 241 | ok = 1; |
| 242 | |
| 243 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 244 | /* |
| 245 | * Key attributes may have been returned by psa_get_key_attributes() |
| 246 | * thus reset them as required. |
| 247 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 248 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 249 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 250 | return( ok ); |
| 251 | } |
| 252 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 253 | /* Assert that a key isn't reported as having a slot number. */ |
| 254 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 255 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 256 | do \ |
| 257 | { \ |
| 258 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 259 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 260 | attributes, \ |
| 261 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 262 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 263 | } \ |
| 264 | while( 0 ) |
| 265 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 266 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 267 | ( (void) 0 ) |
| 268 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 269 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 270 | /* An overapproximation of the amount of storage needed for a key of the |
| 271 | * given type and with the given content. The API doesn't make it easy |
| 272 | * to find a good value for the size. The current implementation doesn't |
| 273 | * care about the value anyway. */ |
| 274 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 275 | ( data )->len |
| 276 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 277 | typedef enum { |
| 278 | IMPORT_KEY = 0, |
| 279 | GENERATE_KEY = 1, |
| 280 | DERIVE_KEY = 2 |
| 281 | } generate_method; |
| 282 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 283 | typedef enum |
| 284 | { |
| 285 | DO_NOT_SET_LENGTHS = 0, |
| 286 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 287 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 288 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 289 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 290 | typedef enum |
| 291 | { |
| 292 | USE_NULL_TAG = 0, |
| 293 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 294 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 295 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 296 | /*! |
| 297 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 298 | * \param key_type_arg Type of key passed in |
| 299 | * \param key_data The encryption / decryption key data |
| 300 | * \param alg_arg The type of algorithm used |
| 301 | * \param nonce Nonce data |
| 302 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 303 | * \param ad_part_len_arg If not -1, the length of chunks to |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 304 | * feed additional data in to be encrypted / |
| 305 | * decrypted. If -1, no chunking. |
| 306 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 307 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 308 | * the data in to be encrypted / decrypted. If |
| 309 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 310 | * \param set_lengths_method A member of the set_lengths_method_t enum is |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 311 | * expected here, this controls whether or not |
| 312 | * to set lengths, and in what order with |
| 313 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 314 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 315 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 316 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 317 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 318 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 319 | */ |
| 320 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 321 | int alg_arg, |
| 322 | data_t *nonce, |
| 323 | data_t *additional_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 324 | int ad_part_len_arg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 325 | data_t *input_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 326 | int data_part_len_arg, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 327 | set_lengths_method_t set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 328 | data_t *expected_output, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 329 | int is_encrypt, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 330 | int do_zero_parts ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 331 | { |
| 332 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 333 | psa_key_type_t key_type = key_type_arg; |
| 334 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 335 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 336 | unsigned char *output_data = NULL; |
| 337 | unsigned char *part_data = NULL; |
| 338 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 339 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 340 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 341 | size_t output_size = 0; |
| 342 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 343 | size_t output_length = 0; |
| 344 | size_t key_bits = 0; |
| 345 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 346 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 347 | size_t part_length = 0; |
| 348 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 349 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 350 | size_t ad_part_len = 0; |
| 351 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 352 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 353 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 354 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 355 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 356 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 357 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 358 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 359 | PSA_ASSERT( psa_crypto_init( ) ); |
| 360 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 361 | if( is_encrypt ) |
| 362 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 363 | else |
| 364 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 365 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 366 | psa_set_key_algorithm( &attributes, alg ); |
| 367 | psa_set_key_type( &attributes, key_type ); |
| 368 | |
| 369 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 370 | &key ) ); |
| 371 | |
| 372 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 373 | key_bits = psa_get_key_bits( &attributes ); |
| 374 | |
| 375 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 376 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 377 | if( is_encrypt ) |
| 378 | { |
| 379 | /* Tag gets written at end of buffer. */ |
| 380 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 381 | ( input_data->len + |
| 382 | tag_length ) ); |
| 383 | data_true_size = input_data->len; |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 388 | ( input_data->len - |
| 389 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 390 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 391 | /* Do not want to attempt to decrypt tag. */ |
| 392 | data_true_size = input_data->len - tag_length; |
| 393 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 394 | |
| 395 | ASSERT_ALLOC( output_data, output_size ); |
| 396 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 397 | if( is_encrypt ) |
| 398 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 399 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 400 | TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 401 | } |
| 402 | else |
| 403 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 404 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 405 | TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 406 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 407 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 408 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 409 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 410 | if( is_encrypt ) |
| 411 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 412 | else |
| 413 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 414 | |
| 415 | /* If the operation is not supported, just skip and not fail in case the |
| 416 | * encryption involves a common limitation of cryptography hardwares and |
| 417 | * an alternative implementation. */ |
| 418 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 419 | { |
| 420 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 421 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 422 | } |
| 423 | |
| 424 | PSA_ASSERT( status ); |
| 425 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 426 | if( set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 427 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 428 | else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 429 | { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 430 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 431 | data_true_size ) ); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 432 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 433 | } |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 434 | else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 435 | { |
| 436 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 437 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 438 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 439 | data_true_size ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 440 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 441 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 442 | if( ad_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 443 | { |
| 444 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 445 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 446 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 447 | for( part_offset = 0, part_count = 0; |
| 448 | part_offset < additional_data->len; |
| 449 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 450 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 451 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 452 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 453 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 454 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 455 | else if( additional_data->len - part_offset < ad_part_len ) |
| 456 | { |
| 457 | part_length = additional_data->len - part_offset; |
| 458 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 459 | else |
| 460 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 461 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 465 | additional_data->x + part_offset, |
| 466 | part_length ) ); |
| 467 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | /* Pass additional data in one go. */ |
| 473 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 474 | additional_data->len ) ); |
| 475 | } |
| 476 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 477 | if( data_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 478 | { |
| 479 | /* Pass data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 480 | data_part_len = ( size_t ) data_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 481 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 482 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 483 | |
| 484 | ASSERT_ALLOC( part_data, part_data_size ); |
| 485 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 486 | for( part_offset = 0, part_count = 0; |
| 487 | part_offset < data_true_size; |
| 488 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 489 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 490 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 491 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 492 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 493 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 494 | else if( ( data_true_size - part_offset ) < data_part_len ) |
| 495 | { |
| 496 | part_length = ( data_true_size - part_offset ); |
| 497 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 498 | else |
| 499 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 500 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | PSA_ASSERT( psa_aead_update( &operation, |
| 504 | ( input_data->x + part_offset ), |
| 505 | part_length, part_data, |
| 506 | part_data_size, |
| 507 | &output_part_length ) ); |
| 508 | |
| 509 | if( output_data && output_part_length ) |
| 510 | { |
Mircea Udrea | 657ff4f | 2022-01-31 13:51:56 +0100 | [diff] [blame] | 511 | memcpy( ( output_data + output_length ), part_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 512 | output_part_length ); |
| 513 | } |
| 514 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 515 | output_length += output_part_length; |
| 516 | } |
| 517 | } |
| 518 | else |
| 519 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 520 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 521 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 522 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 523 | output_size, &output_length ) ); |
| 524 | } |
| 525 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 526 | if( is_encrypt ) |
| 527 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 528 | final_output_size, |
| 529 | &output_part_length, |
| 530 | tag_buffer, tag_length, |
| 531 | &tag_size ) ); |
| 532 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 533 | { |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 534 | PSA_ASSERT( psa_aead_verify( &operation, final_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 535 | final_output_size, |
| 536 | &output_part_length, |
| 537 | ( input_data->x + data_true_size ), |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 538 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 539 | } |
| 540 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 541 | if( output_data && output_part_length ) |
| 542 | memcpy( ( output_data + output_length ), final_data, |
| 543 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 544 | |
| 545 | output_length += output_part_length; |
| 546 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 547 | |
| 548 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 549 | * should be exact.*/ |
| 550 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 551 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 552 | TEST_EQUAL( tag_length, tag_size ); |
| 553 | |
| 554 | if( output_data && tag_length ) |
| 555 | memcpy( ( output_data + output_length ), tag_buffer, |
| 556 | tag_length ); |
| 557 | |
| 558 | output_length += tag_length; |
| 559 | |
| 560 | TEST_EQUAL( output_length, |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 561 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 562 | input_data->len ) ); |
| 563 | TEST_LE_U( output_length, |
| 564 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 565 | } |
| 566 | else |
| 567 | { |
| 568 | TEST_EQUAL( output_length, |
| 569 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 570 | input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 571 | TEST_LE_U( output_length, |
| 572 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 575 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 576 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 577 | output_data, output_length ); |
| 578 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 579 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 580 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 581 | |
| 582 | exit: |
| 583 | psa_destroy_key( key ); |
| 584 | psa_aead_abort( &operation ); |
| 585 | mbedtls_free( output_data ); |
| 586 | mbedtls_free( part_data ); |
| 587 | mbedtls_free( final_data ); |
| 588 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 589 | |
| 590 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 593 | /*! |
| 594 | * \brief Internal Function for MAC multipart tests. |
| 595 | * \param key_type_arg Type of key passed in |
| 596 | * \param key_data The encryption / decryption key data |
| 597 | * \param alg_arg The type of algorithm used |
| 598 | * \param input_data Data to encrypt / decrypt |
| 599 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 600 | * the data in to be encrypted / decrypted. If |
| 601 | * -1, no chunking |
| 602 | * \param expected_output Expected output |
| 603 | * \param is_verify If non-zero this is an verify operation. |
| 604 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 605 | * with normal length chunks. |
| 606 | * \return int Zero on failure, non-zero on success. |
| 607 | */ |
| 608 | static int mac_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 609 | int alg_arg, |
| 610 | data_t *input_data, |
| 611 | int data_part_len_arg, |
| 612 | data_t *expected_output, |
| 613 | int is_verify, |
| 614 | int do_zero_parts ) |
| 615 | { |
| 616 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 617 | psa_key_type_t key_type = key_type_arg; |
| 618 | psa_algorithm_t alg = alg_arg; |
| 619 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 620 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 621 | size_t part_offset = 0; |
| 622 | size_t part_length = 0; |
| 623 | size_t data_part_len = 0; |
| 624 | size_t mac_len = 0; |
| 625 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 626 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 627 | |
| 628 | int test_ok = 0; |
| 629 | size_t part_count = 0; |
| 630 | |
Neil Armstrong | fd4c259 | 2022-03-07 10:11:11 +0100 | [diff] [blame] | 631 | PSA_INIT( ); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 632 | |
| 633 | if( is_verify ) |
| 634 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 635 | else |
| 636 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 637 | |
| 638 | psa_set_key_algorithm( &attributes, alg ); |
| 639 | psa_set_key_type( &attributes, key_type ); |
| 640 | |
| 641 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 642 | &key ) ); |
| 643 | |
| 644 | if( is_verify ) |
| 645 | status = psa_mac_verify_setup( &operation, key, alg ); |
| 646 | else |
| 647 | status = psa_mac_sign_setup( &operation, key, alg ); |
| 648 | |
| 649 | PSA_ASSERT( status ); |
| 650 | |
| 651 | if( data_part_len_arg != -1 ) |
| 652 | { |
| 653 | /* Pass data in parts */ |
| 654 | data_part_len = ( size_t ) data_part_len_arg; |
| 655 | |
| 656 | for( part_offset = 0, part_count = 0; |
| 657 | part_offset < input_data->len; |
| 658 | part_offset += part_length, part_count++ ) |
| 659 | { |
| 660 | if( do_zero_parts && ( part_count & 0x01 ) ) |
| 661 | { |
| 662 | part_length = 0; |
| 663 | } |
| 664 | else if( ( input_data->len - part_offset ) < data_part_len ) |
| 665 | { |
| 666 | part_length = ( input_data->len - part_offset ); |
| 667 | } |
| 668 | else |
| 669 | { |
| 670 | part_length = data_part_len; |
| 671 | } |
| 672 | |
| 673 | PSA_ASSERT( psa_mac_update( &operation, |
| 674 | ( input_data->x + part_offset ), |
| 675 | part_length ) ); |
| 676 | } |
| 677 | } |
| 678 | else |
| 679 | { |
| 680 | /* Pass all data in one go. */ |
| 681 | PSA_ASSERT( psa_mac_update( &operation, input_data->x, |
| 682 | input_data->len ) ); |
| 683 | } |
| 684 | |
| 685 | if( is_verify ) |
| 686 | { |
| 687 | PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x, |
| 688 | expected_output->len ) ); |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | PSA_ASSERT( psa_mac_sign_finish( &operation, mac, |
| 693 | PSA_MAC_MAX_SIZE, &mac_len ) ); |
| 694 | |
| 695 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 696 | mac, mac_len ); |
| 697 | } |
| 698 | |
| 699 | test_ok = 1; |
| 700 | |
| 701 | exit: |
| 702 | psa_destroy_key( key ); |
| 703 | psa_mac_abort( &operation ); |
| 704 | PSA_DONE( ); |
| 705 | |
| 706 | return( test_ok ); |
| 707 | } |
| 708 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 709 | #if defined(PSA_WANT_ALG_JPAKE) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 710 | static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive, |
| 711 | psa_pake_operation_t *server, |
| 712 | psa_pake_operation_t *client, |
| 713 | int client_input_first, |
| 714 | int round, int inject_error ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 715 | { |
| 716 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 717 | size_t buffer_length = ( |
| 718 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 719 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 720 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2; |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 721 | /* The output should be exactly this size according to the spec */ |
| 722 | const size_t expected_size_key_share = |
| 723 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 724 | /* The output should be exactly this size according to the spec */ |
| 725 | const size_t expected_size_zk_public = |
| 726 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 727 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 728 | const size_t max_expected_size_zk_proof = |
| 729 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 730 | size_t buffer0_off = 0; |
| 731 | size_t buffer1_off = 0; |
| 732 | size_t s_g1_len, s_g2_len, s_a_len; |
| 733 | size_t s_g1_off, s_g2_off, s_a_off; |
| 734 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 735 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 736 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 737 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 738 | size_t c_g1_len, c_g2_len, c_a_len; |
| 739 | size_t c_g1_off, c_g2_off, c_a_off; |
| 740 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 741 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 742 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 743 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 744 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 745 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 746 | |
| 747 | ASSERT_ALLOC( buffer0, buffer_length ); |
| 748 | ASSERT_ALLOC( buffer1, buffer_length ); |
| 749 | |
| 750 | switch( round ) |
| 751 | { |
| 752 | case 1: |
| 753 | /* Server first round Output */ |
| 754 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 755 | buffer0 + buffer0_off, |
| 756 | 512 - buffer0_off, &s_g1_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 757 | TEST_EQUAL( s_g1_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 758 | s_g1_off = buffer0_off; |
| 759 | buffer0_off += s_g1_len; |
| 760 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 761 | buffer0 + buffer0_off, |
| 762 | 512 - buffer0_off, &s_x1_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 763 | TEST_EQUAL( s_x1_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 764 | s_x1_pk_off = buffer0_off; |
| 765 | buffer0_off += s_x1_pk_len; |
| 766 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 767 | buffer0 + buffer0_off, |
| 768 | 512 - buffer0_off, &s_x1_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 769 | TEST_LE_U( s_x1_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 770 | s_x1_pr_off = buffer0_off; |
| 771 | buffer0_off += s_x1_pr_len; |
| 772 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 773 | buffer0 + buffer0_off, |
| 774 | 512 - buffer0_off, &s_g2_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 775 | TEST_EQUAL( s_g2_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 776 | s_g2_off = buffer0_off; |
| 777 | buffer0_off += s_g2_len; |
| 778 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 779 | buffer0 + buffer0_off, |
| 780 | 512 - buffer0_off, &s_x2_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 781 | TEST_EQUAL( s_x2_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 782 | s_x2_pk_off = buffer0_off; |
| 783 | buffer0_off += s_x2_pk_len; |
| 784 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 785 | buffer0 + buffer0_off, |
| 786 | 512 - buffer0_off, &s_x2_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 787 | TEST_LE_U( s_x2_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 788 | s_x2_pr_off = buffer0_off; |
| 789 | buffer0_off += s_x2_pr_len; |
| 790 | |
| 791 | if( inject_error == 1 ) |
| 792 | { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 793 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 794 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 795 | expected_status = PSA_ERROR_DATA_INVALID; |
| 796 | } |
| 797 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 798 | /* |
| 799 | * When injecting errors in inputs, the implementation is |
| 800 | * free to detect it right away of with a delay. |
| 801 | * This permits delaying the error until the end of the input |
| 802 | * sequence, if no error appears then, this will be treated |
| 803 | * as an error. |
| 804 | */ |
| 805 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 806 | if( client_input_first == 1 ) |
| 807 | { |
| 808 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 809 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 810 | buffer0 + s_g1_off, s_g1_len ); |
| 811 | if( inject_error == 1 && status != PSA_SUCCESS ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 812 | { |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 813 | TEST_EQUAL( status, expected_status ); |
| 814 | break; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 815 | } |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 816 | else |
| 817 | { |
| 818 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 819 | } |
| 820 | |
| 821 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 822 | buffer0 + s_x1_pk_off, |
| 823 | s_x1_pk_len ); |
| 824 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 825 | { |
| 826 | TEST_EQUAL( status, expected_status ); |
| 827 | break; |
| 828 | } |
| 829 | else |
| 830 | { |
| 831 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 832 | } |
| 833 | |
| 834 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 835 | buffer0 + s_x1_pr_off, |
| 836 | s_x1_pr_len ); |
| 837 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 838 | { |
| 839 | TEST_EQUAL( status, expected_status ); |
| 840 | break; |
| 841 | } |
| 842 | else |
| 843 | { |
| 844 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 845 | } |
| 846 | |
| 847 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 848 | buffer0 + s_g2_off, |
| 849 | s_g2_len ); |
| 850 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 851 | { |
| 852 | TEST_EQUAL( status, expected_status ); |
| 853 | break; |
| 854 | } |
| 855 | else |
| 856 | { |
| 857 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 858 | } |
| 859 | |
| 860 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 861 | buffer0 + s_x2_pk_off, |
| 862 | s_x2_pk_len ); |
| 863 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 864 | { |
| 865 | TEST_EQUAL( status, expected_status ); |
| 866 | break; |
| 867 | } |
| 868 | else |
| 869 | { |
| 870 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 871 | } |
| 872 | |
| 873 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 874 | buffer0 + s_x2_pr_off, |
| 875 | s_x2_pr_len ); |
| 876 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 877 | { |
| 878 | TEST_EQUAL( status, expected_status ); |
| 879 | break; |
| 880 | } |
| 881 | else |
| 882 | { |
| 883 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 884 | } |
| 885 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 886 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 887 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 888 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /* Client first round Output */ |
| 892 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 893 | buffer1 + buffer1_off, |
| 894 | 512 - buffer1_off, &c_g1_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 895 | TEST_EQUAL( c_g1_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 896 | c_g1_off = buffer1_off; |
| 897 | buffer1_off += c_g1_len; |
| 898 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 899 | buffer1 + buffer1_off, |
| 900 | 512 - buffer1_off, &c_x1_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 901 | TEST_EQUAL( c_x1_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 902 | c_x1_pk_off = buffer1_off; |
| 903 | buffer1_off += c_x1_pk_len; |
| 904 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 905 | buffer1 + buffer1_off, |
| 906 | 512 - buffer1_off, &c_x1_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 907 | TEST_LE_U( c_x1_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 908 | c_x1_pr_off = buffer1_off; |
| 909 | buffer1_off += c_x1_pr_len; |
| 910 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 911 | buffer1 + buffer1_off, |
| 912 | 512 - buffer1_off, &c_g2_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 913 | TEST_EQUAL( c_g2_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 914 | c_g2_off = buffer1_off; |
| 915 | buffer1_off += c_g2_len; |
| 916 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 917 | buffer1 + buffer1_off, |
| 918 | 512 - buffer1_off, &c_x2_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 919 | TEST_EQUAL( c_x2_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 920 | c_x2_pk_off = buffer1_off; |
| 921 | buffer1_off += c_x2_pk_len; |
| 922 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 923 | buffer1 + buffer1_off, |
| 924 | 512 - buffer1_off, &c_x2_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 925 | TEST_LE_U( c_x2_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 926 | c_x2_pr_off = buffer1_off; |
| 927 | buffer1_off += c_x2_pr_len; |
| 928 | |
| 929 | if( client_input_first == 0 ) |
| 930 | { |
| 931 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 932 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 933 | buffer0 + s_g1_off, s_g1_len ); |
| 934 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 935 | { |
| 936 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 937 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 938 | } |
| 939 | else |
| 940 | { |
| 941 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 942 | } |
| 943 | |
| 944 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 945 | buffer0 + s_x1_pk_off, |
| 946 | s_x1_pk_len ); |
| 947 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 948 | { |
| 949 | TEST_EQUAL( status, expected_status ); |
| 950 | break; |
| 951 | } |
| 952 | else |
| 953 | { |
| 954 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 955 | } |
| 956 | |
| 957 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 958 | buffer0 + s_x1_pr_off, |
| 959 | s_x1_pr_len ); |
| 960 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 961 | { |
| 962 | TEST_EQUAL( status, expected_status ); |
| 963 | break; |
| 964 | } |
| 965 | else |
| 966 | { |
| 967 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 968 | } |
| 969 | |
| 970 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 971 | buffer0 + s_g2_off, |
| 972 | s_g2_len ); |
| 973 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 974 | { |
| 975 | TEST_EQUAL( status, expected_status ); |
| 976 | break; |
| 977 | } |
| 978 | else |
| 979 | { |
| 980 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 981 | } |
| 982 | |
| 983 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 984 | buffer0 + s_x2_pk_off, |
| 985 | s_x2_pk_len ); |
| 986 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 987 | { |
| 988 | TEST_EQUAL( status, expected_status ); |
| 989 | break; |
| 990 | } |
| 991 | else |
| 992 | { |
| 993 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 994 | } |
| 995 | |
| 996 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 997 | buffer0 + s_x2_pr_off, |
| 998 | s_x2_pr_len ); |
| 999 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 1000 | { |
| 1001 | TEST_EQUAL( status, expected_status ); |
| 1002 | break; |
| 1003 | } |
| 1004 | else |
| 1005 | { |
| 1006 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1007 | } |
| 1008 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1009 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1010 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1011 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | if( inject_error == 2 ) |
| 1015 | { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 1016 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 1017 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1018 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1019 | } |
| 1020 | |
| 1021 | /* Server first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1022 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1023 | buffer1 + c_g1_off, c_g1_len ); |
| 1024 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1025 | { |
| 1026 | TEST_EQUAL( status, expected_status ); |
| 1027 | break; |
| 1028 | } |
| 1029 | else |
| 1030 | { |
| 1031 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1032 | } |
| 1033 | |
| 1034 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1035 | buffer1 + c_x1_pk_off, c_x1_pk_len ); |
| 1036 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1037 | { |
| 1038 | TEST_EQUAL( status, expected_status ); |
| 1039 | break; |
| 1040 | } |
| 1041 | else |
| 1042 | { |
| 1043 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1044 | } |
| 1045 | |
| 1046 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1047 | buffer1 + c_x1_pr_off, c_x1_pr_len ); |
| 1048 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1049 | { |
| 1050 | TEST_EQUAL( status, expected_status ); |
| 1051 | break; |
| 1052 | } |
| 1053 | else |
| 1054 | { |
| 1055 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1056 | } |
| 1057 | |
| 1058 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1059 | buffer1 + c_g2_off, c_g2_len ); |
| 1060 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1061 | { |
| 1062 | TEST_EQUAL( status, expected_status ); |
| 1063 | break; |
| 1064 | } |
| 1065 | else |
| 1066 | { |
| 1067 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1068 | } |
| 1069 | |
| 1070 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1071 | buffer1 + c_x2_pk_off, c_x2_pk_len ); |
| 1072 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1073 | { |
| 1074 | TEST_EQUAL( status, expected_status ); |
| 1075 | break; |
| 1076 | } |
| 1077 | else |
| 1078 | { |
| 1079 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1080 | } |
| 1081 | |
| 1082 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1083 | buffer1 + c_x2_pr_off, c_x2_pr_len ); |
| 1084 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1085 | { |
| 1086 | TEST_EQUAL( status, expected_status ); |
| 1087 | break; |
| 1088 | } |
| 1089 | else |
| 1090 | { |
| 1091 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1092 | } |
| 1093 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1094 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1095 | if( inject_error == 2 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1096 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1097 | |
| 1098 | break; |
| 1099 | |
| 1100 | case 2: |
| 1101 | /* Server second round Output */ |
| 1102 | buffer0_off = 0; |
| 1103 | |
| 1104 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1105 | buffer0 + buffer0_off, |
| 1106 | 512 - buffer0_off, &s_a_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1107 | TEST_EQUAL( s_a_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1108 | s_a_off = buffer0_off; |
| 1109 | buffer0_off += s_a_len; |
| 1110 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1111 | buffer0 + buffer0_off, |
| 1112 | 512 - buffer0_off, &s_x2s_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1113 | TEST_EQUAL( s_x2s_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1114 | s_x2s_pk_off = buffer0_off; |
| 1115 | buffer0_off += s_x2s_pk_len; |
| 1116 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1117 | buffer0 + buffer0_off, |
| 1118 | 512 - buffer0_off, &s_x2s_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1119 | TEST_LE_U( s_x2s_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1120 | s_x2s_pr_off = buffer0_off; |
| 1121 | buffer0_off += s_x2s_pr_len; |
| 1122 | |
| 1123 | if( inject_error == 3 ) |
| 1124 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1125 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1126 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1127 | } |
| 1128 | |
| 1129 | if( client_input_first == 1 ) |
| 1130 | { |
| 1131 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1132 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1133 | buffer0 + s_a_off, s_a_len ); |
| 1134 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1135 | { |
| 1136 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1137 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1138 | } |
| 1139 | else |
| 1140 | { |
| 1141 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1142 | } |
| 1143 | |
| 1144 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1145 | buffer0 + s_x2s_pk_off, |
| 1146 | s_x2s_pk_len ); |
| 1147 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1148 | { |
| 1149 | TEST_EQUAL( status, expected_status ); |
| 1150 | break; |
| 1151 | } |
| 1152 | else |
| 1153 | { |
| 1154 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1155 | } |
| 1156 | |
| 1157 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1158 | buffer0 + s_x2s_pr_off, |
| 1159 | s_x2s_pr_len ); |
| 1160 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1161 | { |
| 1162 | TEST_EQUAL( status, expected_status ); |
| 1163 | break; |
| 1164 | } |
| 1165 | else |
| 1166 | { |
| 1167 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1168 | } |
| 1169 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1170 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1171 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1172 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /* Client second round Output */ |
| 1176 | buffer1_off = 0; |
| 1177 | |
| 1178 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1179 | buffer1 + buffer1_off, |
| 1180 | 512 - buffer1_off, &c_a_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1181 | TEST_EQUAL( c_a_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1182 | c_a_off = buffer1_off; |
| 1183 | buffer1_off += c_a_len; |
| 1184 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1185 | buffer1 + buffer1_off, |
| 1186 | 512 - buffer1_off, &c_x2s_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1187 | TEST_EQUAL( c_x2s_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1188 | c_x2s_pk_off = buffer1_off; |
| 1189 | buffer1_off += c_x2s_pk_len; |
| 1190 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1191 | buffer1 + buffer1_off, |
| 1192 | 512 - buffer1_off, &c_x2s_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1193 | TEST_LE_U( c_x2s_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1194 | c_x2s_pr_off = buffer1_off; |
| 1195 | buffer1_off += c_x2s_pr_len; |
| 1196 | |
| 1197 | if( client_input_first == 0 ) |
| 1198 | { |
| 1199 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1200 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1201 | buffer0 + s_a_off, s_a_len ); |
| 1202 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1203 | { |
| 1204 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1205 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1206 | } |
| 1207 | else |
| 1208 | { |
| 1209 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1210 | } |
| 1211 | |
| 1212 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1213 | buffer0 + s_x2s_pk_off, |
| 1214 | s_x2s_pk_len ); |
| 1215 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1216 | { |
| 1217 | TEST_EQUAL( status, expected_status ); |
| 1218 | break; |
| 1219 | } |
| 1220 | else |
| 1221 | { |
| 1222 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1223 | } |
| 1224 | |
| 1225 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1226 | buffer0 + s_x2s_pr_off, |
| 1227 | s_x2s_pr_len ); |
| 1228 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1229 | { |
| 1230 | TEST_EQUAL( status, expected_status ); |
| 1231 | break; |
| 1232 | } |
| 1233 | else |
| 1234 | { |
| 1235 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1236 | } |
| 1237 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1238 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1239 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1240 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | if( inject_error == 4 ) |
| 1244 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1245 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1246 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1247 | } |
| 1248 | |
| 1249 | /* Server second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1250 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1251 | buffer1 + c_a_off, c_a_len ); |
| 1252 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1253 | { |
| 1254 | TEST_EQUAL( status, expected_status ); |
| 1255 | break; |
| 1256 | } |
| 1257 | else |
| 1258 | { |
| 1259 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1260 | } |
| 1261 | |
| 1262 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1263 | buffer1 + c_x2s_pk_off, c_x2s_pk_len ); |
| 1264 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1265 | { |
| 1266 | TEST_EQUAL( status, expected_status ); |
| 1267 | break; |
| 1268 | } |
| 1269 | else |
| 1270 | { |
| 1271 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1272 | } |
| 1273 | |
| 1274 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1275 | buffer1 + c_x2s_pr_off, c_x2s_pr_len ); |
| 1276 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1277 | { |
| 1278 | TEST_EQUAL( status, expected_status ); |
| 1279 | break; |
| 1280 | } |
| 1281 | else |
| 1282 | { |
| 1283 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1284 | } |
| 1285 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1286 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1287 | if( inject_error == 4 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1288 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1289 | |
| 1290 | break; |
| 1291 | |
| 1292 | } |
| 1293 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1294 | exit: |
| 1295 | mbedtls_free( buffer0 ); |
| 1296 | mbedtls_free( buffer1 ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1297 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1298 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1299 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1300 | /* END_HEADER */ |
| 1301 | |
| 1302 | /* BEGIN_DEPENDENCIES |
| 1303 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1304 | * END_DEPENDENCIES |
| 1305 | */ |
| 1306 | |
| 1307 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1308 | void static_checks( ) |
| 1309 | { |
| 1310 | size_t max_truncated_mac_size = |
| 1311 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1312 | |
| 1313 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1314 | * encoding. The shifted mask is the maximum truncated value. The |
| 1315 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1316 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1317 | } |
| 1318 | /* END_CASE */ |
| 1319 | |
| 1320 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1321 | void import_with_policy( int type_arg, |
| 1322 | int usage_arg, int alg_arg, |
| 1323 | int expected_status_arg ) |
| 1324 | { |
| 1325 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1326 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1327 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1328 | psa_key_type_t type = type_arg; |
| 1329 | psa_key_usage_t usage = usage_arg; |
| 1330 | psa_algorithm_t alg = alg_arg; |
| 1331 | psa_status_t expected_status = expected_status_arg; |
| 1332 | const uint8_t key_material[16] = {0}; |
| 1333 | psa_status_t status; |
| 1334 | |
| 1335 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1336 | |
| 1337 | psa_set_key_type( &attributes, type ); |
| 1338 | psa_set_key_usage_flags( &attributes, usage ); |
| 1339 | psa_set_key_algorithm( &attributes, alg ); |
| 1340 | |
| 1341 | status = psa_import_key( &attributes, |
| 1342 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1343 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1344 | TEST_EQUAL( status, expected_status ); |
| 1345 | if( status != PSA_SUCCESS ) |
| 1346 | goto exit; |
| 1347 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1348 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1349 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1350 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1351 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1352 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1353 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1354 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1355 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1356 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1357 | |
| 1358 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1359 | /* |
| 1360 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1361 | * thus reset them as required. |
| 1362 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1363 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1364 | |
| 1365 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1366 | PSA_DONE( ); |
| 1367 | } |
| 1368 | /* END_CASE */ |
| 1369 | |
| 1370 | /* BEGIN_CASE */ |
| 1371 | void import_with_data( data_t *data, int type_arg, |
| 1372 | int attr_bits_arg, |
| 1373 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1374 | { |
| 1375 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1376 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1377 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1378 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1379 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1380 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1381 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1382 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1383 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1384 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1385 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1386 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1387 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1388 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1389 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1390 | if( status != PSA_SUCCESS ) |
| 1391 | goto exit; |
| 1392 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1393 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1394 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1395 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1396 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1397 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1398 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1399 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1400 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1401 | |
| 1402 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1403 | /* |
| 1404 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1405 | * thus reset them as required. |
| 1406 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1407 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1408 | |
| 1409 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1410 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1411 | } |
| 1412 | /* END_CASE */ |
| 1413 | |
| 1414 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1415 | void import_large_key( int type_arg, int byte_size_arg, |
| 1416 | int expected_status_arg ) |
| 1417 | { |
| 1418 | psa_key_type_t type = type_arg; |
| 1419 | size_t byte_size = byte_size_arg; |
| 1420 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1421 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1422 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1423 | psa_status_t status; |
| 1424 | uint8_t *buffer = NULL; |
| 1425 | size_t buffer_size = byte_size + 1; |
| 1426 | size_t n; |
| 1427 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1428 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1429 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1430 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1431 | memset( buffer, 'K', byte_size ); |
| 1432 | |
| 1433 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1434 | |
| 1435 | /* Try importing the key */ |
| 1436 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1437 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1438 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1439 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1440 | TEST_EQUAL( status, expected_status ); |
| 1441 | |
| 1442 | if( status == PSA_SUCCESS ) |
| 1443 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1444 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1445 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1446 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1447 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1448 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1449 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1450 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1451 | for( n = 0; n < byte_size; n++ ) |
| 1452 | TEST_EQUAL( buffer[n], 'K' ); |
| 1453 | for( n = byte_size; n < buffer_size; n++ ) |
| 1454 | TEST_EQUAL( buffer[n], 0 ); |
| 1455 | } |
| 1456 | |
| 1457 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1458 | /* |
| 1459 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1460 | * thus reset them as required. |
| 1461 | */ |
| 1462 | psa_reset_key_attributes( &attributes ); |
| 1463 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1464 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1465 | PSA_DONE( ); |
| 1466 | mbedtls_free( buffer ); |
| 1467 | } |
| 1468 | /* END_CASE */ |
| 1469 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1470 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1471 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1472 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1473 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1474 | size_t bits = bits_arg; |
| 1475 | psa_status_t expected_status = expected_status_arg; |
| 1476 | psa_status_t status; |
| 1477 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1478 | 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] | 1479 | size_t buffer_size = /* Slight overapproximations */ |
| 1480 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1481 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1482 | unsigned char *p; |
| 1483 | int ret; |
| 1484 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1485 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1486 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1487 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1488 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1489 | |
| 1490 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1491 | bits, keypair ) ) >= 0 ); |
| 1492 | length = ret; |
| 1493 | |
| 1494 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1495 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1496 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1497 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1498 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1499 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1500 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1501 | |
| 1502 | exit: |
| 1503 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1504 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1505 | } |
| 1506 | /* END_CASE */ |
| 1507 | |
| 1508 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1509 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1510 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1511 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1512 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1513 | int expected_bits, |
| 1514 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1515 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1516 | int canonical_input ) |
| 1517 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1518 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1519 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1520 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1521 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1522 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1523 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1524 | unsigned char *exported = NULL; |
| 1525 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1526 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1527 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1528 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1529 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1530 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1531 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1532 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1533 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1534 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1535 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1536 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1537 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1538 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1539 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1540 | psa_set_key_algorithm( &attributes, alg ); |
| 1541 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1542 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1543 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1544 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1545 | |
| 1546 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1547 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1548 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1549 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1550 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1551 | |
| 1552 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1553 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1554 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1555 | |
| 1556 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1557 | * and export_size. On errors, the exported length must be 0. */ |
| 1558 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1559 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1560 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1561 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1562 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1563 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1564 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1565 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1566 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1567 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1568 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1569 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1570 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1571 | * this validates the canonical representations. For canonical inputs, |
| 1572 | * this doesn't directly validate the implementation, but it still helps |
| 1573 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1574 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1575 | { |
| 1576 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1577 | goto exit; |
| 1578 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1579 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1580 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1581 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1582 | else |
| 1583 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1584 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1585 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1586 | &key2 ) ); |
| 1587 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1588 | reexported, |
| 1589 | export_size, |
| 1590 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1591 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1592 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1593 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1594 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1595 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1596 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1597 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1598 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1599 | |
| 1600 | destroy: |
| 1601 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1602 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1603 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1604 | |
| 1605 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1606 | /* |
| 1607 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1608 | * thus reset them as required. |
| 1609 | */ |
| 1610 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1611 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1612 | mbedtls_free( exported ); |
| 1613 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1614 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1615 | } |
| 1616 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1617 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1618 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1619 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1620 | int type_arg, |
| 1621 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1622 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1623 | int export_size_delta, |
| 1624 | int expected_export_status_arg, |
| 1625 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1626 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1627 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1628 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1629 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1630 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1631 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1632 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1633 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1634 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1635 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1636 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1637 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1638 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1639 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1640 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1641 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1642 | psa_set_key_algorithm( &attributes, alg ); |
| 1643 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1644 | |
| 1645 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1646 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1647 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1648 | /* Export the public key */ |
| 1649 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1650 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1651 | exported, export_size, |
| 1652 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1653 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1654 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1655 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1656 | 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] | 1657 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1658 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1659 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1660 | TEST_LE_U( expected_public_key->len, |
| 1661 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1662 | TEST_LE_U( expected_public_key->len, |
| 1663 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1664 | TEST_LE_U( expected_public_key->len, |
| 1665 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1666 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1667 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1668 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1669 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1670 | /* |
| 1671 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1672 | * thus reset them as required. |
| 1673 | */ |
| 1674 | psa_reset_key_attributes( &attributes ); |
| 1675 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1676 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1677 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1678 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1679 | } |
| 1680 | /* END_CASE */ |
| 1681 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1682 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1683 | void import_and_exercise_key( data_t *data, |
| 1684 | int type_arg, |
| 1685 | int bits_arg, |
| 1686 | int alg_arg ) |
| 1687 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1688 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1689 | psa_key_type_t type = type_arg; |
| 1690 | size_t bits = bits_arg; |
| 1691 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1692 | 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] | 1693 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1694 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1695 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1696 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1697 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1698 | psa_set_key_usage_flags( &attributes, usage ); |
| 1699 | psa_set_key_algorithm( &attributes, alg ); |
| 1700 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1701 | |
| 1702 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1703 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1704 | |
| 1705 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1706 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1707 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1708 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1709 | |
| 1710 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1711 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1712 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1713 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1714 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1715 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1716 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1717 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1718 | /* |
| 1719 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1720 | * thus reset them as required. |
| 1721 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1722 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1723 | |
| 1724 | psa_reset_key_attributes( &attributes ); |
| 1725 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1726 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1727 | } |
| 1728 | /* END_CASE */ |
| 1729 | |
| 1730 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1731 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1732 | int bits_arg, int expected_bits_arg, |
| 1733 | int usage_arg, int expected_usage_arg, |
| 1734 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1735 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1736 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1737 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1738 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1739 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1740 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1741 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1742 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1743 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1744 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1745 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1746 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1747 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1748 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1749 | psa_set_key_usage_flags( &attributes, usage ); |
| 1750 | psa_set_key_algorithm( &attributes, alg ); |
| 1751 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1752 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1753 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1754 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1755 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1756 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1757 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1758 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1759 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1760 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1761 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1762 | |
| 1763 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1764 | /* |
| 1765 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1766 | * thus reset them as required. |
| 1767 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1768 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1769 | |
| 1770 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1771 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1772 | } |
| 1773 | /* END_CASE */ |
| 1774 | |
| 1775 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1776 | void check_key_policy( int type_arg, int bits_arg, |
| 1777 | int usage_arg, int alg_arg ) |
| 1778 | { |
| 1779 | 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] | 1780 | usage_arg, |
| 1781 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1782 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1783 | goto exit; |
| 1784 | } |
| 1785 | /* END_CASE */ |
| 1786 | |
| 1787 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1788 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1789 | { |
| 1790 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1791 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1792 | * 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] | 1793 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1794 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1795 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1796 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1797 | |
| 1798 | memset( &zero, 0, sizeof( zero ) ); |
| 1799 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1800 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1801 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1802 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1803 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1804 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1805 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1806 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1807 | |
| 1808 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1809 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1810 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1811 | |
| 1812 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1813 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1814 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1815 | |
| 1816 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1817 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1818 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1819 | } |
| 1820 | /* END_CASE */ |
| 1821 | |
| 1822 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1823 | void mac_key_policy( int policy_usage_arg, |
| 1824 | int policy_alg_arg, |
| 1825 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1826 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1827 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1828 | int expected_status_sign_arg, |
| 1829 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1830 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1831 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1832 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1833 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1834 | psa_key_type_t key_type = key_type_arg; |
| 1835 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1836 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1837 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1838 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1839 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1840 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1841 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1842 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1843 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1844 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1845 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1846 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1847 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1848 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1849 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1850 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1851 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1852 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1853 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1854 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1855 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1856 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1857 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1858 | /* Calculate the MAC, one-shot case. */ |
| 1859 | uint8_t input[128] = {0}; |
| 1860 | size_t mac_len; |
| 1861 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1862 | input, 128, |
| 1863 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1864 | expected_status_sign ); |
| 1865 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1866 | /* Calculate the MAC, multi-part case. */ |
| 1867 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1868 | status = psa_mac_sign_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 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1874 | &mac_len ), |
| 1875 | expected_status_sign ); |
| 1876 | else |
| 1877 | TEST_EQUAL( status, expected_status_sign ); |
| 1878 | } |
| 1879 | else |
| 1880 | { |
| 1881 | TEST_EQUAL( status, expected_status_sign ); |
| 1882 | } |
| 1883 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1884 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1885 | /* Verify correct MAC, one-shot case. */ |
| 1886 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1887 | mac, mac_len ); |
| 1888 | |
| 1889 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1890 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1891 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1892 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1893 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1894 | /* Verify correct MAC, multi-part case. */ |
| 1895 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1896 | if( status == PSA_SUCCESS ) |
| 1897 | { |
| 1898 | status = psa_mac_update( &operation, input, 128 ); |
| 1899 | if( status == PSA_SUCCESS ) |
| 1900 | { |
| 1901 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1902 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1903 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1904 | else |
| 1905 | TEST_EQUAL( status, expected_status_verify ); |
| 1906 | } |
| 1907 | else |
| 1908 | { |
| 1909 | TEST_EQUAL( status, expected_status_verify ); |
| 1910 | } |
| 1911 | } |
| 1912 | else |
| 1913 | { |
| 1914 | TEST_EQUAL( status, expected_status_verify ); |
| 1915 | } |
| 1916 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1917 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1918 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1919 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1920 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1921 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1922 | |
| 1923 | exit: |
| 1924 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1925 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1926 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1927 | } |
| 1928 | /* END_CASE */ |
| 1929 | |
| 1930 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1931 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1932 | int policy_alg, |
| 1933 | int key_type, |
| 1934 | data_t *key_data, |
| 1935 | int exercise_alg ) |
| 1936 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1937 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1938 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1939 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1940 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1941 | size_t output_buffer_size = 0; |
| 1942 | size_t input_buffer_size = 0; |
| 1943 | size_t output_length = 0; |
| 1944 | uint8_t *output = NULL; |
| 1945 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1946 | psa_status_t status; |
| 1947 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1948 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1949 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1950 | input_buffer_size ); |
| 1951 | |
| 1952 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1953 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1954 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1955 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1956 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1957 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1958 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1959 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1960 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1961 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1962 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1963 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1964 | /* Check if no key usage flag implication is done */ |
| 1965 | TEST_EQUAL( policy_usage, |
| 1966 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1967 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1968 | /* Encrypt check, one-shot */ |
| 1969 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1970 | output, output_buffer_size, |
| 1971 | &output_length); |
| 1972 | if( policy_alg == exercise_alg && |
| 1973 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1974 | PSA_ASSERT( status ); |
| 1975 | else |
| 1976 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1977 | |
| 1978 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1979 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1980 | if( policy_alg == exercise_alg && |
| 1981 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1982 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1983 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1984 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1985 | psa_cipher_abort( &operation ); |
| 1986 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1987 | /* Decrypt check, one-shot */ |
| 1988 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 1989 | input, input_buffer_size, |
| 1990 | &output_length); |
| 1991 | if( policy_alg == exercise_alg && |
| 1992 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1993 | PSA_ASSERT( status ); |
| 1994 | else |
| 1995 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1996 | |
| 1997 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1998 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1999 | if( policy_alg == exercise_alg && |
| 2000 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2001 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2002 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2003 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2004 | |
| 2005 | exit: |
| 2006 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2007 | mbedtls_free( input ); |
| 2008 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2009 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2010 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2011 | } |
| 2012 | /* END_CASE */ |
| 2013 | |
| 2014 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2015 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2016 | int policy_alg, |
| 2017 | int key_type, |
| 2018 | data_t *key_data, |
| 2019 | int nonce_length_arg, |
| 2020 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2021 | int exercise_alg, |
| 2022 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2023 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2024 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2025 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2026 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2027 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2028 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2029 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2030 | unsigned char nonce[16] = {0}; |
| 2031 | size_t nonce_length = nonce_length_arg; |
| 2032 | unsigned char tag[16]; |
| 2033 | size_t tag_length = tag_length_arg; |
| 2034 | size_t output_length; |
| 2035 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2036 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 2037 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2038 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2039 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2040 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2041 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2042 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2043 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2044 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2045 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2046 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2047 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2048 | /* Check if no key usage implication is done */ |
| 2049 | TEST_EQUAL( policy_usage, |
| 2050 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2051 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2052 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2053 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2054 | nonce, nonce_length, |
| 2055 | NULL, 0, |
| 2056 | NULL, 0, |
| 2057 | tag, tag_length, |
| 2058 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2059 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2060 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2061 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2062 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2063 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2064 | /* Encrypt check, multi-part */ |
| 2065 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 2066 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2067 | TEST_EQUAL( status, expected_status ); |
| 2068 | else |
| 2069 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2070 | |
| 2071 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2072 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2073 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2074 | nonce, nonce_length, |
| 2075 | NULL, 0, |
| 2076 | tag, tag_length, |
| 2077 | NULL, 0, |
| 2078 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2079 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2080 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2081 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2082 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2083 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2084 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2085 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2086 | /* Decrypt check, multi-part */ |
| 2087 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 2088 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 2089 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2090 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2091 | else |
| 2092 | TEST_EQUAL( status, expected_status ); |
| 2093 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2094 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2095 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2096 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2097 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2098 | } |
| 2099 | /* END_CASE */ |
| 2100 | |
| 2101 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2102 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2103 | int policy_alg, |
| 2104 | int key_type, |
| 2105 | data_t *key_data, |
| 2106 | int exercise_alg ) |
| 2107 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2108 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2109 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2110 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2111 | psa_status_t status; |
| 2112 | size_t key_bits; |
| 2113 | size_t buffer_length; |
| 2114 | unsigned char *buffer = NULL; |
| 2115 | size_t output_length; |
| 2116 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2117 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2118 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2119 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2120 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2121 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2122 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2123 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2124 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2125 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2126 | /* Check if no key usage implication is done */ |
| 2127 | TEST_EQUAL( policy_usage, |
| 2128 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2129 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2130 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2131 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2132 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2133 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2134 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2135 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2136 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2137 | NULL, 0, |
| 2138 | NULL, 0, |
| 2139 | buffer, buffer_length, |
| 2140 | &output_length ); |
| 2141 | if( policy_alg == exercise_alg && |
| 2142 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2143 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2144 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2145 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2146 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2147 | if( buffer_length != 0 ) |
| 2148 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2149 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2150 | buffer, buffer_length, |
| 2151 | NULL, 0, |
| 2152 | buffer, buffer_length, |
| 2153 | &output_length ); |
| 2154 | if( policy_alg == exercise_alg && |
| 2155 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2156 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2157 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2158 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2159 | |
| 2160 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2161 | /* |
| 2162 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2163 | * thus reset them as required. |
| 2164 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2165 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2166 | |
| 2167 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2168 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2169 | mbedtls_free( buffer ); |
| 2170 | } |
| 2171 | /* END_CASE */ |
| 2172 | |
| 2173 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2174 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2175 | int policy_alg, |
| 2176 | int key_type, |
| 2177 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2178 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2179 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2180 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2181 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2182 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2183 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2184 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2185 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2186 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2187 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2188 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2189 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2190 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2191 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2192 | int compatible_alg = payload_length_arg > 0; |
| 2193 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2194 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2195 | size_t signature_length; |
| 2196 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2197 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2198 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2199 | TEST_EQUAL( expected_usage, |
| 2200 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2201 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2202 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2203 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2204 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2205 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2206 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2207 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2208 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2209 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2210 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2211 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 2212 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2213 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2214 | payload, payload_length, |
| 2215 | signature, sizeof( signature ), |
| 2216 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2217 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2218 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2219 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2220 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2221 | |
| 2222 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2223 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2224 | payload, payload_length, |
| 2225 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2226 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2227 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2228 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2229 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2230 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2231 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 2232 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2233 | { |
| 2234 | status = psa_sign_message( key, exercise_alg, |
| 2235 | payload, payload_length, |
| 2236 | signature, sizeof( signature ), |
| 2237 | &signature_length ); |
| 2238 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 2239 | PSA_ASSERT( status ); |
| 2240 | else |
| 2241 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2242 | |
| 2243 | memset( signature, 0, sizeof( signature ) ); |
| 2244 | status = psa_verify_message( key, exercise_alg, |
| 2245 | payload, payload_length, |
| 2246 | signature, sizeof( signature ) ); |
| 2247 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 2248 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 2249 | else |
| 2250 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2251 | } |
| 2252 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2253 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2254 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2255 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2256 | } |
| 2257 | /* END_CASE */ |
| 2258 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2259 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2260 | void derive_key_policy( int policy_usage, |
| 2261 | int policy_alg, |
| 2262 | int key_type, |
| 2263 | data_t *key_data, |
| 2264 | int exercise_alg ) |
| 2265 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2266 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2267 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2268 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2269 | psa_status_t status; |
| 2270 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2271 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2272 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2273 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2274 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2275 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2276 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2277 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2278 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2279 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2280 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2281 | |
| 2282 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2283 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2284 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2285 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2286 | &operation, |
| 2287 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2288 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2289 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2290 | |
| 2291 | status = psa_key_derivation_input_key( &operation, |
| 2292 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2293 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2294 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2295 | if( policy_alg == exercise_alg && |
| 2296 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2297 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2298 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2299 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2300 | |
| 2301 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2302 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2303 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2304 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2305 | } |
| 2306 | /* END_CASE */ |
| 2307 | |
| 2308 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2309 | void agreement_key_policy( int policy_usage, |
| 2310 | int policy_alg, |
| 2311 | int key_type_arg, |
| 2312 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2313 | int exercise_alg, |
| 2314 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2315 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2316 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2317 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2318 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2319 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2320 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2321 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2322 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2323 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2324 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2325 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2326 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2327 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2328 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2329 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2330 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2331 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2332 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2333 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2334 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2335 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2336 | |
| 2337 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2338 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2339 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2340 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2341 | } |
| 2342 | /* END_CASE */ |
| 2343 | |
| 2344 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2345 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2346 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2347 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2348 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2349 | psa_key_type_t key_type = key_type_arg; |
| 2350 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2351 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2352 | psa_key_usage_t usage = usage_arg; |
| 2353 | psa_algorithm_t alg = alg_arg; |
| 2354 | psa_algorithm_t alg2 = alg2_arg; |
| 2355 | |
| 2356 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2357 | |
| 2358 | psa_set_key_usage_flags( &attributes, usage ); |
| 2359 | psa_set_key_algorithm( &attributes, alg ); |
| 2360 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2361 | psa_set_key_type( &attributes, key_type ); |
| 2362 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2363 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2364 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2365 | /* Update the usage flags to obtain implicit usage flags */ |
| 2366 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2367 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2368 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2369 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2370 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2371 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2372 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2373 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2374 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2375 | goto exit; |
| 2376 | |
| 2377 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2378 | /* |
| 2379 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2380 | * thus reset them as required. |
| 2381 | */ |
| 2382 | psa_reset_key_attributes( &got_attributes ); |
| 2383 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2384 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2385 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2386 | } |
| 2387 | /* END_CASE */ |
| 2388 | |
| 2389 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2390 | void raw_agreement_key_policy( int policy_usage, |
| 2391 | int policy_alg, |
| 2392 | int key_type_arg, |
| 2393 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2394 | int exercise_alg, |
| 2395 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2396 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2397 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2398 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2399 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2400 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2401 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2402 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2403 | |
| 2404 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2405 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2406 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2407 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2408 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2409 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2410 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2411 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2412 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2413 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2414 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2415 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2416 | |
| 2417 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2418 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2419 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2420 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2421 | } |
| 2422 | /* END_CASE */ |
| 2423 | |
| 2424 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2425 | void copy_success( int source_usage_arg, |
| 2426 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2427 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2428 | int type_arg, data_t *material, |
| 2429 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2430 | int target_usage_arg, |
| 2431 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2432 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2433 | int expected_usage_arg, |
| 2434 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2435 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2436 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2437 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2438 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2439 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2440 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2441 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2442 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2443 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2444 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2445 | uint8_t *export_buffer = NULL; |
| 2446 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2447 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2448 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2449 | /* Prepare the source key. */ |
| 2450 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2451 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2452 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2453 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2454 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2455 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2456 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2457 | &source_key ) ); |
| 2458 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2459 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2460 | /* Prepare the target attributes. */ |
| 2461 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2462 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2463 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2464 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2465 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2466 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2467 | if( target_usage_arg != -1 ) |
| 2468 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2469 | if( target_alg_arg != -1 ) |
| 2470 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2471 | if( target_alg2_arg != -1 ) |
| 2472 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2473 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2474 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2475 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2476 | PSA_ASSERT( psa_copy_key( source_key, |
| 2477 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2478 | |
| 2479 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2480 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2481 | |
| 2482 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2483 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2484 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2485 | psa_get_key_type( &target_attributes ) ); |
| 2486 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2487 | psa_get_key_bits( &target_attributes ) ); |
| 2488 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2489 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2490 | TEST_EQUAL( expected_alg2, |
| 2491 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2492 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2493 | { |
| 2494 | size_t length; |
| 2495 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2496 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2497 | material->len, &length ) ); |
| 2498 | ASSERT_COMPARE( material->x, material->len, |
| 2499 | export_buffer, length ); |
| 2500 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2501 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2502 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2503 | { |
| 2504 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2505 | goto exit; |
| 2506 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2507 | goto exit; |
| 2508 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2509 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2510 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2511 | |
| 2512 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2513 | /* |
| 2514 | * Source and target key attributes may have been returned by |
| 2515 | * psa_get_key_attributes() thus reset them as required. |
| 2516 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2517 | psa_reset_key_attributes( &source_attributes ); |
| 2518 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2519 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2520 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2521 | mbedtls_free( export_buffer ); |
| 2522 | } |
| 2523 | /* END_CASE */ |
| 2524 | |
| 2525 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2526 | void copy_fail( int source_usage_arg, |
| 2527 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2528 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2529 | int type_arg, data_t *material, |
| 2530 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2531 | int target_usage_arg, |
| 2532 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2533 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2534 | int expected_status_arg ) |
| 2535 | { |
| 2536 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2537 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2538 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2539 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2540 | 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] | 2541 | |
| 2542 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2543 | |
| 2544 | /* Prepare the source key. */ |
| 2545 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2546 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2547 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2548 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2549 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2550 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2551 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2552 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2553 | |
| 2554 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2555 | psa_set_key_id( &target_attributes, key_id ); |
| 2556 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2557 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2558 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2559 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2560 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2561 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2562 | |
| 2563 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2564 | TEST_EQUAL( psa_copy_key( source_key, |
| 2565 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2566 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2567 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2568 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2569 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2570 | exit: |
| 2571 | psa_reset_key_attributes( &source_attributes ); |
| 2572 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2573 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2574 | } |
| 2575 | /* END_CASE */ |
| 2576 | |
| 2577 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2578 | void hash_operation_init( ) |
| 2579 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2580 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2581 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2582 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2583 | * 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] | 2584 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2585 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2586 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2587 | psa_hash_operation_t zero; |
| 2588 | |
| 2589 | memset( &zero, 0, sizeof( zero ) ); |
| 2590 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2591 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2592 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2593 | PSA_ERROR_BAD_STATE ); |
| 2594 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2595 | PSA_ERROR_BAD_STATE ); |
| 2596 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2597 | PSA_ERROR_BAD_STATE ); |
| 2598 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2599 | /* A default hash operation should be abortable without error. */ |
| 2600 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2601 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2602 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2603 | } |
| 2604 | /* END_CASE */ |
| 2605 | |
| 2606 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2607 | void hash_setup( int alg_arg, |
| 2608 | int expected_status_arg ) |
| 2609 | { |
| 2610 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2611 | uint8_t *output = NULL; |
| 2612 | size_t output_size = 0; |
| 2613 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2614 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2615 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2616 | psa_status_t status; |
| 2617 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2618 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2619 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2620 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2621 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2622 | ASSERT_ALLOC( output, output_size ); |
| 2623 | |
| 2624 | status = psa_hash_compute( alg, NULL, 0, |
| 2625 | output, output_size, &output_length ); |
| 2626 | TEST_EQUAL( status, expected_status ); |
| 2627 | |
| 2628 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2629 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2630 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2631 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2632 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2633 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2634 | |
| 2635 | /* If setup failed, reproduce the failure, so as to |
| 2636 | * test the resulting state of the operation object. */ |
| 2637 | if( status != PSA_SUCCESS ) |
| 2638 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2639 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2640 | /* Now the operation object should be reusable. */ |
| 2641 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2642 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2643 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2644 | #endif |
| 2645 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2646 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2647 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2648 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2649 | } |
| 2650 | /* END_CASE */ |
| 2651 | |
| 2652 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2653 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2654 | int output_size_arg, int expected_status_arg ) |
| 2655 | { |
| 2656 | psa_algorithm_t alg = alg_arg; |
| 2657 | uint8_t *output = NULL; |
| 2658 | size_t output_size = output_size_arg; |
| 2659 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2660 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2661 | psa_status_t expected_status = expected_status_arg; |
| 2662 | psa_status_t status; |
| 2663 | |
| 2664 | ASSERT_ALLOC( output, output_size ); |
| 2665 | |
| 2666 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2667 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2668 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2669 | status = psa_hash_compute( alg, input->x, input->len, |
| 2670 | output, output_size, &output_length ); |
| 2671 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2672 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2673 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2674 | /* Hash Compute, multi-part */ |
| 2675 | status = psa_hash_setup( &operation, alg ); |
| 2676 | if( status == PSA_SUCCESS ) |
| 2677 | { |
| 2678 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2679 | if( status == PSA_SUCCESS ) |
| 2680 | { |
| 2681 | status = psa_hash_finish( &operation, output, output_size, |
| 2682 | &output_length ); |
| 2683 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2684 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2685 | else |
| 2686 | TEST_EQUAL( status, expected_status ); |
| 2687 | } |
| 2688 | else |
| 2689 | { |
| 2690 | TEST_EQUAL( status, expected_status ); |
| 2691 | } |
| 2692 | } |
| 2693 | else |
| 2694 | { |
| 2695 | TEST_EQUAL( status, expected_status ); |
| 2696 | } |
| 2697 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2698 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2699 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2700 | mbedtls_free( output ); |
| 2701 | PSA_DONE( ); |
| 2702 | } |
| 2703 | /* END_CASE */ |
| 2704 | |
| 2705 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2706 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2707 | data_t *reference_hash, |
| 2708 | int expected_status_arg ) |
| 2709 | { |
| 2710 | psa_algorithm_t alg = alg_arg; |
| 2711 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2712 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2713 | psa_status_t status; |
| 2714 | |
| 2715 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2716 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2717 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2718 | status = psa_hash_compare( alg, input->x, input->len, |
| 2719 | reference_hash->x, reference_hash->len ); |
| 2720 | TEST_EQUAL( status, expected_status ); |
| 2721 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2722 | /* Hash Compare, multi-part */ |
| 2723 | status = psa_hash_setup( &operation, alg ); |
| 2724 | if( status == PSA_SUCCESS ) |
| 2725 | { |
| 2726 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2727 | if( status == PSA_SUCCESS ) |
| 2728 | { |
| 2729 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2730 | reference_hash->len ); |
| 2731 | TEST_EQUAL( status, expected_status ); |
| 2732 | } |
| 2733 | else |
| 2734 | { |
| 2735 | TEST_EQUAL( status, expected_status ); |
| 2736 | } |
| 2737 | } |
| 2738 | else |
| 2739 | { |
| 2740 | TEST_EQUAL( status, expected_status ); |
| 2741 | } |
| 2742 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2743 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2744 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2745 | PSA_DONE( ); |
| 2746 | } |
| 2747 | /* END_CASE */ |
| 2748 | |
| 2749 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2750 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2751 | data_t *expected_output ) |
| 2752 | { |
| 2753 | psa_algorithm_t alg = alg_arg; |
| 2754 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2755 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2756 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2757 | size_t i; |
| 2758 | |
| 2759 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2760 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2761 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2762 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2763 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2764 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2765 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2766 | ASSERT_COMPARE( output, output_length, |
| 2767 | expected_output->x, expected_output->len ); |
| 2768 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2769 | /* Compute with tight buffer, multi-part */ |
| 2770 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2771 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2772 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2773 | PSA_HASH_LENGTH( alg ), |
| 2774 | &output_length ) ); |
| 2775 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2776 | ASSERT_COMPARE( output, output_length, |
| 2777 | expected_output->x, expected_output->len ); |
| 2778 | |
| 2779 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2780 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2781 | output, sizeof( output ), |
| 2782 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2783 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2784 | ASSERT_COMPARE( output, output_length, |
| 2785 | expected_output->x, expected_output->len ); |
| 2786 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2787 | /* Compute with larger buffer, multi-part */ |
| 2788 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2789 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2790 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2791 | sizeof( output ), &output_length ) ); |
| 2792 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2793 | ASSERT_COMPARE( output, output_length, |
| 2794 | expected_output->x, expected_output->len ); |
| 2795 | |
| 2796 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2797 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2798 | output, output_length ) ); |
| 2799 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2800 | /* Compare with correct hash, multi-part */ |
| 2801 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2802 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2803 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2804 | output_length ) ); |
| 2805 | |
| 2806 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2807 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2808 | output, output_length + 1 ), |
| 2809 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2810 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2811 | /* Compare with trailing garbage, multi-part */ |
| 2812 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2813 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2814 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2815 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2816 | |
| 2817 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2818 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2819 | output, output_length - 1 ), |
| 2820 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2821 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2822 | /* Compare with truncated hash, multi-part */ |
| 2823 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2824 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2825 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2826 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2827 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2828 | /* Compare with corrupted value */ |
| 2829 | for( i = 0; i < output_length; i++ ) |
| 2830 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2831 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2832 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2833 | |
| 2834 | /* One-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2835 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2836 | output, output_length ), |
| 2837 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2838 | |
| 2839 | /* Multi-Part */ |
| 2840 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2841 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2842 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length ), |
| 2843 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2844 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2845 | output[i] ^= 1; |
| 2846 | } |
| 2847 | |
| 2848 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2849 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2850 | PSA_DONE( ); |
| 2851 | } |
| 2852 | /* END_CASE */ |
| 2853 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2854 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2855 | void hash_bad_order( ) |
| 2856 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2857 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2858 | unsigned char input[] = ""; |
| 2859 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2860 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2861 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2862 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2863 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2864 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2865 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2866 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2867 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2868 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2869 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2870 | /* Call setup twice in a row. */ |
| 2871 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2872 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2873 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2874 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2875 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2876 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2877 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2878 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2879 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2880 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2881 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2882 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2883 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2884 | /* Check that update calls abort on error. */ |
| 2885 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2886 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2887 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2888 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2889 | PSA_ERROR_BAD_STATE ); |
| 2890 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2891 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2892 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2893 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2894 | /* Call update after finish. */ |
| 2895 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2896 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2897 | hash, sizeof( hash ), &hash_len ) ); |
| 2898 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2899 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2900 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2901 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2902 | /* Call verify without calling setup beforehand. */ |
| 2903 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2904 | valid_hash, sizeof( valid_hash ) ), |
| 2905 | PSA_ERROR_BAD_STATE ); |
| 2906 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2907 | |
| 2908 | /* Call verify after finish. */ |
| 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_verify( &operation, |
| 2913 | valid_hash, sizeof( valid_hash ) ), |
| 2914 | PSA_ERROR_BAD_STATE ); |
| 2915 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2916 | |
| 2917 | /* Call verify twice in a row. */ |
| 2918 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2919 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2920 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2921 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2922 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2923 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2924 | valid_hash, sizeof( valid_hash ) ), |
| 2925 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2926 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2927 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2928 | |
| 2929 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2930 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2931 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2932 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2933 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2934 | |
| 2935 | /* Call finish twice in a row. */ |
| 2936 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2937 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2938 | hash, sizeof( hash ), &hash_len ) ); |
| 2939 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2940 | hash, sizeof( hash ), &hash_len ), |
| 2941 | PSA_ERROR_BAD_STATE ); |
| 2942 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2943 | |
| 2944 | /* Call finish after calling verify. */ |
| 2945 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2946 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2947 | valid_hash, sizeof( valid_hash ) ) ); |
| 2948 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2949 | hash, sizeof( hash ), &hash_len ), |
| 2950 | PSA_ERROR_BAD_STATE ); |
| 2951 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2952 | |
| 2953 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2954 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2955 | } |
| 2956 | /* END_CASE */ |
| 2957 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2958 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2959 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2960 | { |
| 2961 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2962 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2963 | * appended to it */ |
| 2964 | unsigned char hash[] = { |
| 2965 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2966 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2967 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2968 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2969 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2970 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2971 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2972 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2973 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2974 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2975 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2976 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2977 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2978 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2979 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2980 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2981 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2982 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2983 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2984 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2985 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2986 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2987 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2988 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2989 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2990 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2991 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2992 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2993 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2994 | } |
| 2995 | /* END_CASE */ |
| 2996 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2997 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2998 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2999 | { |
| 3000 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3001 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3002 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3003 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3004 | size_t hash_len; |
| 3005 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3006 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3007 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3008 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3009 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3010 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3011 | hash, expected_size - 1, &hash_len ), |
| 3012 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3013 | |
| 3014 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3015 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3016 | } |
| 3017 | /* END_CASE */ |
| 3018 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3019 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3020 | void hash_clone_source_state( ) |
| 3021 | { |
| 3022 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3023 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3024 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3025 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3026 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3027 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3028 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3029 | size_t hash_len; |
| 3030 | |
| 3031 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3032 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 3033 | |
| 3034 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3035 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3036 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3037 | hash, sizeof( hash ), &hash_len ) ); |
| 3038 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3039 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3040 | |
| 3041 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 3042 | PSA_ERROR_BAD_STATE ); |
| 3043 | |
| 3044 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 3045 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 3046 | hash, sizeof( hash ), &hash_len ) ); |
| 3047 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 3048 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3049 | hash, sizeof( hash ), &hash_len ) ); |
| 3050 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 3051 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 3052 | hash, sizeof( hash ), &hash_len ) ); |
| 3053 | |
| 3054 | exit: |
| 3055 | psa_hash_abort( &op_source ); |
| 3056 | psa_hash_abort( &op_init ); |
| 3057 | psa_hash_abort( &op_setup ); |
| 3058 | psa_hash_abort( &op_finished ); |
| 3059 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3060 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3061 | } |
| 3062 | /* END_CASE */ |
| 3063 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3064 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3065 | void hash_clone_target_state( ) |
| 3066 | { |
| 3067 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3068 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3069 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3070 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3071 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3072 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3073 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3074 | size_t hash_len; |
| 3075 | |
| 3076 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3077 | |
| 3078 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3079 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3080 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3081 | hash, sizeof( hash ), &hash_len ) ); |
| 3082 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3083 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3084 | |
| 3085 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 3086 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 3087 | hash, sizeof( hash ), &hash_len ) ); |
| 3088 | |
| 3089 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 3090 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 3091 | PSA_ERROR_BAD_STATE ); |
| 3092 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 3093 | PSA_ERROR_BAD_STATE ); |
| 3094 | |
| 3095 | exit: |
| 3096 | psa_hash_abort( &op_target ); |
| 3097 | psa_hash_abort( &op_init ); |
| 3098 | psa_hash_abort( &op_setup ); |
| 3099 | psa_hash_abort( &op_finished ); |
| 3100 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3101 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3102 | } |
| 3103 | /* END_CASE */ |
| 3104 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3105 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3106 | void mac_operation_init( ) |
| 3107 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3108 | const uint8_t input[1] = { 0 }; |
| 3109 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3110 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3111 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3112 | * 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] | 3113 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3114 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 3115 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3116 | psa_mac_operation_t zero; |
| 3117 | |
| 3118 | memset( &zero, 0, sizeof( zero ) ); |
| 3119 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3120 | /* A freshly-initialized MAC operation should not be usable. */ |
| 3121 | TEST_EQUAL( psa_mac_update( &func, |
| 3122 | input, sizeof( input ) ), |
| 3123 | PSA_ERROR_BAD_STATE ); |
| 3124 | TEST_EQUAL( psa_mac_update( &init, |
| 3125 | input, sizeof( input ) ), |
| 3126 | PSA_ERROR_BAD_STATE ); |
| 3127 | TEST_EQUAL( psa_mac_update( &zero, |
| 3128 | input, sizeof( input ) ), |
| 3129 | PSA_ERROR_BAD_STATE ); |
| 3130 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3131 | /* A default MAC operation should be abortable without error. */ |
| 3132 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 3133 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 3134 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3135 | } |
| 3136 | /* END_CASE */ |
| 3137 | |
| 3138 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3139 | void mac_setup( int key_type_arg, |
| 3140 | data_t *key, |
| 3141 | int alg_arg, |
| 3142 | int expected_status_arg ) |
| 3143 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3144 | psa_key_type_t key_type = key_type_arg; |
| 3145 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3146 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3147 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3148 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3149 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3150 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3151 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3152 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3153 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3154 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3155 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 3156 | &operation, &status ) ) |
| 3157 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3158 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3159 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3160 | /* The operation object should be reusable. */ |
| 3161 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3162 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3163 | smoke_test_key_data, |
| 3164 | sizeof( smoke_test_key_data ), |
| 3165 | KNOWN_SUPPORTED_MAC_ALG, |
| 3166 | &operation, &status ) ) |
| 3167 | goto exit; |
| 3168 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3169 | #endif |
| 3170 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3171 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3172 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3173 | } |
| 3174 | /* END_CASE */ |
| 3175 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3176 | /* 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] | 3177 | void mac_bad_order( ) |
| 3178 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3179 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3180 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3181 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3182 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3183 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3184 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3185 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3186 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3187 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3188 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3189 | size_t sign_mac_length = 0; |
| 3190 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3191 | const uint8_t verify_mac[] = { |
| 3192 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3193 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 3194 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 3195 | |
| 3196 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3197 | 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] | 3198 | psa_set_key_algorithm( &attributes, alg ); |
| 3199 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3200 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3201 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3202 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3203 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3204 | /* Call update without calling setup beforehand. */ |
| 3205 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3206 | PSA_ERROR_BAD_STATE ); |
| 3207 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3208 | |
| 3209 | /* Call sign finish without calling setup beforehand. */ |
| 3210 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 3211 | &sign_mac_length), |
| 3212 | PSA_ERROR_BAD_STATE ); |
| 3213 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3214 | |
| 3215 | /* Call verify finish without calling setup beforehand. */ |
| 3216 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3217 | verify_mac, sizeof( verify_mac ) ), |
| 3218 | PSA_ERROR_BAD_STATE ); |
| 3219 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3220 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3221 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3222 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3223 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3224 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3225 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3226 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3227 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3228 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3229 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3230 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3231 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3232 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3233 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3234 | sign_mac, sizeof( sign_mac ), |
| 3235 | &sign_mac_length ) ); |
| 3236 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3237 | PSA_ERROR_BAD_STATE ); |
| 3238 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3239 | |
| 3240 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3241 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3242 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3243 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3244 | verify_mac, sizeof( verify_mac ) ) ); |
| 3245 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3246 | PSA_ERROR_BAD_STATE ); |
| 3247 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3248 | |
| 3249 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3250 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3251 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3252 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3253 | sign_mac, sizeof( sign_mac ), |
| 3254 | &sign_mac_length ) ); |
| 3255 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3256 | sign_mac, sizeof( sign_mac ), |
| 3257 | &sign_mac_length ), |
| 3258 | PSA_ERROR_BAD_STATE ); |
| 3259 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3260 | |
| 3261 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3262 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3263 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3264 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3265 | verify_mac, sizeof( verify_mac ) ) ); |
| 3266 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3267 | verify_mac, sizeof( verify_mac ) ), |
| 3268 | PSA_ERROR_BAD_STATE ); |
| 3269 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3270 | |
| 3271 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3272 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3273 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3274 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3275 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3276 | verify_mac, sizeof( verify_mac ) ), |
| 3277 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3278 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3279 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3280 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3281 | |
| 3282 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3283 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3284 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3285 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3286 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3287 | sign_mac, sizeof( sign_mac ), |
| 3288 | &sign_mac_length ), |
| 3289 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3290 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3291 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3292 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3293 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3294 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3295 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3296 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3297 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3298 | } |
| 3299 | /* END_CASE */ |
| 3300 | |
| 3301 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3302 | void mac_sign_verify_multi( int key_type_arg, |
| 3303 | data_t *key_data, |
| 3304 | int alg_arg, |
| 3305 | data_t *input, |
| 3306 | int is_verify, |
| 3307 | data_t *expected_mac ) |
| 3308 | { |
| 3309 | size_t data_part_len = 0; |
| 3310 | |
| 3311 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3312 | { |
| 3313 | /* Split data into length(data_part_len) parts. */ |
| 3314 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3315 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3316 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3317 | alg_arg, |
| 3318 | input, data_part_len, |
| 3319 | expected_mac, |
| 3320 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3321 | break; |
| 3322 | |
| 3323 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3324 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3325 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3326 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3327 | alg_arg, |
| 3328 | input, data_part_len, |
| 3329 | expected_mac, |
| 3330 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3331 | break; |
| 3332 | } |
| 3333 | |
| 3334 | /* Goto is required to silence warnings about unused labels, as we |
| 3335 | * don't actually do any test assertions in this function. */ |
| 3336 | goto exit; |
| 3337 | } |
| 3338 | /* END_CASE */ |
| 3339 | |
| 3340 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3341 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3342 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3343 | int alg_arg, |
| 3344 | data_t *input, |
| 3345 | data_t *expected_mac ) |
| 3346 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3347 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3348 | psa_key_type_t key_type = key_type_arg; |
| 3349 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3350 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3351 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3352 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3353 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3354 | 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] | 3355 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3356 | const size_t output_sizes_to_test[] = { |
| 3357 | 0, |
| 3358 | 1, |
| 3359 | expected_mac->len - 1, |
| 3360 | expected_mac->len, |
| 3361 | expected_mac->len + 1, |
| 3362 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3363 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3364 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3365 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3366 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3367 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3368 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3369 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3370 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3371 | psa_set_key_algorithm( &attributes, alg ); |
| 3372 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3373 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3374 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3375 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3376 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3377 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3378 | { |
| 3379 | const size_t output_size = output_sizes_to_test[i]; |
| 3380 | psa_status_t expected_status = |
| 3381 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3382 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3383 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3384 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3385 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3386 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3387 | /* Calculate the MAC, one-shot case. */ |
| 3388 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3389 | input->x, input->len, |
| 3390 | actual_mac, output_size, &mac_length ), |
| 3391 | expected_status ); |
| 3392 | if( expected_status == PSA_SUCCESS ) |
| 3393 | { |
| 3394 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3395 | actual_mac, mac_length ); |
| 3396 | } |
| 3397 | |
| 3398 | if( output_size > 0 ) |
| 3399 | memset( actual_mac, 0, output_size ); |
| 3400 | |
| 3401 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3402 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3403 | PSA_ASSERT( psa_mac_update( &operation, |
| 3404 | input->x, input->len ) ); |
| 3405 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3406 | actual_mac, output_size, |
| 3407 | &mac_length ), |
| 3408 | expected_status ); |
| 3409 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3410 | |
| 3411 | if( expected_status == PSA_SUCCESS ) |
| 3412 | { |
| 3413 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3414 | actual_mac, mac_length ); |
| 3415 | } |
| 3416 | mbedtls_free( actual_mac ); |
| 3417 | actual_mac = NULL; |
| 3418 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3419 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3420 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3421 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3422 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3423 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3424 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3425 | } |
| 3426 | /* END_CASE */ |
| 3427 | |
| 3428 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3429 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3430 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3431 | int alg_arg, |
| 3432 | data_t *input, |
| 3433 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3434 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3435 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3436 | psa_key_type_t key_type = key_type_arg; |
| 3437 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3438 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3439 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3440 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3441 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3442 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3443 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3444 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3445 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3446 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3447 | psa_set_key_algorithm( &attributes, alg ); |
| 3448 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3449 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3450 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3451 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3452 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3453 | /* Verify correct MAC, one-shot case. */ |
| 3454 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3455 | expected_mac->x, expected_mac->len ) ); |
| 3456 | |
| 3457 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3458 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3459 | PSA_ASSERT( psa_mac_update( &operation, |
| 3460 | input->x, input->len ) ); |
| 3461 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3462 | expected_mac->x, |
| 3463 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3464 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3465 | /* Test a MAC that's too short, one-shot case. */ |
| 3466 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3467 | input->x, input->len, |
| 3468 | expected_mac->x, |
| 3469 | expected_mac->len - 1 ), |
| 3470 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3471 | |
| 3472 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3473 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3474 | PSA_ASSERT( psa_mac_update( &operation, |
| 3475 | input->x, input->len ) ); |
| 3476 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3477 | expected_mac->x, |
| 3478 | expected_mac->len - 1 ), |
| 3479 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3480 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3481 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3482 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3483 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3484 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3485 | input->x, input->len, |
| 3486 | perturbed_mac, expected_mac->len + 1 ), |
| 3487 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3488 | |
| 3489 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3490 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3491 | PSA_ASSERT( psa_mac_update( &operation, |
| 3492 | input->x, input->len ) ); |
| 3493 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3494 | perturbed_mac, |
| 3495 | expected_mac->len + 1 ), |
| 3496 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3497 | |
| 3498 | /* Test changing one byte. */ |
| 3499 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3500 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3501 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3502 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3503 | |
| 3504 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3505 | input->x, input->len, |
| 3506 | perturbed_mac, expected_mac->len ), |
| 3507 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3508 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3509 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3510 | PSA_ASSERT( psa_mac_update( &operation, |
| 3511 | input->x, input->len ) ); |
| 3512 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3513 | perturbed_mac, |
| 3514 | expected_mac->len ), |
| 3515 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3516 | perturbed_mac[i] ^= 1; |
| 3517 | } |
| 3518 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3519 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3520 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3521 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3522 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3523 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3524 | } |
| 3525 | /* END_CASE */ |
| 3526 | |
| 3527 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3528 | void cipher_operation_init( ) |
| 3529 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3530 | const uint8_t input[1] = { 0 }; |
| 3531 | unsigned char output[1] = { 0 }; |
| 3532 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3533 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3534 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3535 | * 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] | 3536 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3537 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3538 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3539 | psa_cipher_operation_t zero; |
| 3540 | |
| 3541 | memset( &zero, 0, sizeof( zero ) ); |
| 3542 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3543 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3544 | TEST_EQUAL( psa_cipher_update( &func, |
| 3545 | input, sizeof( input ), |
| 3546 | output, sizeof( output ), |
| 3547 | &output_length ), |
| 3548 | PSA_ERROR_BAD_STATE ); |
| 3549 | TEST_EQUAL( psa_cipher_update( &init, |
| 3550 | input, sizeof( input ), |
| 3551 | output, sizeof( output ), |
| 3552 | &output_length ), |
| 3553 | PSA_ERROR_BAD_STATE ); |
| 3554 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3555 | input, sizeof( input ), |
| 3556 | output, sizeof( output ), |
| 3557 | &output_length ), |
| 3558 | PSA_ERROR_BAD_STATE ); |
| 3559 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3560 | /* A default cipher operation should be abortable without error. */ |
| 3561 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3562 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3563 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3564 | } |
| 3565 | /* END_CASE */ |
| 3566 | |
| 3567 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3568 | void cipher_setup( int key_type_arg, |
| 3569 | data_t *key, |
| 3570 | int alg_arg, |
| 3571 | int expected_status_arg ) |
| 3572 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3573 | psa_key_type_t key_type = key_type_arg; |
| 3574 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3575 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3576 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3577 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3578 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3579 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3580 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3581 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3582 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3583 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3584 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3585 | &operation, &status ) ) |
| 3586 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3587 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3588 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3589 | /* The operation object should be reusable. */ |
| 3590 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3591 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3592 | smoke_test_key_data, |
| 3593 | sizeof( smoke_test_key_data ), |
| 3594 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3595 | &operation, &status ) ) |
| 3596 | goto exit; |
| 3597 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3598 | #endif |
| 3599 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3600 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3601 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3602 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3603 | } |
| 3604 | /* END_CASE */ |
| 3605 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3606 | /* 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] | 3607 | void cipher_bad_order( ) |
| 3608 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3609 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3610 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3611 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3612 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3613 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3614 | 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] | 3615 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3616 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3617 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3618 | const uint8_t text[] = { |
| 3619 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3620 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3621 | 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] | 3622 | size_t length = 0; |
| 3623 | |
| 3624 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3625 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3626 | psa_set_key_algorithm( &attributes, alg ); |
| 3627 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3628 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3629 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3630 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3631 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3632 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3633 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3634 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3635 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3636 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3637 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3638 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3639 | |
| 3640 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3641 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3642 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3643 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3644 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3645 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3646 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3647 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3648 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3649 | /* Generate an IV without calling setup beforehand. */ |
| 3650 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3651 | buffer, sizeof( buffer ), |
| 3652 | &length ), |
| 3653 | PSA_ERROR_BAD_STATE ); |
| 3654 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3655 | |
| 3656 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3657 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3658 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3659 | buffer, sizeof( buffer ), |
| 3660 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3661 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3662 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3663 | buffer, sizeof( buffer ), |
| 3664 | &length ), |
| 3665 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3666 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3667 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3668 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3669 | |
| 3670 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3671 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3672 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3673 | iv, sizeof( iv ) ) ); |
| 3674 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3675 | buffer, sizeof( buffer ), |
| 3676 | &length ), |
| 3677 | PSA_ERROR_BAD_STATE ); |
| 3678 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3679 | |
| 3680 | /* Set an IV without calling setup beforehand. */ |
| 3681 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3682 | iv, sizeof( iv ) ), |
| 3683 | PSA_ERROR_BAD_STATE ); |
| 3684 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3685 | |
| 3686 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3687 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3688 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3689 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3690 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3691 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3692 | iv, sizeof( iv ) ), |
| 3693 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3694 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3695 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3696 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3697 | |
| 3698 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3699 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3700 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3701 | buffer, sizeof( buffer ), |
| 3702 | &length ) ); |
| 3703 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3704 | iv, sizeof( iv ) ), |
| 3705 | PSA_ERROR_BAD_STATE ); |
| 3706 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3707 | |
| 3708 | /* Call update without calling setup beforehand. */ |
| 3709 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3710 | text, sizeof( text ), |
| 3711 | buffer, sizeof( buffer ), |
| 3712 | &length ), |
| 3713 | PSA_ERROR_BAD_STATE ); |
| 3714 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3715 | |
| 3716 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3717 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3718 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3719 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3720 | text, sizeof( text ), |
| 3721 | buffer, sizeof( buffer ), |
| 3722 | &length ), |
| 3723 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3724 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3725 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3726 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3727 | |
| 3728 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3729 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3730 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3731 | iv, sizeof( iv ) ) ); |
| 3732 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3733 | buffer, sizeof( buffer ), &length ) ); |
| 3734 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3735 | text, sizeof( text ), |
| 3736 | buffer, sizeof( buffer ), |
| 3737 | &length ), |
| 3738 | PSA_ERROR_BAD_STATE ); |
| 3739 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3740 | |
| 3741 | /* Call finish without calling setup beforehand. */ |
| 3742 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3743 | buffer, sizeof( buffer ), &length ), |
| 3744 | PSA_ERROR_BAD_STATE ); |
| 3745 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3746 | |
| 3747 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3748 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3749 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3750 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3751 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3752 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3753 | buffer, sizeof( buffer ), &length ), |
| 3754 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3755 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3756 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3757 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3758 | |
| 3759 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3760 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3761 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3762 | iv, sizeof( iv ) ) ); |
| 3763 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3764 | buffer, sizeof( buffer ), &length ) ); |
| 3765 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3766 | buffer, sizeof( buffer ), &length ), |
| 3767 | PSA_ERROR_BAD_STATE ); |
| 3768 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3769 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3770 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3771 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3772 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3773 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3774 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3775 | } |
| 3776 | /* END_CASE */ |
| 3777 | |
| 3778 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3779 | void cipher_encrypt_fail( int alg_arg, |
| 3780 | int key_type_arg, |
| 3781 | data_t *key_data, |
| 3782 | data_t *input, |
| 3783 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3784 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3785 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3786 | psa_status_t status; |
| 3787 | psa_key_type_t key_type = key_type_arg; |
| 3788 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3789 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3790 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3791 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3792 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3793 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3794 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3795 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3796 | size_t function_output_length; |
| 3797 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3798 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3799 | |
| 3800 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3801 | { |
| 3802 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3803 | |
| 3804 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3805 | psa_set_key_algorithm( &attributes, alg ); |
| 3806 | psa_set_key_type( &attributes, key_type ); |
| 3807 | |
| 3808 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3809 | input->len ); |
| 3810 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3811 | |
| 3812 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3813 | &key ) ); |
| 3814 | } |
| 3815 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3816 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3817 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3818 | output_buffer_size, &output_length ); |
| 3819 | |
| 3820 | TEST_EQUAL( status, expected_status ); |
| 3821 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3822 | /* Encrypt, multi-part */ |
| 3823 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3824 | if( status == PSA_SUCCESS ) |
| 3825 | { |
| 3826 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3827 | { |
| 3828 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3829 | iv, iv_size, |
| 3830 | &iv_length ) ); |
| 3831 | } |
| 3832 | |
| 3833 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3834 | output, output_buffer_size, |
| 3835 | &function_output_length ); |
| 3836 | if( status == PSA_SUCCESS ) |
| 3837 | { |
| 3838 | output_length += function_output_length; |
| 3839 | |
| 3840 | status = psa_cipher_finish( &operation, output + output_length, |
| 3841 | output_buffer_size - output_length, |
| 3842 | &function_output_length ); |
| 3843 | |
| 3844 | TEST_EQUAL( status, expected_status ); |
| 3845 | } |
| 3846 | else |
| 3847 | { |
| 3848 | TEST_EQUAL( status, expected_status ); |
| 3849 | } |
| 3850 | } |
| 3851 | else |
| 3852 | { |
| 3853 | TEST_EQUAL( status, expected_status ); |
| 3854 | } |
| 3855 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3856 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3857 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3858 | mbedtls_free( output ); |
| 3859 | psa_destroy_key( key ); |
| 3860 | PSA_DONE( ); |
| 3861 | } |
| 3862 | /* END_CASE */ |
| 3863 | |
| 3864 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3865 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3866 | data_t *input, int iv_length, |
| 3867 | int expected_result ) |
| 3868 | { |
| 3869 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3870 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3871 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3872 | size_t output_buffer_size = 0; |
| 3873 | unsigned char *output = NULL; |
| 3874 | |
| 3875 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3876 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3877 | |
| 3878 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3879 | |
| 3880 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3881 | psa_set_key_algorithm( &attributes, alg ); |
| 3882 | psa_set_key_type( &attributes, key_type ); |
| 3883 | |
| 3884 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3885 | &key ) ); |
| 3886 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3887 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3888 | iv_length ) ); |
| 3889 | |
| 3890 | exit: |
| 3891 | psa_cipher_abort( &operation ); |
| 3892 | mbedtls_free( output ); |
| 3893 | psa_destroy_key( key ); |
| 3894 | PSA_DONE( ); |
| 3895 | } |
| 3896 | /* END_CASE */ |
| 3897 | |
| 3898 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3899 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3900 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3901 | { |
| 3902 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3903 | psa_key_type_t key_type = key_type_arg; |
| 3904 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3905 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3906 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3907 | unsigned char *output = NULL; |
| 3908 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3909 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3910 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3911 | |
| 3912 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3913 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3914 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3915 | TEST_LE_U( ciphertext->len, |
| 3916 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3917 | 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] | 3918 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3919 | TEST_LE_U( plaintext->len, |
| 3920 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3921 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3922 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3923 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3924 | |
| 3925 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3926 | psa_set_key_usage_flags( &attributes, |
| 3927 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3928 | psa_set_key_algorithm( &attributes, alg ); |
| 3929 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3930 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3931 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3932 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3933 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3934 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3935 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3936 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3937 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3938 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3939 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3940 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3941 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3942 | PSA_ERROR_BAD_STATE ); |
| 3943 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3944 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3945 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3946 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3947 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3948 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3949 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3950 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3951 | &length ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3952 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3953 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3954 | /* Multipart encryption */ |
| 3955 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3956 | output_length = 0; |
| 3957 | length = ~0; |
| 3958 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3959 | plaintext->x, plaintext->len, |
| 3960 | output, output_buffer_size, |
| 3961 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3962 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3963 | output_length += length; |
| 3964 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3965 | output + output_length, |
| 3966 | output_buffer_size - output_length, |
| 3967 | &length ) ); |
| 3968 | output_length += length; |
| 3969 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3970 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3971 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3972 | /* Multipart encryption */ |
| 3973 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3974 | output_length = 0; |
| 3975 | length = ~0; |
| 3976 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3977 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3978 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3979 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3980 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3981 | output_length += length; |
| 3982 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3983 | output + output_length, |
| 3984 | output_buffer_size - output_length, |
| 3985 | &length ) ); |
| 3986 | output_length += length; |
| 3987 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 3988 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3989 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3990 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3991 | output_length = ~0; |
| 3992 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 3993 | output, output_buffer_size, |
| 3994 | &output_length ) ); |
| 3995 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 3996 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3997 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3998 | /* One-shot decryption */ |
| 3999 | output_length = ~0; |
| 4000 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 4001 | output, output_buffer_size, |
| 4002 | &output_length ) ); |
| 4003 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4004 | output, output_length ); |
| 4005 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4006 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4007 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4008 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4009 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4010 | psa_destroy_key( key ); |
| 4011 | PSA_DONE( ); |
| 4012 | } |
| 4013 | /* END_CASE */ |
| 4014 | |
| 4015 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4016 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 4017 | { |
| 4018 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4019 | psa_algorithm_t alg = alg_arg; |
| 4020 | psa_key_type_t key_type = key_type_arg; |
| 4021 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4022 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4023 | psa_status_t status; |
| 4024 | |
| 4025 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4026 | |
| 4027 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4028 | psa_set_key_algorithm( &attributes, alg ); |
| 4029 | psa_set_key_type( &attributes, key_type ); |
| 4030 | |
| 4031 | /* Usage of either of these two size macros would cause divide by zero |
| 4032 | * with incorrect key types previously. Input length should be irrelevant |
| 4033 | * here. */ |
| 4034 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 4035 | 0 ); |
| 4036 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 4037 | |
| 4038 | |
| 4039 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4040 | &key ) ); |
| 4041 | |
| 4042 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4043 | * Encrypt or decrypt will end up in the same place. */ |
| 4044 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 4045 | |
| 4046 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 4047 | |
| 4048 | exit: |
| 4049 | psa_cipher_abort( &operation ); |
| 4050 | psa_destroy_key( key ); |
| 4051 | PSA_DONE( ); |
| 4052 | } |
| 4053 | /* END_CASE */ |
| 4054 | |
| 4055 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4056 | void cipher_encrypt_validation( int alg_arg, |
| 4057 | int key_type_arg, |
| 4058 | data_t *key_data, |
| 4059 | data_t *input ) |
| 4060 | { |
| 4061 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4062 | psa_key_type_t key_type = key_type_arg; |
| 4063 | psa_algorithm_t alg = alg_arg; |
| 4064 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 4065 | unsigned char *output1 = NULL; |
| 4066 | size_t output1_buffer_size = 0; |
| 4067 | size_t output1_length = 0; |
| 4068 | unsigned char *output2 = NULL; |
| 4069 | size_t output2_buffer_size = 0; |
| 4070 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4071 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4072 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4073 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
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_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4076 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4077 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4078 | psa_set_key_algorithm( &attributes, alg ); |
| 4079 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4080 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4081 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 4082 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4083 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4084 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 4085 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 4086 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4087 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4088 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4089 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4090 | /* The one-shot cipher encryption uses generated iv so validating |
| 4091 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4092 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 4093 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4094 | TEST_LE_U( output1_length, |
| 4095 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4096 | TEST_LE_U( output1_length, |
| 4097 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4098 | |
| 4099 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 4100 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4101 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4102 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4103 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4104 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4105 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4106 | TEST_LE_U( function_output_length, |
| 4107 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4108 | TEST_LE_U( function_output_length, |
| 4109 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4110 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4111 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4112 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4113 | output2 + output2_length, |
| 4114 | output2_buffer_size - output2_length, |
| 4115 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4116 | TEST_LE_U( function_output_length, |
| 4117 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4118 | TEST_LE_U( function_output_length, |
| 4119 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4120 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4121 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4122 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4123 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 4124 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4125 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4126 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4127 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4128 | mbedtls_free( output1 ); |
| 4129 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4130 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4131 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4132 | } |
| 4133 | /* END_CASE */ |
| 4134 | |
| 4135 | /* BEGIN_CASE */ |
| 4136 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4137 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4138 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4139 | int first_part_size_arg, |
| 4140 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4141 | data_t *expected_output, |
| 4142 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4143 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4144 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4145 | psa_key_type_t key_type = key_type_arg; |
| 4146 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4147 | psa_status_t status; |
| 4148 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4149 | size_t first_part_size = first_part_size_arg; |
| 4150 | size_t output1_length = output1_length_arg; |
| 4151 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4152 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4153 | size_t output_buffer_size = 0; |
| 4154 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4155 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4156 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4157 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4158 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4159 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4160 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4161 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4162 | psa_set_key_algorithm( &attributes, alg ); |
| 4163 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4164 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4165 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4166 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4167 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4168 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4169 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4170 | if( iv->len > 0 ) |
| 4171 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4172 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4173 | } |
| 4174 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4175 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4176 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4177 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4178 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4179 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4180 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 4181 | output, output_buffer_size, |
| 4182 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4183 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4184 | TEST_LE_U( function_output_length, |
| 4185 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4186 | TEST_LE_U( function_output_length, |
| 4187 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4188 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4189 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4190 | if( first_part_size < input->len ) |
| 4191 | { |
| 4192 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4193 | input->x + first_part_size, |
| 4194 | input->len - first_part_size, |
| 4195 | ( output_buffer_size == 0 ? NULL : |
| 4196 | output + total_output_length ), |
| 4197 | output_buffer_size - total_output_length, |
| 4198 | &function_output_length ) ); |
| 4199 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4200 | TEST_LE_U( function_output_length, |
| 4201 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4202 | alg, |
| 4203 | input->len - first_part_size ) ); |
| 4204 | TEST_LE_U( function_output_length, |
| 4205 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4206 | total_output_length += function_output_length; |
| 4207 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4208 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4209 | status = psa_cipher_finish( &operation, |
| 4210 | ( output_buffer_size == 0 ? NULL : |
| 4211 | output + total_output_length ), |
| 4212 | output_buffer_size - total_output_length, |
| 4213 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4214 | TEST_LE_U( function_output_length, |
| 4215 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4216 | TEST_LE_U( function_output_length, |
| 4217 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4218 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4219 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4220 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4221 | if( expected_status == PSA_SUCCESS ) |
| 4222 | { |
| 4223 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4224 | |
| 4225 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4226 | output, total_output_length ); |
| 4227 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4228 | |
| 4229 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4230 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4231 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4232 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4233 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4234 | } |
| 4235 | /* END_CASE */ |
| 4236 | |
| 4237 | /* BEGIN_CASE */ |
| 4238 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4239 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4240 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4241 | int first_part_size_arg, |
| 4242 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4243 | data_t *expected_output, |
| 4244 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4245 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4246 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4247 | psa_key_type_t key_type = key_type_arg; |
| 4248 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4249 | psa_status_t status; |
| 4250 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4251 | size_t first_part_size = first_part_size_arg; |
| 4252 | size_t output1_length = output1_length_arg; |
| 4253 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4254 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4255 | size_t output_buffer_size = 0; |
| 4256 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4257 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4258 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4259 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4260 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4261 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4262 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4263 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4264 | psa_set_key_algorithm( &attributes, alg ); |
| 4265 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4266 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4267 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4268 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4269 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4270 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4271 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4272 | if( iv->len > 0 ) |
| 4273 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4274 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4275 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4276 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4277 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4278 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4279 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4280 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4281 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4282 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4283 | input->x, first_part_size, |
| 4284 | output, output_buffer_size, |
| 4285 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4286 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4287 | TEST_LE_U( function_output_length, |
| 4288 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4289 | TEST_LE_U( function_output_length, |
| 4290 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4291 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4292 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4293 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4294 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4295 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4296 | input->x + first_part_size, |
| 4297 | input->len - first_part_size, |
| 4298 | ( output_buffer_size == 0 ? NULL : |
| 4299 | output + total_output_length ), |
| 4300 | output_buffer_size - total_output_length, |
| 4301 | &function_output_length ) ); |
| 4302 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4303 | TEST_LE_U( function_output_length, |
| 4304 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4305 | alg, |
| 4306 | input->len - first_part_size ) ); |
| 4307 | TEST_LE_U( function_output_length, |
| 4308 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4309 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4310 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4311 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4312 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4313 | ( output_buffer_size == 0 ? NULL : |
| 4314 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4315 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4316 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4317 | TEST_LE_U( function_output_length, |
| 4318 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4319 | TEST_LE_U( function_output_length, |
| 4320 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4321 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4322 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4323 | |
| 4324 | if( expected_status == PSA_SUCCESS ) |
| 4325 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4326 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4327 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4328 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4329 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4330 | } |
| 4331 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4332 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4333 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4334 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4335 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4336 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4337 | } |
| 4338 | /* END_CASE */ |
| 4339 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4340 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4341 | void cipher_decrypt_fail( int alg_arg, |
| 4342 | int key_type_arg, |
| 4343 | data_t *key_data, |
| 4344 | data_t *iv, |
| 4345 | data_t *input_arg, |
| 4346 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4347 | { |
| 4348 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4349 | psa_status_t status; |
| 4350 | psa_key_type_t key_type = key_type_arg; |
| 4351 | psa_algorithm_t alg = alg_arg; |
| 4352 | psa_status_t expected_status = expected_status_arg; |
| 4353 | unsigned char *input = NULL; |
| 4354 | size_t input_buffer_size = 0; |
| 4355 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4356 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4357 | size_t output_buffer_size = 0; |
| 4358 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4359 | size_t function_output_length; |
| 4360 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4361 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4362 | |
| 4363 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4364 | { |
| 4365 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4366 | |
| 4367 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4368 | psa_set_key_algorithm( &attributes, alg ); |
| 4369 | psa_set_key_type( &attributes, key_type ); |
| 4370 | |
| 4371 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4372 | &key ) ); |
| 4373 | } |
| 4374 | |
| 4375 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4376 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4377 | if ( input_buffer_size > 0 ) |
| 4378 | { |
| 4379 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4380 | memcpy( input, iv->x, iv->len ); |
| 4381 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4382 | } |
| 4383 | |
| 4384 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4385 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4386 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4387 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4388 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4389 | output_buffer_size, &output_length ); |
| 4390 | TEST_EQUAL( status, expected_status ); |
| 4391 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4392 | /* Decrypt, multi-part */ |
| 4393 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4394 | if( status == PSA_SUCCESS ) |
| 4395 | { |
| 4396 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4397 | input_arg->len ) + |
| 4398 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4399 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4400 | |
| 4401 | if( iv->len > 0 ) |
| 4402 | { |
| 4403 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4404 | |
| 4405 | if( status != PSA_SUCCESS ) |
| 4406 | TEST_EQUAL( status, expected_status ); |
| 4407 | } |
| 4408 | |
| 4409 | if( status == PSA_SUCCESS ) |
| 4410 | { |
| 4411 | status = psa_cipher_update( &operation, |
| 4412 | input_arg->x, input_arg->len, |
| 4413 | output_multi, output_buffer_size, |
| 4414 | &function_output_length ); |
| 4415 | if( status == PSA_SUCCESS ) |
| 4416 | { |
| 4417 | output_length = function_output_length; |
| 4418 | |
| 4419 | status = psa_cipher_finish( &operation, |
| 4420 | output_multi + output_length, |
| 4421 | output_buffer_size - output_length, |
| 4422 | &function_output_length ); |
| 4423 | |
| 4424 | TEST_EQUAL( status, expected_status ); |
| 4425 | } |
| 4426 | else |
| 4427 | { |
| 4428 | TEST_EQUAL( status, expected_status ); |
| 4429 | } |
| 4430 | } |
| 4431 | else |
| 4432 | { |
| 4433 | TEST_EQUAL( status, expected_status ); |
| 4434 | } |
| 4435 | } |
| 4436 | else |
| 4437 | { |
| 4438 | TEST_EQUAL( status, expected_status ); |
| 4439 | } |
| 4440 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4441 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4442 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4443 | mbedtls_free( input ); |
| 4444 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4445 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4446 | psa_destroy_key( key ); |
| 4447 | PSA_DONE( ); |
| 4448 | } |
| 4449 | /* END_CASE */ |
| 4450 | |
| 4451 | /* BEGIN_CASE */ |
| 4452 | void cipher_decrypt( int alg_arg, |
| 4453 | int key_type_arg, |
| 4454 | data_t *key_data, |
| 4455 | data_t *iv, |
| 4456 | data_t *input_arg, |
| 4457 | data_t *expected_output ) |
| 4458 | { |
| 4459 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4460 | psa_key_type_t key_type = key_type_arg; |
| 4461 | psa_algorithm_t alg = alg_arg; |
| 4462 | unsigned char *input = NULL; |
| 4463 | size_t input_buffer_size = 0; |
| 4464 | unsigned char *output = NULL; |
| 4465 | size_t output_buffer_size = 0; |
| 4466 | size_t output_length = 0; |
| 4467 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4468 | |
| 4469 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4470 | |
| 4471 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4472 | psa_set_key_algorithm( &attributes, alg ); |
| 4473 | psa_set_key_type( &attributes, key_type ); |
| 4474 | |
| 4475 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4476 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4477 | if ( input_buffer_size > 0 ) |
| 4478 | { |
| 4479 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4480 | memcpy( input, iv->x, iv->len ); |
| 4481 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4482 | } |
| 4483 | |
| 4484 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4485 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4486 | |
| 4487 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4488 | &key ) ); |
| 4489 | |
| 4490 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4491 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4492 | TEST_LE_U( output_length, |
| 4493 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4494 | TEST_LE_U( output_length, |
| 4495 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4496 | |
| 4497 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4498 | output, output_length ); |
| 4499 | exit: |
| 4500 | mbedtls_free( input ); |
| 4501 | mbedtls_free( output ); |
| 4502 | psa_destroy_key( key ); |
| 4503 | PSA_DONE( ); |
| 4504 | } |
| 4505 | /* END_CASE */ |
| 4506 | |
| 4507 | /* BEGIN_CASE */ |
| 4508 | void cipher_verify_output( int alg_arg, |
| 4509 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4510 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4511 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4512 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4513 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4514 | psa_key_type_t key_type = key_type_arg; |
| 4515 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4516 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4517 | size_t output1_size = 0; |
| 4518 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4519 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4520 | size_t output2_size = 0; |
| 4521 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4522 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4523 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4524 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4525 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4526 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4527 | psa_set_key_algorithm( &attributes, alg ); |
| 4528 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4529 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4530 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4531 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4532 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4533 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4534 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4535 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4536 | output1, output1_size, |
| 4537 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4538 | TEST_LE_U( output1_length, |
| 4539 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4540 | TEST_LE_U( output1_length, |
| 4541 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4542 | |
| 4543 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4544 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4545 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4546 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4547 | output2, output2_size, |
| 4548 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4549 | TEST_LE_U( output2_length, |
| 4550 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4551 | TEST_LE_U( output2_length, |
| 4552 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4553 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4554 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4555 | |
| 4556 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4557 | mbedtls_free( output1 ); |
| 4558 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4559 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4560 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4561 | } |
| 4562 | /* END_CASE */ |
| 4563 | |
| 4564 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4565 | void cipher_verify_output_multipart( int alg_arg, |
| 4566 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4567 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4568 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4569 | int first_part_size_arg ) |
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 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4572 | psa_key_type_t key_type = key_type_arg; |
| 4573 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4574 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4575 | unsigned char iv[16] = {0}; |
| 4576 | size_t iv_size = 16; |
| 4577 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4578 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4579 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4580 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4581 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4582 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4583 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4584 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4585 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4586 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4587 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4588 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4589 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4590 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4591 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4592 | psa_set_key_algorithm( &attributes, alg ); |
| 4593 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4594 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4595 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4596 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4597 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4598 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4599 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4600 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4601 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4602 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4603 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4604 | iv, iv_size, |
| 4605 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4606 | } |
| 4607 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4608 | 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] | 4609 | TEST_LE_U( output1_buffer_size, |
| 4610 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4611 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4612 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4613 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4614 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4615 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4616 | output1, output1_buffer_size, |
| 4617 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4618 | TEST_LE_U( function_output_length, |
| 4619 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4620 | TEST_LE_U( function_output_length, |
| 4621 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4622 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4623 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4624 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4625 | input->x + first_part_size, |
| 4626 | input->len - first_part_size, |
| 4627 | output1, output1_buffer_size, |
| 4628 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4629 | TEST_LE_U( function_output_length, |
| 4630 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4631 | alg, |
| 4632 | input->len - first_part_size ) ); |
| 4633 | TEST_LE_U( function_output_length, |
| 4634 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4635 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4636 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4637 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4638 | output1 + output1_length, |
| 4639 | output1_buffer_size - output1_length, |
| 4640 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4641 | TEST_LE_U( function_output_length, |
| 4642 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4643 | TEST_LE_U( function_output_length, |
| 4644 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4645 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4646 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4647 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4648 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4649 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4650 | TEST_LE_U( output2_buffer_size, |
| 4651 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4652 | TEST_LE_U( output2_buffer_size, |
| 4653 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4654 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4655 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4656 | if( iv_length > 0 ) |
| 4657 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4658 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4659 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4660 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4661 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4662 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4663 | output2, output2_buffer_size, |
| 4664 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4665 | TEST_LE_U( function_output_length, |
| 4666 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4667 | TEST_LE_U( function_output_length, |
| 4668 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4669 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4670 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4671 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4672 | output1 + first_part_size, |
| 4673 | output1_length - first_part_size, |
| 4674 | output2, output2_buffer_size, |
| 4675 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4676 | TEST_LE_U( function_output_length, |
| 4677 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4678 | alg, |
| 4679 | output1_length - first_part_size ) ); |
| 4680 | TEST_LE_U( function_output_length, |
| 4681 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4682 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4683 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4684 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4685 | output2 + output2_length, |
| 4686 | output2_buffer_size - output2_length, |
| 4687 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4688 | TEST_LE_U( function_output_length, |
| 4689 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4690 | TEST_LE_U( function_output_length, |
| 4691 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4692 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4693 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4694 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4695 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4696 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4697 | |
| 4698 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4699 | psa_cipher_abort( &operation1 ); |
| 4700 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4701 | mbedtls_free( output1 ); |
| 4702 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4703 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4704 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4705 | } |
| 4706 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4707 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4708 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4709 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4710 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4711 | data_t *nonce, |
| 4712 | data_t *additional_data, |
| 4713 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4714 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4715 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4716 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4717 | psa_key_type_t key_type = key_type_arg; |
| 4718 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4719 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4720 | unsigned char *output_data = NULL; |
| 4721 | size_t output_size = 0; |
| 4722 | size_t output_length = 0; |
| 4723 | unsigned char *output_data2 = NULL; |
| 4724 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4725 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4726 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4727 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4728 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4729 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4730 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4731 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4732 | psa_set_key_algorithm( &attributes, alg ); |
| 4733 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4734 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4735 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4736 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4737 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4738 | key_bits = psa_get_key_bits( &attributes ); |
| 4739 | |
| 4740 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4741 | alg ); |
| 4742 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4743 | * should be exact. */ |
| 4744 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4745 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4746 | { |
| 4747 | TEST_EQUAL( output_size, |
| 4748 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4749 | TEST_LE_U( output_size, |
| 4750 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4751 | } |
| 4752 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4753 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4754 | status = psa_aead_encrypt( key, alg, |
| 4755 | nonce->x, nonce->len, |
| 4756 | additional_data->x, |
| 4757 | additional_data->len, |
| 4758 | input_data->x, input_data->len, |
| 4759 | output_data, output_size, |
| 4760 | &output_length ); |
| 4761 | |
| 4762 | /* If the operation is not supported, just skip and not fail in case the |
| 4763 | * encryption involves a common limitation of cryptography hardwares and |
| 4764 | * an alternative implementation. */ |
| 4765 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4766 | { |
| 4767 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4768 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4769 | } |
| 4770 | |
| 4771 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4772 | |
| 4773 | if( PSA_SUCCESS == expected_result ) |
| 4774 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4775 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4776 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4777 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4778 | * should be exact. */ |
| 4779 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4780 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4781 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4782 | TEST_LE_U( input_data->len, |
| 4783 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4784 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4785 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4786 | nonce->x, nonce->len, |
| 4787 | additional_data->x, |
| 4788 | additional_data->len, |
| 4789 | output_data, output_length, |
| 4790 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4791 | &output_length2 ), |
| 4792 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4793 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4794 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4795 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4796 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4797 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4798 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4799 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4800 | mbedtls_free( output_data ); |
| 4801 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4802 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4803 | } |
| 4804 | /* END_CASE */ |
| 4805 | |
| 4806 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4807 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4808 | int alg_arg, |
| 4809 | data_t *nonce, |
| 4810 | data_t *additional_data, |
| 4811 | data_t *input_data, |
| 4812 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4813 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4814 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4815 | psa_key_type_t key_type = key_type_arg; |
| 4816 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4817 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4818 | unsigned char *output_data = NULL; |
| 4819 | size_t output_size = 0; |
| 4820 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4821 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4822 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4823 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4824 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4825 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4826 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4827 | psa_set_key_algorithm( &attributes, alg ); |
| 4828 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4829 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4830 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4831 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4832 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4833 | key_bits = psa_get_key_bits( &attributes ); |
| 4834 | |
| 4835 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4836 | alg ); |
| 4837 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4838 | * should be exact. */ |
| 4839 | TEST_EQUAL( output_size, |
| 4840 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4841 | TEST_LE_U( output_size, |
| 4842 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4843 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4844 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4845 | status = psa_aead_encrypt( key, alg, |
| 4846 | nonce->x, nonce->len, |
| 4847 | additional_data->x, additional_data->len, |
| 4848 | input_data->x, input_data->len, |
| 4849 | output_data, output_size, |
| 4850 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4851 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4852 | /* If the operation is not supported, just skip and not fail in case the |
| 4853 | * encryption involves a common limitation of cryptography hardwares and |
| 4854 | * an alternative implementation. */ |
| 4855 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4856 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4857 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4858 | 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] | 4859 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4860 | |
| 4861 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4862 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4863 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4864 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4865 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4866 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4867 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4868 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4869 | } |
| 4870 | /* END_CASE */ |
| 4871 | |
| 4872 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4873 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4874 | int alg_arg, |
| 4875 | data_t *nonce, |
| 4876 | data_t *additional_data, |
| 4877 | data_t *input_data, |
| 4878 | data_t *expected_data, |
| 4879 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4880 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4881 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4882 | psa_key_type_t key_type = key_type_arg; |
| 4883 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4884 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4885 | unsigned char *output_data = NULL; |
| 4886 | size_t output_size = 0; |
| 4887 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4888 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4889 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4890 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4891 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4892 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4893 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4894 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4895 | psa_set_key_algorithm( &attributes, alg ); |
| 4896 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4897 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4898 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4899 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4900 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4901 | key_bits = psa_get_key_bits( &attributes ); |
| 4902 | |
| 4903 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4904 | alg ); |
| 4905 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4906 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4907 | { |
| 4908 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4909 | * should be exact. */ |
| 4910 | TEST_EQUAL( output_size, |
| 4911 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4912 | TEST_LE_U( output_size, |
| 4913 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4914 | } |
| 4915 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4916 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4917 | status = psa_aead_decrypt( key, alg, |
| 4918 | nonce->x, nonce->len, |
| 4919 | additional_data->x, |
| 4920 | additional_data->len, |
| 4921 | input_data->x, input_data->len, |
| 4922 | output_data, output_size, |
| 4923 | &output_length ); |
| 4924 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4925 | /* If the operation is not supported, just skip and not fail in case the |
| 4926 | * decryption involves a common limitation of cryptography hardwares and |
| 4927 | * an alternative implementation. */ |
| 4928 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4929 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4930 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4931 | 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] | 4932 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4933 | |
| 4934 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4935 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4936 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4937 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4938 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4939 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4940 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4941 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4942 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4943 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4944 | } |
| 4945 | /* END_CASE */ |
| 4946 | |
| 4947 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4948 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4949 | int alg_arg, |
| 4950 | data_t *nonce, |
| 4951 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4952 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4953 | int do_set_lengths, |
| 4954 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4955 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4956 | size_t ad_part_len = 0; |
| 4957 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4958 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4959 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4960 | 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] | 4961 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4962 | mbedtls_test_set_step( ad_part_len ); |
| 4963 | |
| 4964 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4965 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4966 | if( ad_part_len & 0x01 ) |
| 4967 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4968 | else |
| 4969 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4970 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4971 | |
| 4972 | /* Split ad into length(ad_part_len) parts. */ |
| 4973 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4974 | alg_arg, nonce, |
| 4975 | additional_data, |
| 4976 | ad_part_len, |
| 4977 | input_data, -1, |
| 4978 | set_lengths_method, |
| 4979 | expected_output, |
| 4980 | 1, 0 ) ) |
| 4981 | break; |
| 4982 | |
| 4983 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4984 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4985 | |
| 4986 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4987 | alg_arg, nonce, |
| 4988 | additional_data, |
| 4989 | ad_part_len, |
| 4990 | input_data, -1, |
| 4991 | set_lengths_method, |
| 4992 | expected_output, |
| 4993 | 1, 1 ) ) |
| 4994 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4995 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4996 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4997 | 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] | 4998 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4999 | /* Split data into length(data_part_len) parts. */ |
| 5000 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5001 | |
| 5002 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5003 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5004 | if( data_part_len & 0x01 ) |
| 5005 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5006 | else |
| 5007 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5008 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5009 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5010 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5011 | alg_arg, nonce, |
| 5012 | additional_data, -1, |
| 5013 | input_data, data_part_len, |
| 5014 | set_lengths_method, |
| 5015 | expected_output, |
| 5016 | 1, 0 ) ) |
| 5017 | break; |
| 5018 | |
| 5019 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5020 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5021 | |
| 5022 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5023 | alg_arg, nonce, |
| 5024 | additional_data, -1, |
| 5025 | input_data, data_part_len, |
| 5026 | set_lengths_method, |
| 5027 | expected_output, |
| 5028 | 1, 1 ) ) |
| 5029 | break; |
| 5030 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5031 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5032 | /* Goto is required to silence warnings about unused labels, as we |
| 5033 | * don't actually do any test assertions in this function. */ |
| 5034 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5035 | } |
| 5036 | /* END_CASE */ |
| 5037 | |
| 5038 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5039 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 5040 | int alg_arg, |
| 5041 | data_t *nonce, |
| 5042 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5043 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 5044 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5045 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5046 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5047 | size_t ad_part_len = 0; |
| 5048 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5049 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5050 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5051 | 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] | 5052 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5053 | /* Split ad into length(ad_part_len) parts. */ |
| 5054 | mbedtls_test_set_step( ad_part_len ); |
| 5055 | |
| 5056 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5057 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5058 | if( ad_part_len & 0x01 ) |
| 5059 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5060 | else |
| 5061 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
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 | |
| 5064 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5065 | alg_arg, nonce, |
| 5066 | additional_data, |
| 5067 | ad_part_len, |
| 5068 | input_data, -1, |
| 5069 | set_lengths_method, |
| 5070 | expected_output, |
| 5071 | 0, 0 ) ) |
| 5072 | break; |
| 5073 | |
| 5074 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5075 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5076 | |
| 5077 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5078 | alg_arg, nonce, |
| 5079 | additional_data, |
| 5080 | ad_part_len, |
| 5081 | input_data, -1, |
| 5082 | set_lengths_method, |
| 5083 | expected_output, |
| 5084 | 0, 1 ) ) |
| 5085 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5086 | } |
| 5087 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5088 | 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] | 5089 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5090 | /* Split data into length(data_part_len) parts. */ |
| 5091 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5092 | |
| 5093 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5094 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5095 | if( data_part_len & 0x01 ) |
| 5096 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5097 | else |
| 5098 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5099 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5100 | |
| 5101 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5102 | alg_arg, nonce, |
| 5103 | additional_data, -1, |
| 5104 | input_data, data_part_len, |
| 5105 | set_lengths_method, |
| 5106 | expected_output, |
| 5107 | 0, 0 ) ) |
| 5108 | break; |
| 5109 | |
| 5110 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5111 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5112 | |
| 5113 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5114 | alg_arg, nonce, |
| 5115 | additional_data, -1, |
| 5116 | input_data, data_part_len, |
| 5117 | set_lengths_method, |
| 5118 | expected_output, |
| 5119 | 0, 1 ) ) |
| 5120 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5121 | } |
| 5122 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5123 | /* Goto is required to silence warnings about unused labels, as we |
| 5124 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5125 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5126 | } |
| 5127 | /* END_CASE */ |
| 5128 | |
| 5129 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5130 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 5131 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5132 | int nonce_length, |
| 5133 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5134 | data_t *additional_data, |
| 5135 | data_t *input_data, |
| 5136 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5137 | { |
| 5138 | |
| 5139 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5140 | psa_key_type_t key_type = key_type_arg; |
| 5141 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5142 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5143 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5144 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5145 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5146 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5147 | size_t actual_nonce_length = 0; |
| 5148 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5149 | unsigned char *output = NULL; |
| 5150 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5151 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5152 | size_t ciphertext_size = 0; |
| 5153 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5154 | size_t tag_length = 0; |
| 5155 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5156 | |
| 5157 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5158 | |
| 5159 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5160 | psa_set_key_algorithm( & attributes, alg ); |
| 5161 | psa_set_key_type( & attributes, key_type ); |
| 5162 | |
| 5163 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5164 | &key ) ); |
| 5165 | |
| 5166 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5167 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5168 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5169 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5170 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5171 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5172 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5173 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5174 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5175 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5176 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5177 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5178 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5179 | |
| 5180 | /* If the operation is not supported, just skip and not fail in case the |
| 5181 | * encryption involves a common limitation of cryptography hardwares and |
| 5182 | * an alternative implementation. */ |
| 5183 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5184 | { |
| 5185 | 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] | 5186 | 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] | 5187 | } |
| 5188 | |
| 5189 | PSA_ASSERT( status ); |
| 5190 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5191 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5192 | nonce_length, |
| 5193 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5194 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5195 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5196 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5197 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5198 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5199 | if( expected_status == PSA_SUCCESS ) |
| 5200 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 5201 | alg ) ); |
| 5202 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5203 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5204 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5205 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5206 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5207 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 5208 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5209 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5210 | |
| 5211 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5212 | additional_data->len ) ); |
| 5213 | |
| 5214 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5215 | output, output_size, |
| 5216 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5217 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5218 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5219 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5220 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5221 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5222 | |
| 5223 | exit: |
| 5224 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5225 | mbedtls_free( output ); |
| 5226 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5227 | psa_aead_abort( &operation ); |
| 5228 | PSA_DONE( ); |
| 5229 | } |
| 5230 | /* END_CASE */ |
| 5231 | |
| 5232 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5233 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 5234 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5235 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5236 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5237 | data_t *additional_data, |
| 5238 | data_t *input_data, |
| 5239 | int expected_status_arg ) |
| 5240 | { |
| 5241 | |
| 5242 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5243 | psa_key_type_t key_type = key_type_arg; |
| 5244 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5245 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5246 | uint8_t *nonce_buffer = NULL; |
| 5247 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5248 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5249 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5250 | unsigned char *output = NULL; |
| 5251 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5252 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5253 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5254 | size_t ciphertext_size = 0; |
| 5255 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5256 | size_t tag_length = 0; |
| 5257 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5258 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5259 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5260 | |
| 5261 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5262 | |
| 5263 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5264 | psa_set_key_algorithm( &attributes, alg ); |
| 5265 | psa_set_key_type( &attributes, key_type ); |
| 5266 | |
| 5267 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5268 | &key ) ); |
| 5269 | |
| 5270 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5271 | |
| 5272 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5273 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5274 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5275 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5276 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5277 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5278 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5279 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5280 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5281 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5282 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5283 | |
| 5284 | /* If the operation is not supported, just skip and not fail in case the |
| 5285 | * encryption involves a common limitation of cryptography hardwares and |
| 5286 | * an alternative implementation. */ |
| 5287 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5288 | { |
| 5289 | 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] | 5290 | 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] | 5291 | } |
| 5292 | |
| 5293 | PSA_ASSERT( status ); |
| 5294 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5295 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 5296 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5297 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 5298 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5299 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5300 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5301 | } |
| 5302 | else |
| 5303 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5304 | /* If length is zero, then this will return NULL. */ |
| 5305 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5306 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5307 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5308 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5309 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5310 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5311 | { |
| 5312 | nonce_buffer[index] = 'a' + index; |
| 5313 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5314 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5315 | } |
| 5316 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5317 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5318 | { |
| 5319 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5320 | input_data->len ) ); |
| 5321 | } |
| 5322 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5323 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5324 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5325 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5326 | |
| 5327 | if( expected_status == PSA_SUCCESS ) |
| 5328 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5329 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5330 | { |
| 5331 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5332 | input_data->len ) ); |
| 5333 | } |
| 5334 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5335 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5336 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5337 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5338 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5339 | additional_data->len ), |
| 5340 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5341 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5342 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5343 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5344 | &ciphertext_length ), |
| 5345 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5346 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5347 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5348 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5349 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5350 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5351 | } |
| 5352 | |
| 5353 | exit: |
| 5354 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5355 | mbedtls_free( output ); |
| 5356 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5357 | mbedtls_free( nonce_buffer ); |
| 5358 | psa_aead_abort( &operation ); |
| 5359 | PSA_DONE( ); |
| 5360 | } |
| 5361 | /* END_CASE */ |
| 5362 | |
| 5363 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5364 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5365 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5366 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5367 | data_t *nonce, |
| 5368 | data_t *additional_data, |
| 5369 | data_t *input_data, |
| 5370 | int expected_status_arg ) |
| 5371 | { |
| 5372 | |
| 5373 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5374 | psa_key_type_t key_type = key_type_arg; |
| 5375 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5376 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5377 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5378 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5379 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5380 | unsigned char *output = NULL; |
| 5381 | unsigned char *ciphertext = NULL; |
| 5382 | size_t output_size = output_size_arg; |
| 5383 | size_t ciphertext_size = 0; |
| 5384 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5385 | size_t tag_length = 0; |
| 5386 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5387 | |
| 5388 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5389 | |
| 5390 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5391 | psa_set_key_algorithm( &attributes, alg ); |
| 5392 | psa_set_key_type( &attributes, key_type ); |
| 5393 | |
| 5394 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5395 | &key ) ); |
| 5396 | |
| 5397 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5398 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5399 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5400 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5401 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5402 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5403 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5404 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5405 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5406 | |
| 5407 | /* If the operation is not supported, just skip and not fail in case the |
| 5408 | * encryption involves a common limitation of cryptography hardwares and |
| 5409 | * an alternative implementation. */ |
| 5410 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5411 | { |
| 5412 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5413 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5414 | } |
| 5415 | |
| 5416 | PSA_ASSERT( status ); |
| 5417 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5418 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5419 | input_data->len ) ); |
| 5420 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5421 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5422 | |
| 5423 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5424 | additional_data->len ) ); |
| 5425 | |
| 5426 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5427 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5428 | |
| 5429 | TEST_EQUAL( status, expected_status ); |
| 5430 | |
| 5431 | if( expected_status == PSA_SUCCESS ) |
| 5432 | { |
| 5433 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5434 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5435 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5436 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5437 | } |
| 5438 | |
| 5439 | exit: |
| 5440 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5441 | mbedtls_free( output ); |
| 5442 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5443 | psa_aead_abort( &operation ); |
| 5444 | PSA_DONE( ); |
| 5445 | } |
| 5446 | /* END_CASE */ |
| 5447 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5448 | /* BEGIN_CASE */ |
| 5449 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5450 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5451 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5452 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5453 | data_t *nonce, |
| 5454 | data_t *additional_data, |
| 5455 | data_t *input_data, |
| 5456 | int expected_status_arg ) |
| 5457 | { |
| 5458 | |
| 5459 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5460 | psa_key_type_t key_type = key_type_arg; |
| 5461 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5462 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5463 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5464 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5465 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5466 | unsigned char *ciphertext = NULL; |
| 5467 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5468 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5469 | size_t ciphertext_size = 0; |
| 5470 | size_t ciphertext_length = 0; |
| 5471 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5472 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5473 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5474 | |
| 5475 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5476 | |
| 5477 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5478 | psa_set_key_algorithm( &attributes, alg ); |
| 5479 | psa_set_key_type( &attributes, key_type ); |
| 5480 | |
| 5481 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5482 | &key ) ); |
| 5483 | |
| 5484 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5485 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5486 | 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] | 5487 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5488 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5489 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5490 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5491 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5492 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5493 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5494 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5495 | |
| 5496 | /* If the operation is not supported, just skip and not fail in case the |
| 5497 | * encryption involves a common limitation of cryptography hardwares and |
| 5498 | * an alternative implementation. */ |
| 5499 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5500 | { |
| 5501 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5502 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5503 | } |
| 5504 | |
| 5505 | PSA_ASSERT( status ); |
| 5506 | |
| 5507 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5508 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5509 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5510 | input_data->len ) ); |
| 5511 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5512 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5513 | additional_data->len ) ); |
| 5514 | |
| 5515 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5516 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5517 | |
| 5518 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5519 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5520 | finish_ciphertext_size, |
| 5521 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5522 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5523 | |
| 5524 | TEST_EQUAL( status, expected_status ); |
| 5525 | |
| 5526 | exit: |
| 5527 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5528 | mbedtls_free( ciphertext ); |
| 5529 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5530 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5531 | psa_aead_abort( &operation ); |
| 5532 | PSA_DONE( ); |
| 5533 | } |
| 5534 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5535 | |
| 5536 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5537 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5538 | int alg_arg, |
| 5539 | data_t *nonce, |
| 5540 | data_t *additional_data, |
| 5541 | data_t *input_data, |
| 5542 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5543 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5544 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5545 | int expected_status_arg ) |
| 5546 | { |
| 5547 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5548 | psa_key_type_t key_type = key_type_arg; |
| 5549 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5550 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5551 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5552 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5553 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5554 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5555 | unsigned char *plaintext = NULL; |
| 5556 | unsigned char *finish_plaintext = NULL; |
| 5557 | size_t plaintext_size = 0; |
| 5558 | size_t plaintext_length = 0; |
| 5559 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5560 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5561 | unsigned char *tag_buffer = NULL; |
| 5562 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5563 | |
| 5564 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5565 | |
| 5566 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5567 | psa_set_key_algorithm( &attributes, alg ); |
| 5568 | psa_set_key_type( &attributes, key_type ); |
| 5569 | |
| 5570 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5571 | &key ) ); |
| 5572 | |
| 5573 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5574 | |
| 5575 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5576 | input_data->len ); |
| 5577 | |
| 5578 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5579 | |
| 5580 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5581 | |
| 5582 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5583 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5584 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5585 | |
| 5586 | /* If the operation is not supported, just skip and not fail in case the |
| 5587 | * encryption involves a common limitation of cryptography hardwares and |
| 5588 | * an alternative implementation. */ |
| 5589 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5590 | { |
| 5591 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5592 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5593 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5594 | TEST_EQUAL( status, expected_setup_status ); |
| 5595 | |
| 5596 | if( status != PSA_SUCCESS ) |
| 5597 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5598 | |
| 5599 | PSA_ASSERT( status ); |
| 5600 | |
| 5601 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5602 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5603 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5604 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5605 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5606 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5607 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5608 | additional_data->len ) ); |
| 5609 | |
| 5610 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5611 | input_data->len, |
| 5612 | plaintext, plaintext_size, |
| 5613 | &plaintext_length ) ); |
| 5614 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5615 | if( tag_usage == USE_GIVEN_TAG ) |
| 5616 | { |
| 5617 | tag_buffer = tag->x; |
| 5618 | tag_size = tag->len; |
| 5619 | } |
| 5620 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5621 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5622 | verify_plaintext_size, |
| 5623 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5624 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5625 | |
| 5626 | TEST_EQUAL( status, expected_status ); |
| 5627 | |
| 5628 | exit: |
| 5629 | psa_destroy_key( key ); |
| 5630 | mbedtls_free( plaintext ); |
| 5631 | mbedtls_free( finish_plaintext ); |
| 5632 | psa_aead_abort( &operation ); |
| 5633 | PSA_DONE( ); |
| 5634 | } |
| 5635 | /* END_CASE */ |
| 5636 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5637 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5638 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5639 | int alg_arg, int expected_status_arg ) |
| 5640 | { |
| 5641 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5642 | psa_key_type_t key_type = key_type_arg; |
| 5643 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5644 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5645 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5646 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5647 | psa_status_t expected_status = expected_status_arg; |
| 5648 | |
| 5649 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5650 | |
| 5651 | psa_set_key_usage_flags( &attributes, |
| 5652 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5653 | psa_set_key_algorithm( &attributes, alg ); |
| 5654 | psa_set_key_type( &attributes, key_type ); |
| 5655 | |
| 5656 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5657 | &key ) ); |
| 5658 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5659 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5660 | |
| 5661 | TEST_EQUAL( status, expected_status ); |
| 5662 | |
| 5663 | psa_aead_abort( &operation ); |
| 5664 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5665 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5666 | |
| 5667 | TEST_EQUAL(status, expected_status ); |
| 5668 | |
| 5669 | exit: |
| 5670 | psa_destroy_key( key ); |
| 5671 | psa_aead_abort( &operation ); |
| 5672 | PSA_DONE( ); |
| 5673 | } |
| 5674 | /* END_CASE */ |
| 5675 | |
| 5676 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5677 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5678 | int alg_arg, |
| 5679 | data_t *nonce, |
| 5680 | data_t *additional_data, |
| 5681 | data_t *input_data ) |
| 5682 | { |
| 5683 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5684 | psa_key_type_t key_type = key_type_arg; |
| 5685 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5686 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5687 | unsigned char *output_data = NULL; |
| 5688 | unsigned char *final_data = NULL; |
| 5689 | size_t output_size = 0; |
| 5690 | size_t finish_output_size = 0; |
| 5691 | size_t output_length = 0; |
| 5692 | size_t key_bits = 0; |
| 5693 | size_t tag_length = 0; |
| 5694 | size_t tag_size = 0; |
| 5695 | size_t nonce_length = 0; |
| 5696 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5697 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5698 | size_t output_part_length = 0; |
| 5699 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5700 | |
| 5701 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5702 | |
| 5703 | psa_set_key_usage_flags( & attributes, |
| 5704 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5705 | psa_set_key_algorithm( & attributes, alg ); |
| 5706 | psa_set_key_type( & attributes, key_type ); |
| 5707 | |
| 5708 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5709 | &key ) ); |
| 5710 | |
| 5711 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5712 | key_bits = psa_get_key_bits( &attributes ); |
| 5713 | |
| 5714 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5715 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5716 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5717 | |
| 5718 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5719 | |
| 5720 | ASSERT_ALLOC( output_data, output_size ); |
| 5721 | |
| 5722 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5723 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5724 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5725 | |
| 5726 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5727 | |
| 5728 | /* Test all operations error without calling setup first. */ |
| 5729 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5730 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5731 | PSA_ERROR_BAD_STATE ); |
| 5732 | |
| 5733 | psa_aead_abort( &operation ); |
| 5734 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5735 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5736 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5737 | &nonce_length ), |
| 5738 | PSA_ERROR_BAD_STATE ); |
| 5739 | |
| 5740 | psa_aead_abort( &operation ); |
| 5741 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5742 | /* ------------------------------------------------------- */ |
| 5743 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5744 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5745 | input_data->len ), |
| 5746 | PSA_ERROR_BAD_STATE ); |
| 5747 | |
| 5748 | psa_aead_abort( &operation ); |
| 5749 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5750 | /* ------------------------------------------------------- */ |
| 5751 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5752 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5753 | additional_data->len ), |
| 5754 | PSA_ERROR_BAD_STATE ); |
| 5755 | |
| 5756 | psa_aead_abort( &operation ); |
| 5757 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5758 | /* ------------------------------------------------------- */ |
| 5759 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5760 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5761 | input_data->len, output_data, |
| 5762 | output_size, &output_length ), |
| 5763 | PSA_ERROR_BAD_STATE ); |
| 5764 | |
| 5765 | psa_aead_abort( &operation ); |
| 5766 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5767 | /* ------------------------------------------------------- */ |
| 5768 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5769 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5770 | finish_output_size, |
| 5771 | &output_part_length, |
| 5772 | tag_buffer, tag_length, |
| 5773 | &tag_size ), |
| 5774 | PSA_ERROR_BAD_STATE ); |
| 5775 | |
| 5776 | psa_aead_abort( &operation ); |
| 5777 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5778 | /* ------------------------------------------------------- */ |
| 5779 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5780 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5781 | finish_output_size, |
| 5782 | &output_part_length, |
| 5783 | tag_buffer, |
| 5784 | tag_length ), |
| 5785 | PSA_ERROR_BAD_STATE ); |
| 5786 | |
| 5787 | psa_aead_abort( &operation ); |
| 5788 | |
| 5789 | /* Test for double setups. */ |
| 5790 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5791 | PSA_ASSERT( psa_aead_encrypt_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 | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5798 | /* ------------------------------------------------------- */ |
| 5799 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5800 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5801 | |
| 5802 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5803 | PSA_ERROR_BAD_STATE ); |
| 5804 | |
| 5805 | psa_aead_abort( &operation ); |
| 5806 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5807 | /* ------------------------------------------------------- */ |
| 5808 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5809 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5810 | |
| 5811 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5812 | PSA_ERROR_BAD_STATE ); |
| 5813 | |
| 5814 | psa_aead_abort( &operation ); |
| 5815 | |
| 5816 | /* ------------------------------------------------------- */ |
| 5817 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5818 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5819 | |
| 5820 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5821 | PSA_ERROR_BAD_STATE ); |
| 5822 | |
| 5823 | psa_aead_abort( &operation ); |
| 5824 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5825 | /* Test for not setting a nonce. */ |
| 5826 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5827 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5828 | |
| 5829 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5830 | additional_data->len ), |
| 5831 | PSA_ERROR_BAD_STATE ); |
| 5832 | |
| 5833 | psa_aead_abort( &operation ); |
| 5834 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5835 | /* ------------------------------------------------------- */ |
| 5836 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5837 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5838 | |
| 5839 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5840 | input_data->len, output_data, |
| 5841 | output_size, &output_length ), |
| 5842 | PSA_ERROR_BAD_STATE ); |
| 5843 | |
| 5844 | psa_aead_abort( &operation ); |
| 5845 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5846 | /* ------------------------------------------------------- */ |
| 5847 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5848 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5849 | |
| 5850 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5851 | finish_output_size, |
| 5852 | &output_part_length, |
| 5853 | tag_buffer, tag_length, |
| 5854 | &tag_size ), |
| 5855 | PSA_ERROR_BAD_STATE ); |
| 5856 | |
| 5857 | psa_aead_abort( &operation ); |
| 5858 | |
| 5859 | /* ------------------------------------------------------- */ |
| 5860 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5861 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5862 | |
| 5863 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5864 | finish_output_size, |
| 5865 | &output_part_length, |
| 5866 | tag_buffer, |
| 5867 | tag_length ), |
| 5868 | PSA_ERROR_BAD_STATE ); |
| 5869 | |
| 5870 | psa_aead_abort( &operation ); |
| 5871 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5872 | /* Test for double setting nonce. */ |
| 5873 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5874 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5875 | |
| 5876 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5877 | |
| 5878 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5879 | PSA_ERROR_BAD_STATE ); |
| 5880 | |
| 5881 | psa_aead_abort( &operation ); |
| 5882 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5883 | /* Test for double generating nonce. */ |
| 5884 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5885 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5886 | |
| 5887 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5888 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5889 | &nonce_length ) ); |
| 5890 | |
| 5891 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5892 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5893 | &nonce_length ), |
| 5894 | PSA_ERROR_BAD_STATE ); |
| 5895 | |
| 5896 | |
| 5897 | psa_aead_abort( &operation ); |
| 5898 | |
| 5899 | /* Test for generate nonce then set and vice versa */ |
| 5900 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5901 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5902 | |
| 5903 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5904 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5905 | &nonce_length ) ); |
| 5906 | |
| 5907 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5908 | PSA_ERROR_BAD_STATE ); |
| 5909 | |
| 5910 | psa_aead_abort( &operation ); |
| 5911 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5912 | /* Test for generating nonce after calling set lengths */ |
| 5913 | |
| 5914 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5915 | |
| 5916 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5917 | input_data->len ) ); |
| 5918 | |
| 5919 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5920 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5921 | &nonce_length ) ); |
| 5922 | |
| 5923 | psa_aead_abort( &operation ); |
| 5924 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5925 | /* 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] | 5926 | |
| 5927 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5928 | |
| 5929 | if( operation.alg == PSA_ALG_CCM ) |
| 5930 | { |
| 5931 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5932 | input_data->len ), |
| 5933 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5934 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5935 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5936 | &nonce_length ), |
| 5937 | PSA_ERROR_BAD_STATE ); |
| 5938 | } |
| 5939 | else |
| 5940 | { |
| 5941 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5942 | input_data->len ) ); |
| 5943 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5944 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5945 | &nonce_length ) ); |
| 5946 | } |
| 5947 | |
| 5948 | psa_aead_abort( &operation ); |
| 5949 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5950 | /* 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] | 5951 | #if SIZE_MAX > UINT32_MAX |
| 5952 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5953 | |
| 5954 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5955 | { |
| 5956 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5957 | input_data->len ), |
| 5958 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5959 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5960 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5961 | &nonce_length ), |
| 5962 | PSA_ERROR_BAD_STATE ); |
| 5963 | } |
| 5964 | else |
| 5965 | { |
| 5966 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5967 | input_data->len ) ); |
| 5968 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5969 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5970 | &nonce_length ) ); |
| 5971 | } |
| 5972 | |
| 5973 | psa_aead_abort( &operation ); |
| 5974 | #endif |
| 5975 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5976 | /* 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] | 5977 | |
| 5978 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5979 | |
| 5980 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5981 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5982 | &nonce_length ) ); |
| 5983 | |
| 5984 | if( operation.alg == PSA_ALG_CCM ) |
| 5985 | { |
| 5986 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5987 | input_data->len ), |
| 5988 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5989 | } |
| 5990 | else |
| 5991 | { |
| 5992 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5993 | input_data->len ) ); |
| 5994 | } |
| 5995 | |
| 5996 | psa_aead_abort( &operation ); |
| 5997 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5998 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5999 | /* Test for setting nonce after calling set lengths */ |
| 6000 | |
| 6001 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6002 | |
| 6003 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6004 | input_data->len ) ); |
| 6005 | |
| 6006 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6007 | |
| 6008 | psa_aead_abort( &operation ); |
| 6009 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6010 | /* 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] | 6011 | |
| 6012 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6013 | |
| 6014 | if( operation.alg == PSA_ALG_CCM ) |
| 6015 | { |
| 6016 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6017 | input_data->len ), |
| 6018 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6019 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6020 | PSA_ERROR_BAD_STATE ); |
| 6021 | } |
| 6022 | else |
| 6023 | { |
| 6024 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6025 | input_data->len ) ); |
| 6026 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6027 | } |
| 6028 | |
| 6029 | psa_aead_abort( &operation ); |
| 6030 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6031 | /* 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] | 6032 | #if SIZE_MAX > UINT32_MAX |
| 6033 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6034 | |
| 6035 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 6036 | { |
| 6037 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6038 | input_data->len ), |
| 6039 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6040 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6041 | PSA_ERROR_BAD_STATE ); |
| 6042 | } |
| 6043 | else |
| 6044 | { |
| 6045 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6046 | input_data->len ) ); |
| 6047 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6048 | } |
| 6049 | |
| 6050 | psa_aead_abort( &operation ); |
| 6051 | #endif |
| 6052 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6053 | /* 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] | 6054 | |
| 6055 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6056 | |
| 6057 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6058 | |
| 6059 | if( operation.alg == PSA_ALG_CCM ) |
| 6060 | { |
| 6061 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6062 | input_data->len ), |
| 6063 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6064 | } |
| 6065 | else |
| 6066 | { |
| 6067 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6068 | input_data->len ) ); |
| 6069 | } |
| 6070 | |
| 6071 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6072 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6073 | /* 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] | 6074 | #if SIZE_MAX > UINT32_MAX |
| 6075 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6076 | |
| 6077 | if( operation.alg == PSA_ALG_GCM ) |
| 6078 | { |
| 6079 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6080 | SIZE_MAX ), |
| 6081 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6082 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6083 | PSA_ERROR_BAD_STATE ); |
| 6084 | } |
| 6085 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6086 | { |
| 6087 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6088 | SIZE_MAX ) ); |
| 6089 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6090 | } |
| 6091 | |
| 6092 | psa_aead_abort( &operation ); |
| 6093 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6094 | /* 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] | 6095 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6096 | |
| 6097 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6098 | |
| 6099 | if( operation.alg == PSA_ALG_GCM ) |
| 6100 | { |
| 6101 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6102 | SIZE_MAX ), |
| 6103 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6104 | } |
| 6105 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6106 | { |
| 6107 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6108 | SIZE_MAX ) ); |
| 6109 | } |
| 6110 | |
| 6111 | psa_aead_abort( &operation ); |
| 6112 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6113 | |
| 6114 | /* ------------------------------------------------------- */ |
| 6115 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6116 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6117 | |
| 6118 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6119 | |
| 6120 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6121 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6122 | &nonce_length ), |
| 6123 | PSA_ERROR_BAD_STATE ); |
| 6124 | |
| 6125 | psa_aead_abort( &operation ); |
| 6126 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6127 | /* Test for generating nonce in decrypt setup. */ |
| 6128 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6129 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6130 | |
| 6131 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6132 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6133 | &nonce_length ), |
| 6134 | PSA_ERROR_BAD_STATE ); |
| 6135 | |
| 6136 | psa_aead_abort( &operation ); |
| 6137 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6138 | /* Test for setting lengths twice. */ |
| 6139 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6140 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6141 | |
| 6142 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6143 | |
| 6144 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6145 | input_data->len ) ); |
| 6146 | |
| 6147 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6148 | input_data->len ), |
| 6149 | PSA_ERROR_BAD_STATE ); |
| 6150 | |
| 6151 | psa_aead_abort( &operation ); |
| 6152 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6153 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6154 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6155 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6156 | |
| 6157 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6158 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6159 | if( operation.alg == PSA_ALG_CCM ) |
| 6160 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6161 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6162 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6163 | additional_data->len ), |
| 6164 | PSA_ERROR_BAD_STATE ); |
| 6165 | } |
| 6166 | else |
| 6167 | { |
| 6168 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6169 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6170 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6171 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6172 | input_data->len ), |
| 6173 | PSA_ERROR_BAD_STATE ); |
| 6174 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6175 | psa_aead_abort( &operation ); |
| 6176 | |
| 6177 | /* ------------------------------------------------------- */ |
| 6178 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6179 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6180 | |
| 6181 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6182 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6183 | if( operation.alg == PSA_ALG_CCM ) |
| 6184 | { |
| 6185 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6186 | input_data->len, output_data, |
| 6187 | output_size, &output_length ), |
| 6188 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6189 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6190 | } |
| 6191 | else |
| 6192 | { |
| 6193 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6194 | input_data->len, output_data, |
| 6195 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6196 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6197 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6198 | input_data->len ), |
| 6199 | PSA_ERROR_BAD_STATE ); |
| 6200 | } |
| 6201 | psa_aead_abort( &operation ); |
| 6202 | |
| 6203 | /* ------------------------------------------------------- */ |
| 6204 | |
| 6205 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6206 | |
| 6207 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6208 | |
| 6209 | if( operation.alg == PSA_ALG_CCM ) |
| 6210 | { |
| 6211 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6212 | finish_output_size, |
| 6213 | &output_part_length, |
| 6214 | tag_buffer, tag_length, |
| 6215 | &tag_size ) ); |
| 6216 | } |
| 6217 | else |
| 6218 | { |
| 6219 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6220 | finish_output_size, |
| 6221 | &output_part_length, |
| 6222 | tag_buffer, tag_length, |
| 6223 | &tag_size ) ); |
| 6224 | |
| 6225 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6226 | input_data->len ), |
| 6227 | PSA_ERROR_BAD_STATE ); |
| 6228 | } |
| 6229 | psa_aead_abort( &operation ); |
| 6230 | |
| 6231 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6232 | |
| 6233 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6234 | |
| 6235 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6236 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6237 | &nonce_length ) ); |
| 6238 | if( operation.alg == PSA_ALG_CCM ) |
| 6239 | { |
| 6240 | |
| 6241 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6242 | additional_data->len ), |
| 6243 | PSA_ERROR_BAD_STATE ); |
| 6244 | } |
| 6245 | else |
| 6246 | { |
| 6247 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6248 | additional_data->len ) ); |
| 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 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6266 | input_data->len, output_data, |
| 6267 | output_size, &output_length ), |
| 6268 | PSA_ERROR_BAD_STATE ); |
| 6269 | |
| 6270 | } |
| 6271 | else |
| 6272 | { |
| 6273 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6274 | input_data->len, output_data, |
| 6275 | output_size, &output_length ) ); |
| 6276 | |
| 6277 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6278 | input_data->len ), |
| 6279 | PSA_ERROR_BAD_STATE ); |
| 6280 | } |
| 6281 | psa_aead_abort( &operation ); |
| 6282 | |
| 6283 | /* ------------------------------------------------------- */ |
| 6284 | |
| 6285 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6286 | |
| 6287 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6288 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6289 | &nonce_length ) ); |
| 6290 | if( operation.alg == PSA_ALG_CCM ) |
| 6291 | { |
| 6292 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6293 | finish_output_size, |
| 6294 | &output_part_length, |
| 6295 | tag_buffer, tag_length, |
| 6296 | &tag_size ) ); |
| 6297 | } |
| 6298 | else |
| 6299 | { |
| 6300 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6301 | finish_output_size, |
| 6302 | &output_part_length, |
| 6303 | tag_buffer, tag_length, |
| 6304 | &tag_size ) ); |
| 6305 | |
| 6306 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6307 | input_data->len ), |
| 6308 | PSA_ERROR_BAD_STATE ); |
| 6309 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6310 | psa_aead_abort( &operation ); |
| 6311 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6312 | /* Test for not sending any additional data or data after setting non zero |
| 6313 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6314 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6315 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6316 | |
| 6317 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6318 | |
| 6319 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6320 | input_data->len ) ); |
| 6321 | |
| 6322 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6323 | finish_output_size, |
| 6324 | &output_part_length, |
| 6325 | tag_buffer, tag_length, |
| 6326 | &tag_size ), |
| 6327 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6328 | |
| 6329 | psa_aead_abort( &operation ); |
| 6330 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6331 | /* Test for not sending any additional data or data after setting non-zero |
| 6332 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6333 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6334 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6335 | |
| 6336 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6337 | |
| 6338 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6339 | input_data->len ) ); |
| 6340 | |
| 6341 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6342 | finish_output_size, |
| 6343 | &output_part_length, |
| 6344 | tag_buffer, |
| 6345 | tag_length ), |
| 6346 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6347 | |
| 6348 | psa_aead_abort( &operation ); |
| 6349 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6350 | /* Test for not sending any additional data after setting a non-zero length |
| 6351 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6352 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6353 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6354 | |
| 6355 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6356 | |
| 6357 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6358 | input_data->len ) ); |
| 6359 | |
| 6360 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6361 | input_data->len, output_data, |
| 6362 | output_size, &output_length ), |
| 6363 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6364 | |
| 6365 | psa_aead_abort( &operation ); |
| 6366 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6367 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6368 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6369 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6370 | |
| 6371 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6372 | |
| 6373 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6374 | input_data->len ) ); |
| 6375 | |
| 6376 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6377 | additional_data->len ) ); |
| 6378 | |
| 6379 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6380 | finish_output_size, |
| 6381 | &output_part_length, |
| 6382 | tag_buffer, tag_length, |
| 6383 | &tag_size ), |
| 6384 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6385 | |
| 6386 | psa_aead_abort( &operation ); |
| 6387 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6388 | /* Test for sending too much additional data after setting lengths. */ |
| 6389 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6390 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6391 | |
| 6392 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6393 | |
| 6394 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6395 | |
| 6396 | |
| 6397 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6398 | additional_data->len ), |
| 6399 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6400 | |
| 6401 | psa_aead_abort( &operation ); |
| 6402 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6403 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6404 | |
| 6405 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6406 | |
| 6407 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6408 | |
| 6409 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6410 | input_data->len ) ); |
| 6411 | |
| 6412 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6413 | additional_data->len ) ); |
| 6414 | |
| 6415 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6416 | 1 ), |
| 6417 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6418 | |
| 6419 | psa_aead_abort( &operation ); |
| 6420 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6421 | /* Test for sending too much data after setting lengths. */ |
| 6422 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6423 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6424 | |
| 6425 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6426 | |
| 6427 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6428 | |
| 6429 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6430 | input_data->len, output_data, |
| 6431 | output_size, &output_length ), |
| 6432 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6433 | |
| 6434 | psa_aead_abort( &operation ); |
| 6435 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6436 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6437 | |
| 6438 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6439 | |
| 6440 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6441 | |
| 6442 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6443 | input_data->len ) ); |
| 6444 | |
| 6445 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6446 | additional_data->len ) ); |
| 6447 | |
| 6448 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6449 | input_data->len, output_data, |
| 6450 | output_size, &output_length ) ); |
| 6451 | |
| 6452 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6453 | 1, output_data, |
| 6454 | output_size, &output_length ), |
| 6455 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6456 | |
| 6457 | psa_aead_abort( &operation ); |
| 6458 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6459 | /* Test sending additional data after data. */ |
| 6460 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6461 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6462 | |
| 6463 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6464 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6465 | if( operation.alg != PSA_ALG_CCM ) |
| 6466 | { |
| 6467 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6468 | input_data->len, output_data, |
| 6469 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6470 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6471 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6472 | additional_data->len ), |
| 6473 | PSA_ERROR_BAD_STATE ); |
| 6474 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6475 | psa_aead_abort( &operation ); |
| 6476 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6477 | /* Test calling finish on decryption. */ |
| 6478 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6479 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6480 | |
| 6481 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6482 | |
| 6483 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6484 | finish_output_size, |
| 6485 | &output_part_length, |
| 6486 | tag_buffer, tag_length, |
| 6487 | &tag_size ), |
| 6488 | PSA_ERROR_BAD_STATE ); |
| 6489 | |
| 6490 | psa_aead_abort( &operation ); |
| 6491 | |
| 6492 | /* Test calling verify on encryption. */ |
| 6493 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6494 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6495 | |
| 6496 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6497 | |
| 6498 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6499 | finish_output_size, |
| 6500 | &output_part_length, |
| 6501 | tag_buffer, |
| 6502 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6503 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6504 | |
| 6505 | psa_aead_abort( &operation ); |
| 6506 | |
| 6507 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6508 | exit: |
| 6509 | psa_destroy_key( key ); |
| 6510 | psa_aead_abort( &operation ); |
| 6511 | mbedtls_free( output_data ); |
| 6512 | mbedtls_free( final_data ); |
| 6513 | PSA_DONE( ); |
| 6514 | } |
| 6515 | /* END_CASE */ |
| 6516 | |
| 6517 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6518 | void signature_size( int type_arg, |
| 6519 | int bits, |
| 6520 | int alg_arg, |
| 6521 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6522 | { |
| 6523 | psa_key_type_t type = type_arg; |
| 6524 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6525 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6526 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6527 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6528 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6529 | exit: |
| 6530 | ; |
| 6531 | } |
| 6532 | /* END_CASE */ |
| 6533 | |
| 6534 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6535 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6536 | int alg_arg, data_t *input_data, |
| 6537 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6538 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6539 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6540 | psa_key_type_t key_type = key_type_arg; |
| 6541 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6542 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6543 | unsigned char *signature = NULL; |
| 6544 | size_t signature_size; |
| 6545 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6546 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6547 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6548 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6549 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6550 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6551 | psa_set_key_algorithm( &attributes, alg ); |
| 6552 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6553 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6554 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6555 | &key ) ); |
| 6556 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6557 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6558 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6559 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6560 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6561 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6562 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6563 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6564 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6565 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6566 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6567 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6568 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6569 | input_data->x, input_data->len, |
| 6570 | signature, signature_size, |
| 6571 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6572 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6573 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6574 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6575 | |
| 6576 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6577 | /* |
| 6578 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6579 | * thus reset them as required. |
| 6580 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6581 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6582 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6583 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6584 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6585 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6586 | } |
| 6587 | /* END_CASE */ |
| 6588 | |
| 6589 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6590 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6591 | int alg_arg, data_t *input_data, |
| 6592 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6593 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6594 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6595 | psa_key_type_t key_type = key_type_arg; |
| 6596 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6597 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6598 | psa_status_t actual_status; |
| 6599 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6600 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6601 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6602 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6603 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6604 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6605 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6606 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6607 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6608 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6609 | psa_set_key_algorithm( &attributes, alg ); |
| 6610 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6611 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6612 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6613 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6614 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6615 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6616 | input_data->x, input_data->len, |
| 6617 | signature, signature_size, |
| 6618 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6619 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6620 | /* The value of *signature_length is unspecified on error, but |
| 6621 | * whatever it is, it should be less than signature_size, so that |
| 6622 | * if the caller tries to read *signature_length bytes without |
| 6623 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6624 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6625 | |
| 6626 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6627 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6628 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6629 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6630 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6631 | } |
| 6632 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6633 | |
| 6634 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6635 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6636 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6637 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6638 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6639 | psa_key_type_t key_type = key_type_arg; |
| 6640 | psa_algorithm_t alg = alg_arg; |
| 6641 | size_t key_bits; |
| 6642 | unsigned char *signature = NULL; |
| 6643 | size_t signature_size; |
| 6644 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6645 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6646 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6647 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6648 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6649 | 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] | 6650 | psa_set_key_algorithm( &attributes, alg ); |
| 6651 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6652 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6653 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6654 | &key ) ); |
| 6655 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6656 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6657 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6658 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6659 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6660 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6661 | key_bits, alg ); |
| 6662 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6663 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6664 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6665 | |
| 6666 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6667 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6668 | input_data->x, input_data->len, |
| 6669 | signature, signature_size, |
| 6670 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6671 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6672 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6673 | TEST_ASSERT( signature_length > 0 ); |
| 6674 | |
| 6675 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6676 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6677 | input_data->x, input_data->len, |
| 6678 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6679 | |
| 6680 | if( input_data->len != 0 ) |
| 6681 | { |
| 6682 | /* Flip a bit in the input and verify that the signature is now |
| 6683 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6684 | * because ECDSA may ignore the last few bits of the input. */ |
| 6685 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6686 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6687 | input_data->x, input_data->len, |
| 6688 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6689 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6690 | } |
| 6691 | |
| 6692 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6693 | /* |
| 6694 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6695 | * thus reset them as required. |
| 6696 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6697 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6698 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6699 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6700 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6701 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6702 | } |
| 6703 | /* END_CASE */ |
| 6704 | |
| 6705 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6706 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6707 | int alg_arg, data_t *hash_data, |
| 6708 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6709 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6710 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6711 | psa_key_type_t key_type = key_type_arg; |
| 6712 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6713 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6714 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6715 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6716 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6717 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6718 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6719 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6720 | psa_set_key_algorithm( &attributes, alg ); |
| 6721 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6722 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6723 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6724 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6725 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6726 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6727 | hash_data->x, hash_data->len, |
| 6728 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6729 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6730 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6731 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6732 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6733 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6734 | } |
| 6735 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6736 | |
| 6737 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6738 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6739 | int alg_arg, data_t *hash_data, |
| 6740 | data_t *signature_data, |
| 6741 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6742 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6743 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6744 | psa_key_type_t key_type = key_type_arg; |
| 6745 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6746 | psa_status_t actual_status; |
| 6747 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6748 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6749 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6750 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6751 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6752 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6753 | psa_set_key_algorithm( &attributes, alg ); |
| 6754 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6755 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6756 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6757 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6758 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6759 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6760 | hash_data->x, hash_data->len, |
| 6761 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6762 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6763 | |
| 6764 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6765 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6766 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6767 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6768 | } |
| 6769 | /* END_CASE */ |
| 6770 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6771 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6772 | void sign_message_deterministic( int key_type_arg, |
| 6773 | data_t *key_data, |
| 6774 | int alg_arg, |
| 6775 | data_t *input_data, |
| 6776 | data_t *output_data ) |
| 6777 | { |
| 6778 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6779 | psa_key_type_t key_type = key_type_arg; |
| 6780 | psa_algorithm_t alg = alg_arg; |
| 6781 | size_t key_bits; |
| 6782 | unsigned char *signature = NULL; |
| 6783 | size_t signature_size; |
| 6784 | size_t signature_length = 0xdeadbeef; |
| 6785 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6786 | |
| 6787 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6788 | |
| 6789 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6790 | psa_set_key_algorithm( &attributes, alg ); |
| 6791 | psa_set_key_type( &attributes, key_type ); |
| 6792 | |
| 6793 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6794 | &key ) ); |
| 6795 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6796 | key_bits = psa_get_key_bits( &attributes ); |
| 6797 | |
| 6798 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6799 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6800 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6801 | ASSERT_ALLOC( signature, signature_size ); |
| 6802 | |
| 6803 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6804 | input_data->x, input_data->len, |
| 6805 | signature, signature_size, |
| 6806 | &signature_length ) ); |
| 6807 | |
| 6808 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6809 | signature, signature_length ); |
| 6810 | |
| 6811 | exit: |
| 6812 | psa_reset_key_attributes( &attributes ); |
| 6813 | |
| 6814 | psa_destroy_key( key ); |
| 6815 | mbedtls_free( signature ); |
| 6816 | PSA_DONE( ); |
| 6817 | |
| 6818 | } |
| 6819 | /* END_CASE */ |
| 6820 | |
| 6821 | /* BEGIN_CASE */ |
| 6822 | void sign_message_fail( int key_type_arg, |
| 6823 | data_t *key_data, |
| 6824 | int alg_arg, |
| 6825 | data_t *input_data, |
| 6826 | int signature_size_arg, |
| 6827 | int expected_status_arg ) |
| 6828 | { |
| 6829 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6830 | psa_key_type_t key_type = key_type_arg; |
| 6831 | psa_algorithm_t alg = alg_arg; |
| 6832 | size_t signature_size = signature_size_arg; |
| 6833 | psa_status_t actual_status; |
| 6834 | psa_status_t expected_status = expected_status_arg; |
| 6835 | unsigned char *signature = NULL; |
| 6836 | size_t signature_length = 0xdeadbeef; |
| 6837 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6838 | |
| 6839 | ASSERT_ALLOC( signature, signature_size ); |
| 6840 | |
| 6841 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6842 | |
| 6843 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6844 | psa_set_key_algorithm( &attributes, alg ); |
| 6845 | psa_set_key_type( &attributes, key_type ); |
| 6846 | |
| 6847 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6848 | &key ) ); |
| 6849 | |
| 6850 | actual_status = psa_sign_message( key, alg, |
| 6851 | input_data->x, input_data->len, |
| 6852 | signature, signature_size, |
| 6853 | &signature_length ); |
| 6854 | TEST_EQUAL( actual_status, expected_status ); |
| 6855 | /* The value of *signature_length is unspecified on error, but |
| 6856 | * whatever it is, it should be less than signature_size, so that |
| 6857 | * if the caller tries to read *signature_length bytes without |
| 6858 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6859 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6860 | |
| 6861 | exit: |
| 6862 | psa_reset_key_attributes( &attributes ); |
| 6863 | psa_destroy_key( key ); |
| 6864 | mbedtls_free( signature ); |
| 6865 | PSA_DONE( ); |
| 6866 | } |
| 6867 | /* END_CASE */ |
| 6868 | |
| 6869 | /* BEGIN_CASE */ |
| 6870 | void sign_verify_message( int key_type_arg, |
| 6871 | data_t *key_data, |
| 6872 | int alg_arg, |
| 6873 | data_t *input_data ) |
| 6874 | { |
| 6875 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6876 | psa_key_type_t key_type = key_type_arg; |
| 6877 | psa_algorithm_t alg = alg_arg; |
| 6878 | size_t key_bits; |
| 6879 | unsigned char *signature = NULL; |
| 6880 | size_t signature_size; |
| 6881 | size_t signature_length = 0xdeadbeef; |
| 6882 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6883 | |
| 6884 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6885 | |
| 6886 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6887 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6888 | psa_set_key_algorithm( &attributes, alg ); |
| 6889 | psa_set_key_type( &attributes, key_type ); |
| 6890 | |
| 6891 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6892 | &key ) ); |
| 6893 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6894 | key_bits = psa_get_key_bits( &attributes ); |
| 6895 | |
| 6896 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6897 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6898 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6899 | ASSERT_ALLOC( signature, signature_size ); |
| 6900 | |
| 6901 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6902 | input_data->x, input_data->len, |
| 6903 | signature, signature_size, |
| 6904 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6905 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6906 | TEST_ASSERT( signature_length > 0 ); |
| 6907 | |
| 6908 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6909 | input_data->x, input_data->len, |
| 6910 | signature, signature_length ) ); |
| 6911 | |
| 6912 | if( input_data->len != 0 ) |
| 6913 | { |
| 6914 | /* Flip a bit in the input and verify that the signature is now |
| 6915 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6916 | * because ECDSA may ignore the last few bits of the input. */ |
| 6917 | input_data->x[0] ^= 1; |
| 6918 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6919 | input_data->x, input_data->len, |
| 6920 | signature, signature_length ), |
| 6921 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6922 | } |
| 6923 | |
| 6924 | exit: |
| 6925 | psa_reset_key_attributes( &attributes ); |
| 6926 | |
| 6927 | psa_destroy_key( key ); |
| 6928 | mbedtls_free( signature ); |
| 6929 | PSA_DONE( ); |
| 6930 | } |
| 6931 | /* END_CASE */ |
| 6932 | |
| 6933 | /* BEGIN_CASE */ |
| 6934 | void verify_message( int key_type_arg, |
| 6935 | data_t *key_data, |
| 6936 | int alg_arg, |
| 6937 | data_t *input_data, |
| 6938 | data_t *signature_data ) |
| 6939 | { |
| 6940 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6941 | psa_key_type_t key_type = key_type_arg; |
| 6942 | psa_algorithm_t alg = alg_arg; |
| 6943 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6944 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6945 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6946 | |
| 6947 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6948 | |
| 6949 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6950 | psa_set_key_algorithm( &attributes, alg ); |
| 6951 | psa_set_key_type( &attributes, key_type ); |
| 6952 | |
| 6953 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6954 | &key ) ); |
| 6955 | |
| 6956 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6957 | input_data->x, input_data->len, |
| 6958 | signature_data->x, signature_data->len ) ); |
| 6959 | |
| 6960 | exit: |
| 6961 | psa_reset_key_attributes( &attributes ); |
| 6962 | psa_destroy_key( key ); |
| 6963 | PSA_DONE( ); |
| 6964 | } |
| 6965 | /* END_CASE */ |
| 6966 | |
| 6967 | /* BEGIN_CASE */ |
| 6968 | void verify_message_fail( int key_type_arg, |
| 6969 | data_t *key_data, |
| 6970 | int alg_arg, |
| 6971 | data_t *hash_data, |
| 6972 | data_t *signature_data, |
| 6973 | int expected_status_arg ) |
| 6974 | { |
| 6975 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6976 | psa_key_type_t key_type = key_type_arg; |
| 6977 | psa_algorithm_t alg = alg_arg; |
| 6978 | psa_status_t actual_status; |
| 6979 | psa_status_t expected_status = expected_status_arg; |
| 6980 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6981 | |
| 6982 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6983 | |
| 6984 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6985 | psa_set_key_algorithm( &attributes, alg ); |
| 6986 | psa_set_key_type( &attributes, key_type ); |
| 6987 | |
| 6988 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6989 | &key ) ); |
| 6990 | |
| 6991 | actual_status = psa_verify_message( key, alg, |
| 6992 | hash_data->x, hash_data->len, |
| 6993 | signature_data->x, |
| 6994 | signature_data->len ); |
| 6995 | TEST_EQUAL( actual_status, expected_status ); |
| 6996 | |
| 6997 | exit: |
| 6998 | psa_reset_key_attributes( &attributes ); |
| 6999 | psa_destroy_key( key ); |
| 7000 | PSA_DONE( ); |
| 7001 | } |
| 7002 | /* END_CASE */ |
| 7003 | |
| 7004 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7005 | void asymmetric_encrypt( int key_type_arg, |
| 7006 | data_t *key_data, |
| 7007 | int alg_arg, |
| 7008 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7009 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7010 | int expected_output_length_arg, |
| 7011 | int expected_status_arg ) |
| 7012 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7013 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7014 | psa_key_type_t key_type = key_type_arg; |
| 7015 | psa_algorithm_t alg = alg_arg; |
| 7016 | size_t expected_output_length = expected_output_length_arg; |
| 7017 | size_t key_bits; |
| 7018 | unsigned char *output = NULL; |
| 7019 | size_t output_size; |
| 7020 | size_t output_length = ~0; |
| 7021 | psa_status_t actual_status; |
| 7022 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7023 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7024 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7025 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 7026 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7027 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7028 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7029 | psa_set_key_algorithm( &attributes, alg ); |
| 7030 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7031 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7032 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7033 | |
| 7034 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7035 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7036 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7037 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7038 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7039 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7040 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7041 | |
| 7042 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7043 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7044 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7045 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7046 | output, output_size, |
| 7047 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7048 | TEST_EQUAL( actual_status, expected_status ); |
| 7049 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7050 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7051 | /* If the label is empty, the test framework puts a non-null pointer |
| 7052 | * in label->x. Test that a null pointer works as well. */ |
| 7053 | if( label->len == 0 ) |
| 7054 | { |
| 7055 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7056 | if( output_size != 0 ) |
| 7057 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7058 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7059 | input_data->x, input_data->len, |
| 7060 | NULL, label->len, |
| 7061 | output, output_size, |
| 7062 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7063 | TEST_EQUAL( actual_status, expected_status ); |
| 7064 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7065 | } |
| 7066 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7067 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7068 | /* |
| 7069 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7070 | * thus reset them as required. |
| 7071 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7072 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7073 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7074 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7075 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7076 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7077 | } |
| 7078 | /* END_CASE */ |
| 7079 | |
| 7080 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7081 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 7082 | data_t *key_data, |
| 7083 | int alg_arg, |
| 7084 | data_t *input_data, |
| 7085 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7086 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7087 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7088 | psa_key_type_t key_type = key_type_arg; |
| 7089 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7090 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7091 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7092 | size_t output_size; |
| 7093 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7094 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7095 | size_t output2_size; |
| 7096 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7097 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7098 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7099 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7100 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7101 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 7102 | psa_set_key_algorithm( &attributes, alg ); |
| 7103 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7104 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7105 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7106 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7107 | |
| 7108 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7109 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7110 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7111 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7112 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7113 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7114 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7115 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7116 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7117 | TEST_LE_U( output2_size, |
| 7118 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 7119 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7120 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7121 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7122 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7123 | * the original plaintext because of the non-optional random |
| 7124 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7125 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7126 | input_data->x, input_data->len, |
| 7127 | label->x, label->len, |
| 7128 | output, output_size, |
| 7129 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7130 | /* We don't know what ciphertext length to expect, but check that |
| 7131 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7132 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7133 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7134 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7135 | output, output_length, |
| 7136 | label->x, label->len, |
| 7137 | output2, output2_size, |
| 7138 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7139 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 7140 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7141 | |
| 7142 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7143 | /* |
| 7144 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7145 | * thus reset them as required. |
| 7146 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7147 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7148 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7149 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7150 | mbedtls_free( output ); |
| 7151 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7152 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7153 | } |
| 7154 | /* END_CASE */ |
| 7155 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7156 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7157 | void asymmetric_decrypt( int key_type_arg, |
| 7158 | data_t *key_data, |
| 7159 | int alg_arg, |
| 7160 | data_t *input_data, |
| 7161 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 7162 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7163 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7164 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7165 | psa_key_type_t key_type = key_type_arg; |
| 7166 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7167 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7168 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7169 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7170 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7171 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7172 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7173 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7174 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7175 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7176 | psa_set_key_algorithm( &attributes, alg ); |
| 7177 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7178 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7179 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7180 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7181 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7182 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 7183 | key_bits = psa_get_key_bits( &attributes ); |
| 7184 | |
| 7185 | /* Determine the maximum ciphertext length */ |
| 7186 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7187 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7188 | ASSERT_ALLOC( output, output_size ); |
| 7189 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7190 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7191 | input_data->x, input_data->len, |
| 7192 | label->x, label->len, |
| 7193 | output, |
| 7194 | output_size, |
| 7195 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7196 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7197 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7198 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7199 | /* If the label is empty, the test framework puts a non-null pointer |
| 7200 | * in label->x. Test that a null pointer works as well. */ |
| 7201 | if( label->len == 0 ) |
| 7202 | { |
| 7203 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7204 | if( output_size != 0 ) |
| 7205 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7206 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7207 | input_data->x, input_data->len, |
| 7208 | NULL, label->len, |
| 7209 | output, |
| 7210 | output_size, |
| 7211 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7212 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7213 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7214 | } |
| 7215 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7216 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7217 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7218 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7219 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7220 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7221 | } |
| 7222 | /* END_CASE */ |
| 7223 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7224 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7225 | void asymmetric_decrypt_fail( int key_type_arg, |
| 7226 | data_t *key_data, |
| 7227 | int alg_arg, |
| 7228 | data_t *input_data, |
| 7229 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7230 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7231 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7232 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7233 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7234 | psa_key_type_t key_type = key_type_arg; |
| 7235 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7236 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7237 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7238 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7239 | psa_status_t actual_status; |
| 7240 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7241 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7242 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7243 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7244 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7245 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7246 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7247 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7248 | psa_set_key_algorithm( &attributes, alg ); |
| 7249 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7250 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7251 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7252 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7253 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7254 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7255 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7256 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7257 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7258 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7259 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7260 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7261 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7262 | /* If the label is empty, the test framework puts a non-null pointer |
| 7263 | * in label->x. Test that a null pointer works as well. */ |
| 7264 | if( label->len == 0 ) |
| 7265 | { |
| 7266 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7267 | if( output_size != 0 ) |
| 7268 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7269 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7270 | input_data->x, input_data->len, |
| 7271 | NULL, label->len, |
| 7272 | output, output_size, |
| 7273 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7274 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7275 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7276 | } |
| 7277 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7278 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7279 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7280 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7281 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7282 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7283 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7284 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7285 | |
| 7286 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 7287 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7288 | { |
| 7289 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7290 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7291 | * 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] | 7292 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7293 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7294 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 7295 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7296 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7297 | |
| 7298 | memset( &zero, 0, sizeof( zero ) ); |
| 7299 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7300 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7301 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7302 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7303 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7304 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7305 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7306 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7307 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7308 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7309 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7310 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7311 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7312 | } |
| 7313 | /* END_CASE */ |
| 7314 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7315 | /* BEGIN_CASE */ |
| 7316 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7317 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7318 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7319 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7320 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7321 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7322 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7323 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7324 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7325 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7326 | |
| 7327 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7328 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7329 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7330 | } |
| 7331 | /* END_CASE */ |
| 7332 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7333 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7334 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7335 | int expected_status_arg ) |
| 7336 | { |
| 7337 | psa_algorithm_t alg = alg_arg; |
| 7338 | size_t capacity = capacity_arg; |
| 7339 | psa_status_t expected_status = expected_status_arg; |
| 7340 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7341 | |
| 7342 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7343 | |
| 7344 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7345 | |
| 7346 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7347 | expected_status ); |
| 7348 | |
| 7349 | exit: |
| 7350 | psa_key_derivation_abort( &operation ); |
| 7351 | PSA_DONE( ); |
| 7352 | } |
| 7353 | /* END_CASE */ |
| 7354 | |
| 7355 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7356 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7357 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7358 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7359 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7360 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7361 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7362 | int expected_status_arg3, |
| 7363 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7364 | { |
| 7365 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7366 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7367 | 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] | 7368 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7369 | expected_status_arg2, |
| 7370 | expected_status_arg3}; |
| 7371 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7372 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7373 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7374 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7375 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7376 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7377 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7378 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7379 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7380 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7381 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7382 | |
| 7383 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7384 | |
| 7385 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7386 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7387 | |
| 7388 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7389 | |
| 7390 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7391 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7392 | mbedtls_test_set_step( i ); |
| 7393 | if( steps[i] == 0 ) |
| 7394 | { |
| 7395 | /* Skip this step */ |
| 7396 | } |
| 7397 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7398 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7399 | psa_set_key_type( &attributes, key_types[i] ); |
| 7400 | PSA_ASSERT( psa_import_key( &attributes, |
| 7401 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7402 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7403 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7404 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7405 | { |
| 7406 | // When taking a private key as secret input, use key agreement |
| 7407 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7408 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7409 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7410 | expected_statuses[i] ); |
| 7411 | } |
| 7412 | else |
| 7413 | { |
| 7414 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7415 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7416 | expected_statuses[i] ); |
| 7417 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7418 | } |
| 7419 | else |
| 7420 | { |
| 7421 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7422 | &operation, steps[i], |
| 7423 | inputs[i]->x, inputs[i]->len ), |
| 7424 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7425 | } |
| 7426 | } |
| 7427 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7428 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7429 | { |
| 7430 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7431 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7432 | psa_set_key_bits( &attributes, 8 ); |
| 7433 | actual_output_status = |
| 7434 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7435 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7436 | } |
| 7437 | else |
| 7438 | { |
| 7439 | uint8_t buffer[1]; |
| 7440 | actual_output_status = |
| 7441 | psa_key_derivation_output_bytes( &operation, |
| 7442 | buffer, sizeof( buffer ) ); |
| 7443 | } |
| 7444 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7445 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7446 | exit: |
| 7447 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7448 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7449 | psa_destroy_key( keys[i] ); |
| 7450 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7451 | PSA_DONE( ); |
| 7452 | } |
| 7453 | /* END_CASE */ |
| 7454 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7455 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7456 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7457 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7458 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7459 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7460 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7461 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7462 | unsigned char input1[] = "Input 1"; |
| 7463 | size_t input1_length = sizeof( input1 ); |
| 7464 | unsigned char input2[] = "Input 2"; |
| 7465 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7466 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7467 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7468 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7469 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7470 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7471 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7472 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7473 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7474 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7475 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7476 | psa_set_key_algorithm( &attributes, alg ); |
| 7477 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7478 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7479 | PSA_ASSERT( psa_import_key( &attributes, |
| 7480 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7481 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7482 | |
| 7483 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7484 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7485 | input1, input1_length, |
| 7486 | input2, input2_length, |
| 7487 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7488 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7489 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7490 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7491 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [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_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7495 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7496 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7497 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7498 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7499 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7500 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7501 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7502 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7503 | } |
| 7504 | /* END_CASE */ |
| 7505 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7506 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7507 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7508 | { |
| 7509 | uint8_t output_buffer[16]; |
| 7510 | size_t buffer_size = 16; |
| 7511 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7512 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7513 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7514 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7515 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7516 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7517 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7518 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7519 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7520 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7521 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7522 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7523 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7524 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7525 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7526 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7527 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7528 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7529 | |
| 7530 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7531 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7532 | } |
| 7533 | /* END_CASE */ |
| 7534 | |
| 7535 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7536 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7537 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7538 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7539 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7540 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7541 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7542 | int requested_capacity_arg, |
| 7543 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7544 | data_t *expected_output2, |
| 7545 | int other_key_input_type, |
| 7546 | int key_input_type, |
| 7547 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7548 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7549 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7550 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7551 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7552 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7553 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7554 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7555 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7556 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7557 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7558 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7559 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7560 | uint8_t *expected_outputs[2] = |
| 7561 | {expected_output1->x, expected_output2->x}; |
| 7562 | size_t output_sizes[2] = |
| 7563 | {expected_output1->len, expected_output2->len}; |
| 7564 | size_t output_buffer_size = 0; |
| 7565 | uint8_t *output_buffer = NULL; |
| 7566 | size_t expected_capacity; |
| 7567 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7568 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7569 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7570 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7571 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7572 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7573 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7574 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7575 | |
| 7576 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7577 | { |
| 7578 | if( output_sizes[i] > output_buffer_size ) |
| 7579 | output_buffer_size = output_sizes[i]; |
| 7580 | if( output_sizes[i] == 0 ) |
| 7581 | expected_outputs[i] = NULL; |
| 7582 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7583 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7584 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7585 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7586 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7587 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7588 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7589 | requested_capacity ) ); |
| 7590 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7591 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7592 | switch( steps[i] ) |
| 7593 | { |
| 7594 | case 0: |
| 7595 | break; |
| 7596 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7597 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7598 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7599 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7600 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7601 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7602 | inputs[i]->x, inputs[i]->len ), |
| 7603 | statuses[i] ); |
| 7604 | |
| 7605 | if( statuses[i] != PSA_SUCCESS ) |
| 7606 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7607 | break; |
| 7608 | case 1: // input key |
| 7609 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7610 | psa_set_key_algorithm( &attributes1, alg ); |
| 7611 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7612 | |
| 7613 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7614 | inputs[i]->x, inputs[i]->len, |
| 7615 | &keys[i] ) ); |
| 7616 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7617 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7618 | { |
| 7619 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7620 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7621 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7622 | } |
| 7623 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7624 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7625 | steps[i], |
| 7626 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7627 | break; |
| 7628 | default: |
| 7629 | TEST_ASSERT( ! "default case not supported" ); |
| 7630 | break; |
| 7631 | } |
| 7632 | break; |
| 7633 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7634 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7635 | { |
| 7636 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7637 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7638 | steps[i], |
| 7639 | inputs[i]->x, |
| 7640 | inputs[i]->len ), |
| 7641 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7642 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7643 | case 1: // input key, type DERIVE |
| 7644 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7645 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7646 | psa_set_key_algorithm( &attributes2, alg ); |
| 7647 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7648 | |
| 7649 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7650 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7651 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7652 | |
| 7653 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7654 | inputs[i]->x, inputs[i]->len, |
| 7655 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7656 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7657 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7658 | steps[i], |
| 7659 | keys[i] ), |
| 7660 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7661 | break; |
| 7662 | case 2: // key agreement |
| 7663 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7664 | psa_set_key_algorithm( &attributes3, alg ); |
| 7665 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7666 | |
| 7667 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7668 | inputs[i]->x, inputs[i]->len, |
| 7669 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7670 | |
| 7671 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7672 | &operation, |
| 7673 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7674 | keys[i], key_agreement_peer_key->x, |
| 7675 | key_agreement_peer_key->len ), statuses[i] ); |
| 7676 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7677 | default: |
| 7678 | TEST_ASSERT( ! "default case not supported" ); |
| 7679 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7680 | } |
| 7681 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7682 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7683 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7684 | break; |
| 7685 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7686 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7687 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7688 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7689 | |
| 7690 | if( statuses[i] != PSA_SUCCESS ) |
| 7691 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7692 | break; |
| 7693 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7694 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7695 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7696 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7697 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7698 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7699 | expected_capacity = requested_capacity; |
| 7700 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7701 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7702 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7703 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7704 | |
| 7705 | /* For output key derivation secret must be provided using |
| 7706 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7707 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7708 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7709 | |
| 7710 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7711 | psa_set_key_algorithm( &attributes4, alg ); |
| 7712 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7713 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7714 | |
| 7715 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7716 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7717 | } |
| 7718 | else // output bytes |
| 7719 | { |
| 7720 | /* Expansion phase. */ |
| 7721 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7722 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7723 | /* Read some bytes. */ |
| 7724 | status = psa_key_derivation_output_bytes( &operation, |
| 7725 | output_buffer, output_sizes[i] ); |
| 7726 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7727 | { |
| 7728 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7729 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7730 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7731 | continue; |
| 7732 | } |
| 7733 | else if( expected_capacity == 0 || |
| 7734 | output_sizes[i] > expected_capacity ) |
| 7735 | { |
| 7736 | /* Capacity exceeded. */ |
| 7737 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7738 | expected_capacity = 0; |
| 7739 | continue; |
| 7740 | } |
| 7741 | /* Success. Check the read data. */ |
| 7742 | PSA_ASSERT( status ); |
| 7743 | if( output_sizes[i] != 0 ) |
| 7744 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7745 | expected_outputs[i], output_sizes[i] ); |
| 7746 | /* Check the operation status. */ |
| 7747 | expected_capacity -= output_sizes[i]; |
| 7748 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7749 | ¤t_capacity ) ); |
| 7750 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7751 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7752 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7753 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7754 | |
| 7755 | exit: |
| 7756 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7757 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7758 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7759 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7760 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7761 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7762 | } |
| 7763 | /* END_CASE */ |
| 7764 | |
| 7765 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7766 | void derive_full( int alg_arg, |
| 7767 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7768 | data_t *input1, |
| 7769 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7770 | int requested_capacity_arg ) |
| 7771 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7772 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7773 | psa_algorithm_t alg = alg_arg; |
| 7774 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7775 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7776 | unsigned char output_buffer[16]; |
| 7777 | size_t expected_capacity = requested_capacity; |
| 7778 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7779 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7780 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7781 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7782 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7783 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7784 | psa_set_key_algorithm( &attributes, alg ); |
| 7785 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7786 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7787 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7788 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7789 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7790 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7791 | input1->x, input1->len, |
| 7792 | input2->x, input2->len, |
| 7793 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7794 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7795 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7796 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7797 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7798 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7799 | |
| 7800 | /* Expansion phase. */ |
| 7801 | while( current_capacity > 0 ) |
| 7802 | { |
| 7803 | size_t read_size = sizeof( output_buffer ); |
| 7804 | if( read_size > current_capacity ) |
| 7805 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7806 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7807 | output_buffer, |
| 7808 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7809 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7810 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7811 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7812 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7813 | } |
| 7814 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7815 | /* Check that the operation refuses to go over capacity. */ |
| 7816 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7817 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7818 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7819 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7820 | |
| 7821 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7822 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7823 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7824 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7825 | } |
| 7826 | /* END_CASE */ |
| 7827 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 7828 | /* 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] | 7829 | 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] | 7830 | int derivation_step, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7831 | int capacity, int expected_capacity_status_arg, |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7832 | data_t *expected_output, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7833 | int expected_output_status_arg ) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7834 | { |
| 7835 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7836 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 7837 | 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] | 7838 | uint8_t *output_buffer = NULL; |
| 7839 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7840 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 7841 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 7842 | 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] | 7843 | |
| 7844 | ASSERT_ALLOC( output_buffer, expected_output->len ); |
| 7845 | PSA_ASSERT( psa_crypto_init() ); |
| 7846 | |
| 7847 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7848 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7849 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7850 | |
| 7851 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7852 | step, input->x, input->len ), |
| 7853 | expected_input_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7854 | |
| 7855 | if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS ) |
| 7856 | goto exit; |
| 7857 | |
| 7858 | status = psa_key_derivation_output_bytes( &operation, output_buffer, |
| 7859 | expected_output->len ); |
| 7860 | |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7861 | TEST_EQUAL( status, expected_output_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7862 | if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS ) |
| 7863 | ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x, |
| 7864 | expected_output->len ); |
| 7865 | |
| 7866 | exit: |
| 7867 | mbedtls_free( output_buffer ); |
| 7868 | psa_key_derivation_abort( &operation ); |
| 7869 | PSA_DONE(); |
| 7870 | } |
| 7871 | /* END_CASE */ |
| 7872 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7873 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7874 | void derive_key_exercise( int alg_arg, |
| 7875 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7876 | data_t *input1, |
| 7877 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7878 | int derived_type_arg, |
| 7879 | int derived_bits_arg, |
| 7880 | int derived_usage_arg, |
| 7881 | int derived_alg_arg ) |
| 7882 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7883 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7884 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7885 | psa_algorithm_t alg = alg_arg; |
| 7886 | psa_key_type_t derived_type = derived_type_arg; |
| 7887 | size_t derived_bits = derived_bits_arg; |
| 7888 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7889 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7890 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7891 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7892 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7893 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7894 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7895 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7896 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7897 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7898 | psa_set_key_algorithm( &attributes, alg ); |
| 7899 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7900 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7901 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7902 | |
| 7903 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7904 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7905 | input1->x, input1->len, |
| 7906 | input2->x, input2->len, |
| 7907 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7908 | goto exit; |
| 7909 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7910 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7911 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7912 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7913 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7914 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7915 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7916 | |
| 7917 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7918 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7919 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7920 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7921 | |
| 7922 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7923 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7924 | goto exit; |
| 7925 | |
| 7926 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7927 | /* |
| 7928 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7929 | * thus reset them as required. |
| 7930 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7931 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7932 | |
| 7933 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7934 | psa_destroy_key( base_key ); |
| 7935 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7936 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7937 | } |
| 7938 | /* END_CASE */ |
| 7939 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7940 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7941 | void derive_key_export( int alg_arg, |
| 7942 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7943 | data_t *input1, |
| 7944 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7945 | int bytes1_arg, |
| 7946 | int bytes2_arg ) |
| 7947 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7948 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7949 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7950 | psa_algorithm_t alg = alg_arg; |
| 7951 | size_t bytes1 = bytes1_arg; |
| 7952 | size_t bytes2 = bytes2_arg; |
| 7953 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7954 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7955 | uint8_t *output_buffer = NULL; |
| 7956 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7957 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7958 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7959 | size_t length; |
| 7960 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7961 | ASSERT_ALLOC( output_buffer, capacity ); |
| 7962 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7963 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7964 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7965 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7966 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7967 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7968 | 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] | 7969 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7970 | |
| 7971 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7972 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7973 | input1->x, input1->len, |
| 7974 | input2->x, input2->len, |
| 7975 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7976 | goto exit; |
| 7977 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7978 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7979 | output_buffer, |
| 7980 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7981 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7982 | |
| 7983 | /* 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] | 7984 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7985 | input1->x, input1->len, |
| 7986 | input2->x, input2->len, |
| 7987 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7988 | goto exit; |
| 7989 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7990 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7991 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7992 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7993 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7994 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7995 | &derived_key ) ); |
| 7996 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7997 | export_buffer, bytes1, |
| 7998 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7999 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8000 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8001 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8002 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8003 | &derived_key ) ); |
| 8004 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8005 | export_buffer + bytes1, bytes2, |
| 8006 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8007 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8008 | |
| 8009 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8010 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 8011 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8012 | |
| 8013 | exit: |
| 8014 | mbedtls_free( output_buffer ); |
| 8015 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8016 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8017 | psa_destroy_key( base_key ); |
| 8018 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8019 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8020 | } |
| 8021 | /* END_CASE */ |
| 8022 | |
| 8023 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8024 | void derive_key_type( int alg_arg, |
| 8025 | data_t *key_data, |
| 8026 | data_t *input1, |
| 8027 | data_t *input2, |
| 8028 | int key_type_arg, int bits_arg, |
| 8029 | data_t *expected_export ) |
| 8030 | { |
| 8031 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8032 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8033 | const psa_algorithm_t alg = alg_arg; |
| 8034 | const psa_key_type_t key_type = key_type_arg; |
| 8035 | const size_t bits = bits_arg; |
| 8036 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8037 | const size_t export_buffer_size = |
| 8038 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 8039 | uint8_t *export_buffer = NULL; |
| 8040 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8041 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8042 | size_t export_length; |
| 8043 | |
| 8044 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 8045 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8046 | |
| 8047 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8048 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8049 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8050 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 8051 | &base_key ) ); |
| 8052 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8053 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8054 | &operation, base_key, alg, |
| 8055 | input1->x, input1->len, |
| 8056 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8057 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8058 | goto exit; |
| 8059 | |
| 8060 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8061 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8062 | psa_set_key_type( &derived_attributes, key_type ); |
| 8063 | psa_set_key_bits( &derived_attributes, bits ); |
| 8064 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 8065 | &derived_key ) ); |
| 8066 | |
| 8067 | PSA_ASSERT( psa_export_key( derived_key, |
| 8068 | export_buffer, export_buffer_size, |
| 8069 | &export_length ) ); |
| 8070 | ASSERT_COMPARE( export_buffer, export_length, |
| 8071 | expected_export->x, expected_export->len ); |
| 8072 | |
| 8073 | exit: |
| 8074 | mbedtls_free( export_buffer ); |
| 8075 | psa_key_derivation_abort( &operation ); |
| 8076 | psa_destroy_key( base_key ); |
| 8077 | psa_destroy_key( derived_key ); |
| 8078 | PSA_DONE( ); |
| 8079 | } |
| 8080 | /* END_CASE */ |
| 8081 | |
| 8082 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8083 | void derive_key( int alg_arg, |
| 8084 | data_t *key_data, data_t *input1, data_t *input2, |
| 8085 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8086 | int expected_status_arg, |
| 8087 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8088 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8089 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8090 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8091 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8092 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8093 | size_t bits = bits_arg; |
| 8094 | psa_status_t expected_status = expected_status_arg; |
| 8095 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8096 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8097 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8098 | |
| 8099 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8100 | |
| 8101 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8102 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8103 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8104 | 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] | 8105 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8106 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8107 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8108 | input1->x, input1->len, |
| 8109 | input2->x, input2->len, |
| 8110 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8111 | goto exit; |
| 8112 | |
| 8113 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8114 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8115 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8116 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8117 | |
| 8118 | psa_status_t status = |
| 8119 | psa_key_derivation_output_key( &derived_attributes, |
| 8120 | &operation, |
| 8121 | &derived_key ); |
| 8122 | if( is_large_output > 0 ) |
| 8123 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8124 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8125 | |
| 8126 | exit: |
| 8127 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8128 | psa_destroy_key( base_key ); |
| 8129 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8130 | PSA_DONE( ); |
| 8131 | } |
| 8132 | /* END_CASE */ |
| 8133 | |
| 8134 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8135 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 8136 | int our_key_type_arg, int our_key_alg_arg, |
| 8137 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8138 | int expected_status_arg ) |
| 8139 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8140 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8141 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8142 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8143 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8144 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8145 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8146 | psa_status_t expected_status = expected_status_arg; |
| 8147 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8148 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8149 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8150 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8151 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8152 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8153 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8154 | PSA_ASSERT( psa_import_key( &attributes, |
| 8155 | our_key_data->x, our_key_data->len, |
| 8156 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8157 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8158 | /* The tests currently include inputs that should fail at either step. |
| 8159 | * Test cases that fail at the setup step should be changed to call |
| 8160 | * key_derivation_setup instead, and this function should be renamed |
| 8161 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8162 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8163 | if( status == PSA_SUCCESS ) |
| 8164 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8165 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 8166 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8167 | our_key, |
| 8168 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8169 | expected_status ); |
| 8170 | } |
| 8171 | else |
| 8172 | { |
| 8173 | TEST_ASSERT( status == expected_status ); |
| 8174 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8175 | |
| 8176 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8177 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8178 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8179 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8180 | } |
| 8181 | /* END_CASE */ |
| 8182 | |
| 8183 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8184 | void raw_key_agreement( int alg_arg, |
| 8185 | int our_key_type_arg, data_t *our_key_data, |
| 8186 | data_t *peer_key_data, |
| 8187 | data_t *expected_output ) |
| 8188 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8189 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8190 | psa_algorithm_t alg = alg_arg; |
| 8191 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8192 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8193 | unsigned char *output = NULL; |
| 8194 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8195 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8196 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8197 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8198 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8199 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8200 | psa_set_key_algorithm( &attributes, alg ); |
| 8201 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8202 | PSA_ASSERT( psa_import_key( &attributes, |
| 8203 | our_key_data->x, our_key_data->len, |
| 8204 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8205 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8206 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 8207 | key_bits = psa_get_key_bits( &attributes ); |
| 8208 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8209 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8210 | TEST_LE_U( expected_output->len, |
| 8211 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 8212 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 8213 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8214 | |
| 8215 | /* Good case with exact output size */ |
| 8216 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 8217 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8218 | peer_key_data->x, peer_key_data->len, |
| 8219 | output, expected_output->len, |
| 8220 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8221 | ASSERT_COMPARE( output, output_length, |
| 8222 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8223 | mbedtls_free( output ); |
| 8224 | output = NULL; |
| 8225 | output_length = ~0; |
| 8226 | |
| 8227 | /* Larger buffer */ |
| 8228 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 8229 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8230 | peer_key_data->x, peer_key_data->len, |
| 8231 | output, expected_output->len + 1, |
| 8232 | &output_length ) ); |
| 8233 | ASSERT_COMPARE( output, output_length, |
| 8234 | expected_output->x, expected_output->len ); |
| 8235 | mbedtls_free( output ); |
| 8236 | output = NULL; |
| 8237 | output_length = ~0; |
| 8238 | |
| 8239 | /* Buffer too small */ |
| 8240 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 8241 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 8242 | peer_key_data->x, peer_key_data->len, |
| 8243 | output, expected_output->len - 1, |
| 8244 | &output_length ), |
| 8245 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8246 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8247 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8248 | mbedtls_free( output ); |
| 8249 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8250 | |
| 8251 | exit: |
| 8252 | mbedtls_free( output ); |
| 8253 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8254 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8255 | } |
| 8256 | /* END_CASE */ |
| 8257 | |
| 8258 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8259 | void key_agreement_capacity( int alg_arg, |
| 8260 | int our_key_type_arg, data_t *our_key_data, |
| 8261 | data_t *peer_key_data, |
| 8262 | int expected_capacity_arg ) |
| 8263 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8264 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8265 | psa_algorithm_t alg = alg_arg; |
| 8266 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8267 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8268 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8269 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8270 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8271 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8272 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8273 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8274 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8275 | psa_set_key_algorithm( &attributes, alg ); |
| 8276 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8277 | PSA_ASSERT( psa_import_key( &attributes, |
| 8278 | our_key_data->x, our_key_data->len, |
| 8279 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8280 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8281 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8282 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8283 | &operation, |
| 8284 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8285 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8286 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8287 | { |
| 8288 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8289 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8290 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8291 | NULL, 0 ) ); |
| 8292 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8293 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8294 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8295 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8296 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8297 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8298 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8299 | /* Test the actual capacity by reading the output. */ |
| 8300 | while( actual_capacity > sizeof( output ) ) |
| 8301 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8302 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8303 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8304 | actual_capacity -= sizeof( output ); |
| 8305 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8306 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8307 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8308 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 8309 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8310 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8311 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8312 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8313 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8314 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8315 | } |
| 8316 | /* END_CASE */ |
| 8317 | |
| 8318 | /* BEGIN_CASE */ |
| 8319 | void key_agreement_output( int alg_arg, |
| 8320 | int our_key_type_arg, data_t *our_key_data, |
| 8321 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8322 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8323 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8324 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8325 | psa_algorithm_t alg = alg_arg; |
| 8326 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8327 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8328 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8329 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8330 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8331 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 8332 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8333 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8334 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8335 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8336 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8337 | psa_set_key_algorithm( &attributes, alg ); |
| 8338 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8339 | PSA_ASSERT( psa_import_key( &attributes, |
| 8340 | our_key_data->x, our_key_data->len, |
| 8341 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8342 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8343 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8344 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8345 | &operation, |
| 8346 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8347 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8348 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8349 | { |
| 8350 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8351 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8352 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8353 | NULL, 0 ) ); |
| 8354 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8355 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8356 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8357 | actual_output, |
| 8358 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8359 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8360 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8361 | if( expected_output2->len != 0 ) |
| 8362 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8363 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8364 | actual_output, |
| 8365 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8366 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8367 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8368 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8369 | |
| 8370 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8371 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8372 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8373 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8374 | mbedtls_free( actual_output ); |
| 8375 | } |
| 8376 | /* END_CASE */ |
| 8377 | |
| 8378 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8379 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8380 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8381 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8382 | unsigned char *output = NULL; |
| 8383 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8384 | size_t i; |
| 8385 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8386 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8387 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8388 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8389 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8390 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8391 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8392 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8393 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8394 | /* Run several times, to ensure that every output byte will be |
| 8395 | * nonzero at least once with overwhelming probability |
| 8396 | * (2^(-8*number_of_runs)). */ |
| 8397 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8398 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8399 | if( bytes != 0 ) |
| 8400 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8401 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8402 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8403 | for( i = 0; i < bytes; i++ ) |
| 8404 | { |
| 8405 | if( output[i] != 0 ) |
| 8406 | ++changed[i]; |
| 8407 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8408 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8409 | |
| 8410 | /* Check that every byte was changed to nonzero at least once. This |
| 8411 | * validates that psa_generate_random is overwriting every byte of |
| 8412 | * the output buffer. */ |
| 8413 | for( i = 0; i < bytes; i++ ) |
| 8414 | { |
| 8415 | TEST_ASSERT( changed[i] != 0 ); |
| 8416 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8417 | |
| 8418 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8419 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8420 | mbedtls_free( output ); |
| 8421 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8422 | } |
| 8423 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8424 | |
| 8425 | /* BEGIN_CASE */ |
| 8426 | void generate_key( int type_arg, |
| 8427 | int bits_arg, |
| 8428 | int usage_arg, |
| 8429 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8430 | int expected_status_arg, |
| 8431 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8432 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8433 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8434 | psa_key_type_t type = type_arg; |
| 8435 | psa_key_usage_t usage = usage_arg; |
| 8436 | size_t bits = bits_arg; |
| 8437 | psa_algorithm_t alg = alg_arg; |
| 8438 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8439 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8440 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8441 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8442 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8443 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8444 | psa_set_key_usage_flags( &attributes, usage ); |
| 8445 | psa_set_key_algorithm( &attributes, alg ); |
| 8446 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8447 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8448 | |
| 8449 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8450 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8451 | |
| 8452 | if( is_large_key > 0 ) |
| 8453 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8454 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8455 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8456 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8457 | |
| 8458 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8459 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8460 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8461 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8462 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8463 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8464 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8465 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8466 | |
| 8467 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8468 | /* |
| 8469 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8470 | * thus reset them as required. |
| 8471 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8472 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8473 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8474 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8475 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8476 | } |
| 8477 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8478 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8479 | /* 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] | 8480 | void generate_key_rsa( int bits_arg, |
| 8481 | data_t *e_arg, |
| 8482 | int expected_status_arg ) |
| 8483 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8484 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8485 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8486 | size_t bits = bits_arg; |
| 8487 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8488 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8489 | psa_status_t expected_status = expected_status_arg; |
| 8490 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8491 | uint8_t *exported = NULL; |
| 8492 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8493 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8494 | size_t exported_length = SIZE_MAX; |
| 8495 | uint8_t *e_read_buffer = NULL; |
| 8496 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8497 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8498 | size_t e_read_length = SIZE_MAX; |
| 8499 | |
| 8500 | if( e_arg->len == 0 || |
| 8501 | ( e_arg->len == 3 && |
| 8502 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8503 | { |
| 8504 | is_default_public_exponent = 1; |
| 8505 | e_read_size = 0; |
| 8506 | } |
| 8507 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8508 | ASSERT_ALLOC( exported, exported_size ); |
| 8509 | |
| 8510 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8511 | |
| 8512 | psa_set_key_usage_flags( &attributes, usage ); |
| 8513 | psa_set_key_algorithm( &attributes, alg ); |
| 8514 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8515 | e_arg->x, e_arg->len ) ); |
| 8516 | psa_set_key_bits( &attributes, bits ); |
| 8517 | |
| 8518 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8519 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8520 | if( expected_status != PSA_SUCCESS ) |
| 8521 | goto exit; |
| 8522 | |
| 8523 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8524 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8525 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8526 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8527 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8528 | e_read_buffer, e_read_size, |
| 8529 | &e_read_length ) ); |
| 8530 | if( is_default_public_exponent ) |
| 8531 | TEST_EQUAL( e_read_length, 0 ); |
| 8532 | else |
| 8533 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8534 | |
| 8535 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8536 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8537 | goto exit; |
| 8538 | |
| 8539 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8540 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8541 | exported, exported_size, |
| 8542 | &exported_length ) ); |
| 8543 | { |
| 8544 | uint8_t *p = exported; |
| 8545 | uint8_t *end = exported + exported_length; |
| 8546 | size_t len; |
| 8547 | /* RSAPublicKey ::= SEQUENCE { |
| 8548 | * modulus INTEGER, -- n |
| 8549 | * publicExponent INTEGER } -- e |
| 8550 | */ |
| 8551 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8552 | MBEDTLS_ASN1_SEQUENCE | |
| 8553 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8554 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8555 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8556 | MBEDTLS_ASN1_INTEGER ) ); |
| 8557 | if( len >= 1 && p[0] == 0 ) |
| 8558 | { |
| 8559 | ++p; |
| 8560 | --len; |
| 8561 | } |
| 8562 | if( e_arg->len == 0 ) |
| 8563 | { |
| 8564 | TEST_EQUAL( len, 3 ); |
| 8565 | TEST_EQUAL( p[0], 1 ); |
| 8566 | TEST_EQUAL( p[1], 0 ); |
| 8567 | TEST_EQUAL( p[2], 1 ); |
| 8568 | } |
| 8569 | else |
| 8570 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8571 | } |
| 8572 | |
| 8573 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8574 | /* |
| 8575 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8576 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8577 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8578 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8579 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8580 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8581 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8582 | mbedtls_free( e_read_buffer ); |
| 8583 | mbedtls_free( exported ); |
| 8584 | } |
| 8585 | /* END_CASE */ |
| 8586 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8587 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8588 | void persistent_key_load_key_from_storage( data_t *data, |
| 8589 | int type_arg, int bits_arg, |
| 8590 | int usage_flags_arg, int alg_arg, |
| 8591 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8592 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8593 | 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] | 8594 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8595 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8596 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8597 | psa_key_type_t type = type_arg; |
| 8598 | size_t bits = bits_arg; |
| 8599 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8600 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8601 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8602 | unsigned char *first_export = NULL; |
| 8603 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8604 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8605 | size_t first_exported_length; |
| 8606 | size_t second_exported_length; |
| 8607 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8608 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8609 | { |
| 8610 | ASSERT_ALLOC( first_export, export_size ); |
| 8611 | ASSERT_ALLOC( second_export, export_size ); |
| 8612 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8613 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8614 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8615 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8616 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8617 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8618 | psa_set_key_algorithm( &attributes, alg ); |
| 8619 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8620 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8621 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8622 | switch( generation_method ) |
| 8623 | { |
| 8624 | case IMPORT_KEY: |
| 8625 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8626 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8627 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8628 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8629 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8630 | case GENERATE_KEY: |
| 8631 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8632 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8633 | break; |
| 8634 | |
| 8635 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8636 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8637 | { |
| 8638 | /* Create base key */ |
| 8639 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8640 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8641 | psa_set_key_usage_flags( &base_attributes, |
| 8642 | PSA_KEY_USAGE_DERIVE ); |
| 8643 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8644 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8645 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8646 | data->x, data->len, |
| 8647 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8648 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8649 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8650 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8651 | &operation, |
| 8652 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8653 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8654 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8655 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8656 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8657 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8658 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8659 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8660 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8661 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8662 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8663 | #else |
| 8664 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8665 | #endif |
| 8666 | break; |
| 8667 | |
| 8668 | default: |
| 8669 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8670 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8671 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8672 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8673 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8674 | /* Export the key if permitted by the key policy. */ |
| 8675 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8676 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8677 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8678 | first_export, export_size, |
| 8679 | &first_exported_length ) ); |
| 8680 | if( generation_method == IMPORT_KEY ) |
| 8681 | ASSERT_COMPARE( data->x, data->len, |
| 8682 | first_export, first_exported_length ); |
| 8683 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8684 | |
| 8685 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8686 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8687 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8688 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8689 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8690 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8691 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8692 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8693 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8694 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8695 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8696 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8697 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8698 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8699 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8700 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8701 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8702 | /* Export the key again if permitted by the key policy. */ |
| 8703 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8704 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8705 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8706 | second_export, export_size, |
| 8707 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8708 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8709 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8710 | } |
| 8711 | |
| 8712 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8713 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8714 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8715 | |
| 8716 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8717 | /* |
| 8718 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8719 | * thus reset them as required. |
| 8720 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8721 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8722 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8723 | mbedtls_free( first_export ); |
| 8724 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8725 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8726 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8727 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8728 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8729 | } |
| 8730 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8731 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8732 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8733 | void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 8734 | int primitive_arg, int hash_arg, int role_arg, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8735 | int input_first, data_t *pw_data, |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8736 | int expected_status_setup_arg, |
| 8737 | int expected_status_set_role_arg, |
| 8738 | int expected_status_set_password_key_arg, |
| 8739 | int expected_status_input_output_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8740 | { |
| 8741 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8742 | psa_pake_operation_t operation = psa_pake_operation_init(); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8743 | psa_pake_operation_t op_copy = psa_pake_operation_init(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8744 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8745 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8746 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 8747 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8748 | psa_algorithm_t hash_alg = hash_arg; |
| 8749 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8750 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8751 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8752 | psa_status_t expected_status_setup = expected_status_setup_arg; |
| 8753 | psa_status_t expected_status_set_role = expected_status_set_role_arg; |
| 8754 | psa_status_t expected_status_set_password_key = |
| 8755 | expected_status_set_password_key_arg; |
| 8756 | psa_status_t expected_status_input_output = |
| 8757 | expected_status_input_output_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8758 | unsigned char *output_buffer = NULL; |
| 8759 | size_t output_len = 0; |
| 8760 | |
| 8761 | PSA_INIT( ); |
| 8762 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8763 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8764 | PSA_PAKE_STEP_KEY_SHARE); |
| 8765 | ASSERT_ALLOC( output_buffer, buf_size ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8766 | |
| 8767 | if( pw_data->len > 0 ) |
| 8768 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8769 | psa_set_key_usage_flags( &attributes, key_usage_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8770 | psa_set_key_algorithm( &attributes, alg ); |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8771 | psa_set_key_type( &attributes, key_type_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8772 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8773 | &key ) ); |
| 8774 | } |
| 8775 | |
| 8776 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8777 | psa_pake_cs_set_primitive( &cipher_suite, primitive ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8778 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8779 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8780 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8781 | |
| 8782 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8783 | PSA_ERROR_BAD_STATE ); |
| 8784 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8785 | PSA_ERROR_BAD_STATE ); |
| 8786 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8787 | PSA_ERROR_BAD_STATE ); |
| 8788 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8789 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8790 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8791 | NULL, 0, NULL ), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8792 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8793 | 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] | 8794 | PSA_ERROR_BAD_STATE ); |
| 8795 | |
| 8796 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8797 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8798 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8799 | expected_status_setup ); |
| 8800 | if( expected_status_setup != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8801 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8802 | |
Neil Armstrong | 50de0ae | 2022-06-08 17:46:24 +0200 | [diff] [blame] | 8803 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8804 | PSA_ERROR_BAD_STATE ); |
| 8805 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8806 | TEST_EQUAL( psa_pake_set_role( &operation, role), |
| 8807 | expected_status_set_role ); |
| 8808 | if( expected_status_set_role != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8809 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8810 | |
| 8811 | if( pw_data->len > 0 ) |
| 8812 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8813 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8814 | expected_status_set_password_key ); |
| 8815 | if( expected_status_set_password_key != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8816 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8817 | } |
| 8818 | |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8819 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8820 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8821 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8822 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8823 | |
| 8824 | const uint8_t unsupported_id[] = "abcd"; |
| 8825 | |
| 8826 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8827 | PSA_ERROR_NOT_SUPPORTED ); |
| 8828 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8829 | PSA_ERROR_NOT_SUPPORTED ); |
| 8830 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8831 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8832 | PSA_PAKE_STEP_KEY_SHARE ); |
| 8833 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8834 | PSA_PAKE_STEP_ZK_PUBLIC ); |
| 8835 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8836 | PSA_PAKE_STEP_ZK_PROOF ); |
| 8837 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8838 | /* First round */ |
| 8839 | if( input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8840 | { |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8841 | /* Invalid parameters (input) */ |
| 8842 | op_copy = operation; |
| 8843 | TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8844 | NULL, 0 ), |
| 8845 | PSA_ERROR_INVALID_ARGUMENT ); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8846 | /* Invalid parameters (step) */ |
| 8847 | op_copy = operation; |
| 8848 | TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF + 10, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8849 | output_buffer, size_zk_proof ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8850 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8851 | /* Invalid first step */ |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8852 | op_copy = operation; |
| 8853 | TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8854 | output_buffer, size_zk_proof ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8855 | PSA_ERROR_BAD_STATE ); |
| 8856 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8857 | /* Possibly valid */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8858 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8859 | output_buffer, size_key_share ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8860 | expected_status_input_output); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8861 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8862 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8863 | { |
| 8864 | /* Buffer too large */ |
| 8865 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8866 | output_buffer, size_zk_public + 1 ), |
| 8867 | PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8868 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8869 | /* The operation's state should be invalidated at this point */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8870 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8871 | output_buffer, size_zk_public ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8872 | PSA_ERROR_BAD_STATE ); |
| 8873 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8874 | } |
| 8875 | else |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8876 | { |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8877 | /* Invalid parameters (output) */ |
| 8878 | op_copy = operation; |
| 8879 | TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8880 | NULL, 0, NULL ), |
| 8881 | PSA_ERROR_INVALID_ARGUMENT ); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8882 | op_copy = operation; |
| 8883 | /* Invalid parameters (step) */ |
| 8884 | TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF + 10, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8885 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8886 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8887 | /* Invalid first step */ |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8888 | op_copy = operation; |
| 8889 | TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8890 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8891 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8892 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8893 | /* Possibly valid */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8894 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8895 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8896 | expected_status_input_output ); |
Neil Armstrong | 98506ab | 2022-06-08 17:43:20 +0200 | [diff] [blame] | 8897 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8898 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8899 | { |
| 8900 | TEST_ASSERT( output_len > 0 ); |
| 8901 | |
| 8902 | /* Buffer too small */ |
| 8903 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8904 | output_buffer, size_zk_public - 1, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8905 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8906 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8907 | /* The operation's state should be invalidated at this point */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8908 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8909 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8910 | PSA_ERROR_BAD_STATE ); |
| 8911 | } |
| 8912 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8913 | |
| 8914 | exit: |
| 8915 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 8916 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8917 | mbedtls_free( output_buffer ); |
| 8918 | PSA_DONE( ); |
| 8919 | } |
| 8920 | /* END_CASE */ |
| 8921 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8922 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8923 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 8924 | int client_input_first, int inject_error, |
| 8925 | data_t *pw_data ) |
| 8926 | { |
| 8927 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8928 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8929 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8930 | psa_algorithm_t alg = alg_arg; |
| 8931 | psa_algorithm_t hash_alg = hash_arg; |
| 8932 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8933 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8934 | |
| 8935 | PSA_INIT( ); |
| 8936 | |
| 8937 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8938 | psa_set_key_algorithm( &attributes, alg ); |
| 8939 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8940 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8941 | &key ) ); |
| 8942 | |
| 8943 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8944 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8945 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8946 | |
| 8947 | |
| 8948 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8949 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8950 | |
| 8951 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8952 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8953 | |
| 8954 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8955 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8956 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8957 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8958 | client_input_first, 1, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8959 | |
| 8960 | if( inject_error == 1 || inject_error == 2 ) |
| 8961 | goto exit; |
| 8962 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8963 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8964 | client_input_first, 2, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8965 | |
| 8966 | exit: |
| 8967 | psa_destroy_key( key ); |
| 8968 | psa_pake_abort( &server ); |
| 8969 | psa_pake_abort( &client ); |
| 8970 | PSA_DONE( ); |
| 8971 | } |
| 8972 | /* END_CASE */ |
| 8973 | |
| 8974 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8975 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8976 | int derive_alg_arg, data_t *pw_data, |
| 8977 | int client_input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8978 | { |
| 8979 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8980 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8981 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8982 | psa_algorithm_t alg = alg_arg; |
| 8983 | psa_algorithm_t hash_alg = hash_arg; |
| 8984 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 8985 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8986 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8987 | psa_key_derivation_operation_t server_derive = |
| 8988 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8989 | psa_key_derivation_operation_t client_derive = |
| 8990 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8991 | |
| 8992 | PSA_INIT( ); |
| 8993 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8994 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8995 | psa_set_key_algorithm( &attributes, alg ); |
| 8996 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8997 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8998 | &key ) ); |
| 8999 | |
| 9000 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 9001 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 9002 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 9003 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9004 | /* Get shared key */ |
| 9005 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 9006 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 9007 | |
| 9008 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 9009 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 9010 | { |
| 9011 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 9012 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9013 | (const uint8_t*) "", 0) ); |
| 9014 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 9015 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9016 | (const uint8_t*) "", 0) ); |
| 9017 | } |
| 9018 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9019 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 9020 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 9021 | |
| 9022 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 9023 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 9024 | |
| 9025 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 9026 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 9027 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9028 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 9029 | PSA_ERROR_BAD_STATE ); |
| 9030 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 9031 | PSA_ERROR_BAD_STATE ); |
| 9032 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9033 | /* First round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9034 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9035 | client_input_first, 1, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9036 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9037 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 9038 | PSA_ERROR_BAD_STATE ); |
| 9039 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 9040 | PSA_ERROR_BAD_STATE ); |
| 9041 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9042 | /* Second round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9043 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9044 | client_input_first, 2, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9045 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9046 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 9047 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 9048 | |
| 9049 | exit: |
| 9050 | psa_key_derivation_abort( &server_derive ); |
| 9051 | psa_key_derivation_abort( &client_derive ); |
| 9052 | psa_destroy_key( key ); |
| 9053 | psa_pake_abort( &server ); |
| 9054 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9055 | PSA_DONE( ); |
| 9056 | } |
| 9057 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9058 | |
| 9059 | /* BEGIN_CASE */ |
| 9060 | void ecjpake_size_macros( ) |
| 9061 | { |
| 9062 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 9063 | const size_t bits = 256; |
| 9064 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
| 9065 | PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits ); |
| 9066 | const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( |
| 9067 | PSA_ECC_FAMILY_SECP_R1 ); |
| 9068 | |
| 9069 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 9070 | /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */ |
| 9071 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9072 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9073 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9074 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9075 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
| 9076 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9077 | PSA_BITS_TO_BYTES( bits ) ); |
| 9078 | |
| 9079 | /* Input sizes are the same as output sizes */ |
| 9080 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9081 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) ); |
| 9082 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9083 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) ); |
| 9084 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9085 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) ); |
| 9086 | |
| 9087 | /* These inequalities will always hold even when other PAKEs are added */ |
| 9088 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9089 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9090 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9091 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9092 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9093 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9094 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9095 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9096 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9097 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9098 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9099 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9100 | } |
| 9101 | /* END_CASE */ |