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 |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 24 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 25 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 26 | #define UNUSED 0xdeadbeef |
| 27 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 28 | /* Assert that an operation is (not) active. |
| 29 | * This serves as a proxy for checking if the operation is aborted. */ |
| 30 | #define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 ) |
| 31 | #define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 32 | |
| 33 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 34 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 35 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 36 | /** Test if a buffer contains a constant byte value. |
| 37 | * |
| 38 | * `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] | 39 | * |
| 40 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 41 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 42 | * \param size Size of the buffer in bytes. |
| 43 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 44 | * \return 1 if the buffer is all-bits-zero. |
| 45 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 46 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 47 | 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] | 48 | { |
| 49 | size_t i; |
| 50 | for( i = 0; i < size; i++ ) |
| 51 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 52 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 53 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 54 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 55 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 56 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 57 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 58 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 59 | static int asn1_write_10x( unsigned char **p, |
| 60 | unsigned char *start, |
| 61 | size_t bits, |
| 62 | unsigned char x ) |
| 63 | { |
| 64 | int ret; |
| 65 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 66 | if( bits == 0 ) |
| 67 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 68 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 69 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 70 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 71 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 72 | *p -= len; |
| 73 | ( *p )[len-1] = x; |
| 74 | if( bits % 8 == 0 ) |
| 75 | ( *p )[1] |= 1; |
| 76 | else |
| 77 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 78 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 79 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 80 | MBEDTLS_ASN1_INTEGER ) ); |
| 81 | return( len ); |
| 82 | } |
| 83 | |
| 84 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 85 | size_t buffer_size, |
| 86 | unsigned char **p, |
| 87 | size_t bits, |
| 88 | int keypair ) |
| 89 | { |
| 90 | size_t half_bits = ( bits + 1 ) / 2; |
| 91 | int ret; |
| 92 | int len = 0; |
| 93 | /* Construct something that looks like a DER encoding of |
| 94 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 95 | * RSAPrivateKey ::= SEQUENCE { |
| 96 | * version Version, |
| 97 | * modulus INTEGER, -- n |
| 98 | * publicExponent INTEGER, -- e |
| 99 | * privateExponent INTEGER, -- d |
| 100 | * prime1 INTEGER, -- p |
| 101 | * prime2 INTEGER, -- q |
| 102 | * exponent1 INTEGER, -- d mod (p-1) |
| 103 | * exponent2 INTEGER, -- d mod (q-1) |
| 104 | * coefficient INTEGER, -- (inverse of q) mod p |
| 105 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 106 | * } |
| 107 | * Or, for a public key, the same structure with only |
| 108 | * version, modulus and publicExponent. |
| 109 | */ |
| 110 | *p = buffer + buffer_size; |
| 111 | if( keypair ) |
| 112 | { |
| 113 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 114 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 115 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 116 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 117 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 118 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 119 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 120 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 121 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 122 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 123 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 124 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 125 | } |
| 126 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 127 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 128 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 129 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 130 | if( keypair ) |
| 131 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 132 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 133 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 134 | { |
| 135 | const unsigned char tag = |
| 136 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 137 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 138 | } |
| 139 | return( len ); |
| 140 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 141 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 142 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 143 | int exercise_mac_setup( psa_key_type_t key_type, |
| 144 | const unsigned char *key_bytes, |
| 145 | size_t key_length, |
| 146 | psa_algorithm_t alg, |
| 147 | psa_mac_operation_t *operation, |
| 148 | psa_status_t *status ) |
| 149 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 150 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 151 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 152 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 153 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 154 | psa_set_key_algorithm( &attributes, alg ); |
| 155 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 156 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 157 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 158 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 159 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 160 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 161 | /* If setup failed, reproduce the failure, so that the caller can |
| 162 | * test the resulting state of the operation object. */ |
| 163 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 164 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 165 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 168 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 169 | return( 1 ); |
| 170 | |
| 171 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 172 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 173 | return( 0 ); |
| 174 | } |
| 175 | |
| 176 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 177 | const unsigned char *key_bytes, |
| 178 | size_t key_length, |
| 179 | psa_algorithm_t alg, |
| 180 | psa_cipher_operation_t *operation, |
| 181 | psa_status_t *status ) |
| 182 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 183 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 184 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 185 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 186 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 187 | psa_set_key_algorithm( &attributes, alg ); |
| 188 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 189 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 190 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 191 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 192 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 193 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 194 | /* If setup failed, reproduce the failure, so that the caller can |
| 195 | * test the resulting state of the operation object. */ |
| 196 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 197 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 198 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 199 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 202 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 203 | return( 1 ); |
| 204 | |
| 205 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 206 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 207 | return( 0 ); |
| 208 | } |
| 209 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 210 | 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] | 211 | { |
| 212 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 213 | 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] | 214 | uint8_t buffer[1]; |
| 215 | size_t length; |
| 216 | int ok = 0; |
| 217 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 218 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 219 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 220 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 221 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 222 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 223 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 224 | TEST_EQUAL( |
| 225 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 226 | TEST_EQUAL( |
| 227 | 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] | 228 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 229 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 230 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 231 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 232 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 233 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 234 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 235 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 236 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 237 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 238 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 239 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 240 | ok = 1; |
| 241 | |
| 242 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 243 | /* |
| 244 | * Key attributes may have been returned by psa_get_key_attributes() |
| 245 | * thus reset them as required. |
| 246 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 247 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 248 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 249 | return( ok ); |
| 250 | } |
| 251 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 252 | /* Assert that a key isn't reported as having a slot number. */ |
| 253 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 254 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 255 | do \ |
| 256 | { \ |
| 257 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 258 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 259 | attributes, \ |
| 260 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 261 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 262 | } \ |
| 263 | while( 0 ) |
| 264 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 265 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 266 | ( (void) 0 ) |
| 267 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 268 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 269 | /* An overapproximation of the amount of storage needed for a key of the |
| 270 | * given type and with the given content. The API doesn't make it easy |
| 271 | * to find a good value for the size. The current implementation doesn't |
| 272 | * care about the value anyway. */ |
| 273 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 274 | ( data )->len |
| 275 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 276 | typedef enum { |
| 277 | IMPORT_KEY = 0, |
| 278 | GENERATE_KEY = 1, |
| 279 | DERIVE_KEY = 2 |
| 280 | } generate_method; |
| 281 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 282 | typedef enum |
| 283 | { |
| 284 | DO_NOT_SET_LENGTHS = 0, |
| 285 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 286 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 287 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 288 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 289 | typedef enum |
| 290 | { |
| 291 | USE_NULL_TAG = 0, |
| 292 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 293 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 294 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 295 | /*! |
| 296 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 297 | * \param key_type_arg Type of key passed in |
| 298 | * \param key_data The encryption / decryption key data |
| 299 | * \param alg_arg The type of algorithm used |
| 300 | * \param nonce Nonce data |
| 301 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 302 | * \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] | 303 | * feed additional data in to be encrypted / |
| 304 | * decrypted. If -1, no chunking. |
| 305 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 306 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 307 | * the data in to be encrypted / decrypted. If |
| 308 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 309 | * \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] | 310 | * expected here, this controls whether or not |
| 311 | * to set lengths, and in what order with |
| 312 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 313 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 314 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 315 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 316 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 317 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 318 | */ |
| 319 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 320 | int alg_arg, |
| 321 | data_t *nonce, |
| 322 | data_t *additional_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 323 | int ad_part_len_arg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 324 | data_t *input_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 325 | int data_part_len_arg, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 326 | set_lengths_method_t set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 327 | data_t *expected_output, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 328 | int is_encrypt, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 329 | int do_zero_parts ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 330 | { |
| 331 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 332 | psa_key_type_t key_type = key_type_arg; |
| 333 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 334 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 335 | unsigned char *output_data = NULL; |
| 336 | unsigned char *part_data = NULL; |
| 337 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 338 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 339 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 340 | size_t output_size = 0; |
| 341 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 342 | size_t output_length = 0; |
| 343 | size_t key_bits = 0; |
| 344 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 345 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 346 | size_t part_length = 0; |
| 347 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 348 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 349 | size_t ad_part_len = 0; |
| 350 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 351 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 352 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 353 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 354 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 355 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 356 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 357 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 358 | PSA_ASSERT( psa_crypto_init( ) ); |
| 359 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 360 | if( is_encrypt ) |
| 361 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 362 | else |
| 363 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 364 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 365 | psa_set_key_algorithm( &attributes, alg ); |
| 366 | psa_set_key_type( &attributes, key_type ); |
| 367 | |
| 368 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 369 | &key ) ); |
| 370 | |
| 371 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 372 | key_bits = psa_get_key_bits( &attributes ); |
| 373 | |
| 374 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 375 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 376 | if( is_encrypt ) |
| 377 | { |
| 378 | /* Tag gets written at end of buffer. */ |
| 379 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 380 | ( input_data->len + |
| 381 | tag_length ) ); |
| 382 | data_true_size = input_data->len; |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 387 | ( input_data->len - |
| 388 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 389 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 390 | /* Do not want to attempt to decrypt tag. */ |
| 391 | data_true_size = input_data->len - tag_length; |
| 392 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 393 | |
| 394 | ASSERT_ALLOC( output_data, output_size ); |
| 395 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 396 | if( is_encrypt ) |
| 397 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 398 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 399 | TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 400 | } |
| 401 | else |
| 402 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 403 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 404 | TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 405 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 406 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 407 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 408 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 409 | if( is_encrypt ) |
| 410 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 411 | else |
| 412 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 413 | |
| 414 | /* If the operation is not supported, just skip and not fail in case the |
| 415 | * encryption involves a common limitation of cryptography hardwares and |
| 416 | * an alternative implementation. */ |
| 417 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 418 | { |
| 419 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 420 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 421 | } |
| 422 | |
| 423 | PSA_ASSERT( status ); |
| 424 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 425 | if( set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 426 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 427 | else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 428 | { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 429 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 430 | data_true_size ) ); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 431 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 432 | } |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 433 | else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 434 | { |
| 435 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 436 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 437 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 438 | data_true_size ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 439 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 440 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 441 | if( ad_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 442 | { |
| 443 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 444 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 445 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 446 | for( part_offset = 0, part_count = 0; |
| 447 | part_offset < additional_data->len; |
| 448 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 449 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 450 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 451 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 452 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 453 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 454 | else if( additional_data->len - part_offset < ad_part_len ) |
| 455 | { |
| 456 | part_length = additional_data->len - part_offset; |
| 457 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 458 | else |
| 459 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 460 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 464 | additional_data->x + part_offset, |
| 465 | part_length ) ); |
| 466 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | else |
| 470 | { |
| 471 | /* Pass additional data in one go. */ |
| 472 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 473 | additional_data->len ) ); |
| 474 | } |
| 475 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 476 | if( data_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 477 | { |
| 478 | /* Pass data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 479 | data_part_len = ( size_t ) data_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 480 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 481 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 482 | |
| 483 | ASSERT_ALLOC( part_data, part_data_size ); |
| 484 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 485 | for( part_offset = 0, part_count = 0; |
| 486 | part_offset < data_true_size; |
| 487 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 488 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 489 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 490 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 491 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 492 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 493 | else if( ( data_true_size - part_offset ) < data_part_len ) |
| 494 | { |
| 495 | part_length = ( data_true_size - part_offset ); |
| 496 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 497 | else |
| 498 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 499 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | PSA_ASSERT( psa_aead_update( &operation, |
| 503 | ( input_data->x + part_offset ), |
| 504 | part_length, part_data, |
| 505 | part_data_size, |
| 506 | &output_part_length ) ); |
| 507 | |
| 508 | if( output_data && output_part_length ) |
| 509 | { |
Mircea Udrea | 657ff4f | 2022-01-31 13:51:56 +0100 | [diff] [blame] | 510 | memcpy( ( output_data + output_length ), part_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 511 | output_part_length ); |
| 512 | } |
| 513 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 514 | output_length += output_part_length; |
| 515 | } |
| 516 | } |
| 517 | else |
| 518 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 519 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 520 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 521 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 522 | output_size, &output_length ) ); |
| 523 | } |
| 524 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 525 | if( is_encrypt ) |
| 526 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 527 | final_output_size, |
| 528 | &output_part_length, |
| 529 | tag_buffer, tag_length, |
| 530 | &tag_size ) ); |
| 531 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 532 | { |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 533 | PSA_ASSERT( psa_aead_verify( &operation, final_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 534 | final_output_size, |
| 535 | &output_part_length, |
| 536 | ( input_data->x + data_true_size ), |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 537 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 540 | if( output_data && output_part_length ) |
| 541 | memcpy( ( output_data + output_length ), final_data, |
| 542 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 543 | |
| 544 | output_length += output_part_length; |
| 545 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 546 | |
| 547 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 548 | * should be exact.*/ |
| 549 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 550 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 551 | TEST_EQUAL( tag_length, tag_size ); |
| 552 | |
| 553 | if( output_data && tag_length ) |
| 554 | memcpy( ( output_data + output_length ), tag_buffer, |
| 555 | tag_length ); |
| 556 | |
| 557 | output_length += tag_length; |
| 558 | |
| 559 | TEST_EQUAL( output_length, |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 560 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 561 | input_data->len ) ); |
| 562 | TEST_LE_U( output_length, |
| 563 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 564 | } |
| 565 | else |
| 566 | { |
| 567 | TEST_EQUAL( output_length, |
| 568 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 569 | input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 570 | TEST_LE_U( output_length, |
| 571 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 574 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 575 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 576 | output_data, output_length ); |
| 577 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 578 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 579 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 580 | |
| 581 | exit: |
| 582 | psa_destroy_key( key ); |
| 583 | psa_aead_abort( &operation ); |
| 584 | mbedtls_free( output_data ); |
| 585 | mbedtls_free( part_data ); |
| 586 | mbedtls_free( final_data ); |
| 587 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 588 | |
| 589 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 592 | /*! |
| 593 | * \brief Internal Function for MAC multipart tests. |
| 594 | * \param key_type_arg Type of key passed in |
| 595 | * \param key_data The encryption / decryption key data |
| 596 | * \param alg_arg The type of algorithm used |
| 597 | * \param input_data Data to encrypt / decrypt |
| 598 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 599 | * the data in to be encrypted / decrypted. If |
| 600 | * -1, no chunking |
| 601 | * \param expected_output Expected output |
| 602 | * \param is_verify If non-zero this is an verify operation. |
| 603 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 604 | * with normal length chunks. |
| 605 | * \return int Zero on failure, non-zero on success. |
| 606 | */ |
| 607 | static int mac_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 608 | int alg_arg, |
| 609 | data_t *input_data, |
| 610 | int data_part_len_arg, |
| 611 | data_t *expected_output, |
| 612 | int is_verify, |
| 613 | int do_zero_parts ) |
| 614 | { |
| 615 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 616 | psa_key_type_t key_type = key_type_arg; |
| 617 | psa_algorithm_t alg = alg_arg; |
| 618 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 619 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 620 | size_t part_offset = 0; |
| 621 | size_t part_length = 0; |
| 622 | size_t data_part_len = 0; |
| 623 | size_t mac_len = 0; |
| 624 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 625 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 626 | |
| 627 | int test_ok = 0; |
| 628 | size_t part_count = 0; |
| 629 | |
Neil Armstrong | fd4c259 | 2022-03-07 10:11:11 +0100 | [diff] [blame] | 630 | PSA_INIT( ); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 631 | |
| 632 | if( is_verify ) |
| 633 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 634 | else |
| 635 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 636 | |
| 637 | psa_set_key_algorithm( &attributes, alg ); |
| 638 | psa_set_key_type( &attributes, key_type ); |
| 639 | |
| 640 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 641 | &key ) ); |
| 642 | |
| 643 | if( is_verify ) |
| 644 | status = psa_mac_verify_setup( &operation, key, alg ); |
| 645 | else |
| 646 | status = psa_mac_sign_setup( &operation, key, alg ); |
| 647 | |
| 648 | PSA_ASSERT( status ); |
| 649 | |
| 650 | if( data_part_len_arg != -1 ) |
| 651 | { |
| 652 | /* Pass data in parts */ |
| 653 | data_part_len = ( size_t ) data_part_len_arg; |
| 654 | |
| 655 | for( part_offset = 0, part_count = 0; |
| 656 | part_offset < input_data->len; |
| 657 | part_offset += part_length, part_count++ ) |
| 658 | { |
| 659 | if( do_zero_parts && ( part_count & 0x01 ) ) |
| 660 | { |
| 661 | part_length = 0; |
| 662 | } |
| 663 | else if( ( input_data->len - part_offset ) < data_part_len ) |
| 664 | { |
| 665 | part_length = ( input_data->len - part_offset ); |
| 666 | } |
| 667 | else |
| 668 | { |
| 669 | part_length = data_part_len; |
| 670 | } |
| 671 | |
| 672 | PSA_ASSERT( psa_mac_update( &operation, |
| 673 | ( input_data->x + part_offset ), |
| 674 | part_length ) ); |
| 675 | } |
| 676 | } |
| 677 | else |
| 678 | { |
| 679 | /* Pass all data in one go. */ |
| 680 | PSA_ASSERT( psa_mac_update( &operation, input_data->x, |
| 681 | input_data->len ) ); |
| 682 | } |
| 683 | |
| 684 | if( is_verify ) |
| 685 | { |
| 686 | PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x, |
| 687 | expected_output->len ) ); |
| 688 | } |
| 689 | else |
| 690 | { |
| 691 | PSA_ASSERT( psa_mac_sign_finish( &operation, mac, |
| 692 | PSA_MAC_MAX_SIZE, &mac_len ) ); |
| 693 | |
| 694 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 695 | mac, mac_len ); |
| 696 | } |
| 697 | |
| 698 | test_ok = 1; |
| 699 | |
| 700 | exit: |
| 701 | psa_destroy_key( key ); |
| 702 | psa_mac_abort( &operation ); |
| 703 | PSA_DONE( ); |
| 704 | |
| 705 | return( test_ok ); |
| 706 | } |
| 707 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 708 | #if defined(PSA_WANT_ALG_JPAKE) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 709 | static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive, |
| 710 | psa_pake_operation_t *server, |
| 711 | psa_pake_operation_t *client, |
| 712 | int client_input_first, |
| 713 | int round, int inject_error ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 714 | { |
| 715 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 716 | size_t buffer_length = ( |
| 717 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 718 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 719 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2; |
| 720 | size_t buffer0_off = 0; |
| 721 | size_t buffer1_off = 0; |
| 722 | size_t s_g1_len, s_g2_len, s_a_len; |
| 723 | size_t s_g1_off, s_g2_off, s_a_off; |
| 724 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 725 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 726 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 727 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 728 | size_t c_g1_len, c_g2_len, c_a_len; |
| 729 | size_t c_g1_off, c_g2_off, c_a_off; |
| 730 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 731 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 732 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 733 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 734 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 735 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 736 | |
| 737 | ASSERT_ALLOC( buffer0, buffer_length ); |
| 738 | ASSERT_ALLOC( buffer1, buffer_length ); |
| 739 | |
| 740 | switch( round ) |
| 741 | { |
| 742 | case 1: |
| 743 | /* Server first round Output */ |
| 744 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 745 | buffer0 + buffer0_off, |
| 746 | 512 - buffer0_off, &s_g1_len ) ); |
| 747 | s_g1_off = buffer0_off; |
| 748 | buffer0_off += s_g1_len; |
| 749 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 750 | buffer0 + buffer0_off, |
| 751 | 512 - buffer0_off, &s_x1_pk_len ) ); |
| 752 | s_x1_pk_off = buffer0_off; |
| 753 | buffer0_off += s_x1_pk_len; |
| 754 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 755 | buffer0 + buffer0_off, |
| 756 | 512 - buffer0_off, &s_x1_pr_len ) ); |
| 757 | s_x1_pr_off = buffer0_off; |
| 758 | buffer0_off += s_x1_pr_len; |
| 759 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 760 | buffer0 + buffer0_off, |
| 761 | 512 - buffer0_off, &s_g2_len ) ); |
| 762 | s_g2_off = buffer0_off; |
| 763 | buffer0_off += s_g2_len; |
| 764 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 765 | buffer0 + buffer0_off, |
| 766 | 512 - buffer0_off, &s_x2_pk_len ) ); |
| 767 | s_x2_pk_off = buffer0_off; |
| 768 | buffer0_off += s_x2_pk_len; |
| 769 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 770 | buffer0 + buffer0_off, |
| 771 | 512 - buffer0_off, &s_x2_pr_len ) ); |
| 772 | s_x2_pr_off = buffer0_off; |
| 773 | buffer0_off += s_x2_pr_len; |
| 774 | |
| 775 | if( inject_error == 1 ) |
| 776 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 777 | buffer0[s_x1_pk_off + 8] >>= 4; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 778 | buffer0[s_x2_pk_off + 7] <<= 4; |
| 779 | expected_status = PSA_ERROR_DATA_INVALID; |
| 780 | } |
| 781 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 782 | /* |
| 783 | * When injecting errors in inputs, the implementation is |
| 784 | * free to detect it right away of with a delay. |
| 785 | * This permits delaying the error until the end of the input |
| 786 | * sequence, if no error appears then, this will be treated |
| 787 | * as an error. |
| 788 | */ |
| 789 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 790 | if( client_input_first == 1 ) |
| 791 | { |
| 792 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 793 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 794 | buffer0 + s_g1_off, s_g1_len ); |
| 795 | if( inject_error == 1 && status != PSA_SUCCESS ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 796 | { |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 797 | TEST_EQUAL( status, expected_status ); |
| 798 | break; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 799 | } |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 800 | else |
| 801 | { |
| 802 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 803 | } |
| 804 | |
| 805 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 806 | buffer0 + s_x1_pk_off, |
| 807 | s_x1_pk_len ); |
| 808 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 809 | { |
| 810 | TEST_EQUAL( status, expected_status ); |
| 811 | break; |
| 812 | } |
| 813 | else |
| 814 | { |
| 815 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 816 | } |
| 817 | |
| 818 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 819 | buffer0 + s_x1_pr_off, |
| 820 | s_x1_pr_len ); |
| 821 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 822 | { |
| 823 | TEST_EQUAL( status, expected_status ); |
| 824 | break; |
| 825 | } |
| 826 | else |
| 827 | { |
| 828 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 829 | } |
| 830 | |
| 831 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 832 | buffer0 + s_g2_off, |
| 833 | s_g2_len ); |
| 834 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 835 | { |
| 836 | TEST_EQUAL( status, expected_status ); |
| 837 | break; |
| 838 | } |
| 839 | else |
| 840 | { |
| 841 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 842 | } |
| 843 | |
| 844 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 845 | buffer0 + s_x2_pk_off, |
| 846 | s_x2_pk_len ); |
| 847 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 848 | { |
| 849 | TEST_EQUAL( status, expected_status ); |
| 850 | break; |
| 851 | } |
| 852 | else |
| 853 | { |
| 854 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 855 | } |
| 856 | |
| 857 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 858 | buffer0 + s_x2_pr_off, |
| 859 | s_x2_pr_len ); |
| 860 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 861 | { |
| 862 | TEST_EQUAL( status, expected_status ); |
| 863 | break; |
| 864 | } |
| 865 | else |
| 866 | { |
| 867 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 868 | } |
| 869 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 870 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 871 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 872 | 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] | 873 | } |
| 874 | |
| 875 | /* Client first round Output */ |
| 876 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 877 | buffer1 + buffer1_off, |
| 878 | 512 - buffer1_off, &c_g1_len ) ); |
| 879 | c_g1_off = buffer1_off; |
| 880 | buffer1_off += c_g1_len; |
| 881 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 882 | buffer1 + buffer1_off, |
| 883 | 512 - buffer1_off, &c_x1_pk_len ) ); |
| 884 | c_x1_pk_off = buffer1_off; |
| 885 | buffer1_off += c_x1_pk_len; |
| 886 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 887 | buffer1 + buffer1_off, |
| 888 | 512 - buffer1_off, &c_x1_pr_len ) ); |
| 889 | c_x1_pr_off = buffer1_off; |
| 890 | buffer1_off += c_x1_pr_len; |
| 891 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 892 | buffer1 + buffer1_off, |
| 893 | 512 - buffer1_off, &c_g2_len ) ); |
| 894 | c_g2_off = buffer1_off; |
| 895 | buffer1_off += c_g2_len; |
| 896 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 897 | buffer1 + buffer1_off, |
| 898 | 512 - buffer1_off, &c_x2_pk_len ) ); |
| 899 | c_x2_pk_off = buffer1_off; |
| 900 | buffer1_off += c_x2_pk_len; |
| 901 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 902 | buffer1 + buffer1_off, |
| 903 | 512 - buffer1_off, &c_x2_pr_len ) ); |
| 904 | c_x2_pr_off = buffer1_off; |
| 905 | buffer1_off += c_x2_pr_len; |
| 906 | |
| 907 | if( client_input_first == 0 ) |
| 908 | { |
| 909 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 910 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 911 | buffer0 + s_g1_off, s_g1_len ); |
| 912 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 913 | { |
| 914 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 915 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 916 | } |
| 917 | else |
| 918 | { |
| 919 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 920 | } |
| 921 | |
| 922 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 923 | buffer0 + s_x1_pk_off, |
| 924 | s_x1_pk_len ); |
| 925 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 926 | { |
| 927 | TEST_EQUAL( status, expected_status ); |
| 928 | break; |
| 929 | } |
| 930 | else |
| 931 | { |
| 932 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 933 | } |
| 934 | |
| 935 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 936 | buffer0 + s_x1_pr_off, |
| 937 | s_x1_pr_len ); |
| 938 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 939 | { |
| 940 | TEST_EQUAL( status, expected_status ); |
| 941 | break; |
| 942 | } |
| 943 | else |
| 944 | { |
| 945 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 946 | } |
| 947 | |
| 948 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 949 | buffer0 + s_g2_off, |
| 950 | s_g2_len ); |
| 951 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 952 | { |
| 953 | TEST_EQUAL( status, expected_status ); |
| 954 | break; |
| 955 | } |
| 956 | else |
| 957 | { |
| 958 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 959 | } |
| 960 | |
| 961 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 962 | buffer0 + s_x2_pk_off, |
| 963 | s_x2_pk_len ); |
| 964 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 965 | { |
| 966 | TEST_EQUAL( status, expected_status ); |
| 967 | break; |
| 968 | } |
| 969 | else |
| 970 | { |
| 971 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 972 | } |
| 973 | |
| 974 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 975 | buffer0 + s_x2_pr_off, |
| 976 | s_x2_pr_len ); |
| 977 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 978 | { |
| 979 | TEST_EQUAL( status, expected_status ); |
| 980 | break; |
| 981 | } |
| 982 | else |
| 983 | { |
| 984 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 985 | } |
| 986 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 987 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 988 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 989 | 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] | 990 | } |
| 991 | |
| 992 | if( inject_error == 2 ) |
| 993 | { |
| 994 | buffer1[c_x1_pk_off + 12] >>= 4; |
| 995 | buffer1[c_x2_pk_off + 7] <<= 4; |
| 996 | expected_status = PSA_ERROR_DATA_INVALID; |
| 997 | } |
| 998 | |
| 999 | /* Server first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1000 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1001 | buffer1 + c_g1_off, c_g1_len ); |
| 1002 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1003 | { |
| 1004 | TEST_EQUAL( status, expected_status ); |
| 1005 | break; |
| 1006 | } |
| 1007 | else |
| 1008 | { |
| 1009 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1010 | } |
| 1011 | |
| 1012 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1013 | buffer1 + c_x1_pk_off, c_x1_pk_len ); |
| 1014 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1015 | { |
| 1016 | TEST_EQUAL( status, expected_status ); |
| 1017 | break; |
| 1018 | } |
| 1019 | else |
| 1020 | { |
| 1021 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1022 | } |
| 1023 | |
| 1024 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1025 | buffer1 + c_x1_pr_off, c_x1_pr_len ); |
| 1026 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1027 | { |
| 1028 | TEST_EQUAL( status, expected_status ); |
| 1029 | break; |
| 1030 | } |
| 1031 | else |
| 1032 | { |
| 1033 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1034 | } |
| 1035 | |
| 1036 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1037 | buffer1 + c_g2_off, c_g2_len ); |
| 1038 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1039 | { |
| 1040 | TEST_EQUAL( status, expected_status ); |
| 1041 | break; |
| 1042 | } |
| 1043 | else |
| 1044 | { |
| 1045 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1046 | } |
| 1047 | |
| 1048 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1049 | buffer1 + c_x2_pk_off, c_x2_pk_len ); |
| 1050 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1051 | { |
| 1052 | TEST_EQUAL( status, expected_status ); |
| 1053 | break; |
| 1054 | } |
| 1055 | else |
| 1056 | { |
| 1057 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1058 | } |
| 1059 | |
| 1060 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1061 | buffer1 + c_x2_pr_off, c_x2_pr_len ); |
| 1062 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1063 | { |
| 1064 | TEST_EQUAL( status, expected_status ); |
| 1065 | break; |
| 1066 | } |
| 1067 | else |
| 1068 | { |
| 1069 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1070 | } |
| 1071 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1072 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1073 | if( inject_error == 2 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1074 | 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] | 1075 | |
| 1076 | break; |
| 1077 | |
| 1078 | case 2: |
| 1079 | /* Server second round Output */ |
| 1080 | buffer0_off = 0; |
| 1081 | |
| 1082 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1083 | buffer0 + buffer0_off, |
| 1084 | 512 - buffer0_off, &s_a_len ) ); |
| 1085 | s_a_off = buffer0_off; |
| 1086 | buffer0_off += s_a_len; |
| 1087 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1088 | buffer0 + buffer0_off, |
| 1089 | 512 - buffer0_off, &s_x2s_pk_len ) ); |
| 1090 | s_x2s_pk_off = buffer0_off; |
| 1091 | buffer0_off += s_x2s_pk_len; |
| 1092 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1093 | buffer0 + buffer0_off, |
| 1094 | 512 - buffer0_off, &s_x2s_pr_len ) ); |
| 1095 | s_x2s_pr_off = buffer0_off; |
| 1096 | buffer0_off += s_x2s_pr_len; |
| 1097 | |
| 1098 | if( inject_error == 3 ) |
| 1099 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1100 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1101 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1102 | } |
| 1103 | |
| 1104 | if( client_input_first == 1 ) |
| 1105 | { |
| 1106 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1107 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1108 | buffer0 + s_a_off, s_a_len ); |
| 1109 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1110 | { |
| 1111 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1112 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1113 | } |
| 1114 | else |
| 1115 | { |
| 1116 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1117 | } |
| 1118 | |
| 1119 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1120 | buffer0 + s_x2s_pk_off, |
| 1121 | s_x2s_pk_len ); |
| 1122 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1123 | { |
| 1124 | TEST_EQUAL( status, expected_status ); |
| 1125 | break; |
| 1126 | } |
| 1127 | else |
| 1128 | { |
| 1129 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1130 | } |
| 1131 | |
| 1132 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1133 | buffer0 + s_x2s_pr_off, |
| 1134 | s_x2s_pr_len ); |
| 1135 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1136 | { |
| 1137 | TEST_EQUAL( status, expected_status ); |
| 1138 | break; |
| 1139 | } |
| 1140 | else |
| 1141 | { |
| 1142 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1143 | } |
| 1144 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1145 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1146 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1147 | 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] | 1148 | } |
| 1149 | |
| 1150 | /* Client second round Output */ |
| 1151 | buffer1_off = 0; |
| 1152 | |
| 1153 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1154 | buffer1 + buffer1_off, |
| 1155 | 512 - buffer1_off, &c_a_len ) ); |
| 1156 | c_a_off = buffer1_off; |
| 1157 | buffer1_off += c_a_len; |
| 1158 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1159 | buffer1 + buffer1_off, |
| 1160 | 512 - buffer1_off, &c_x2s_pk_len ) ); |
| 1161 | c_x2s_pk_off = buffer1_off; |
| 1162 | buffer1_off += c_x2s_pk_len; |
| 1163 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1164 | buffer1 + buffer1_off, |
| 1165 | 512 - buffer1_off, &c_x2s_pr_len ) ); |
| 1166 | c_x2s_pr_off = buffer1_off; |
| 1167 | buffer1_off += c_x2s_pr_len; |
| 1168 | |
| 1169 | if( client_input_first == 0 ) |
| 1170 | { |
| 1171 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1172 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1173 | buffer0 + s_a_off, s_a_len ); |
| 1174 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1175 | { |
| 1176 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1177 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1178 | } |
| 1179 | else |
| 1180 | { |
| 1181 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1182 | } |
| 1183 | |
| 1184 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1185 | buffer0 + s_x2s_pk_off, |
| 1186 | s_x2s_pk_len ); |
| 1187 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1188 | { |
| 1189 | TEST_EQUAL( status, expected_status ); |
| 1190 | break; |
| 1191 | } |
| 1192 | else |
| 1193 | { |
| 1194 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1195 | } |
| 1196 | |
| 1197 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1198 | buffer0 + s_x2s_pr_off, |
| 1199 | s_x2s_pr_len ); |
| 1200 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1201 | { |
| 1202 | TEST_EQUAL( status, expected_status ); |
| 1203 | break; |
| 1204 | } |
| 1205 | else |
| 1206 | { |
| 1207 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1208 | } |
| 1209 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1210 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1211 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1212 | 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] | 1213 | } |
| 1214 | |
| 1215 | if( inject_error == 4 ) |
| 1216 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1217 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1218 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1219 | } |
| 1220 | |
| 1221 | /* Server second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1222 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1223 | buffer1 + c_a_off, c_a_len ); |
| 1224 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1225 | { |
| 1226 | TEST_EQUAL( status, expected_status ); |
| 1227 | break; |
| 1228 | } |
| 1229 | else |
| 1230 | { |
| 1231 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1232 | } |
| 1233 | |
| 1234 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1235 | buffer1 + c_x2s_pk_off, c_x2s_pk_len ); |
| 1236 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1237 | { |
| 1238 | TEST_EQUAL( status, expected_status ); |
| 1239 | break; |
| 1240 | } |
| 1241 | else |
| 1242 | { |
| 1243 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1244 | } |
| 1245 | |
| 1246 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1247 | buffer1 + c_x2s_pr_off, c_x2s_pr_len ); |
| 1248 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1249 | { |
| 1250 | TEST_EQUAL( status, expected_status ); |
| 1251 | break; |
| 1252 | } |
| 1253 | else |
| 1254 | { |
| 1255 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1256 | } |
| 1257 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1258 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1259 | if( inject_error == 4 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1260 | 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] | 1261 | |
| 1262 | break; |
| 1263 | |
| 1264 | } |
| 1265 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1266 | exit: |
| 1267 | mbedtls_free( buffer0 ); |
| 1268 | mbedtls_free( buffer1 ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1269 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1270 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1271 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1272 | /* END_HEADER */ |
| 1273 | |
| 1274 | /* BEGIN_DEPENDENCIES |
| 1275 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1276 | * END_DEPENDENCIES |
| 1277 | */ |
| 1278 | |
| 1279 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1280 | void static_checks( ) |
| 1281 | { |
| 1282 | size_t max_truncated_mac_size = |
| 1283 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1284 | |
| 1285 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1286 | * encoding. The shifted mask is the maximum truncated value. The |
| 1287 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1288 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1289 | } |
| 1290 | /* END_CASE */ |
| 1291 | |
| 1292 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1293 | void import_with_policy( int type_arg, |
| 1294 | int usage_arg, int alg_arg, |
| 1295 | int expected_status_arg ) |
| 1296 | { |
| 1297 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1298 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1299 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1300 | psa_key_type_t type = type_arg; |
| 1301 | psa_key_usage_t usage = usage_arg; |
| 1302 | psa_algorithm_t alg = alg_arg; |
| 1303 | psa_status_t expected_status = expected_status_arg; |
| 1304 | const uint8_t key_material[16] = {0}; |
| 1305 | psa_status_t status; |
| 1306 | |
| 1307 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1308 | |
| 1309 | psa_set_key_type( &attributes, type ); |
| 1310 | psa_set_key_usage_flags( &attributes, usage ); |
| 1311 | psa_set_key_algorithm( &attributes, alg ); |
| 1312 | |
| 1313 | status = psa_import_key( &attributes, |
| 1314 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1315 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1316 | TEST_EQUAL( status, expected_status ); |
| 1317 | if( status != PSA_SUCCESS ) |
| 1318 | goto exit; |
| 1319 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1320 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1321 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1322 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1323 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1324 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1325 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1326 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1327 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1328 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1329 | |
| 1330 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1331 | /* |
| 1332 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1333 | * thus reset them as required. |
| 1334 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1335 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1336 | |
| 1337 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1338 | PSA_DONE( ); |
| 1339 | } |
| 1340 | /* END_CASE */ |
| 1341 | |
| 1342 | /* BEGIN_CASE */ |
| 1343 | void import_with_data( data_t *data, int type_arg, |
| 1344 | int attr_bits_arg, |
| 1345 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1346 | { |
| 1347 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1348 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1349 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1350 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1351 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1352 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1353 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1354 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1355 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1356 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1357 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1358 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1359 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1360 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1361 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1362 | if( status != PSA_SUCCESS ) |
| 1363 | goto exit; |
| 1364 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1365 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1366 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1367 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1368 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1369 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1370 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1371 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1372 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1373 | |
| 1374 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1375 | /* |
| 1376 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1377 | * thus reset them as required. |
| 1378 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1379 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1380 | |
| 1381 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1382 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1383 | } |
| 1384 | /* END_CASE */ |
| 1385 | |
| 1386 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1387 | void import_large_key( int type_arg, int byte_size_arg, |
| 1388 | int expected_status_arg ) |
| 1389 | { |
| 1390 | psa_key_type_t type = type_arg; |
| 1391 | size_t byte_size = byte_size_arg; |
| 1392 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1393 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1394 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1395 | psa_status_t status; |
| 1396 | uint8_t *buffer = NULL; |
| 1397 | size_t buffer_size = byte_size + 1; |
| 1398 | size_t n; |
| 1399 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1400 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1401 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1402 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1403 | memset( buffer, 'K', byte_size ); |
| 1404 | |
| 1405 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1406 | |
| 1407 | /* Try importing the key */ |
| 1408 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1409 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1410 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1411 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1412 | TEST_EQUAL( status, expected_status ); |
| 1413 | |
| 1414 | if( status == PSA_SUCCESS ) |
| 1415 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1416 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1417 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1418 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1419 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1420 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1421 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1422 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1423 | for( n = 0; n < byte_size; n++ ) |
| 1424 | TEST_EQUAL( buffer[n], 'K' ); |
| 1425 | for( n = byte_size; n < buffer_size; n++ ) |
| 1426 | TEST_EQUAL( buffer[n], 0 ); |
| 1427 | } |
| 1428 | |
| 1429 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1430 | /* |
| 1431 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1432 | * thus reset them as required. |
| 1433 | */ |
| 1434 | psa_reset_key_attributes( &attributes ); |
| 1435 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1436 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1437 | PSA_DONE( ); |
| 1438 | mbedtls_free( buffer ); |
| 1439 | } |
| 1440 | /* END_CASE */ |
| 1441 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1442 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1443 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1444 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1445 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1446 | size_t bits = bits_arg; |
| 1447 | psa_status_t expected_status = expected_status_arg; |
| 1448 | psa_status_t status; |
| 1449 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1450 | 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] | 1451 | size_t buffer_size = /* Slight overapproximations */ |
| 1452 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1453 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1454 | unsigned char *p; |
| 1455 | int ret; |
| 1456 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1457 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1458 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1459 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1460 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1461 | |
| 1462 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1463 | bits, keypair ) ) >= 0 ); |
| 1464 | length = ret; |
| 1465 | |
| 1466 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1467 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1468 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1469 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1470 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1471 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1472 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1473 | |
| 1474 | exit: |
| 1475 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1476 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1477 | } |
| 1478 | /* END_CASE */ |
| 1479 | |
| 1480 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1481 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1482 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1483 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1484 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1485 | int expected_bits, |
| 1486 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1487 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1488 | int canonical_input ) |
| 1489 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1490 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1491 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1492 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1493 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1494 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1495 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1496 | unsigned char *exported = NULL; |
| 1497 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1498 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1499 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1500 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1501 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1502 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1503 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1504 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1505 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1506 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1507 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1508 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1509 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1510 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1511 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1512 | psa_set_key_algorithm( &attributes, alg ); |
| 1513 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1514 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1515 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1516 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1517 | |
| 1518 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1519 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1520 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1521 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1522 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1523 | |
| 1524 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1525 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1526 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1527 | |
| 1528 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1529 | * and export_size. On errors, the exported length must be 0. */ |
| 1530 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1531 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1532 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1533 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1534 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1535 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1536 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1537 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1538 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1539 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1540 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1541 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1542 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1543 | * this validates the canonical representations. For canonical inputs, |
| 1544 | * this doesn't directly validate the implementation, but it still helps |
| 1545 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1546 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1547 | { |
| 1548 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1549 | goto exit; |
| 1550 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1551 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1552 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1553 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1554 | else |
| 1555 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1556 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1557 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1558 | &key2 ) ); |
| 1559 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1560 | reexported, |
| 1561 | export_size, |
| 1562 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1563 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1564 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1565 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1566 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1567 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1568 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1569 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1570 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1571 | |
| 1572 | destroy: |
| 1573 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1574 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1575 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1576 | |
| 1577 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1578 | /* |
| 1579 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1580 | * thus reset them as required. |
| 1581 | */ |
| 1582 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1583 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1584 | mbedtls_free( exported ); |
| 1585 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1586 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1587 | } |
| 1588 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1589 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1590 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1591 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1592 | int type_arg, |
| 1593 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1594 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1595 | int export_size_delta, |
| 1596 | int expected_export_status_arg, |
| 1597 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1598 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1599 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1600 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1601 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1602 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1603 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1604 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1605 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1606 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1607 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1608 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1609 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1610 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1611 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1612 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1613 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1614 | psa_set_key_algorithm( &attributes, alg ); |
| 1615 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1616 | |
| 1617 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1618 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1619 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1620 | /* Export the public key */ |
| 1621 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1622 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1623 | exported, export_size, |
| 1624 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1625 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1626 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1627 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1628 | 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] | 1629 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1630 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1631 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1632 | TEST_LE_U( expected_public_key->len, |
| 1633 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1634 | TEST_LE_U( expected_public_key->len, |
| 1635 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1636 | TEST_LE_U( expected_public_key->len, |
| 1637 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1638 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1639 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1640 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1641 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1642 | /* |
| 1643 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1644 | * thus reset them as required. |
| 1645 | */ |
| 1646 | psa_reset_key_attributes( &attributes ); |
| 1647 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1648 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1649 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1650 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1651 | } |
| 1652 | /* END_CASE */ |
| 1653 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1654 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1655 | void import_and_exercise_key( data_t *data, |
| 1656 | int type_arg, |
| 1657 | int bits_arg, |
| 1658 | int alg_arg ) |
| 1659 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1660 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1661 | psa_key_type_t type = type_arg; |
| 1662 | size_t bits = bits_arg; |
| 1663 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1664 | 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] | 1665 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1666 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1667 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1668 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1669 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1670 | psa_set_key_usage_flags( &attributes, usage ); |
| 1671 | psa_set_key_algorithm( &attributes, alg ); |
| 1672 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1673 | |
| 1674 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1675 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1676 | |
| 1677 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1678 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1679 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1680 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1681 | |
| 1682 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1683 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1684 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1685 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1686 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1687 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1688 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1689 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1690 | /* |
| 1691 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1692 | * thus reset them as required. |
| 1693 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1694 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1695 | |
| 1696 | psa_reset_key_attributes( &attributes ); |
| 1697 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1698 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1699 | } |
| 1700 | /* END_CASE */ |
| 1701 | |
| 1702 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1703 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1704 | int bits_arg, int expected_bits_arg, |
| 1705 | int usage_arg, int expected_usage_arg, |
| 1706 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1707 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1708 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1709 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1710 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1711 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1712 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1713 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1714 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1715 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1716 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1717 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1718 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1719 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1720 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1721 | psa_set_key_usage_flags( &attributes, usage ); |
| 1722 | psa_set_key_algorithm( &attributes, alg ); |
| 1723 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1724 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1725 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1726 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1727 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1728 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1729 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1730 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1731 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1732 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1733 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1734 | |
| 1735 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1736 | /* |
| 1737 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1738 | * thus reset them as required. |
| 1739 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1740 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1741 | |
| 1742 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1743 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1744 | } |
| 1745 | /* END_CASE */ |
| 1746 | |
| 1747 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1748 | void check_key_policy( int type_arg, int bits_arg, |
| 1749 | int usage_arg, int alg_arg ) |
| 1750 | { |
| 1751 | 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] | 1752 | usage_arg, |
| 1753 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1754 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1755 | goto exit; |
| 1756 | } |
| 1757 | /* END_CASE */ |
| 1758 | |
| 1759 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1760 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1761 | { |
| 1762 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1763 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1764 | * 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] | 1765 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1766 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1767 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1768 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1769 | |
| 1770 | memset( &zero, 0, sizeof( zero ) ); |
| 1771 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1772 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1773 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1774 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1775 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1776 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1777 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1778 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1779 | |
| 1780 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1781 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1782 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1783 | |
| 1784 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1785 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1786 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1787 | |
| 1788 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1789 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1790 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1791 | } |
| 1792 | /* END_CASE */ |
| 1793 | |
| 1794 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1795 | void mac_key_policy( int policy_usage_arg, |
| 1796 | int policy_alg_arg, |
| 1797 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1798 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1799 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1800 | int expected_status_sign_arg, |
| 1801 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1802 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1803 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1804 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1805 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1806 | psa_key_type_t key_type = key_type_arg; |
| 1807 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1808 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1809 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1810 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1811 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1812 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1813 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1814 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1815 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1816 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1817 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1818 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1819 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1820 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1821 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1822 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1823 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1824 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1825 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1826 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1827 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1828 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1829 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1830 | /* Calculate the MAC, one-shot case. */ |
| 1831 | uint8_t input[128] = {0}; |
| 1832 | size_t mac_len; |
| 1833 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1834 | input, 128, |
| 1835 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1836 | expected_status_sign ); |
| 1837 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1838 | /* Calculate the MAC, multi-part case. */ |
| 1839 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1840 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1841 | if( status == PSA_SUCCESS ) |
| 1842 | { |
| 1843 | status = psa_mac_update( &operation, input, 128 ); |
| 1844 | if( status == PSA_SUCCESS ) |
| 1845 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1846 | &mac_len ), |
| 1847 | expected_status_sign ); |
| 1848 | else |
| 1849 | TEST_EQUAL( status, expected_status_sign ); |
| 1850 | } |
| 1851 | else |
| 1852 | { |
| 1853 | TEST_EQUAL( status, expected_status_sign ); |
| 1854 | } |
| 1855 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1856 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1857 | /* Verify correct MAC, one-shot case. */ |
| 1858 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1859 | mac, mac_len ); |
| 1860 | |
| 1861 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1862 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1863 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1864 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1865 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1866 | /* Verify correct MAC, multi-part case. */ |
| 1867 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1868 | if( status == PSA_SUCCESS ) |
| 1869 | { |
| 1870 | status = psa_mac_update( &operation, input, 128 ); |
| 1871 | if( status == PSA_SUCCESS ) |
| 1872 | { |
| 1873 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1874 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1875 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1876 | else |
| 1877 | TEST_EQUAL( status, expected_status_verify ); |
| 1878 | } |
| 1879 | else |
| 1880 | { |
| 1881 | TEST_EQUAL( status, expected_status_verify ); |
| 1882 | } |
| 1883 | } |
| 1884 | else |
| 1885 | { |
| 1886 | TEST_EQUAL( status, expected_status_verify ); |
| 1887 | } |
| 1888 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1889 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1890 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1891 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1892 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1893 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1894 | |
| 1895 | exit: |
| 1896 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1897 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1898 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1899 | } |
| 1900 | /* END_CASE */ |
| 1901 | |
| 1902 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1903 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1904 | int policy_alg, |
| 1905 | int key_type, |
| 1906 | data_t *key_data, |
| 1907 | int exercise_alg ) |
| 1908 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1909 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1910 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1911 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1912 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1913 | size_t output_buffer_size = 0; |
| 1914 | size_t input_buffer_size = 0; |
| 1915 | size_t output_length = 0; |
| 1916 | uint8_t *output = NULL; |
| 1917 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1918 | psa_status_t status; |
| 1919 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1920 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1921 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1922 | input_buffer_size ); |
| 1923 | |
| 1924 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1925 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1926 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1927 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1928 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1929 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1930 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1931 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1932 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1933 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1934 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1935 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1936 | /* Check if no key usage flag implication is done */ |
| 1937 | TEST_EQUAL( policy_usage, |
| 1938 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1939 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1940 | /* Encrypt check, one-shot */ |
| 1941 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1942 | output, output_buffer_size, |
| 1943 | &output_length); |
| 1944 | if( policy_alg == exercise_alg && |
| 1945 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1946 | PSA_ASSERT( status ); |
| 1947 | else |
| 1948 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1949 | |
| 1950 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1951 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1952 | if( policy_alg == exercise_alg && |
| 1953 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1954 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1955 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1956 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1957 | psa_cipher_abort( &operation ); |
| 1958 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1959 | /* Decrypt check, one-shot */ |
| 1960 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 1961 | input, input_buffer_size, |
| 1962 | &output_length); |
| 1963 | if( policy_alg == exercise_alg && |
| 1964 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1965 | PSA_ASSERT( status ); |
| 1966 | else |
| 1967 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1968 | |
| 1969 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1970 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1971 | if( policy_alg == exercise_alg && |
| 1972 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1973 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1974 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1975 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1976 | |
| 1977 | exit: |
| 1978 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1979 | mbedtls_free( input ); |
| 1980 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1981 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1982 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1983 | } |
| 1984 | /* END_CASE */ |
| 1985 | |
| 1986 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1987 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1988 | int policy_alg, |
| 1989 | int key_type, |
| 1990 | data_t *key_data, |
| 1991 | int nonce_length_arg, |
| 1992 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1993 | int exercise_alg, |
| 1994 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1995 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1996 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1997 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1998 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1999 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2000 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2001 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2002 | unsigned char nonce[16] = {0}; |
| 2003 | size_t nonce_length = nonce_length_arg; |
| 2004 | unsigned char tag[16]; |
| 2005 | size_t tag_length = tag_length_arg; |
| 2006 | size_t output_length; |
| 2007 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2008 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 2009 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2010 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2011 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2012 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2013 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2014 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2015 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2016 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2017 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2018 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2019 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2020 | /* Check if no key usage implication is done */ |
| 2021 | TEST_EQUAL( policy_usage, |
| 2022 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2023 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2024 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2025 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2026 | nonce, nonce_length, |
| 2027 | NULL, 0, |
| 2028 | NULL, 0, |
| 2029 | tag, tag_length, |
| 2030 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2031 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2032 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2033 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2034 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2035 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2036 | /* Encrypt check, multi-part */ |
| 2037 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 2038 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2039 | TEST_EQUAL( status, expected_status ); |
| 2040 | else |
| 2041 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2042 | |
| 2043 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2044 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2045 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2046 | nonce, nonce_length, |
| 2047 | NULL, 0, |
| 2048 | tag, tag_length, |
| 2049 | NULL, 0, |
| 2050 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2051 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2052 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2053 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2054 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2055 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2056 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2057 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2058 | /* Decrypt check, multi-part */ |
| 2059 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 2060 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 2061 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2062 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2063 | else |
| 2064 | TEST_EQUAL( status, expected_status ); |
| 2065 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2066 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2067 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2068 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2069 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2070 | } |
| 2071 | /* END_CASE */ |
| 2072 | |
| 2073 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2074 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2075 | int policy_alg, |
| 2076 | int key_type, |
| 2077 | data_t *key_data, |
| 2078 | int exercise_alg ) |
| 2079 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2080 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2081 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2082 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2083 | psa_status_t status; |
| 2084 | size_t key_bits; |
| 2085 | size_t buffer_length; |
| 2086 | unsigned char *buffer = NULL; |
| 2087 | size_t output_length; |
| 2088 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2089 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2090 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2091 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2092 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2093 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2094 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2095 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2096 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2097 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2098 | /* Check if no key usage implication is done */ |
| 2099 | TEST_EQUAL( policy_usage, |
| 2100 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2101 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2102 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2103 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2104 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2105 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2106 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2107 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2108 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2109 | NULL, 0, |
| 2110 | NULL, 0, |
| 2111 | buffer, buffer_length, |
| 2112 | &output_length ); |
| 2113 | if( policy_alg == exercise_alg && |
| 2114 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2115 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2116 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2117 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2118 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2119 | if( buffer_length != 0 ) |
| 2120 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2121 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2122 | buffer, buffer_length, |
| 2123 | NULL, 0, |
| 2124 | buffer, buffer_length, |
| 2125 | &output_length ); |
| 2126 | if( policy_alg == exercise_alg && |
| 2127 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2128 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2129 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2130 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2131 | |
| 2132 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2133 | /* |
| 2134 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2135 | * thus reset them as required. |
| 2136 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2137 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2138 | |
| 2139 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2140 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2141 | mbedtls_free( buffer ); |
| 2142 | } |
| 2143 | /* END_CASE */ |
| 2144 | |
| 2145 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2146 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2147 | int policy_alg, |
| 2148 | int key_type, |
| 2149 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2150 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2151 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2152 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2153 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2154 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2155 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2156 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2157 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2158 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2159 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2160 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2161 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2162 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2163 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2164 | int compatible_alg = payload_length_arg > 0; |
| 2165 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2166 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2167 | size_t signature_length; |
| 2168 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2169 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2170 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2171 | TEST_EQUAL( expected_usage, |
| 2172 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2173 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2174 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2175 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2176 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2177 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2178 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2179 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2180 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2181 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2182 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2183 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 2184 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2185 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2186 | payload, payload_length, |
| 2187 | signature, sizeof( signature ), |
| 2188 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2189 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2190 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2191 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2192 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2193 | |
| 2194 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2195 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2196 | payload, payload_length, |
| 2197 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2198 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2199 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2200 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2201 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2202 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2203 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 2204 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2205 | { |
| 2206 | status = psa_sign_message( key, exercise_alg, |
| 2207 | payload, payload_length, |
| 2208 | signature, sizeof( signature ), |
| 2209 | &signature_length ); |
| 2210 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 2211 | PSA_ASSERT( status ); |
| 2212 | else |
| 2213 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2214 | |
| 2215 | memset( signature, 0, sizeof( signature ) ); |
| 2216 | status = psa_verify_message( key, exercise_alg, |
| 2217 | payload, payload_length, |
| 2218 | signature, sizeof( signature ) ); |
| 2219 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 2220 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 2221 | else |
| 2222 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2223 | } |
| 2224 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2225 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2226 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2227 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2228 | } |
| 2229 | /* END_CASE */ |
| 2230 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2231 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2232 | void derive_key_policy( int policy_usage, |
| 2233 | int policy_alg, |
| 2234 | int key_type, |
| 2235 | data_t *key_data, |
| 2236 | int exercise_alg ) |
| 2237 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2238 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2239 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2240 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2241 | psa_status_t status; |
| 2242 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2243 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2244 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2245 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2246 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2247 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2248 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2249 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2250 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2251 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2252 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2253 | |
| 2254 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2255 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2256 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2257 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2258 | &operation, |
| 2259 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2260 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2261 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2262 | |
| 2263 | status = psa_key_derivation_input_key( &operation, |
| 2264 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2265 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2266 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2267 | if( policy_alg == exercise_alg && |
| 2268 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2269 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2270 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2271 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2272 | |
| 2273 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2274 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2275 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2276 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2277 | } |
| 2278 | /* END_CASE */ |
| 2279 | |
| 2280 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2281 | void agreement_key_policy( int policy_usage, |
| 2282 | int policy_alg, |
| 2283 | int key_type_arg, |
| 2284 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2285 | int exercise_alg, |
| 2286 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2287 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2288 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2289 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2290 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2291 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2292 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2293 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2294 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2295 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2296 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2297 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2298 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2299 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2300 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2301 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2302 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2303 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2304 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2305 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2306 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2307 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2308 | |
| 2309 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2310 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2311 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2312 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2313 | } |
| 2314 | /* END_CASE */ |
| 2315 | |
| 2316 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2317 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2318 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2319 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2320 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2321 | psa_key_type_t key_type = key_type_arg; |
| 2322 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2323 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2324 | psa_key_usage_t usage = usage_arg; |
| 2325 | psa_algorithm_t alg = alg_arg; |
| 2326 | psa_algorithm_t alg2 = alg2_arg; |
| 2327 | |
| 2328 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2329 | |
| 2330 | psa_set_key_usage_flags( &attributes, usage ); |
| 2331 | psa_set_key_algorithm( &attributes, alg ); |
| 2332 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2333 | psa_set_key_type( &attributes, key_type ); |
| 2334 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2335 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2336 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2337 | /* Update the usage flags to obtain implicit usage flags */ |
| 2338 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2339 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2340 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2341 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2342 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2343 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2344 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2345 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2346 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2347 | goto exit; |
| 2348 | |
| 2349 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2350 | /* |
| 2351 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2352 | * thus reset them as required. |
| 2353 | */ |
| 2354 | psa_reset_key_attributes( &got_attributes ); |
| 2355 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2356 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2357 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2358 | } |
| 2359 | /* END_CASE */ |
| 2360 | |
| 2361 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2362 | void raw_agreement_key_policy( int policy_usage, |
| 2363 | int policy_alg, |
| 2364 | int key_type_arg, |
| 2365 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2366 | int exercise_alg, |
| 2367 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2368 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2369 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2370 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2371 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2372 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2373 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2374 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2375 | |
| 2376 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2377 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2378 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2379 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2380 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2381 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2382 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2383 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2384 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2385 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2386 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2387 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2388 | |
| 2389 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2390 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2391 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2392 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2393 | } |
| 2394 | /* END_CASE */ |
| 2395 | |
| 2396 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2397 | void copy_success( int source_usage_arg, |
| 2398 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2399 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2400 | int type_arg, data_t *material, |
| 2401 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2402 | int target_usage_arg, |
| 2403 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2404 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2405 | int expected_usage_arg, |
| 2406 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2407 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2408 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2409 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2410 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2411 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2412 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2413 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2414 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2415 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2416 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2417 | uint8_t *export_buffer = NULL; |
| 2418 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2419 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2420 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2421 | /* Prepare the source key. */ |
| 2422 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2423 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2424 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2425 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2426 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2427 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2428 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2429 | &source_key ) ); |
| 2430 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2431 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2432 | /* Prepare the target attributes. */ |
| 2433 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2434 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2435 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2436 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2437 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2438 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2439 | if( target_usage_arg != -1 ) |
| 2440 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2441 | if( target_alg_arg != -1 ) |
| 2442 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2443 | if( target_alg2_arg != -1 ) |
| 2444 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2445 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2446 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2447 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2448 | PSA_ASSERT( psa_copy_key( source_key, |
| 2449 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2450 | |
| 2451 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2452 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2453 | |
| 2454 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2455 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2456 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2457 | psa_get_key_type( &target_attributes ) ); |
| 2458 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2459 | psa_get_key_bits( &target_attributes ) ); |
| 2460 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2461 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2462 | TEST_EQUAL( expected_alg2, |
| 2463 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2464 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2465 | { |
| 2466 | size_t length; |
| 2467 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2468 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2469 | material->len, &length ) ); |
| 2470 | ASSERT_COMPARE( material->x, material->len, |
| 2471 | export_buffer, length ); |
| 2472 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2473 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2474 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2475 | { |
| 2476 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2477 | goto exit; |
| 2478 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2479 | goto exit; |
| 2480 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2481 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2482 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2483 | |
| 2484 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2485 | /* |
| 2486 | * Source and target key attributes may have been returned by |
| 2487 | * psa_get_key_attributes() thus reset them as required. |
| 2488 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2489 | psa_reset_key_attributes( &source_attributes ); |
| 2490 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2491 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2492 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2493 | mbedtls_free( export_buffer ); |
| 2494 | } |
| 2495 | /* END_CASE */ |
| 2496 | |
| 2497 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2498 | void copy_fail( int source_usage_arg, |
| 2499 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2500 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2501 | int type_arg, data_t *material, |
| 2502 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2503 | int target_usage_arg, |
| 2504 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2505 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2506 | int expected_status_arg ) |
| 2507 | { |
| 2508 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2509 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2510 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2511 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2512 | 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] | 2513 | |
| 2514 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2515 | |
| 2516 | /* Prepare the source key. */ |
| 2517 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2518 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2519 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2520 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2521 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2522 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2523 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2524 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2525 | |
| 2526 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2527 | psa_set_key_id( &target_attributes, key_id ); |
| 2528 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2529 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2530 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2531 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2532 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2533 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2534 | |
| 2535 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2536 | TEST_EQUAL( psa_copy_key( source_key, |
| 2537 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2538 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2539 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2540 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2541 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2542 | exit: |
| 2543 | psa_reset_key_attributes( &source_attributes ); |
| 2544 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2545 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2546 | } |
| 2547 | /* END_CASE */ |
| 2548 | |
| 2549 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2550 | void hash_operation_init( ) |
| 2551 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2552 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2553 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2554 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2555 | * 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] | 2556 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2557 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2558 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2559 | psa_hash_operation_t zero; |
| 2560 | |
| 2561 | memset( &zero, 0, sizeof( zero ) ); |
| 2562 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2563 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2564 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2565 | PSA_ERROR_BAD_STATE ); |
| 2566 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2567 | PSA_ERROR_BAD_STATE ); |
| 2568 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2569 | PSA_ERROR_BAD_STATE ); |
| 2570 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2571 | /* A default hash operation should be abortable without error. */ |
| 2572 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2573 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2574 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2575 | } |
| 2576 | /* END_CASE */ |
| 2577 | |
| 2578 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2579 | void hash_setup( int alg_arg, |
| 2580 | int expected_status_arg ) |
| 2581 | { |
| 2582 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2583 | uint8_t *output = NULL; |
| 2584 | size_t output_size = 0; |
| 2585 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2586 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2587 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2588 | psa_status_t status; |
| 2589 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2590 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2591 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2592 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2593 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2594 | ASSERT_ALLOC( output, output_size ); |
| 2595 | |
| 2596 | status = psa_hash_compute( alg, NULL, 0, |
| 2597 | output, output_size, &output_length ); |
| 2598 | TEST_EQUAL( status, expected_status ); |
| 2599 | |
| 2600 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2601 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2602 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2603 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2604 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2605 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2606 | |
| 2607 | /* If setup failed, reproduce the failure, so as to |
| 2608 | * test the resulting state of the operation object. */ |
| 2609 | if( status != PSA_SUCCESS ) |
| 2610 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2611 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2612 | /* Now the operation object should be reusable. */ |
| 2613 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2614 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2615 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2616 | #endif |
| 2617 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2618 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2619 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2620 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2621 | } |
| 2622 | /* END_CASE */ |
| 2623 | |
| 2624 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2625 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2626 | int output_size_arg, int expected_status_arg ) |
| 2627 | { |
| 2628 | psa_algorithm_t alg = alg_arg; |
| 2629 | uint8_t *output = NULL; |
| 2630 | size_t output_size = output_size_arg; |
| 2631 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2632 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2633 | psa_status_t expected_status = expected_status_arg; |
| 2634 | psa_status_t status; |
| 2635 | |
| 2636 | ASSERT_ALLOC( output, output_size ); |
| 2637 | |
| 2638 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2639 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2640 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2641 | status = psa_hash_compute( alg, input->x, input->len, |
| 2642 | output, output_size, &output_length ); |
| 2643 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2644 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2645 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2646 | /* Hash Compute, multi-part */ |
| 2647 | status = psa_hash_setup( &operation, alg ); |
| 2648 | if( status == PSA_SUCCESS ) |
| 2649 | { |
| 2650 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2651 | if( status == PSA_SUCCESS ) |
| 2652 | { |
| 2653 | status = psa_hash_finish( &operation, output, output_size, |
| 2654 | &output_length ); |
| 2655 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2656 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2657 | else |
| 2658 | TEST_EQUAL( status, expected_status ); |
| 2659 | } |
| 2660 | else |
| 2661 | { |
| 2662 | TEST_EQUAL( status, expected_status ); |
| 2663 | } |
| 2664 | } |
| 2665 | else |
| 2666 | { |
| 2667 | TEST_EQUAL( status, expected_status ); |
| 2668 | } |
| 2669 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2670 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2671 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2672 | mbedtls_free( output ); |
| 2673 | PSA_DONE( ); |
| 2674 | } |
| 2675 | /* END_CASE */ |
| 2676 | |
| 2677 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2678 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2679 | data_t *reference_hash, |
| 2680 | int expected_status_arg ) |
| 2681 | { |
| 2682 | psa_algorithm_t alg = alg_arg; |
| 2683 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2684 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2685 | psa_status_t status; |
| 2686 | |
| 2687 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2688 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2689 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2690 | status = psa_hash_compare( alg, input->x, input->len, |
| 2691 | reference_hash->x, reference_hash->len ); |
| 2692 | TEST_EQUAL( status, expected_status ); |
| 2693 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2694 | /* Hash Compare, multi-part */ |
| 2695 | status = psa_hash_setup( &operation, alg ); |
| 2696 | if( status == PSA_SUCCESS ) |
| 2697 | { |
| 2698 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2699 | if( status == PSA_SUCCESS ) |
| 2700 | { |
| 2701 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2702 | reference_hash->len ); |
| 2703 | TEST_EQUAL( status, expected_status ); |
| 2704 | } |
| 2705 | else |
| 2706 | { |
| 2707 | TEST_EQUAL( status, expected_status ); |
| 2708 | } |
| 2709 | } |
| 2710 | else |
| 2711 | { |
| 2712 | TEST_EQUAL( status, expected_status ); |
| 2713 | } |
| 2714 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2715 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2716 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2717 | PSA_DONE( ); |
| 2718 | } |
| 2719 | /* END_CASE */ |
| 2720 | |
| 2721 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2722 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2723 | data_t *expected_output ) |
| 2724 | { |
| 2725 | psa_algorithm_t alg = alg_arg; |
| 2726 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2727 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2728 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2729 | size_t i; |
| 2730 | |
| 2731 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2732 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2733 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2734 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2735 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2736 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2737 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2738 | ASSERT_COMPARE( output, output_length, |
| 2739 | expected_output->x, expected_output->len ); |
| 2740 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2741 | /* Compute with tight buffer, multi-part */ |
| 2742 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2743 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2744 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2745 | PSA_HASH_LENGTH( alg ), |
| 2746 | &output_length ) ); |
| 2747 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2748 | ASSERT_COMPARE( output, output_length, |
| 2749 | expected_output->x, expected_output->len ); |
| 2750 | |
| 2751 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2752 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2753 | output, sizeof( output ), |
| 2754 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2755 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2756 | ASSERT_COMPARE( output, output_length, |
| 2757 | expected_output->x, expected_output->len ); |
| 2758 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2759 | /* Compute with larger buffer, multi-part */ |
| 2760 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2761 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2762 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2763 | sizeof( output ), &output_length ) ); |
| 2764 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2765 | ASSERT_COMPARE( output, output_length, |
| 2766 | expected_output->x, expected_output->len ); |
| 2767 | |
| 2768 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2769 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2770 | output, output_length ) ); |
| 2771 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2772 | /* Compare with correct hash, multi-part */ |
| 2773 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2774 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2775 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2776 | output_length ) ); |
| 2777 | |
| 2778 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2779 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2780 | output, output_length + 1 ), |
| 2781 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2782 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2783 | /* Compare with trailing garbage, multi-part */ |
| 2784 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2785 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2786 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2787 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2788 | |
| 2789 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2790 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2791 | output, output_length - 1 ), |
| 2792 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2793 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2794 | /* Compare with truncated hash, multi-part */ |
| 2795 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2796 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2797 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2798 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2799 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2800 | /* Compare with corrupted value */ |
| 2801 | for( i = 0; i < output_length; i++ ) |
| 2802 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2803 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2804 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2805 | |
| 2806 | /* 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 ), |
| 2809 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2810 | |
| 2811 | /* 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 ), |
| 2815 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2816 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2817 | output[i] ^= 1; |
| 2818 | } |
| 2819 | |
| 2820 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2821 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2822 | PSA_DONE( ); |
| 2823 | } |
| 2824 | /* END_CASE */ |
| 2825 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2826 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2827 | void hash_bad_order( ) |
| 2828 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2829 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2830 | unsigned char input[] = ""; |
| 2831 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2832 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2833 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2834 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2835 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2836 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2837 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2838 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2839 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2840 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2841 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2842 | /* Call setup twice in a row. */ |
| 2843 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2844 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2845 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2846 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2847 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2848 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2849 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2850 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2851 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2852 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2853 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2854 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2855 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2856 | /* Check that update calls abort on error. */ |
| 2857 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2858 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2859 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2860 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2861 | PSA_ERROR_BAD_STATE ); |
| 2862 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2863 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2864 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2865 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2866 | /* Call update after finish. */ |
| 2867 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2868 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2869 | hash, sizeof( hash ), &hash_len ) ); |
| 2870 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2871 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2872 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2873 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2874 | /* Call verify without calling setup beforehand. */ |
| 2875 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2876 | valid_hash, sizeof( valid_hash ) ), |
| 2877 | PSA_ERROR_BAD_STATE ); |
| 2878 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2879 | |
| 2880 | /* Call verify after finish. */ |
| 2881 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2882 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2883 | hash, sizeof( hash ), &hash_len ) ); |
| 2884 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2885 | valid_hash, sizeof( valid_hash ) ), |
| 2886 | PSA_ERROR_BAD_STATE ); |
| 2887 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2888 | |
| 2889 | /* Call verify twice in a row. */ |
| 2890 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2891 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2892 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2893 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2894 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2895 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2896 | valid_hash, sizeof( valid_hash ) ), |
| 2897 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2898 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2899 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2900 | |
| 2901 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2902 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2903 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2904 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2905 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2906 | |
| 2907 | /* Call finish twice in a row. */ |
| 2908 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2909 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2910 | hash, sizeof( hash ), &hash_len ) ); |
| 2911 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2912 | hash, sizeof( hash ), &hash_len ), |
| 2913 | PSA_ERROR_BAD_STATE ); |
| 2914 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2915 | |
| 2916 | /* Call finish after calling verify. */ |
| 2917 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2918 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2919 | valid_hash, sizeof( valid_hash ) ) ); |
| 2920 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2921 | hash, sizeof( hash ), &hash_len ), |
| 2922 | PSA_ERROR_BAD_STATE ); |
| 2923 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2924 | |
| 2925 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2926 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2927 | } |
| 2928 | /* END_CASE */ |
| 2929 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2930 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2931 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2932 | { |
| 2933 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2934 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2935 | * appended to it */ |
| 2936 | unsigned char hash[] = { |
| 2937 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2938 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2939 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2940 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2941 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2942 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2943 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2944 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2945 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2946 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2947 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2948 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2949 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2950 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2951 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2952 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2953 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2954 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2955 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2956 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2957 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2958 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2959 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2960 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2961 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2962 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2963 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2964 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2965 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2966 | } |
| 2967 | /* END_CASE */ |
| 2968 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2969 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2970 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2971 | { |
| 2972 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2973 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2974 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2975 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2976 | size_t hash_len; |
| 2977 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2978 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2979 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2980 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2981 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2982 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2983 | hash, expected_size - 1, &hash_len ), |
| 2984 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2985 | |
| 2986 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2987 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2988 | } |
| 2989 | /* END_CASE */ |
| 2990 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2991 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2992 | void hash_clone_source_state( ) |
| 2993 | { |
| 2994 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2995 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2996 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2997 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2998 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2999 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3000 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3001 | size_t hash_len; |
| 3002 | |
| 3003 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3004 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 3005 | |
| 3006 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3007 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3008 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3009 | hash, sizeof( hash ), &hash_len ) ); |
| 3010 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3011 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3012 | |
| 3013 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 3014 | PSA_ERROR_BAD_STATE ); |
| 3015 | |
| 3016 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 3017 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 3018 | hash, sizeof( hash ), &hash_len ) ); |
| 3019 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 3020 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3021 | hash, sizeof( hash ), &hash_len ) ); |
| 3022 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 3023 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 3024 | hash, sizeof( hash ), &hash_len ) ); |
| 3025 | |
| 3026 | exit: |
| 3027 | psa_hash_abort( &op_source ); |
| 3028 | psa_hash_abort( &op_init ); |
| 3029 | psa_hash_abort( &op_setup ); |
| 3030 | psa_hash_abort( &op_finished ); |
| 3031 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3032 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3033 | } |
| 3034 | /* END_CASE */ |
| 3035 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3036 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3037 | void hash_clone_target_state( ) |
| 3038 | { |
| 3039 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3040 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3041 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3042 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3043 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3044 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3045 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3046 | size_t hash_len; |
| 3047 | |
| 3048 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3049 | |
| 3050 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3051 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3052 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3053 | hash, sizeof( hash ), &hash_len ) ); |
| 3054 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3055 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3056 | |
| 3057 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 3058 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 3059 | hash, sizeof( hash ), &hash_len ) ); |
| 3060 | |
| 3061 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 3062 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 3063 | PSA_ERROR_BAD_STATE ); |
| 3064 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 3065 | PSA_ERROR_BAD_STATE ); |
| 3066 | |
| 3067 | exit: |
| 3068 | psa_hash_abort( &op_target ); |
| 3069 | psa_hash_abort( &op_init ); |
| 3070 | psa_hash_abort( &op_setup ); |
| 3071 | psa_hash_abort( &op_finished ); |
| 3072 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3073 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3074 | } |
| 3075 | /* END_CASE */ |
| 3076 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3077 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3078 | void mac_operation_init( ) |
| 3079 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3080 | const uint8_t input[1] = { 0 }; |
| 3081 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3082 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3083 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3084 | * 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] | 3085 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3086 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 3087 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3088 | psa_mac_operation_t zero; |
| 3089 | |
| 3090 | memset( &zero, 0, sizeof( zero ) ); |
| 3091 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3092 | /* A freshly-initialized MAC operation should not be usable. */ |
| 3093 | TEST_EQUAL( psa_mac_update( &func, |
| 3094 | input, sizeof( input ) ), |
| 3095 | PSA_ERROR_BAD_STATE ); |
| 3096 | TEST_EQUAL( psa_mac_update( &init, |
| 3097 | input, sizeof( input ) ), |
| 3098 | PSA_ERROR_BAD_STATE ); |
| 3099 | TEST_EQUAL( psa_mac_update( &zero, |
| 3100 | input, sizeof( input ) ), |
| 3101 | PSA_ERROR_BAD_STATE ); |
| 3102 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3103 | /* A default MAC operation should be abortable without error. */ |
| 3104 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 3105 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 3106 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3107 | } |
| 3108 | /* END_CASE */ |
| 3109 | |
| 3110 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3111 | void mac_setup( int key_type_arg, |
| 3112 | data_t *key, |
| 3113 | int alg_arg, |
| 3114 | int expected_status_arg ) |
| 3115 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3116 | psa_key_type_t key_type = key_type_arg; |
| 3117 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3118 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3119 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3120 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3121 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3122 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3123 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3124 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3125 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3126 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3127 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 3128 | &operation, &status ) ) |
| 3129 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3130 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3131 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3132 | /* The operation object should be reusable. */ |
| 3133 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3134 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3135 | smoke_test_key_data, |
| 3136 | sizeof( smoke_test_key_data ), |
| 3137 | KNOWN_SUPPORTED_MAC_ALG, |
| 3138 | &operation, &status ) ) |
| 3139 | goto exit; |
| 3140 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3141 | #endif |
| 3142 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3143 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3144 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3145 | } |
| 3146 | /* END_CASE */ |
| 3147 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3148 | /* 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] | 3149 | void mac_bad_order( ) |
| 3150 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3151 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3152 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3153 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3154 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3155 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3156 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3157 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3158 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3159 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3160 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3161 | size_t sign_mac_length = 0; |
| 3162 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3163 | const uint8_t verify_mac[] = { |
| 3164 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3165 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 3166 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 3167 | |
| 3168 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3169 | 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] | 3170 | psa_set_key_algorithm( &attributes, alg ); |
| 3171 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3172 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3173 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3174 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3175 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3176 | /* Call update without calling setup beforehand. */ |
| 3177 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3178 | PSA_ERROR_BAD_STATE ); |
| 3179 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3180 | |
| 3181 | /* Call sign finish without calling setup beforehand. */ |
| 3182 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 3183 | &sign_mac_length), |
| 3184 | PSA_ERROR_BAD_STATE ); |
| 3185 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3186 | |
| 3187 | /* Call verify finish without calling setup beforehand. */ |
| 3188 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3189 | verify_mac, sizeof( verify_mac ) ), |
| 3190 | PSA_ERROR_BAD_STATE ); |
| 3191 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3192 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3193 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3194 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3195 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3196 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3197 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3198 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3199 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3200 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3201 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3202 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3203 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3204 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3205 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3206 | sign_mac, sizeof( sign_mac ), |
| 3207 | &sign_mac_length ) ); |
| 3208 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3209 | PSA_ERROR_BAD_STATE ); |
| 3210 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3211 | |
| 3212 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3213 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3214 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3215 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3216 | verify_mac, sizeof( verify_mac ) ) ); |
| 3217 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3218 | PSA_ERROR_BAD_STATE ); |
| 3219 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3220 | |
| 3221 | /* Call sign finish 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 ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3223 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3224 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3225 | sign_mac, sizeof( sign_mac ), |
| 3226 | &sign_mac_length ) ); |
| 3227 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3228 | sign_mac, sizeof( sign_mac ), |
| 3229 | &sign_mac_length ), |
| 3230 | PSA_ERROR_BAD_STATE ); |
| 3231 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3232 | |
| 3233 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3234 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3235 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3236 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3237 | verify_mac, sizeof( verify_mac ) ) ); |
| 3238 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3239 | verify_mac, sizeof( verify_mac ) ), |
| 3240 | PSA_ERROR_BAD_STATE ); |
| 3241 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3242 | |
| 3243 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3244 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3245 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3246 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3247 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3248 | verify_mac, sizeof( verify_mac ) ), |
| 3249 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3250 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3251 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3252 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3253 | |
| 3254 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3255 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3256 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3257 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3258 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3259 | sign_mac, sizeof( sign_mac ), |
| 3260 | &sign_mac_length ), |
| 3261 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3262 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3263 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3264 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3265 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3266 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3267 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3268 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3269 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3270 | } |
| 3271 | /* END_CASE */ |
| 3272 | |
| 3273 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3274 | void mac_sign_verify_multi( int key_type_arg, |
| 3275 | data_t *key_data, |
| 3276 | int alg_arg, |
| 3277 | data_t *input, |
| 3278 | int is_verify, |
| 3279 | data_t *expected_mac ) |
| 3280 | { |
| 3281 | size_t data_part_len = 0; |
| 3282 | |
| 3283 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3284 | { |
| 3285 | /* Split data into length(data_part_len) parts. */ |
| 3286 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3287 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3288 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3289 | alg_arg, |
| 3290 | input, data_part_len, |
| 3291 | expected_mac, |
| 3292 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3293 | break; |
| 3294 | |
| 3295 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3296 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3297 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3298 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3299 | alg_arg, |
| 3300 | input, data_part_len, |
| 3301 | expected_mac, |
| 3302 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3303 | break; |
| 3304 | } |
| 3305 | |
| 3306 | /* Goto is required to silence warnings about unused labels, as we |
| 3307 | * don't actually do any test assertions in this function. */ |
| 3308 | goto exit; |
| 3309 | } |
| 3310 | /* END_CASE */ |
| 3311 | |
| 3312 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3313 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3314 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3315 | int alg_arg, |
| 3316 | data_t *input, |
| 3317 | data_t *expected_mac ) |
| 3318 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3319 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3320 | psa_key_type_t key_type = key_type_arg; |
| 3321 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3322 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3323 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3324 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3325 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3326 | 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] | 3327 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3328 | const size_t output_sizes_to_test[] = { |
| 3329 | 0, |
| 3330 | 1, |
| 3331 | expected_mac->len - 1, |
| 3332 | expected_mac->len, |
| 3333 | expected_mac->len + 1, |
| 3334 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3335 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3336 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3337 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3338 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3339 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3340 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3341 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3342 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3343 | psa_set_key_algorithm( &attributes, alg ); |
| 3344 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3345 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3346 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3347 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3348 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3349 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3350 | { |
| 3351 | const size_t output_size = output_sizes_to_test[i]; |
| 3352 | psa_status_t expected_status = |
| 3353 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3354 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3355 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3356 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3357 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3358 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3359 | /* Calculate the MAC, one-shot case. */ |
| 3360 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3361 | input->x, input->len, |
| 3362 | actual_mac, output_size, &mac_length ), |
| 3363 | expected_status ); |
| 3364 | if( expected_status == PSA_SUCCESS ) |
| 3365 | { |
| 3366 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3367 | actual_mac, mac_length ); |
| 3368 | } |
| 3369 | |
| 3370 | if( output_size > 0 ) |
| 3371 | memset( actual_mac, 0, output_size ); |
| 3372 | |
| 3373 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3374 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3375 | PSA_ASSERT( psa_mac_update( &operation, |
| 3376 | input->x, input->len ) ); |
| 3377 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3378 | actual_mac, output_size, |
| 3379 | &mac_length ), |
| 3380 | expected_status ); |
| 3381 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3382 | |
| 3383 | if( expected_status == PSA_SUCCESS ) |
| 3384 | { |
| 3385 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3386 | actual_mac, mac_length ); |
| 3387 | } |
| 3388 | mbedtls_free( actual_mac ); |
| 3389 | actual_mac = NULL; |
| 3390 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3391 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3392 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3393 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3394 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3395 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3396 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3397 | } |
| 3398 | /* END_CASE */ |
| 3399 | |
| 3400 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3401 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3402 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3403 | int alg_arg, |
| 3404 | data_t *input, |
| 3405 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3406 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3407 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3408 | psa_key_type_t key_type = key_type_arg; |
| 3409 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3410 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3411 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3412 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3413 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3414 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3415 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3416 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3417 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3418 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3419 | psa_set_key_algorithm( &attributes, alg ); |
| 3420 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3421 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3422 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3423 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3424 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3425 | /* Verify correct MAC, one-shot case. */ |
| 3426 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3427 | expected_mac->x, expected_mac->len ) ); |
| 3428 | |
| 3429 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3430 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3431 | PSA_ASSERT( psa_mac_update( &operation, |
| 3432 | input->x, input->len ) ); |
| 3433 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3434 | expected_mac->x, |
| 3435 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3436 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3437 | /* Test a MAC that's too short, one-shot case. */ |
| 3438 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3439 | input->x, input->len, |
| 3440 | expected_mac->x, |
| 3441 | expected_mac->len - 1 ), |
| 3442 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3443 | |
| 3444 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3445 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3446 | PSA_ASSERT( psa_mac_update( &operation, |
| 3447 | input->x, input->len ) ); |
| 3448 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3449 | expected_mac->x, |
| 3450 | expected_mac->len - 1 ), |
| 3451 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3452 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3453 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3454 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3455 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3456 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3457 | input->x, input->len, |
| 3458 | perturbed_mac, expected_mac->len + 1 ), |
| 3459 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3460 | |
| 3461 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3462 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3463 | PSA_ASSERT( psa_mac_update( &operation, |
| 3464 | input->x, input->len ) ); |
| 3465 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3466 | perturbed_mac, |
| 3467 | expected_mac->len + 1 ), |
| 3468 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3469 | |
| 3470 | /* Test changing one byte. */ |
| 3471 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3472 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3473 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3474 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3475 | |
| 3476 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3477 | input->x, input->len, |
| 3478 | perturbed_mac, expected_mac->len ), |
| 3479 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3480 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3481 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3482 | PSA_ASSERT( psa_mac_update( &operation, |
| 3483 | input->x, input->len ) ); |
| 3484 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3485 | perturbed_mac, |
| 3486 | expected_mac->len ), |
| 3487 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3488 | perturbed_mac[i] ^= 1; |
| 3489 | } |
| 3490 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3491 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3492 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3493 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3494 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3495 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3496 | } |
| 3497 | /* END_CASE */ |
| 3498 | |
| 3499 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3500 | void cipher_operation_init( ) |
| 3501 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3502 | const uint8_t input[1] = { 0 }; |
| 3503 | unsigned char output[1] = { 0 }; |
| 3504 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3505 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3506 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3507 | * 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] | 3508 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3509 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3510 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3511 | psa_cipher_operation_t zero; |
| 3512 | |
| 3513 | memset( &zero, 0, sizeof( zero ) ); |
| 3514 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3515 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3516 | TEST_EQUAL( psa_cipher_update( &func, |
| 3517 | input, sizeof( input ), |
| 3518 | output, sizeof( output ), |
| 3519 | &output_length ), |
| 3520 | PSA_ERROR_BAD_STATE ); |
| 3521 | TEST_EQUAL( psa_cipher_update( &init, |
| 3522 | input, sizeof( input ), |
| 3523 | output, sizeof( output ), |
| 3524 | &output_length ), |
| 3525 | PSA_ERROR_BAD_STATE ); |
| 3526 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3527 | input, sizeof( input ), |
| 3528 | output, sizeof( output ), |
| 3529 | &output_length ), |
| 3530 | PSA_ERROR_BAD_STATE ); |
| 3531 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3532 | /* A default cipher operation should be abortable without error. */ |
| 3533 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3534 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3535 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3536 | } |
| 3537 | /* END_CASE */ |
| 3538 | |
| 3539 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3540 | void cipher_setup( int key_type_arg, |
| 3541 | data_t *key, |
| 3542 | int alg_arg, |
| 3543 | int expected_status_arg ) |
| 3544 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3545 | psa_key_type_t key_type = key_type_arg; |
| 3546 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3547 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3548 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3549 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3550 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3551 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3552 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3553 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3554 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3555 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3556 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3557 | &operation, &status ) ) |
| 3558 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3559 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3560 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3561 | /* The operation object should be reusable. */ |
| 3562 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3563 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3564 | smoke_test_key_data, |
| 3565 | sizeof( smoke_test_key_data ), |
| 3566 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3567 | &operation, &status ) ) |
| 3568 | goto exit; |
| 3569 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3570 | #endif |
| 3571 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3572 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3573 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3574 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3575 | } |
| 3576 | /* END_CASE */ |
| 3577 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3578 | /* 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] | 3579 | void cipher_bad_order( ) |
| 3580 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3581 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3582 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3583 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3584 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3585 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3586 | 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] | 3587 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3588 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3589 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3590 | const uint8_t text[] = { |
| 3591 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3592 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3593 | 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] | 3594 | size_t length = 0; |
| 3595 | |
| 3596 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3597 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3598 | psa_set_key_algorithm( &attributes, alg ); |
| 3599 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3600 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3601 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3602 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3603 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3604 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3605 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3606 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3607 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3608 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3609 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3610 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3611 | |
| 3612 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3613 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3614 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3615 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3616 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3617 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3618 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3619 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3620 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3621 | /* Generate an IV without calling setup beforehand. */ |
| 3622 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3623 | buffer, sizeof( buffer ), |
| 3624 | &length ), |
| 3625 | PSA_ERROR_BAD_STATE ); |
| 3626 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3627 | |
| 3628 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3629 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3630 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3631 | buffer, sizeof( buffer ), |
| 3632 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3633 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3634 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3635 | buffer, sizeof( buffer ), |
| 3636 | &length ), |
| 3637 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3638 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3639 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3640 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3641 | |
| 3642 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3643 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3644 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3645 | iv, sizeof( iv ) ) ); |
| 3646 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3647 | buffer, sizeof( buffer ), |
| 3648 | &length ), |
| 3649 | PSA_ERROR_BAD_STATE ); |
| 3650 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3651 | |
| 3652 | /* Set an IV without calling setup beforehand. */ |
| 3653 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3654 | iv, sizeof( iv ) ), |
| 3655 | PSA_ERROR_BAD_STATE ); |
| 3656 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3657 | |
| 3658 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3659 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3660 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3661 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3662 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3663 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3664 | iv, sizeof( iv ) ), |
| 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 | /* Set an IV after it's already generated. */ |
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_generate_iv( &operation, |
| 3673 | buffer, sizeof( buffer ), |
| 3674 | &length ) ); |
| 3675 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3676 | iv, sizeof( iv ) ), |
| 3677 | PSA_ERROR_BAD_STATE ); |
| 3678 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3679 | |
| 3680 | /* Call update without calling setup beforehand. */ |
| 3681 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3682 | text, sizeof( text ), |
| 3683 | buffer, sizeof( buffer ), |
| 3684 | &length ), |
| 3685 | PSA_ERROR_BAD_STATE ); |
| 3686 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3687 | |
| 3688 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3689 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
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_update( &operation, |
| 3692 | text, sizeof( text ), |
| 3693 | buffer, sizeof( buffer ), |
| 3694 | &length ), |
| 3695 | PSA_ERROR_BAD_STATE ); |
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 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3698 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3699 | |
| 3700 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3701 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3702 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3703 | iv, sizeof( iv ) ) ); |
| 3704 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3705 | buffer, sizeof( buffer ), &length ) ); |
| 3706 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3707 | text, sizeof( text ), |
| 3708 | buffer, sizeof( buffer ), |
| 3709 | &length ), |
| 3710 | PSA_ERROR_BAD_STATE ); |
| 3711 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3712 | |
| 3713 | /* Call finish without calling setup beforehand. */ |
| 3714 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3715 | buffer, sizeof( buffer ), &length ), |
| 3716 | PSA_ERROR_BAD_STATE ); |
| 3717 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3718 | |
| 3719 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3720 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3721 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3722 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3723 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3724 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3725 | buffer, sizeof( buffer ), &length ), |
| 3726 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3727 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3728 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3729 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3730 | |
| 3731 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3732 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3733 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3734 | iv, sizeof( iv ) ) ); |
| 3735 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3736 | buffer, sizeof( buffer ), &length ) ); |
| 3737 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3738 | buffer, sizeof( buffer ), &length ), |
| 3739 | PSA_ERROR_BAD_STATE ); |
| 3740 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3741 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3742 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3743 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3744 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3745 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3746 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3747 | } |
| 3748 | /* END_CASE */ |
| 3749 | |
| 3750 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3751 | void cipher_encrypt_fail( int alg_arg, |
| 3752 | int key_type_arg, |
| 3753 | data_t *key_data, |
| 3754 | data_t *input, |
| 3755 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3756 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3757 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3758 | psa_status_t status; |
| 3759 | psa_key_type_t key_type = key_type_arg; |
| 3760 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3761 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3762 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3763 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3764 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3765 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3766 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3767 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3768 | size_t function_output_length; |
| 3769 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3770 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3771 | |
| 3772 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3773 | { |
| 3774 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3775 | |
| 3776 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3777 | psa_set_key_algorithm( &attributes, alg ); |
| 3778 | psa_set_key_type( &attributes, key_type ); |
| 3779 | |
| 3780 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3781 | input->len ); |
| 3782 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3783 | |
| 3784 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3785 | &key ) ); |
| 3786 | } |
| 3787 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3788 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3789 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3790 | output_buffer_size, &output_length ); |
| 3791 | |
| 3792 | TEST_EQUAL( status, expected_status ); |
| 3793 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3794 | /* Encrypt, multi-part */ |
| 3795 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3796 | if( status == PSA_SUCCESS ) |
| 3797 | { |
| 3798 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3799 | { |
| 3800 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3801 | iv, iv_size, |
| 3802 | &iv_length ) ); |
| 3803 | } |
| 3804 | |
| 3805 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3806 | output, output_buffer_size, |
| 3807 | &function_output_length ); |
| 3808 | if( status == PSA_SUCCESS ) |
| 3809 | { |
| 3810 | output_length += function_output_length; |
| 3811 | |
| 3812 | status = psa_cipher_finish( &operation, output + output_length, |
| 3813 | output_buffer_size - output_length, |
| 3814 | &function_output_length ); |
| 3815 | |
| 3816 | TEST_EQUAL( status, expected_status ); |
| 3817 | } |
| 3818 | else |
| 3819 | { |
| 3820 | TEST_EQUAL( status, expected_status ); |
| 3821 | } |
| 3822 | } |
| 3823 | else |
| 3824 | { |
| 3825 | TEST_EQUAL( status, expected_status ); |
| 3826 | } |
| 3827 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3828 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3829 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3830 | mbedtls_free( output ); |
| 3831 | psa_destroy_key( key ); |
| 3832 | PSA_DONE( ); |
| 3833 | } |
| 3834 | /* END_CASE */ |
| 3835 | |
| 3836 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3837 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3838 | data_t *input, int iv_length, |
| 3839 | int expected_result ) |
| 3840 | { |
| 3841 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3842 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3843 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3844 | size_t output_buffer_size = 0; |
| 3845 | unsigned char *output = NULL; |
| 3846 | |
| 3847 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3848 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3849 | |
| 3850 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3851 | |
| 3852 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3853 | psa_set_key_algorithm( &attributes, alg ); |
| 3854 | psa_set_key_type( &attributes, key_type ); |
| 3855 | |
| 3856 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3857 | &key ) ); |
| 3858 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3859 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3860 | iv_length ) ); |
| 3861 | |
| 3862 | exit: |
| 3863 | psa_cipher_abort( &operation ); |
| 3864 | mbedtls_free( output ); |
| 3865 | psa_destroy_key( key ); |
| 3866 | PSA_DONE( ); |
| 3867 | } |
| 3868 | /* END_CASE */ |
| 3869 | |
| 3870 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3871 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3872 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3873 | { |
| 3874 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3875 | psa_key_type_t key_type = key_type_arg; |
| 3876 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3877 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3878 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3879 | unsigned char *output = NULL; |
| 3880 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3881 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3882 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3883 | |
| 3884 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3885 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3886 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3887 | TEST_LE_U( ciphertext->len, |
| 3888 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3889 | 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] | 3890 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3891 | TEST_LE_U( plaintext->len, |
| 3892 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3893 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3894 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3895 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3896 | |
| 3897 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3898 | psa_set_key_usage_flags( &attributes, |
| 3899 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3900 | psa_set_key_algorithm( &attributes, alg ); |
| 3901 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3902 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3903 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3904 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3905 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3906 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3907 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3908 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3909 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3910 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3911 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3912 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3913 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3914 | PSA_ERROR_BAD_STATE ); |
| 3915 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3916 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3917 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3918 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3919 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3920 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3921 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3922 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3923 | &length ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3924 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3925 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3926 | /* Multipart encryption */ |
| 3927 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3928 | output_length = 0; |
| 3929 | length = ~0; |
| 3930 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3931 | plaintext->x, plaintext->len, |
| 3932 | output, output_buffer_size, |
| 3933 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3934 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3935 | output_length += length; |
| 3936 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3937 | output + output_length, |
| 3938 | output_buffer_size - output_length, |
| 3939 | &length ) ); |
| 3940 | output_length += length; |
| 3941 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3942 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3943 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3944 | /* Multipart encryption */ |
| 3945 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3946 | output_length = 0; |
| 3947 | length = ~0; |
| 3948 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3949 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3950 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3951 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3952 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3953 | output_length += length; |
| 3954 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3955 | output + output_length, |
| 3956 | output_buffer_size - output_length, |
| 3957 | &length ) ); |
| 3958 | output_length += length; |
| 3959 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 3960 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3961 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3962 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3963 | output_length = ~0; |
| 3964 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 3965 | output, output_buffer_size, |
| 3966 | &output_length ) ); |
| 3967 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 3968 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3969 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3970 | /* One-shot decryption */ |
| 3971 | output_length = ~0; |
| 3972 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 3973 | output, output_buffer_size, |
| 3974 | &output_length ) ); |
| 3975 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3976 | output, output_length ); |
| 3977 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3978 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3979 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3980 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3981 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3982 | psa_destroy_key( key ); |
| 3983 | PSA_DONE( ); |
| 3984 | } |
| 3985 | /* END_CASE */ |
| 3986 | |
| 3987 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3988 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 3989 | { |
| 3990 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3991 | psa_algorithm_t alg = alg_arg; |
| 3992 | psa_key_type_t key_type = key_type_arg; |
| 3993 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3994 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3995 | psa_status_t status; |
| 3996 | |
| 3997 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3998 | |
| 3999 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4000 | psa_set_key_algorithm( &attributes, alg ); |
| 4001 | psa_set_key_type( &attributes, key_type ); |
| 4002 | |
| 4003 | /* Usage of either of these two size macros would cause divide by zero |
| 4004 | * with incorrect key types previously. Input length should be irrelevant |
| 4005 | * here. */ |
| 4006 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 4007 | 0 ); |
| 4008 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 4009 | |
| 4010 | |
| 4011 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4012 | &key ) ); |
| 4013 | |
| 4014 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4015 | * Encrypt or decrypt will end up in the same place. */ |
| 4016 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 4017 | |
| 4018 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 4019 | |
| 4020 | exit: |
| 4021 | psa_cipher_abort( &operation ); |
| 4022 | psa_destroy_key( key ); |
| 4023 | PSA_DONE( ); |
| 4024 | } |
| 4025 | /* END_CASE */ |
| 4026 | |
| 4027 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4028 | void cipher_encrypt_validation( int alg_arg, |
| 4029 | int key_type_arg, |
| 4030 | data_t *key_data, |
| 4031 | data_t *input ) |
| 4032 | { |
| 4033 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4034 | psa_key_type_t key_type = key_type_arg; |
| 4035 | psa_algorithm_t alg = alg_arg; |
| 4036 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 4037 | unsigned char *output1 = NULL; |
| 4038 | size_t output1_buffer_size = 0; |
| 4039 | size_t output1_length = 0; |
| 4040 | unsigned char *output2 = NULL; |
| 4041 | size_t output2_buffer_size = 0; |
| 4042 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4043 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4044 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4045 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4046 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4047 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4048 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4049 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4050 | psa_set_key_algorithm( &attributes, alg ); |
| 4051 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4052 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4053 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 4054 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4055 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4056 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 4057 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 4058 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4059 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4060 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4061 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4062 | /* The one-shot cipher encryption uses generated iv so validating |
| 4063 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4064 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 4065 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4066 | TEST_LE_U( output1_length, |
| 4067 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4068 | TEST_LE_U( output1_length, |
| 4069 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4070 | |
| 4071 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 4072 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4073 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4074 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4075 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4076 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4077 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4078 | TEST_LE_U( function_output_length, |
| 4079 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4080 | TEST_LE_U( function_output_length, |
| 4081 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4082 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4083 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4084 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4085 | output2 + output2_length, |
| 4086 | output2_buffer_size - output2_length, |
| 4087 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4088 | TEST_LE_U( function_output_length, |
| 4089 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4090 | TEST_LE_U( function_output_length, |
| 4091 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4092 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4093 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4094 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4095 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 4096 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4097 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4098 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4099 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4100 | mbedtls_free( output1 ); |
| 4101 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4102 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4103 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4104 | } |
| 4105 | /* END_CASE */ |
| 4106 | |
| 4107 | /* BEGIN_CASE */ |
| 4108 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4109 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4110 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4111 | int first_part_size_arg, |
| 4112 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4113 | data_t *expected_output, |
| 4114 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4115 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4116 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4117 | psa_key_type_t key_type = key_type_arg; |
| 4118 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4119 | psa_status_t status; |
| 4120 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4121 | size_t first_part_size = first_part_size_arg; |
| 4122 | size_t output1_length = output1_length_arg; |
| 4123 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4124 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4125 | size_t output_buffer_size = 0; |
| 4126 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4127 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4128 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4129 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4130 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4131 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4132 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4133 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4134 | psa_set_key_algorithm( &attributes, alg ); |
| 4135 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4136 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4137 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4138 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4139 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4140 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4141 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4142 | if( iv->len > 0 ) |
| 4143 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4144 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4145 | } |
| 4146 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4147 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4148 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4149 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4150 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4151 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4152 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 4153 | output, output_buffer_size, |
| 4154 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4155 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4156 | TEST_LE_U( function_output_length, |
| 4157 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4158 | TEST_LE_U( function_output_length, |
| 4159 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4160 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4161 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4162 | if( first_part_size < input->len ) |
| 4163 | { |
| 4164 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4165 | input->x + first_part_size, |
| 4166 | input->len - first_part_size, |
| 4167 | ( output_buffer_size == 0 ? NULL : |
| 4168 | output + total_output_length ), |
| 4169 | output_buffer_size - total_output_length, |
| 4170 | &function_output_length ) ); |
| 4171 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4172 | TEST_LE_U( function_output_length, |
| 4173 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4174 | alg, |
| 4175 | input->len - first_part_size ) ); |
| 4176 | TEST_LE_U( function_output_length, |
| 4177 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4178 | total_output_length += function_output_length; |
| 4179 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4180 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4181 | status = psa_cipher_finish( &operation, |
| 4182 | ( output_buffer_size == 0 ? NULL : |
| 4183 | output + total_output_length ), |
| 4184 | output_buffer_size - total_output_length, |
| 4185 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4186 | TEST_LE_U( function_output_length, |
| 4187 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4188 | TEST_LE_U( function_output_length, |
| 4189 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4190 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4191 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4192 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4193 | if( expected_status == PSA_SUCCESS ) |
| 4194 | { |
| 4195 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4196 | |
| 4197 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4198 | output, total_output_length ); |
| 4199 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4200 | |
| 4201 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4202 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4203 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4204 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4205 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4206 | } |
| 4207 | /* END_CASE */ |
| 4208 | |
| 4209 | /* BEGIN_CASE */ |
| 4210 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4211 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4212 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4213 | int first_part_size_arg, |
| 4214 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4215 | data_t *expected_output, |
| 4216 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4217 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4218 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4219 | psa_key_type_t key_type = key_type_arg; |
| 4220 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4221 | psa_status_t status; |
| 4222 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4223 | size_t first_part_size = first_part_size_arg; |
| 4224 | size_t output1_length = output1_length_arg; |
| 4225 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4226 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4227 | size_t output_buffer_size = 0; |
| 4228 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4229 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4230 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4231 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4232 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4233 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4234 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4235 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4236 | psa_set_key_algorithm( &attributes, alg ); |
| 4237 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4238 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4239 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4240 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4241 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4242 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4243 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4244 | if( iv->len > 0 ) |
| 4245 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4246 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4247 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4248 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4249 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4250 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4251 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4252 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4253 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4254 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4255 | input->x, first_part_size, |
| 4256 | output, output_buffer_size, |
| 4257 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4258 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4259 | TEST_LE_U( function_output_length, |
| 4260 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4261 | TEST_LE_U( function_output_length, |
| 4262 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4263 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4264 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4265 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4266 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4267 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4268 | input->x + first_part_size, |
| 4269 | input->len - first_part_size, |
| 4270 | ( output_buffer_size == 0 ? NULL : |
| 4271 | output + total_output_length ), |
| 4272 | output_buffer_size - total_output_length, |
| 4273 | &function_output_length ) ); |
| 4274 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4275 | TEST_LE_U( function_output_length, |
| 4276 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4277 | alg, |
| 4278 | input->len - first_part_size ) ); |
| 4279 | TEST_LE_U( function_output_length, |
| 4280 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4281 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4282 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4283 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4284 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4285 | ( output_buffer_size == 0 ? NULL : |
| 4286 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4287 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4288 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4289 | TEST_LE_U( function_output_length, |
| 4290 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4291 | TEST_LE_U( function_output_length, |
| 4292 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4293 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4294 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4295 | |
| 4296 | if( expected_status == PSA_SUCCESS ) |
| 4297 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4298 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4299 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4300 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4301 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4302 | } |
| 4303 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4304 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4305 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4306 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4307 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4308 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4309 | } |
| 4310 | /* END_CASE */ |
| 4311 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4312 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4313 | void cipher_decrypt_fail( int alg_arg, |
| 4314 | int key_type_arg, |
| 4315 | data_t *key_data, |
| 4316 | data_t *iv, |
| 4317 | data_t *input_arg, |
| 4318 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4319 | { |
| 4320 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4321 | psa_status_t status; |
| 4322 | psa_key_type_t key_type = key_type_arg; |
| 4323 | psa_algorithm_t alg = alg_arg; |
| 4324 | psa_status_t expected_status = expected_status_arg; |
| 4325 | unsigned char *input = NULL; |
| 4326 | size_t input_buffer_size = 0; |
| 4327 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4328 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4329 | size_t output_buffer_size = 0; |
| 4330 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4331 | size_t function_output_length; |
| 4332 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4333 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4334 | |
| 4335 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4336 | { |
| 4337 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4338 | |
| 4339 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4340 | psa_set_key_algorithm( &attributes, alg ); |
| 4341 | psa_set_key_type( &attributes, key_type ); |
| 4342 | |
| 4343 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4344 | &key ) ); |
| 4345 | } |
| 4346 | |
| 4347 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4348 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4349 | if ( input_buffer_size > 0 ) |
| 4350 | { |
| 4351 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4352 | memcpy( input, iv->x, iv->len ); |
| 4353 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4354 | } |
| 4355 | |
| 4356 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4357 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4358 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4359 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4360 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4361 | output_buffer_size, &output_length ); |
| 4362 | TEST_EQUAL( status, expected_status ); |
| 4363 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4364 | /* Decrypt, multi-part */ |
| 4365 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4366 | if( status == PSA_SUCCESS ) |
| 4367 | { |
| 4368 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4369 | input_arg->len ) + |
| 4370 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4371 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4372 | |
| 4373 | if( iv->len > 0 ) |
| 4374 | { |
| 4375 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4376 | |
| 4377 | if( status != PSA_SUCCESS ) |
| 4378 | TEST_EQUAL( status, expected_status ); |
| 4379 | } |
| 4380 | |
| 4381 | if( status == PSA_SUCCESS ) |
| 4382 | { |
| 4383 | status = psa_cipher_update( &operation, |
| 4384 | input_arg->x, input_arg->len, |
| 4385 | output_multi, output_buffer_size, |
| 4386 | &function_output_length ); |
| 4387 | if( status == PSA_SUCCESS ) |
| 4388 | { |
| 4389 | output_length = function_output_length; |
| 4390 | |
| 4391 | status = psa_cipher_finish( &operation, |
| 4392 | output_multi + output_length, |
| 4393 | output_buffer_size - output_length, |
| 4394 | &function_output_length ); |
| 4395 | |
| 4396 | TEST_EQUAL( status, expected_status ); |
| 4397 | } |
| 4398 | else |
| 4399 | { |
| 4400 | TEST_EQUAL( status, expected_status ); |
| 4401 | } |
| 4402 | } |
| 4403 | else |
| 4404 | { |
| 4405 | TEST_EQUAL( status, expected_status ); |
| 4406 | } |
| 4407 | } |
| 4408 | else |
| 4409 | { |
| 4410 | TEST_EQUAL( status, expected_status ); |
| 4411 | } |
| 4412 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4413 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4414 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4415 | mbedtls_free( input ); |
| 4416 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4417 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4418 | psa_destroy_key( key ); |
| 4419 | PSA_DONE( ); |
| 4420 | } |
| 4421 | /* END_CASE */ |
| 4422 | |
| 4423 | /* BEGIN_CASE */ |
| 4424 | void cipher_decrypt( int alg_arg, |
| 4425 | int key_type_arg, |
| 4426 | data_t *key_data, |
| 4427 | data_t *iv, |
| 4428 | data_t *input_arg, |
| 4429 | data_t *expected_output ) |
| 4430 | { |
| 4431 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4432 | psa_key_type_t key_type = key_type_arg; |
| 4433 | psa_algorithm_t alg = alg_arg; |
| 4434 | unsigned char *input = NULL; |
| 4435 | size_t input_buffer_size = 0; |
| 4436 | unsigned char *output = NULL; |
| 4437 | size_t output_buffer_size = 0; |
| 4438 | size_t output_length = 0; |
| 4439 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4440 | |
| 4441 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4442 | |
| 4443 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4444 | psa_set_key_algorithm( &attributes, alg ); |
| 4445 | psa_set_key_type( &attributes, key_type ); |
| 4446 | |
| 4447 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4448 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4449 | if ( input_buffer_size > 0 ) |
| 4450 | { |
| 4451 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4452 | memcpy( input, iv->x, iv->len ); |
| 4453 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4454 | } |
| 4455 | |
| 4456 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4457 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4458 | |
| 4459 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4460 | &key ) ); |
| 4461 | |
| 4462 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4463 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4464 | TEST_LE_U( output_length, |
| 4465 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4466 | TEST_LE_U( output_length, |
| 4467 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4468 | |
| 4469 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4470 | output, output_length ); |
| 4471 | exit: |
| 4472 | mbedtls_free( input ); |
| 4473 | mbedtls_free( output ); |
| 4474 | psa_destroy_key( key ); |
| 4475 | PSA_DONE( ); |
| 4476 | } |
| 4477 | /* END_CASE */ |
| 4478 | |
| 4479 | /* BEGIN_CASE */ |
| 4480 | void cipher_verify_output( int alg_arg, |
| 4481 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4482 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4483 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4484 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4485 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4486 | psa_key_type_t key_type = key_type_arg; |
| 4487 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4488 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4489 | size_t output1_size = 0; |
| 4490 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4491 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4492 | size_t output2_size = 0; |
| 4493 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4494 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4495 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4496 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4497 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4498 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4499 | psa_set_key_algorithm( &attributes, alg ); |
| 4500 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4501 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4502 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4503 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4504 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4505 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4506 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4507 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4508 | output1, output1_size, |
| 4509 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4510 | TEST_LE_U( output1_length, |
| 4511 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4512 | TEST_LE_U( output1_length, |
| 4513 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4514 | |
| 4515 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4516 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4517 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4518 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4519 | output2, output2_size, |
| 4520 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4521 | TEST_LE_U( output2_length, |
| 4522 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4523 | TEST_LE_U( output2_length, |
| 4524 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4525 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4526 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4527 | |
| 4528 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4529 | mbedtls_free( output1 ); |
| 4530 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4531 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4532 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4533 | } |
| 4534 | /* END_CASE */ |
| 4535 | |
| 4536 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4537 | void cipher_verify_output_multipart( int alg_arg, |
| 4538 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4539 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4540 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4541 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4542 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4543 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4544 | psa_key_type_t key_type = key_type_arg; |
| 4545 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4546 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4547 | unsigned char iv[16] = {0}; |
| 4548 | size_t iv_size = 16; |
| 4549 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4550 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4551 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4552 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4553 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4554 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4555 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4556 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4557 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4558 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4559 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4560 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4561 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4562 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4563 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4564 | psa_set_key_algorithm( &attributes, alg ); |
| 4565 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4566 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4567 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4568 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4569 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4570 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4571 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4572 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4573 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4574 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4575 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4576 | iv, iv_size, |
| 4577 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4578 | } |
| 4579 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4580 | 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] | 4581 | TEST_LE_U( output1_buffer_size, |
| 4582 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4583 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4584 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4585 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4586 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4587 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4588 | output1, output1_buffer_size, |
| 4589 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4590 | TEST_LE_U( function_output_length, |
| 4591 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4592 | TEST_LE_U( function_output_length, |
| 4593 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4594 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4595 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4596 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4597 | input->x + first_part_size, |
| 4598 | input->len - first_part_size, |
| 4599 | output1, output1_buffer_size, |
| 4600 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4601 | TEST_LE_U( function_output_length, |
| 4602 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4603 | alg, |
| 4604 | input->len - first_part_size ) ); |
| 4605 | TEST_LE_U( function_output_length, |
| 4606 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4607 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4608 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4609 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4610 | output1 + output1_length, |
| 4611 | output1_buffer_size - output1_length, |
| 4612 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4613 | TEST_LE_U( function_output_length, |
| 4614 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4615 | TEST_LE_U( function_output_length, |
| 4616 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4617 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4618 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4619 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4620 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4621 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4622 | TEST_LE_U( output2_buffer_size, |
| 4623 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4624 | TEST_LE_U( output2_buffer_size, |
| 4625 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4626 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4627 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4628 | if( iv_length > 0 ) |
| 4629 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4630 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4631 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4632 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4633 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4634 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4635 | output2, output2_buffer_size, |
| 4636 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4637 | TEST_LE_U( function_output_length, |
| 4638 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4639 | TEST_LE_U( function_output_length, |
| 4640 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4641 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4642 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4643 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4644 | output1 + first_part_size, |
| 4645 | output1_length - first_part_size, |
| 4646 | output2, output2_buffer_size, |
| 4647 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4648 | TEST_LE_U( function_output_length, |
| 4649 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4650 | alg, |
| 4651 | output1_length - first_part_size ) ); |
| 4652 | TEST_LE_U( function_output_length, |
| 4653 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4654 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4655 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4656 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4657 | output2 + output2_length, |
| 4658 | output2_buffer_size - output2_length, |
| 4659 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4660 | TEST_LE_U( function_output_length, |
| 4661 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4662 | TEST_LE_U( function_output_length, |
| 4663 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4664 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4665 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4666 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4667 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4668 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4669 | |
| 4670 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4671 | psa_cipher_abort( &operation1 ); |
| 4672 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4673 | mbedtls_free( output1 ); |
| 4674 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4675 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4676 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4677 | } |
| 4678 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4679 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4680 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4681 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4682 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4683 | data_t *nonce, |
| 4684 | data_t *additional_data, |
| 4685 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4686 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4687 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4688 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4689 | psa_key_type_t key_type = key_type_arg; |
| 4690 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4691 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4692 | unsigned char *output_data = NULL; |
| 4693 | size_t output_size = 0; |
| 4694 | size_t output_length = 0; |
| 4695 | unsigned char *output_data2 = NULL; |
| 4696 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4697 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4698 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4699 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4700 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4701 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4702 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4703 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4704 | psa_set_key_algorithm( &attributes, alg ); |
| 4705 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4706 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4707 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4708 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4709 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4710 | key_bits = psa_get_key_bits( &attributes ); |
| 4711 | |
| 4712 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4713 | alg ); |
| 4714 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4715 | * should be exact. */ |
| 4716 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4717 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4718 | { |
| 4719 | TEST_EQUAL( output_size, |
| 4720 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4721 | TEST_LE_U( output_size, |
| 4722 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4723 | } |
| 4724 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4725 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4726 | status = psa_aead_encrypt( key, alg, |
| 4727 | nonce->x, nonce->len, |
| 4728 | additional_data->x, |
| 4729 | additional_data->len, |
| 4730 | input_data->x, input_data->len, |
| 4731 | output_data, output_size, |
| 4732 | &output_length ); |
| 4733 | |
| 4734 | /* If the operation is not supported, just skip and not fail in case the |
| 4735 | * encryption involves a common limitation of cryptography hardwares and |
| 4736 | * an alternative implementation. */ |
| 4737 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4738 | { |
| 4739 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4740 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4741 | } |
| 4742 | |
| 4743 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4744 | |
| 4745 | if( PSA_SUCCESS == expected_result ) |
| 4746 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4747 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4748 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4749 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4750 | * should be exact. */ |
| 4751 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4752 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4753 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4754 | TEST_LE_U( input_data->len, |
| 4755 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4756 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4757 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4758 | nonce->x, nonce->len, |
| 4759 | additional_data->x, |
| 4760 | additional_data->len, |
| 4761 | output_data, output_length, |
| 4762 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4763 | &output_length2 ), |
| 4764 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4765 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4766 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4767 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4768 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4769 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4770 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4771 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4772 | mbedtls_free( output_data ); |
| 4773 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4774 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4775 | } |
| 4776 | /* END_CASE */ |
| 4777 | |
| 4778 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4779 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4780 | int alg_arg, |
| 4781 | data_t *nonce, |
| 4782 | data_t *additional_data, |
| 4783 | data_t *input_data, |
| 4784 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4785 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4786 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4787 | psa_key_type_t key_type = key_type_arg; |
| 4788 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4789 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4790 | unsigned char *output_data = NULL; |
| 4791 | size_t output_size = 0; |
| 4792 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4793 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4794 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4795 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4796 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4797 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4798 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4799 | psa_set_key_algorithm( &attributes, alg ); |
| 4800 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4801 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4802 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4803 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4804 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4805 | key_bits = psa_get_key_bits( &attributes ); |
| 4806 | |
| 4807 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4808 | alg ); |
| 4809 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4810 | * should be exact. */ |
| 4811 | TEST_EQUAL( output_size, |
| 4812 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4813 | TEST_LE_U( output_size, |
| 4814 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4815 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4816 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4817 | status = psa_aead_encrypt( key, alg, |
| 4818 | nonce->x, nonce->len, |
| 4819 | additional_data->x, additional_data->len, |
| 4820 | input_data->x, input_data->len, |
| 4821 | output_data, output_size, |
| 4822 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4823 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4824 | /* If the operation is not supported, just skip and not fail in case the |
| 4825 | * encryption involves a common limitation of cryptography hardwares and |
| 4826 | * an alternative implementation. */ |
| 4827 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4828 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4829 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4830 | 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] | 4831 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4832 | |
| 4833 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4834 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4835 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4836 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4837 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4838 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4839 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4840 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4841 | } |
| 4842 | /* END_CASE */ |
| 4843 | |
| 4844 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4845 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4846 | int alg_arg, |
| 4847 | data_t *nonce, |
| 4848 | data_t *additional_data, |
| 4849 | data_t *input_data, |
| 4850 | data_t *expected_data, |
| 4851 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4852 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4853 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4854 | psa_key_type_t key_type = key_type_arg; |
| 4855 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4856 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4857 | unsigned char *output_data = NULL; |
| 4858 | size_t output_size = 0; |
| 4859 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4860 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4861 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4862 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4863 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4864 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4865 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4866 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4867 | psa_set_key_algorithm( &attributes, alg ); |
| 4868 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4869 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4870 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4871 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4872 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4873 | key_bits = psa_get_key_bits( &attributes ); |
| 4874 | |
| 4875 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4876 | alg ); |
| 4877 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4878 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4879 | { |
| 4880 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4881 | * should be exact. */ |
| 4882 | TEST_EQUAL( output_size, |
| 4883 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4884 | TEST_LE_U( output_size, |
| 4885 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4886 | } |
| 4887 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4888 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4889 | status = psa_aead_decrypt( key, alg, |
| 4890 | nonce->x, nonce->len, |
| 4891 | additional_data->x, |
| 4892 | additional_data->len, |
| 4893 | input_data->x, input_data->len, |
| 4894 | output_data, output_size, |
| 4895 | &output_length ); |
| 4896 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4897 | /* If the operation is not supported, just skip and not fail in case the |
| 4898 | * decryption involves a common limitation of cryptography hardwares and |
| 4899 | * an alternative implementation. */ |
| 4900 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4901 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4902 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4903 | 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] | 4904 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4905 | |
| 4906 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4907 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4908 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4909 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4910 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4911 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4912 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4913 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4914 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4915 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4916 | } |
| 4917 | /* END_CASE */ |
| 4918 | |
| 4919 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4920 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4921 | int alg_arg, |
| 4922 | data_t *nonce, |
| 4923 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4924 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4925 | int do_set_lengths, |
| 4926 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4927 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4928 | size_t ad_part_len = 0; |
| 4929 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4930 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4931 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4932 | 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] | 4933 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4934 | mbedtls_test_set_step( ad_part_len ); |
| 4935 | |
| 4936 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4937 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4938 | if( ad_part_len & 0x01 ) |
| 4939 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4940 | else |
| 4941 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4942 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4943 | |
| 4944 | /* Split ad into length(ad_part_len) parts. */ |
| 4945 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4946 | alg_arg, nonce, |
| 4947 | additional_data, |
| 4948 | ad_part_len, |
| 4949 | input_data, -1, |
| 4950 | set_lengths_method, |
| 4951 | expected_output, |
| 4952 | 1, 0 ) ) |
| 4953 | break; |
| 4954 | |
| 4955 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4956 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4957 | |
| 4958 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4959 | alg_arg, nonce, |
| 4960 | additional_data, |
| 4961 | ad_part_len, |
| 4962 | input_data, -1, |
| 4963 | set_lengths_method, |
| 4964 | expected_output, |
| 4965 | 1, 1 ) ) |
| 4966 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4967 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4968 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4969 | 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] | 4970 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4971 | /* Split data into length(data_part_len) parts. */ |
| 4972 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 4973 | |
| 4974 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4975 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4976 | if( data_part_len & 0x01 ) |
| 4977 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4978 | else |
| 4979 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4980 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4981 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4982 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4983 | alg_arg, nonce, |
| 4984 | additional_data, -1, |
| 4985 | input_data, data_part_len, |
| 4986 | set_lengths_method, |
| 4987 | expected_output, |
| 4988 | 1, 0 ) ) |
| 4989 | break; |
| 4990 | |
| 4991 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 4992 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 4993 | |
| 4994 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4995 | alg_arg, nonce, |
| 4996 | additional_data, -1, |
| 4997 | input_data, data_part_len, |
| 4998 | set_lengths_method, |
| 4999 | expected_output, |
| 5000 | 1, 1 ) ) |
| 5001 | break; |
| 5002 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5003 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5004 | /* Goto is required to silence warnings about unused labels, as we |
| 5005 | * don't actually do any test assertions in this function. */ |
| 5006 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5007 | } |
| 5008 | /* END_CASE */ |
| 5009 | |
| 5010 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5011 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 5012 | int alg_arg, |
| 5013 | data_t *nonce, |
| 5014 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5015 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 5016 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5017 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5018 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5019 | size_t ad_part_len = 0; |
| 5020 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5021 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5022 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5023 | 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] | 5024 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5025 | /* Split ad into length(ad_part_len) parts. */ |
| 5026 | mbedtls_test_set_step( ad_part_len ); |
| 5027 | |
| 5028 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5029 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5030 | if( ad_part_len & 0x01 ) |
| 5031 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5032 | else |
| 5033 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5034 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5035 | |
| 5036 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5037 | alg_arg, nonce, |
| 5038 | additional_data, |
| 5039 | ad_part_len, |
| 5040 | input_data, -1, |
| 5041 | set_lengths_method, |
| 5042 | expected_output, |
| 5043 | 0, 0 ) ) |
| 5044 | break; |
| 5045 | |
| 5046 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5047 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5048 | |
| 5049 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5050 | alg_arg, nonce, |
| 5051 | additional_data, |
| 5052 | ad_part_len, |
| 5053 | input_data, -1, |
| 5054 | set_lengths_method, |
| 5055 | expected_output, |
| 5056 | 0, 1 ) ) |
| 5057 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5058 | } |
| 5059 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5060 | 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] | 5061 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5062 | /* Split data into length(data_part_len) parts. */ |
| 5063 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5064 | |
| 5065 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5066 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5067 | if( data_part_len & 0x01 ) |
| 5068 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5069 | else |
| 5070 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5071 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5072 | |
| 5073 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5074 | alg_arg, nonce, |
| 5075 | additional_data, -1, |
| 5076 | input_data, data_part_len, |
| 5077 | set_lengths_method, |
| 5078 | expected_output, |
| 5079 | 0, 0 ) ) |
| 5080 | break; |
| 5081 | |
| 5082 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5083 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5084 | |
| 5085 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5086 | alg_arg, nonce, |
| 5087 | additional_data, -1, |
| 5088 | input_data, data_part_len, |
| 5089 | set_lengths_method, |
| 5090 | expected_output, |
| 5091 | 0, 1 ) ) |
| 5092 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5093 | } |
| 5094 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5095 | /* Goto is required to silence warnings about unused labels, as we |
| 5096 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5097 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5098 | } |
| 5099 | /* END_CASE */ |
| 5100 | |
| 5101 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5102 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 5103 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5104 | int nonce_length, |
| 5105 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5106 | data_t *additional_data, |
| 5107 | data_t *input_data, |
| 5108 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5109 | { |
| 5110 | |
| 5111 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5112 | psa_key_type_t key_type = key_type_arg; |
| 5113 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5114 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5115 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5116 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5117 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5118 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5119 | size_t actual_nonce_length = 0; |
| 5120 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5121 | unsigned char *output = NULL; |
| 5122 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5123 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5124 | size_t ciphertext_size = 0; |
| 5125 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5126 | size_t tag_length = 0; |
| 5127 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5128 | |
| 5129 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5130 | |
| 5131 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5132 | psa_set_key_algorithm( & attributes, alg ); |
| 5133 | psa_set_key_type( & attributes, key_type ); |
| 5134 | |
| 5135 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5136 | &key ) ); |
| 5137 | |
| 5138 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5139 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5140 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5141 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5142 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5143 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5144 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5145 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5146 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5147 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5148 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5149 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5150 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5151 | |
| 5152 | /* If the operation is not supported, just skip and not fail in case the |
| 5153 | * encryption involves a common limitation of cryptography hardwares and |
| 5154 | * an alternative implementation. */ |
| 5155 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5156 | { |
| 5157 | 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] | 5158 | 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] | 5159 | } |
| 5160 | |
| 5161 | PSA_ASSERT( status ); |
| 5162 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5163 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5164 | nonce_length, |
| 5165 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5166 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5167 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5168 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5169 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5170 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5171 | if( expected_status == PSA_SUCCESS ) |
| 5172 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 5173 | alg ) ); |
| 5174 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5175 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5176 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5177 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5178 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5179 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 5180 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5181 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5182 | |
| 5183 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5184 | additional_data->len ) ); |
| 5185 | |
| 5186 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5187 | output, output_size, |
| 5188 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5189 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5190 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5191 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5192 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5193 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5194 | |
| 5195 | exit: |
| 5196 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5197 | mbedtls_free( output ); |
| 5198 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5199 | psa_aead_abort( &operation ); |
| 5200 | PSA_DONE( ); |
| 5201 | } |
| 5202 | /* END_CASE */ |
| 5203 | |
| 5204 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5205 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 5206 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5207 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5208 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5209 | data_t *additional_data, |
| 5210 | data_t *input_data, |
| 5211 | int expected_status_arg ) |
| 5212 | { |
| 5213 | |
| 5214 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5215 | psa_key_type_t key_type = key_type_arg; |
| 5216 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5217 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5218 | uint8_t *nonce_buffer = NULL; |
| 5219 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5220 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5221 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5222 | unsigned char *output = NULL; |
| 5223 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5224 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5225 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5226 | size_t ciphertext_size = 0; |
| 5227 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5228 | size_t tag_length = 0; |
| 5229 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5230 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5231 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5232 | |
| 5233 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5234 | |
| 5235 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5236 | psa_set_key_algorithm( &attributes, alg ); |
| 5237 | psa_set_key_type( &attributes, key_type ); |
| 5238 | |
| 5239 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5240 | &key ) ); |
| 5241 | |
| 5242 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5243 | |
| 5244 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5245 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5246 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5247 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5248 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5249 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5250 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5251 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5252 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5253 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5254 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5255 | |
| 5256 | /* If the operation is not supported, just skip and not fail in case the |
| 5257 | * encryption involves a common limitation of cryptography hardwares and |
| 5258 | * an alternative implementation. */ |
| 5259 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5260 | { |
| 5261 | 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] | 5262 | 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] | 5263 | } |
| 5264 | |
| 5265 | PSA_ASSERT( status ); |
| 5266 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5267 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 5268 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5269 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 5270 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5271 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5272 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5273 | } |
| 5274 | else |
| 5275 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5276 | /* If length is zero, then this will return NULL. */ |
| 5277 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5278 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5279 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5280 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5281 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5282 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5283 | { |
| 5284 | nonce_buffer[index] = 'a' + index; |
| 5285 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5286 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5287 | } |
| 5288 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5289 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5290 | { |
| 5291 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5292 | input_data->len ) ); |
| 5293 | } |
| 5294 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5295 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5296 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5297 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5298 | |
| 5299 | if( expected_status == PSA_SUCCESS ) |
| 5300 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5301 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5302 | { |
| 5303 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5304 | input_data->len ) ); |
| 5305 | } |
| 5306 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5307 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5308 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5309 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5310 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5311 | additional_data->len ), |
| 5312 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5313 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5314 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5315 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5316 | &ciphertext_length ), |
| 5317 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5318 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5319 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5320 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5321 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5322 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5323 | } |
| 5324 | |
| 5325 | exit: |
| 5326 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5327 | mbedtls_free( output ); |
| 5328 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5329 | mbedtls_free( nonce_buffer ); |
| 5330 | psa_aead_abort( &operation ); |
| 5331 | PSA_DONE( ); |
| 5332 | } |
| 5333 | /* END_CASE */ |
| 5334 | |
| 5335 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5336 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5337 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5338 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5339 | data_t *nonce, |
| 5340 | data_t *additional_data, |
| 5341 | data_t *input_data, |
| 5342 | int expected_status_arg ) |
| 5343 | { |
| 5344 | |
| 5345 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5346 | psa_key_type_t key_type = key_type_arg; |
| 5347 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5348 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5349 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5350 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5351 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5352 | unsigned char *output = NULL; |
| 5353 | unsigned char *ciphertext = NULL; |
| 5354 | size_t output_size = output_size_arg; |
| 5355 | size_t ciphertext_size = 0; |
| 5356 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5357 | size_t tag_length = 0; |
| 5358 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5359 | |
| 5360 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5361 | |
| 5362 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5363 | psa_set_key_algorithm( &attributes, alg ); |
| 5364 | psa_set_key_type( &attributes, key_type ); |
| 5365 | |
| 5366 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5367 | &key ) ); |
| 5368 | |
| 5369 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5370 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5371 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5372 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5373 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5374 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5375 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5376 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5377 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5378 | |
| 5379 | /* If the operation is not supported, just skip and not fail in case the |
| 5380 | * encryption involves a common limitation of cryptography hardwares and |
| 5381 | * an alternative implementation. */ |
| 5382 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5383 | { |
| 5384 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5385 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5386 | } |
| 5387 | |
| 5388 | PSA_ASSERT( status ); |
| 5389 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5390 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5391 | input_data->len ) ); |
| 5392 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5393 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5394 | |
| 5395 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5396 | additional_data->len ) ); |
| 5397 | |
| 5398 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5399 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5400 | |
| 5401 | TEST_EQUAL( status, expected_status ); |
| 5402 | |
| 5403 | if( expected_status == PSA_SUCCESS ) |
| 5404 | { |
| 5405 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5406 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5407 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5408 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5409 | } |
| 5410 | |
| 5411 | exit: |
| 5412 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5413 | mbedtls_free( output ); |
| 5414 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5415 | psa_aead_abort( &operation ); |
| 5416 | PSA_DONE( ); |
| 5417 | } |
| 5418 | /* END_CASE */ |
| 5419 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5420 | /* BEGIN_CASE */ |
| 5421 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5422 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5423 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5424 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5425 | data_t *nonce, |
| 5426 | data_t *additional_data, |
| 5427 | data_t *input_data, |
| 5428 | int expected_status_arg ) |
| 5429 | { |
| 5430 | |
| 5431 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5432 | psa_key_type_t key_type = key_type_arg; |
| 5433 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5434 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5435 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5436 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5437 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5438 | unsigned char *ciphertext = NULL; |
| 5439 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5440 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5441 | size_t ciphertext_size = 0; |
| 5442 | size_t ciphertext_length = 0; |
| 5443 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5444 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5445 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5446 | |
| 5447 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5448 | |
| 5449 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5450 | psa_set_key_algorithm( &attributes, alg ); |
| 5451 | psa_set_key_type( &attributes, key_type ); |
| 5452 | |
| 5453 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5454 | &key ) ); |
| 5455 | |
| 5456 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5457 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5458 | 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] | 5459 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5460 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5461 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5462 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5463 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5464 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5465 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5466 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5467 | |
| 5468 | /* If the operation is not supported, just skip and not fail in case the |
| 5469 | * encryption involves a common limitation of cryptography hardwares and |
| 5470 | * an alternative implementation. */ |
| 5471 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5472 | { |
| 5473 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5474 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5475 | } |
| 5476 | |
| 5477 | PSA_ASSERT( status ); |
| 5478 | |
| 5479 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5480 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5481 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5482 | input_data->len ) ); |
| 5483 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5484 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5485 | additional_data->len ) ); |
| 5486 | |
| 5487 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5488 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5489 | |
| 5490 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5491 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5492 | finish_ciphertext_size, |
| 5493 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5494 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5495 | |
| 5496 | TEST_EQUAL( status, expected_status ); |
| 5497 | |
| 5498 | exit: |
| 5499 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5500 | mbedtls_free( ciphertext ); |
| 5501 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5502 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5503 | psa_aead_abort( &operation ); |
| 5504 | PSA_DONE( ); |
| 5505 | } |
| 5506 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5507 | |
| 5508 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5509 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5510 | int alg_arg, |
| 5511 | data_t *nonce, |
| 5512 | data_t *additional_data, |
| 5513 | data_t *input_data, |
| 5514 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5515 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5516 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5517 | int expected_status_arg ) |
| 5518 | { |
| 5519 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5520 | psa_key_type_t key_type = key_type_arg; |
| 5521 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5522 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5523 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5524 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5525 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5526 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5527 | unsigned char *plaintext = NULL; |
| 5528 | unsigned char *finish_plaintext = NULL; |
| 5529 | size_t plaintext_size = 0; |
| 5530 | size_t plaintext_length = 0; |
| 5531 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5532 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5533 | unsigned char *tag_buffer = NULL; |
| 5534 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5535 | |
| 5536 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5537 | |
| 5538 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5539 | psa_set_key_algorithm( &attributes, alg ); |
| 5540 | psa_set_key_type( &attributes, key_type ); |
| 5541 | |
| 5542 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5543 | &key ) ); |
| 5544 | |
| 5545 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5546 | |
| 5547 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5548 | input_data->len ); |
| 5549 | |
| 5550 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5551 | |
| 5552 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5553 | |
| 5554 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5555 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5556 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5557 | |
| 5558 | /* If the operation is not supported, just skip and not fail in case the |
| 5559 | * encryption involves a common limitation of cryptography hardwares and |
| 5560 | * an alternative implementation. */ |
| 5561 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5562 | { |
| 5563 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5564 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5565 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5566 | TEST_EQUAL( status, expected_setup_status ); |
| 5567 | |
| 5568 | if( status != PSA_SUCCESS ) |
| 5569 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5570 | |
| 5571 | PSA_ASSERT( status ); |
| 5572 | |
| 5573 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5574 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5575 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5576 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5577 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5578 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5579 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5580 | additional_data->len ) ); |
| 5581 | |
| 5582 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5583 | input_data->len, |
| 5584 | plaintext, plaintext_size, |
| 5585 | &plaintext_length ) ); |
| 5586 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5587 | if( tag_usage == USE_GIVEN_TAG ) |
| 5588 | { |
| 5589 | tag_buffer = tag->x; |
| 5590 | tag_size = tag->len; |
| 5591 | } |
| 5592 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5593 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5594 | verify_plaintext_size, |
| 5595 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5596 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5597 | |
| 5598 | TEST_EQUAL( status, expected_status ); |
| 5599 | |
| 5600 | exit: |
| 5601 | psa_destroy_key( key ); |
| 5602 | mbedtls_free( plaintext ); |
| 5603 | mbedtls_free( finish_plaintext ); |
| 5604 | psa_aead_abort( &operation ); |
| 5605 | PSA_DONE( ); |
| 5606 | } |
| 5607 | /* END_CASE */ |
| 5608 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5609 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5610 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5611 | int alg_arg, int expected_status_arg ) |
| 5612 | { |
| 5613 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5614 | psa_key_type_t key_type = key_type_arg; |
| 5615 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5616 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5617 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5618 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5619 | psa_status_t expected_status = expected_status_arg; |
| 5620 | |
| 5621 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5622 | |
| 5623 | psa_set_key_usage_flags( &attributes, |
| 5624 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5625 | psa_set_key_algorithm( &attributes, alg ); |
| 5626 | psa_set_key_type( &attributes, key_type ); |
| 5627 | |
| 5628 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5629 | &key ) ); |
| 5630 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5631 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5632 | |
| 5633 | TEST_EQUAL( status, expected_status ); |
| 5634 | |
| 5635 | psa_aead_abort( &operation ); |
| 5636 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5637 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5638 | |
| 5639 | TEST_EQUAL(status, expected_status ); |
| 5640 | |
| 5641 | exit: |
| 5642 | psa_destroy_key( key ); |
| 5643 | psa_aead_abort( &operation ); |
| 5644 | PSA_DONE( ); |
| 5645 | } |
| 5646 | /* END_CASE */ |
| 5647 | |
| 5648 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5649 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5650 | int alg_arg, |
| 5651 | data_t *nonce, |
| 5652 | data_t *additional_data, |
| 5653 | data_t *input_data ) |
| 5654 | { |
| 5655 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5656 | psa_key_type_t key_type = key_type_arg; |
| 5657 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5658 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5659 | unsigned char *output_data = NULL; |
| 5660 | unsigned char *final_data = NULL; |
| 5661 | size_t output_size = 0; |
| 5662 | size_t finish_output_size = 0; |
| 5663 | size_t output_length = 0; |
| 5664 | size_t key_bits = 0; |
| 5665 | size_t tag_length = 0; |
| 5666 | size_t tag_size = 0; |
| 5667 | size_t nonce_length = 0; |
| 5668 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5669 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5670 | size_t output_part_length = 0; |
| 5671 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5672 | |
| 5673 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5674 | |
| 5675 | psa_set_key_usage_flags( & attributes, |
| 5676 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5677 | psa_set_key_algorithm( & attributes, alg ); |
| 5678 | psa_set_key_type( & attributes, key_type ); |
| 5679 | |
| 5680 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5681 | &key ) ); |
| 5682 | |
| 5683 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5684 | key_bits = psa_get_key_bits( &attributes ); |
| 5685 | |
| 5686 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5687 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5688 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5689 | |
| 5690 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5691 | |
| 5692 | ASSERT_ALLOC( output_data, output_size ); |
| 5693 | |
| 5694 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5695 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5696 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5697 | |
| 5698 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5699 | |
| 5700 | /* Test all operations error without calling setup first. */ |
| 5701 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5702 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5703 | PSA_ERROR_BAD_STATE ); |
| 5704 | |
| 5705 | psa_aead_abort( &operation ); |
| 5706 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5707 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5708 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5709 | &nonce_length ), |
| 5710 | PSA_ERROR_BAD_STATE ); |
| 5711 | |
| 5712 | psa_aead_abort( &operation ); |
| 5713 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5714 | /* ------------------------------------------------------- */ |
| 5715 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5716 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5717 | input_data->len ), |
| 5718 | PSA_ERROR_BAD_STATE ); |
| 5719 | |
| 5720 | psa_aead_abort( &operation ); |
| 5721 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5722 | /* ------------------------------------------------------- */ |
| 5723 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5724 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5725 | additional_data->len ), |
| 5726 | PSA_ERROR_BAD_STATE ); |
| 5727 | |
| 5728 | psa_aead_abort( &operation ); |
| 5729 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5730 | /* ------------------------------------------------------- */ |
| 5731 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5732 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5733 | input_data->len, output_data, |
| 5734 | output_size, &output_length ), |
| 5735 | PSA_ERROR_BAD_STATE ); |
| 5736 | |
| 5737 | psa_aead_abort( &operation ); |
| 5738 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5739 | /* ------------------------------------------------------- */ |
| 5740 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5741 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5742 | finish_output_size, |
| 5743 | &output_part_length, |
| 5744 | tag_buffer, tag_length, |
| 5745 | &tag_size ), |
| 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_verify( &operation, final_data, |
| 5753 | finish_output_size, |
| 5754 | &output_part_length, |
| 5755 | tag_buffer, |
| 5756 | tag_length ), |
| 5757 | PSA_ERROR_BAD_STATE ); |
| 5758 | |
| 5759 | psa_aead_abort( &operation ); |
| 5760 | |
| 5761 | /* Test for double setups. */ |
| 5762 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5763 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5764 | |
| 5765 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5766 | PSA_ERROR_BAD_STATE ); |
| 5767 | |
| 5768 | psa_aead_abort( &operation ); |
| 5769 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5770 | /* ------------------------------------------------------- */ |
| 5771 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5772 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5773 | |
| 5774 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5775 | PSA_ERROR_BAD_STATE ); |
| 5776 | |
| 5777 | psa_aead_abort( &operation ); |
| 5778 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5779 | /* ------------------------------------------------------- */ |
| 5780 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5781 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5782 | |
| 5783 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5784 | PSA_ERROR_BAD_STATE ); |
| 5785 | |
| 5786 | psa_aead_abort( &operation ); |
| 5787 | |
| 5788 | /* ------------------------------------------------------- */ |
| 5789 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5790 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5791 | |
| 5792 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5793 | PSA_ERROR_BAD_STATE ); |
| 5794 | |
| 5795 | psa_aead_abort( &operation ); |
| 5796 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5797 | /* Test for not setting a nonce. */ |
| 5798 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5799 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5800 | |
| 5801 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5802 | additional_data->len ), |
| 5803 | PSA_ERROR_BAD_STATE ); |
| 5804 | |
| 5805 | psa_aead_abort( &operation ); |
| 5806 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5807 | /* ------------------------------------------------------- */ |
| 5808 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5809 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5810 | |
| 5811 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5812 | input_data->len, output_data, |
| 5813 | output_size, &output_length ), |
| 5814 | PSA_ERROR_BAD_STATE ); |
| 5815 | |
| 5816 | psa_aead_abort( &operation ); |
| 5817 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5818 | /* ------------------------------------------------------- */ |
| 5819 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5820 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5821 | |
| 5822 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5823 | finish_output_size, |
| 5824 | &output_part_length, |
| 5825 | tag_buffer, tag_length, |
| 5826 | &tag_size ), |
| 5827 | PSA_ERROR_BAD_STATE ); |
| 5828 | |
| 5829 | psa_aead_abort( &operation ); |
| 5830 | |
| 5831 | /* ------------------------------------------------------- */ |
| 5832 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5833 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5834 | |
| 5835 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5836 | finish_output_size, |
| 5837 | &output_part_length, |
| 5838 | tag_buffer, |
| 5839 | tag_length ), |
| 5840 | PSA_ERROR_BAD_STATE ); |
| 5841 | |
| 5842 | psa_aead_abort( &operation ); |
| 5843 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5844 | /* Test for double setting nonce. */ |
| 5845 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5846 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5847 | |
| 5848 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5849 | |
| 5850 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5851 | PSA_ERROR_BAD_STATE ); |
| 5852 | |
| 5853 | psa_aead_abort( &operation ); |
| 5854 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5855 | /* Test for double generating nonce. */ |
| 5856 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5857 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5858 | |
| 5859 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5860 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5861 | &nonce_length ) ); |
| 5862 | |
| 5863 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5864 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5865 | &nonce_length ), |
| 5866 | PSA_ERROR_BAD_STATE ); |
| 5867 | |
| 5868 | |
| 5869 | psa_aead_abort( &operation ); |
| 5870 | |
| 5871 | /* Test for generate nonce then set and vice versa */ |
| 5872 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5873 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5874 | |
| 5875 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5876 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5877 | &nonce_length ) ); |
| 5878 | |
| 5879 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5880 | PSA_ERROR_BAD_STATE ); |
| 5881 | |
| 5882 | psa_aead_abort( &operation ); |
| 5883 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5884 | /* Test for generating nonce after calling set lengths */ |
| 5885 | |
| 5886 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5887 | |
| 5888 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5889 | input_data->len ) ); |
| 5890 | |
| 5891 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5892 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5893 | &nonce_length ) ); |
| 5894 | |
| 5895 | psa_aead_abort( &operation ); |
| 5896 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5897 | /* 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] | 5898 | |
| 5899 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5900 | |
| 5901 | if( operation.alg == PSA_ALG_CCM ) |
| 5902 | { |
| 5903 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5904 | input_data->len ), |
| 5905 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5906 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5907 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5908 | &nonce_length ), |
| 5909 | PSA_ERROR_BAD_STATE ); |
| 5910 | } |
| 5911 | else |
| 5912 | { |
| 5913 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5914 | input_data->len ) ); |
| 5915 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5916 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5917 | &nonce_length ) ); |
| 5918 | } |
| 5919 | |
| 5920 | psa_aead_abort( &operation ); |
| 5921 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5922 | /* 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] | 5923 | #if SIZE_MAX > UINT32_MAX |
| 5924 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5925 | |
| 5926 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5927 | { |
| 5928 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5929 | input_data->len ), |
| 5930 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5931 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5932 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5933 | &nonce_length ), |
| 5934 | PSA_ERROR_BAD_STATE ); |
| 5935 | } |
| 5936 | else |
| 5937 | { |
| 5938 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5939 | input_data->len ) ); |
| 5940 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5941 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5942 | &nonce_length ) ); |
| 5943 | } |
| 5944 | |
| 5945 | psa_aead_abort( &operation ); |
| 5946 | #endif |
| 5947 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5948 | /* 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] | 5949 | |
| 5950 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5951 | |
| 5952 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5953 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5954 | &nonce_length ) ); |
| 5955 | |
| 5956 | if( operation.alg == PSA_ALG_CCM ) |
| 5957 | { |
| 5958 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5959 | input_data->len ), |
| 5960 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5961 | } |
| 5962 | else |
| 5963 | { |
| 5964 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5965 | input_data->len ) ); |
| 5966 | } |
| 5967 | |
| 5968 | psa_aead_abort( &operation ); |
| 5969 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5970 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5971 | /* Test for setting nonce after calling set lengths */ |
| 5972 | |
| 5973 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5974 | |
| 5975 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5976 | input_data->len ) ); |
| 5977 | |
| 5978 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5979 | |
| 5980 | psa_aead_abort( &operation ); |
| 5981 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5982 | /* 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] | 5983 | |
| 5984 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5985 | |
| 5986 | if( operation.alg == PSA_ALG_CCM ) |
| 5987 | { |
| 5988 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5989 | input_data->len ), |
| 5990 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5991 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5992 | PSA_ERROR_BAD_STATE ); |
| 5993 | } |
| 5994 | else |
| 5995 | { |
| 5996 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5997 | input_data->len ) ); |
| 5998 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5999 | } |
| 6000 | |
| 6001 | psa_aead_abort( &operation ); |
| 6002 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6003 | /* 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] | 6004 | #if SIZE_MAX > UINT32_MAX |
| 6005 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6006 | |
| 6007 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 6008 | { |
| 6009 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6010 | input_data->len ), |
| 6011 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6012 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6013 | PSA_ERROR_BAD_STATE ); |
| 6014 | } |
| 6015 | else |
| 6016 | { |
| 6017 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6018 | input_data->len ) ); |
| 6019 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6020 | } |
| 6021 | |
| 6022 | psa_aead_abort( &operation ); |
| 6023 | #endif |
| 6024 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6025 | /* 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] | 6026 | |
| 6027 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6028 | |
| 6029 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6030 | |
| 6031 | if( operation.alg == PSA_ALG_CCM ) |
| 6032 | { |
| 6033 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6034 | input_data->len ), |
| 6035 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6036 | } |
| 6037 | else |
| 6038 | { |
| 6039 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6040 | input_data->len ) ); |
| 6041 | } |
| 6042 | |
| 6043 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6044 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6045 | /* 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] | 6046 | #if SIZE_MAX > UINT32_MAX |
| 6047 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6048 | |
| 6049 | if( operation.alg == PSA_ALG_GCM ) |
| 6050 | { |
| 6051 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6052 | SIZE_MAX ), |
| 6053 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6054 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6055 | PSA_ERROR_BAD_STATE ); |
| 6056 | } |
| 6057 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6058 | { |
| 6059 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6060 | SIZE_MAX ) ); |
| 6061 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6062 | } |
| 6063 | |
| 6064 | psa_aead_abort( &operation ); |
| 6065 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6066 | /* 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] | 6067 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6068 | |
| 6069 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6070 | |
| 6071 | if( operation.alg == PSA_ALG_GCM ) |
| 6072 | { |
| 6073 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6074 | SIZE_MAX ), |
| 6075 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6076 | } |
| 6077 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6078 | { |
| 6079 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6080 | SIZE_MAX ) ); |
| 6081 | } |
| 6082 | |
| 6083 | psa_aead_abort( &operation ); |
| 6084 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6085 | |
| 6086 | /* ------------------------------------------------------- */ |
| 6087 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6088 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6089 | |
| 6090 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6091 | |
| 6092 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6093 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6094 | &nonce_length ), |
| 6095 | PSA_ERROR_BAD_STATE ); |
| 6096 | |
| 6097 | psa_aead_abort( &operation ); |
| 6098 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6099 | /* Test for generating nonce in decrypt setup. */ |
| 6100 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6101 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6102 | |
| 6103 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6104 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6105 | &nonce_length ), |
| 6106 | PSA_ERROR_BAD_STATE ); |
| 6107 | |
| 6108 | psa_aead_abort( &operation ); |
| 6109 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6110 | /* Test for setting lengths twice. */ |
| 6111 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6112 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6113 | |
| 6114 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6115 | |
| 6116 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6117 | input_data->len ) ); |
| 6118 | |
| 6119 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6120 | input_data->len ), |
| 6121 | PSA_ERROR_BAD_STATE ); |
| 6122 | |
| 6123 | psa_aead_abort( &operation ); |
| 6124 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6125 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6126 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6127 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6128 | |
| 6129 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6130 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6131 | if( operation.alg == PSA_ALG_CCM ) |
| 6132 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6133 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6134 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6135 | additional_data->len ), |
| 6136 | PSA_ERROR_BAD_STATE ); |
| 6137 | } |
| 6138 | else |
| 6139 | { |
| 6140 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6141 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6142 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6143 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6144 | input_data->len ), |
| 6145 | PSA_ERROR_BAD_STATE ); |
| 6146 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6147 | psa_aead_abort( &operation ); |
| 6148 | |
| 6149 | /* ------------------------------------------------------- */ |
| 6150 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6151 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6152 | |
| 6153 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6154 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6155 | if( operation.alg == PSA_ALG_CCM ) |
| 6156 | { |
| 6157 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6158 | input_data->len, output_data, |
| 6159 | output_size, &output_length ), |
| 6160 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6161 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6162 | } |
| 6163 | else |
| 6164 | { |
| 6165 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6166 | input_data->len, output_data, |
| 6167 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6168 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6169 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6170 | input_data->len ), |
| 6171 | PSA_ERROR_BAD_STATE ); |
| 6172 | } |
| 6173 | psa_aead_abort( &operation ); |
| 6174 | |
| 6175 | /* ------------------------------------------------------- */ |
| 6176 | |
| 6177 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6178 | |
| 6179 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6180 | |
| 6181 | if( operation.alg == PSA_ALG_CCM ) |
| 6182 | { |
| 6183 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6184 | finish_output_size, |
| 6185 | &output_part_length, |
| 6186 | tag_buffer, tag_length, |
| 6187 | &tag_size ) ); |
| 6188 | } |
| 6189 | else |
| 6190 | { |
| 6191 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6192 | finish_output_size, |
| 6193 | &output_part_length, |
| 6194 | tag_buffer, tag_length, |
| 6195 | &tag_size ) ); |
| 6196 | |
| 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 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6204 | |
| 6205 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6206 | |
| 6207 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6208 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6209 | &nonce_length ) ); |
| 6210 | if( operation.alg == PSA_ALG_CCM ) |
| 6211 | { |
| 6212 | |
| 6213 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6214 | additional_data->len ), |
| 6215 | PSA_ERROR_BAD_STATE ); |
| 6216 | } |
| 6217 | else |
| 6218 | { |
| 6219 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6220 | additional_data->len ) ); |
| 6221 | |
| 6222 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6223 | input_data->len ), |
| 6224 | PSA_ERROR_BAD_STATE ); |
| 6225 | } |
| 6226 | psa_aead_abort( &operation ); |
| 6227 | |
| 6228 | /* ------------------------------------------------------- */ |
| 6229 | |
| 6230 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6231 | |
| 6232 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6233 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6234 | &nonce_length ) ); |
| 6235 | if( operation.alg == PSA_ALG_CCM ) |
| 6236 | { |
| 6237 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6238 | input_data->len, output_data, |
| 6239 | output_size, &output_length ), |
| 6240 | PSA_ERROR_BAD_STATE ); |
| 6241 | |
| 6242 | } |
| 6243 | else |
| 6244 | { |
| 6245 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6246 | input_data->len, output_data, |
| 6247 | output_size, &output_length ) ); |
| 6248 | |
| 6249 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6250 | input_data->len ), |
| 6251 | PSA_ERROR_BAD_STATE ); |
| 6252 | } |
| 6253 | psa_aead_abort( &operation ); |
| 6254 | |
| 6255 | /* ------------------------------------------------------- */ |
| 6256 | |
| 6257 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6258 | |
| 6259 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6260 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6261 | &nonce_length ) ); |
| 6262 | if( operation.alg == PSA_ALG_CCM ) |
| 6263 | { |
| 6264 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6265 | finish_output_size, |
| 6266 | &output_part_length, |
| 6267 | tag_buffer, tag_length, |
| 6268 | &tag_size ) ); |
| 6269 | } |
| 6270 | else |
| 6271 | { |
| 6272 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6273 | finish_output_size, |
| 6274 | &output_part_length, |
| 6275 | tag_buffer, tag_length, |
| 6276 | &tag_size ) ); |
| 6277 | |
| 6278 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6279 | input_data->len ), |
| 6280 | PSA_ERROR_BAD_STATE ); |
| 6281 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6282 | psa_aead_abort( &operation ); |
| 6283 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6284 | /* Test for not sending any additional data or data after setting non zero |
| 6285 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6286 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6287 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6288 | |
| 6289 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6290 | |
| 6291 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6292 | input_data->len ) ); |
| 6293 | |
| 6294 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6295 | finish_output_size, |
| 6296 | &output_part_length, |
| 6297 | tag_buffer, tag_length, |
| 6298 | &tag_size ), |
| 6299 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6300 | |
| 6301 | psa_aead_abort( &operation ); |
| 6302 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6303 | /* Test for not sending any additional data or data after setting non-zero |
| 6304 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6305 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6306 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6307 | |
| 6308 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6309 | |
| 6310 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6311 | input_data->len ) ); |
| 6312 | |
| 6313 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6314 | finish_output_size, |
| 6315 | &output_part_length, |
| 6316 | tag_buffer, |
| 6317 | tag_length ), |
| 6318 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6319 | |
| 6320 | psa_aead_abort( &operation ); |
| 6321 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6322 | /* Test for not sending any additional data after setting a non-zero length |
| 6323 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6324 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6325 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6326 | |
| 6327 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6328 | |
| 6329 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6330 | input_data->len ) ); |
| 6331 | |
| 6332 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6333 | input_data->len, output_data, |
| 6334 | output_size, &output_length ), |
| 6335 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6336 | |
| 6337 | psa_aead_abort( &operation ); |
| 6338 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6339 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6340 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6341 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6342 | |
| 6343 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6344 | |
| 6345 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6346 | input_data->len ) ); |
| 6347 | |
| 6348 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6349 | additional_data->len ) ); |
| 6350 | |
| 6351 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6352 | finish_output_size, |
| 6353 | &output_part_length, |
| 6354 | tag_buffer, tag_length, |
| 6355 | &tag_size ), |
| 6356 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6357 | |
| 6358 | psa_aead_abort( &operation ); |
| 6359 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6360 | /* Test for sending too much additional data after setting lengths. */ |
| 6361 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6362 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6363 | |
| 6364 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6365 | |
| 6366 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6367 | |
| 6368 | |
| 6369 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6370 | additional_data->len ), |
| 6371 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6372 | |
| 6373 | psa_aead_abort( &operation ); |
| 6374 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6375 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6376 | |
| 6377 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6378 | |
| 6379 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6380 | |
| 6381 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6382 | input_data->len ) ); |
| 6383 | |
| 6384 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6385 | additional_data->len ) ); |
| 6386 | |
| 6387 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6388 | 1 ), |
| 6389 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6390 | |
| 6391 | psa_aead_abort( &operation ); |
| 6392 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6393 | /* Test for sending too much data after setting lengths. */ |
| 6394 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6395 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6396 | |
| 6397 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6398 | |
| 6399 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6400 | |
| 6401 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6402 | input_data->len, output_data, |
| 6403 | output_size, &output_length ), |
| 6404 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6405 | |
| 6406 | psa_aead_abort( &operation ); |
| 6407 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6408 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6409 | |
| 6410 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6411 | |
| 6412 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6413 | |
| 6414 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6415 | input_data->len ) ); |
| 6416 | |
| 6417 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6418 | additional_data->len ) ); |
| 6419 | |
| 6420 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6421 | input_data->len, output_data, |
| 6422 | output_size, &output_length ) ); |
| 6423 | |
| 6424 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6425 | 1, output_data, |
| 6426 | output_size, &output_length ), |
| 6427 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6428 | |
| 6429 | psa_aead_abort( &operation ); |
| 6430 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6431 | /* Test sending additional data after data. */ |
| 6432 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6433 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6434 | |
| 6435 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6436 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6437 | if( operation.alg != PSA_ALG_CCM ) |
| 6438 | { |
| 6439 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6440 | input_data->len, output_data, |
| 6441 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6442 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6443 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6444 | additional_data->len ), |
| 6445 | PSA_ERROR_BAD_STATE ); |
| 6446 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6447 | psa_aead_abort( &operation ); |
| 6448 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6449 | /* Test calling finish on decryption. */ |
| 6450 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6451 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6452 | |
| 6453 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6454 | |
| 6455 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6456 | finish_output_size, |
| 6457 | &output_part_length, |
| 6458 | tag_buffer, tag_length, |
| 6459 | &tag_size ), |
| 6460 | PSA_ERROR_BAD_STATE ); |
| 6461 | |
| 6462 | psa_aead_abort( &operation ); |
| 6463 | |
| 6464 | /* Test calling verify on encryption. */ |
| 6465 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6466 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6467 | |
| 6468 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6469 | |
| 6470 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6471 | finish_output_size, |
| 6472 | &output_part_length, |
| 6473 | tag_buffer, |
| 6474 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6475 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6476 | |
| 6477 | psa_aead_abort( &operation ); |
| 6478 | |
| 6479 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6480 | exit: |
| 6481 | psa_destroy_key( key ); |
| 6482 | psa_aead_abort( &operation ); |
| 6483 | mbedtls_free( output_data ); |
| 6484 | mbedtls_free( final_data ); |
| 6485 | PSA_DONE( ); |
| 6486 | } |
| 6487 | /* END_CASE */ |
| 6488 | |
| 6489 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6490 | void signature_size( int type_arg, |
| 6491 | int bits, |
| 6492 | int alg_arg, |
| 6493 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6494 | { |
| 6495 | psa_key_type_t type = type_arg; |
| 6496 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6497 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6498 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6499 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6500 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6501 | exit: |
| 6502 | ; |
| 6503 | } |
| 6504 | /* END_CASE */ |
| 6505 | |
| 6506 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6507 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6508 | int alg_arg, data_t *input_data, |
| 6509 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6510 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6511 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6512 | psa_key_type_t key_type = key_type_arg; |
| 6513 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6514 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6515 | unsigned char *signature = NULL; |
| 6516 | size_t signature_size; |
| 6517 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6518 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6519 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6520 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6521 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6522 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6523 | psa_set_key_algorithm( &attributes, alg ); |
| 6524 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6525 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6526 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6527 | &key ) ); |
| 6528 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6529 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6530 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6531 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6532 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6533 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6534 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6535 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6536 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6537 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6538 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6539 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6540 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6541 | input_data->x, input_data->len, |
| 6542 | signature, signature_size, |
| 6543 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6544 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6545 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6546 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6547 | |
| 6548 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6549 | /* |
| 6550 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6551 | * thus reset them as required. |
| 6552 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6553 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6554 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6555 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6556 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6557 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6558 | } |
| 6559 | /* END_CASE */ |
| 6560 | |
| 6561 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6562 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6563 | int alg_arg, data_t *input_data, |
| 6564 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6565 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6566 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6567 | psa_key_type_t key_type = key_type_arg; |
| 6568 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6569 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6570 | psa_status_t actual_status; |
| 6571 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6572 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6573 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6574 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6575 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6576 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6577 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6578 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6579 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6580 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6581 | psa_set_key_algorithm( &attributes, alg ); |
| 6582 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6583 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6584 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6585 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6586 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6587 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6588 | input_data->x, input_data->len, |
| 6589 | signature, signature_size, |
| 6590 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6591 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6592 | /* The value of *signature_length is unspecified on error, but |
| 6593 | * whatever it is, it should be less than signature_size, so that |
| 6594 | * if the caller tries to read *signature_length bytes without |
| 6595 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6596 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6597 | |
| 6598 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6599 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6600 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6601 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6602 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6603 | } |
| 6604 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6605 | |
| 6606 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6607 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6608 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6609 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6610 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6611 | psa_key_type_t key_type = key_type_arg; |
| 6612 | psa_algorithm_t alg = alg_arg; |
| 6613 | size_t key_bits; |
| 6614 | unsigned char *signature = NULL; |
| 6615 | size_t signature_size; |
| 6616 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6617 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6618 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6619 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6620 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6621 | 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] | 6622 | psa_set_key_algorithm( &attributes, alg ); |
| 6623 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6624 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6625 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6626 | &key ) ); |
| 6627 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6628 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6629 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6630 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6631 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6632 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6633 | key_bits, alg ); |
| 6634 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6635 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6636 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6637 | |
| 6638 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6639 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6640 | input_data->x, input_data->len, |
| 6641 | signature, signature_size, |
| 6642 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6643 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6644 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6645 | TEST_ASSERT( signature_length > 0 ); |
| 6646 | |
| 6647 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6648 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6649 | input_data->x, input_data->len, |
| 6650 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6651 | |
| 6652 | if( input_data->len != 0 ) |
| 6653 | { |
| 6654 | /* Flip a bit in the input and verify that the signature is now |
| 6655 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6656 | * because ECDSA may ignore the last few bits of the input. */ |
| 6657 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6658 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6659 | input_data->x, input_data->len, |
| 6660 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6661 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6662 | } |
| 6663 | |
| 6664 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6665 | /* |
| 6666 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6667 | * thus reset them as required. |
| 6668 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6669 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6670 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6671 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6672 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6673 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6674 | } |
| 6675 | /* END_CASE */ |
| 6676 | |
| 6677 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6678 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6679 | int alg_arg, data_t *hash_data, |
| 6680 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6681 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6682 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6683 | psa_key_type_t key_type = key_type_arg; |
| 6684 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6685 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6686 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6687 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6688 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6689 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6690 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6691 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6692 | psa_set_key_algorithm( &attributes, alg ); |
| 6693 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6694 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6695 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6696 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6697 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6698 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6699 | hash_data->x, hash_data->len, |
| 6700 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6701 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6702 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6703 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6704 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6705 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6706 | } |
| 6707 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6708 | |
| 6709 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6710 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6711 | int alg_arg, data_t *hash_data, |
| 6712 | data_t *signature_data, |
| 6713 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6714 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6715 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6716 | psa_key_type_t key_type = key_type_arg; |
| 6717 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6718 | psa_status_t actual_status; |
| 6719 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6720 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6721 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6722 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6723 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6724 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6725 | psa_set_key_algorithm( &attributes, alg ); |
| 6726 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6727 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6728 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6729 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6730 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6731 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6732 | hash_data->x, hash_data->len, |
| 6733 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6734 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6735 | |
| 6736 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6737 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6738 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6739 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6740 | } |
| 6741 | /* END_CASE */ |
| 6742 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6743 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6744 | void sign_message_deterministic( int key_type_arg, |
| 6745 | data_t *key_data, |
| 6746 | int alg_arg, |
| 6747 | data_t *input_data, |
| 6748 | data_t *output_data ) |
| 6749 | { |
| 6750 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6751 | psa_key_type_t key_type = key_type_arg; |
| 6752 | psa_algorithm_t alg = alg_arg; |
| 6753 | size_t key_bits; |
| 6754 | unsigned char *signature = NULL; |
| 6755 | size_t signature_size; |
| 6756 | size_t signature_length = 0xdeadbeef; |
| 6757 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6758 | |
| 6759 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6760 | |
| 6761 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6762 | psa_set_key_algorithm( &attributes, alg ); |
| 6763 | psa_set_key_type( &attributes, key_type ); |
| 6764 | |
| 6765 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6766 | &key ) ); |
| 6767 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6768 | key_bits = psa_get_key_bits( &attributes ); |
| 6769 | |
| 6770 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6771 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6772 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6773 | ASSERT_ALLOC( signature, signature_size ); |
| 6774 | |
| 6775 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6776 | input_data->x, input_data->len, |
| 6777 | signature, signature_size, |
| 6778 | &signature_length ) ); |
| 6779 | |
| 6780 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6781 | signature, signature_length ); |
| 6782 | |
| 6783 | exit: |
| 6784 | psa_reset_key_attributes( &attributes ); |
| 6785 | |
| 6786 | psa_destroy_key( key ); |
| 6787 | mbedtls_free( signature ); |
| 6788 | PSA_DONE( ); |
| 6789 | |
| 6790 | } |
| 6791 | /* END_CASE */ |
| 6792 | |
| 6793 | /* BEGIN_CASE */ |
| 6794 | void sign_message_fail( int key_type_arg, |
| 6795 | data_t *key_data, |
| 6796 | int alg_arg, |
| 6797 | data_t *input_data, |
| 6798 | int signature_size_arg, |
| 6799 | int expected_status_arg ) |
| 6800 | { |
| 6801 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6802 | psa_key_type_t key_type = key_type_arg; |
| 6803 | psa_algorithm_t alg = alg_arg; |
| 6804 | size_t signature_size = signature_size_arg; |
| 6805 | psa_status_t actual_status; |
| 6806 | psa_status_t expected_status = expected_status_arg; |
| 6807 | unsigned char *signature = NULL; |
| 6808 | size_t signature_length = 0xdeadbeef; |
| 6809 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6810 | |
| 6811 | ASSERT_ALLOC( signature, signature_size ); |
| 6812 | |
| 6813 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6814 | |
| 6815 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6816 | psa_set_key_algorithm( &attributes, alg ); |
| 6817 | psa_set_key_type( &attributes, key_type ); |
| 6818 | |
| 6819 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6820 | &key ) ); |
| 6821 | |
| 6822 | actual_status = psa_sign_message( key, alg, |
| 6823 | input_data->x, input_data->len, |
| 6824 | signature, signature_size, |
| 6825 | &signature_length ); |
| 6826 | TEST_EQUAL( actual_status, expected_status ); |
| 6827 | /* The value of *signature_length is unspecified on error, but |
| 6828 | * whatever it is, it should be less than signature_size, so that |
| 6829 | * if the caller tries to read *signature_length bytes without |
| 6830 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6831 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6832 | |
| 6833 | exit: |
| 6834 | psa_reset_key_attributes( &attributes ); |
| 6835 | psa_destroy_key( key ); |
| 6836 | mbedtls_free( signature ); |
| 6837 | PSA_DONE( ); |
| 6838 | } |
| 6839 | /* END_CASE */ |
| 6840 | |
| 6841 | /* BEGIN_CASE */ |
| 6842 | void sign_verify_message( int key_type_arg, |
| 6843 | data_t *key_data, |
| 6844 | int alg_arg, |
| 6845 | data_t *input_data ) |
| 6846 | { |
| 6847 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6848 | psa_key_type_t key_type = key_type_arg; |
| 6849 | psa_algorithm_t alg = alg_arg; |
| 6850 | size_t key_bits; |
| 6851 | unsigned char *signature = NULL; |
| 6852 | size_t signature_size; |
| 6853 | size_t signature_length = 0xdeadbeef; |
| 6854 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6855 | |
| 6856 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6857 | |
| 6858 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6859 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6860 | psa_set_key_algorithm( &attributes, alg ); |
| 6861 | psa_set_key_type( &attributes, key_type ); |
| 6862 | |
| 6863 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6864 | &key ) ); |
| 6865 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6866 | key_bits = psa_get_key_bits( &attributes ); |
| 6867 | |
| 6868 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6869 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6870 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6871 | ASSERT_ALLOC( signature, signature_size ); |
| 6872 | |
| 6873 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6874 | input_data->x, input_data->len, |
| 6875 | signature, signature_size, |
| 6876 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6877 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6878 | TEST_ASSERT( signature_length > 0 ); |
| 6879 | |
| 6880 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6881 | input_data->x, input_data->len, |
| 6882 | signature, signature_length ) ); |
| 6883 | |
| 6884 | if( input_data->len != 0 ) |
| 6885 | { |
| 6886 | /* Flip a bit in the input and verify that the signature is now |
| 6887 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6888 | * because ECDSA may ignore the last few bits of the input. */ |
| 6889 | input_data->x[0] ^= 1; |
| 6890 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6891 | input_data->x, input_data->len, |
| 6892 | signature, signature_length ), |
| 6893 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6894 | } |
| 6895 | |
| 6896 | exit: |
| 6897 | psa_reset_key_attributes( &attributes ); |
| 6898 | |
| 6899 | psa_destroy_key( key ); |
| 6900 | mbedtls_free( signature ); |
| 6901 | PSA_DONE( ); |
| 6902 | } |
| 6903 | /* END_CASE */ |
| 6904 | |
| 6905 | /* BEGIN_CASE */ |
| 6906 | void verify_message( int key_type_arg, |
| 6907 | data_t *key_data, |
| 6908 | int alg_arg, |
| 6909 | data_t *input_data, |
| 6910 | data_t *signature_data ) |
| 6911 | { |
| 6912 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6913 | psa_key_type_t key_type = key_type_arg; |
| 6914 | psa_algorithm_t alg = alg_arg; |
| 6915 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6916 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6917 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6918 | |
| 6919 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6920 | |
| 6921 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6922 | psa_set_key_algorithm( &attributes, alg ); |
| 6923 | psa_set_key_type( &attributes, key_type ); |
| 6924 | |
| 6925 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6926 | &key ) ); |
| 6927 | |
| 6928 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6929 | input_data->x, input_data->len, |
| 6930 | signature_data->x, signature_data->len ) ); |
| 6931 | |
| 6932 | exit: |
| 6933 | psa_reset_key_attributes( &attributes ); |
| 6934 | psa_destroy_key( key ); |
| 6935 | PSA_DONE( ); |
| 6936 | } |
| 6937 | /* END_CASE */ |
| 6938 | |
| 6939 | /* BEGIN_CASE */ |
| 6940 | void verify_message_fail( int key_type_arg, |
| 6941 | data_t *key_data, |
| 6942 | int alg_arg, |
| 6943 | data_t *hash_data, |
| 6944 | data_t *signature_data, |
| 6945 | int expected_status_arg ) |
| 6946 | { |
| 6947 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6948 | psa_key_type_t key_type = key_type_arg; |
| 6949 | psa_algorithm_t alg = alg_arg; |
| 6950 | psa_status_t actual_status; |
| 6951 | psa_status_t expected_status = expected_status_arg; |
| 6952 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6953 | |
| 6954 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6955 | |
| 6956 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6957 | psa_set_key_algorithm( &attributes, alg ); |
| 6958 | psa_set_key_type( &attributes, key_type ); |
| 6959 | |
| 6960 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6961 | &key ) ); |
| 6962 | |
| 6963 | actual_status = psa_verify_message( key, alg, |
| 6964 | hash_data->x, hash_data->len, |
| 6965 | signature_data->x, |
| 6966 | signature_data->len ); |
| 6967 | TEST_EQUAL( actual_status, expected_status ); |
| 6968 | |
| 6969 | exit: |
| 6970 | psa_reset_key_attributes( &attributes ); |
| 6971 | psa_destroy_key( key ); |
| 6972 | PSA_DONE( ); |
| 6973 | } |
| 6974 | /* END_CASE */ |
| 6975 | |
| 6976 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6977 | void asymmetric_encrypt( int key_type_arg, |
| 6978 | data_t *key_data, |
| 6979 | int alg_arg, |
| 6980 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6981 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6982 | int expected_output_length_arg, |
| 6983 | int expected_status_arg ) |
| 6984 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6985 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6986 | psa_key_type_t key_type = key_type_arg; |
| 6987 | psa_algorithm_t alg = alg_arg; |
| 6988 | size_t expected_output_length = expected_output_length_arg; |
| 6989 | size_t key_bits; |
| 6990 | unsigned char *output = NULL; |
| 6991 | size_t output_size; |
| 6992 | size_t output_length = ~0; |
| 6993 | psa_status_t actual_status; |
| 6994 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6995 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6996 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6997 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 6998 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6999 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7000 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7001 | psa_set_key_algorithm( &attributes, alg ); |
| 7002 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7003 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7004 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7005 | |
| 7006 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7007 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7008 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7009 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7010 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7011 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7012 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7013 | |
| 7014 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7015 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7016 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7017 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7018 | output, output_size, |
| 7019 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7020 | TEST_EQUAL( actual_status, expected_status ); |
| 7021 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7022 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7023 | /* If the label is empty, the test framework puts a non-null pointer |
| 7024 | * in label->x. Test that a null pointer works as well. */ |
| 7025 | if( label->len == 0 ) |
| 7026 | { |
| 7027 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7028 | if( output_size != 0 ) |
| 7029 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7030 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7031 | input_data->x, input_data->len, |
| 7032 | NULL, label->len, |
| 7033 | output, output_size, |
| 7034 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7035 | TEST_EQUAL( actual_status, expected_status ); |
| 7036 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7037 | } |
| 7038 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7039 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7040 | /* |
| 7041 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7042 | * thus reset them as required. |
| 7043 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7044 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7045 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7046 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7047 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7048 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7049 | } |
| 7050 | /* END_CASE */ |
| 7051 | |
| 7052 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7053 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 7054 | data_t *key_data, |
| 7055 | int alg_arg, |
| 7056 | data_t *input_data, |
| 7057 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7058 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7059 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7060 | psa_key_type_t key_type = key_type_arg; |
| 7061 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7062 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7063 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7064 | size_t output_size; |
| 7065 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7066 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7067 | size_t output2_size; |
| 7068 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7069 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7070 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7071 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7072 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7073 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 7074 | psa_set_key_algorithm( &attributes, alg ); |
| 7075 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7076 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7077 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7078 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7079 | |
| 7080 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7081 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7082 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7083 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7084 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7085 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7086 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7087 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7088 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7089 | TEST_LE_U( output2_size, |
| 7090 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 7091 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7092 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7093 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7094 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7095 | * the original plaintext because of the non-optional random |
| 7096 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7097 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7098 | input_data->x, input_data->len, |
| 7099 | label->x, label->len, |
| 7100 | output, output_size, |
| 7101 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7102 | /* We don't know what ciphertext length to expect, but check that |
| 7103 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7104 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7105 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7106 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7107 | output, output_length, |
| 7108 | label->x, label->len, |
| 7109 | output2, output2_size, |
| 7110 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7111 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 7112 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7113 | |
| 7114 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7115 | /* |
| 7116 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7117 | * thus reset them as required. |
| 7118 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7119 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7120 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7121 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7122 | mbedtls_free( output ); |
| 7123 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7124 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7125 | } |
| 7126 | /* END_CASE */ |
| 7127 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7128 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7129 | void asymmetric_decrypt( int key_type_arg, |
| 7130 | data_t *key_data, |
| 7131 | int alg_arg, |
| 7132 | data_t *input_data, |
| 7133 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 7134 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7135 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7136 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7137 | psa_key_type_t key_type = key_type_arg; |
| 7138 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7139 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7140 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7141 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7142 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7143 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7144 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7145 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7146 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7147 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7148 | psa_set_key_algorithm( &attributes, alg ); |
| 7149 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7150 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7151 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7152 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7153 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7154 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 7155 | key_bits = psa_get_key_bits( &attributes ); |
| 7156 | |
| 7157 | /* Determine the maximum ciphertext length */ |
| 7158 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7159 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7160 | ASSERT_ALLOC( output, output_size ); |
| 7161 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7162 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7163 | input_data->x, input_data->len, |
| 7164 | label->x, label->len, |
| 7165 | output, |
| 7166 | output_size, |
| 7167 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7168 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7169 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7170 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7171 | /* If the label is empty, the test framework puts a non-null pointer |
| 7172 | * in label->x. Test that a null pointer works as well. */ |
| 7173 | if( label->len == 0 ) |
| 7174 | { |
| 7175 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7176 | if( output_size != 0 ) |
| 7177 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7178 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7179 | input_data->x, input_data->len, |
| 7180 | NULL, label->len, |
| 7181 | output, |
| 7182 | output_size, |
| 7183 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7184 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7185 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7186 | } |
| 7187 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7188 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7189 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7190 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7191 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7192 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7193 | } |
| 7194 | /* END_CASE */ |
| 7195 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7196 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7197 | void asymmetric_decrypt_fail( int key_type_arg, |
| 7198 | data_t *key_data, |
| 7199 | int alg_arg, |
| 7200 | data_t *input_data, |
| 7201 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7202 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7203 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7204 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7205 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7206 | psa_key_type_t key_type = key_type_arg; |
| 7207 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7208 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7209 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7210 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7211 | psa_status_t actual_status; |
| 7212 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7213 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7214 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7215 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7216 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7217 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7218 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7219 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7220 | psa_set_key_algorithm( &attributes, alg ); |
| 7221 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7222 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7223 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7224 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7225 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7226 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7227 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7228 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7229 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7230 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7231 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7232 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7233 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7234 | /* If the label is empty, the test framework puts a non-null pointer |
| 7235 | * in label->x. Test that a null pointer works as well. */ |
| 7236 | if( label->len == 0 ) |
| 7237 | { |
| 7238 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7239 | if( output_size != 0 ) |
| 7240 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7241 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7242 | input_data->x, input_data->len, |
| 7243 | NULL, label->len, |
| 7244 | output, output_size, |
| 7245 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7246 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7247 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7248 | } |
| 7249 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7250 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7251 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7252 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7253 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7254 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7255 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7256 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7257 | |
| 7258 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 7259 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7260 | { |
| 7261 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7262 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7263 | * 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] | 7264 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7265 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7266 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 7267 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7268 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7269 | |
| 7270 | memset( &zero, 0, sizeof( zero ) ); |
| 7271 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7272 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7273 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7274 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7275 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7276 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7277 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7278 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7279 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7280 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7281 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7282 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7283 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7284 | } |
| 7285 | /* END_CASE */ |
| 7286 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7287 | /* BEGIN_CASE */ |
| 7288 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7289 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7290 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7291 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7292 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7293 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7294 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7295 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7296 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7297 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7298 | |
| 7299 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7300 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7301 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7302 | } |
| 7303 | /* END_CASE */ |
| 7304 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7305 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7306 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7307 | int expected_status_arg ) |
| 7308 | { |
| 7309 | psa_algorithm_t alg = alg_arg; |
| 7310 | size_t capacity = capacity_arg; |
| 7311 | psa_status_t expected_status = expected_status_arg; |
| 7312 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7313 | |
| 7314 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7315 | |
| 7316 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7317 | |
| 7318 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7319 | expected_status ); |
| 7320 | |
| 7321 | exit: |
| 7322 | psa_key_derivation_abort( &operation ); |
| 7323 | PSA_DONE( ); |
| 7324 | } |
| 7325 | /* END_CASE */ |
| 7326 | |
| 7327 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7328 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7329 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7330 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7331 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7332 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7333 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7334 | int expected_status_arg3, |
| 7335 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7336 | { |
| 7337 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7338 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7339 | 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] | 7340 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7341 | expected_status_arg2, |
| 7342 | expected_status_arg3}; |
| 7343 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7344 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7345 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7346 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7347 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7348 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7349 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7350 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7351 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7352 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7353 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7354 | |
| 7355 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7356 | |
| 7357 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7358 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7359 | |
| 7360 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7361 | |
| 7362 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7363 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7364 | mbedtls_test_set_step( i ); |
| 7365 | if( steps[i] == 0 ) |
| 7366 | { |
| 7367 | /* Skip this step */ |
| 7368 | } |
| 7369 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7370 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7371 | psa_set_key_type( &attributes, key_types[i] ); |
| 7372 | PSA_ASSERT( psa_import_key( &attributes, |
| 7373 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7374 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7375 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7376 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7377 | { |
| 7378 | // When taking a private key as secret input, use key agreement |
| 7379 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7380 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7381 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7382 | expected_statuses[i] ); |
| 7383 | } |
| 7384 | else |
| 7385 | { |
| 7386 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7387 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7388 | expected_statuses[i] ); |
| 7389 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7390 | } |
| 7391 | else |
| 7392 | { |
| 7393 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7394 | &operation, steps[i], |
| 7395 | inputs[i]->x, inputs[i]->len ), |
| 7396 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7397 | } |
| 7398 | } |
| 7399 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7400 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7401 | { |
| 7402 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7403 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7404 | psa_set_key_bits( &attributes, 8 ); |
| 7405 | actual_output_status = |
| 7406 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7407 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7408 | } |
| 7409 | else |
| 7410 | { |
| 7411 | uint8_t buffer[1]; |
| 7412 | actual_output_status = |
| 7413 | psa_key_derivation_output_bytes( &operation, |
| 7414 | buffer, sizeof( buffer ) ); |
| 7415 | } |
| 7416 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7417 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7418 | exit: |
| 7419 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7420 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7421 | psa_destroy_key( keys[i] ); |
| 7422 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7423 | PSA_DONE( ); |
| 7424 | } |
| 7425 | /* END_CASE */ |
| 7426 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7427 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7428 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7429 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7430 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7431 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7432 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7433 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7434 | unsigned char input1[] = "Input 1"; |
| 7435 | size_t input1_length = sizeof( input1 ); |
| 7436 | unsigned char input2[] = "Input 2"; |
| 7437 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7438 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7439 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7440 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7441 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7442 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7443 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7444 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7445 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7446 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7447 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7448 | psa_set_key_algorithm( &attributes, alg ); |
| 7449 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7450 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7451 | PSA_ASSERT( psa_import_key( &attributes, |
| 7452 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7453 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7454 | |
| 7455 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7456 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7457 | input1, input1_length, |
| 7458 | input2, input2_length, |
| 7459 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7460 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7461 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7462 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7463 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7464 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7465 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7466 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7467 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7468 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7469 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7470 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7471 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7472 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7473 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7474 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7475 | } |
| 7476 | /* END_CASE */ |
| 7477 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7478 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7479 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7480 | { |
| 7481 | uint8_t output_buffer[16]; |
| 7482 | size_t buffer_size = 16; |
| 7483 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7484 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7485 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7486 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7487 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7488 | == PSA_ERROR_BAD_STATE ); |
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 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7491 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7492 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7493 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7494 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7495 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7496 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7497 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7498 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7499 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7500 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7501 | |
| 7502 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7503 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7504 | } |
| 7505 | /* END_CASE */ |
| 7506 | |
| 7507 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7508 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7509 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7510 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7511 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7512 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7513 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7514 | int requested_capacity_arg, |
| 7515 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7516 | data_t *expected_output2, |
| 7517 | int other_key_input_type, |
| 7518 | int key_input_type, |
| 7519 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7520 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7521 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7522 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7523 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7524 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7525 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7526 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7527 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7528 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7529 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7530 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7531 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7532 | uint8_t *expected_outputs[2] = |
| 7533 | {expected_output1->x, expected_output2->x}; |
| 7534 | size_t output_sizes[2] = |
| 7535 | {expected_output1->len, expected_output2->len}; |
| 7536 | size_t output_buffer_size = 0; |
| 7537 | uint8_t *output_buffer = NULL; |
| 7538 | size_t expected_capacity; |
| 7539 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7540 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7541 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7542 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7543 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7544 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7545 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7546 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7547 | |
| 7548 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7549 | { |
| 7550 | if( output_sizes[i] > output_buffer_size ) |
| 7551 | output_buffer_size = output_sizes[i]; |
| 7552 | if( output_sizes[i] == 0 ) |
| 7553 | expected_outputs[i] = NULL; |
| 7554 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7555 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7556 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7557 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7558 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7559 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7560 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7561 | requested_capacity ) ); |
| 7562 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7563 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7564 | switch( steps[i] ) |
| 7565 | { |
| 7566 | case 0: |
| 7567 | break; |
| 7568 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7569 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7570 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7571 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7572 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7573 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7574 | inputs[i]->x, inputs[i]->len ), |
| 7575 | statuses[i] ); |
| 7576 | |
| 7577 | if( statuses[i] != PSA_SUCCESS ) |
| 7578 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7579 | break; |
| 7580 | case 1: // input key |
| 7581 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7582 | psa_set_key_algorithm( &attributes1, alg ); |
| 7583 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7584 | |
| 7585 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7586 | inputs[i]->x, inputs[i]->len, |
| 7587 | &keys[i] ) ); |
| 7588 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7589 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7590 | { |
| 7591 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7592 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7593 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7594 | } |
| 7595 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7596 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7597 | steps[i], |
| 7598 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7599 | break; |
| 7600 | default: |
| 7601 | TEST_ASSERT( ! "default case not supported" ); |
| 7602 | break; |
| 7603 | } |
| 7604 | break; |
| 7605 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7606 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7607 | { |
| 7608 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7609 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7610 | steps[i], |
| 7611 | inputs[i]->x, |
| 7612 | inputs[i]->len ), |
| 7613 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7614 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7615 | case 1: // input key, type DERIVE |
| 7616 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7617 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7618 | psa_set_key_algorithm( &attributes2, alg ); |
| 7619 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7620 | |
| 7621 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7622 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7623 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7624 | |
| 7625 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7626 | inputs[i]->x, inputs[i]->len, |
| 7627 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7628 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7629 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7630 | steps[i], |
| 7631 | keys[i] ), |
| 7632 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7633 | break; |
| 7634 | case 2: // key agreement |
| 7635 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7636 | psa_set_key_algorithm( &attributes3, alg ); |
| 7637 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7638 | |
| 7639 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7640 | inputs[i]->x, inputs[i]->len, |
| 7641 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7642 | |
| 7643 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7644 | &operation, |
| 7645 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7646 | keys[i], key_agreement_peer_key->x, |
| 7647 | key_agreement_peer_key->len ), statuses[i] ); |
| 7648 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7649 | default: |
| 7650 | TEST_ASSERT( ! "default case not supported" ); |
| 7651 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7652 | } |
| 7653 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7654 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7655 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7656 | break; |
| 7657 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7658 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7659 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7660 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7661 | |
| 7662 | if( statuses[i] != PSA_SUCCESS ) |
| 7663 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7664 | break; |
| 7665 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7666 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7667 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7668 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7669 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7670 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7671 | expected_capacity = requested_capacity; |
| 7672 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7673 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7674 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7675 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7676 | |
| 7677 | /* For output key derivation secret must be provided using |
| 7678 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7679 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7680 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7681 | |
| 7682 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7683 | psa_set_key_algorithm( &attributes4, alg ); |
| 7684 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7685 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7686 | |
| 7687 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7688 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7689 | } |
| 7690 | else // output bytes |
| 7691 | { |
| 7692 | /* Expansion phase. */ |
| 7693 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7694 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7695 | /* Read some bytes. */ |
| 7696 | status = psa_key_derivation_output_bytes( &operation, |
| 7697 | output_buffer, output_sizes[i] ); |
| 7698 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7699 | { |
| 7700 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7701 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7702 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7703 | continue; |
| 7704 | } |
| 7705 | else if( expected_capacity == 0 || |
| 7706 | output_sizes[i] > expected_capacity ) |
| 7707 | { |
| 7708 | /* Capacity exceeded. */ |
| 7709 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7710 | expected_capacity = 0; |
| 7711 | continue; |
| 7712 | } |
| 7713 | /* Success. Check the read data. */ |
| 7714 | PSA_ASSERT( status ); |
| 7715 | if( output_sizes[i] != 0 ) |
| 7716 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7717 | expected_outputs[i], output_sizes[i] ); |
| 7718 | /* Check the operation status. */ |
| 7719 | expected_capacity -= output_sizes[i]; |
| 7720 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7721 | ¤t_capacity ) ); |
| 7722 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7723 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7724 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7725 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7726 | |
| 7727 | exit: |
| 7728 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7729 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7730 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7731 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7732 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7733 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7734 | } |
| 7735 | /* END_CASE */ |
| 7736 | |
| 7737 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7738 | void derive_full( int alg_arg, |
| 7739 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7740 | data_t *input1, |
| 7741 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7742 | int requested_capacity_arg ) |
| 7743 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7744 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7745 | psa_algorithm_t alg = alg_arg; |
| 7746 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7747 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7748 | unsigned char output_buffer[16]; |
| 7749 | size_t expected_capacity = requested_capacity; |
| 7750 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7751 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7752 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7753 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7754 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7755 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7756 | psa_set_key_algorithm( &attributes, alg ); |
| 7757 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7758 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7759 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7760 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7761 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7762 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7763 | input1->x, input1->len, |
| 7764 | input2->x, input2->len, |
| 7765 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7766 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7767 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7768 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7769 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7770 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7771 | |
| 7772 | /* Expansion phase. */ |
| 7773 | while( current_capacity > 0 ) |
| 7774 | { |
| 7775 | size_t read_size = sizeof( output_buffer ); |
| 7776 | if( read_size > current_capacity ) |
| 7777 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7778 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7779 | output_buffer, |
| 7780 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7781 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7782 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7783 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7784 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7785 | } |
| 7786 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7787 | /* Check that the operation refuses to go over capacity. */ |
| 7788 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7789 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7790 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7791 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7792 | |
| 7793 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7794 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7795 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7796 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7797 | } |
| 7798 | /* END_CASE */ |
| 7799 | |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7800 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS:MBEDTLS_SHA256_C */ |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7801 | 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] | 7802 | int derivation_step, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7803 | int capacity, int expected_capacity_status_arg, |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7804 | data_t *expected_output, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7805 | int expected_output_status_arg ) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7806 | { |
| 7807 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7808 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 7809 | 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] | 7810 | uint8_t *output_buffer = NULL; |
| 7811 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7812 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 7813 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 7814 | 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] | 7815 | |
| 7816 | ASSERT_ALLOC( output_buffer, expected_output->len ); |
| 7817 | PSA_ASSERT( psa_crypto_init() ); |
| 7818 | |
| 7819 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7820 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7821 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7822 | |
| 7823 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7824 | step, input->x, input->len ), |
| 7825 | expected_input_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7826 | |
| 7827 | if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS ) |
| 7828 | goto exit; |
| 7829 | |
| 7830 | status = psa_key_derivation_output_bytes( &operation, output_buffer, |
| 7831 | expected_output->len ); |
| 7832 | |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7833 | TEST_EQUAL( status, expected_output_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7834 | if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS ) |
| 7835 | ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x, |
| 7836 | expected_output->len ); |
| 7837 | |
| 7838 | exit: |
| 7839 | mbedtls_free( output_buffer ); |
| 7840 | psa_key_derivation_abort( &operation ); |
| 7841 | PSA_DONE(); |
| 7842 | } |
| 7843 | /* END_CASE */ |
| 7844 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7845 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7846 | void derive_key_exercise( int alg_arg, |
| 7847 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7848 | data_t *input1, |
| 7849 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7850 | int derived_type_arg, |
| 7851 | int derived_bits_arg, |
| 7852 | int derived_usage_arg, |
| 7853 | int derived_alg_arg ) |
| 7854 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7855 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7856 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7857 | psa_algorithm_t alg = alg_arg; |
| 7858 | psa_key_type_t derived_type = derived_type_arg; |
| 7859 | size_t derived_bits = derived_bits_arg; |
| 7860 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7861 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7862 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7863 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7864 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7865 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7866 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7867 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7868 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7869 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7870 | psa_set_key_algorithm( &attributes, alg ); |
| 7871 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7872 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7873 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7874 | |
| 7875 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7876 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7877 | input1->x, input1->len, |
| 7878 | input2->x, input2->len, |
| 7879 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7880 | goto exit; |
| 7881 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7882 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7883 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7884 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7885 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7886 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7887 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7888 | |
| 7889 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7890 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7891 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7892 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7893 | |
| 7894 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7895 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7896 | goto exit; |
| 7897 | |
| 7898 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7899 | /* |
| 7900 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7901 | * thus reset them as required. |
| 7902 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7903 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7904 | |
| 7905 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7906 | psa_destroy_key( base_key ); |
| 7907 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7908 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7909 | } |
| 7910 | /* END_CASE */ |
| 7911 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7912 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7913 | void derive_key_export( int alg_arg, |
| 7914 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7915 | data_t *input1, |
| 7916 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7917 | int bytes1_arg, |
| 7918 | int bytes2_arg ) |
| 7919 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7920 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7921 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7922 | psa_algorithm_t alg = alg_arg; |
| 7923 | size_t bytes1 = bytes1_arg; |
| 7924 | size_t bytes2 = bytes2_arg; |
| 7925 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7926 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7927 | uint8_t *output_buffer = NULL; |
| 7928 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7929 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7930 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7931 | size_t length; |
| 7932 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7933 | ASSERT_ALLOC( output_buffer, capacity ); |
| 7934 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7935 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7936 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7937 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7938 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7939 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7940 | 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] | 7941 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7942 | |
| 7943 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7944 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7945 | input1->x, input1->len, |
| 7946 | input2->x, input2->len, |
| 7947 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7948 | goto exit; |
| 7949 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7950 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7951 | output_buffer, |
| 7952 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7953 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7954 | |
| 7955 | /* 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] | 7956 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7957 | input1->x, input1->len, |
| 7958 | input2->x, input2->len, |
| 7959 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7960 | goto exit; |
| 7961 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7962 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7963 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7964 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7965 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7966 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7967 | &derived_key ) ); |
| 7968 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7969 | export_buffer, bytes1, |
| 7970 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7971 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7972 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7973 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7974 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7975 | &derived_key ) ); |
| 7976 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7977 | export_buffer + bytes1, bytes2, |
| 7978 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7979 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7980 | |
| 7981 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 7982 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 7983 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7984 | |
| 7985 | exit: |
| 7986 | mbedtls_free( output_buffer ); |
| 7987 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7988 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7989 | psa_destroy_key( base_key ); |
| 7990 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7991 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7992 | } |
| 7993 | /* END_CASE */ |
| 7994 | |
| 7995 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7996 | void derive_key_type( int alg_arg, |
| 7997 | data_t *key_data, |
| 7998 | data_t *input1, |
| 7999 | data_t *input2, |
| 8000 | int key_type_arg, int bits_arg, |
| 8001 | data_t *expected_export ) |
| 8002 | { |
| 8003 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8004 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8005 | const psa_algorithm_t alg = alg_arg; |
| 8006 | const psa_key_type_t key_type = key_type_arg; |
| 8007 | const size_t bits = bits_arg; |
| 8008 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8009 | const size_t export_buffer_size = |
| 8010 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 8011 | uint8_t *export_buffer = NULL; |
| 8012 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8013 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8014 | size_t export_length; |
| 8015 | |
| 8016 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 8017 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8018 | |
| 8019 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8020 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8021 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8022 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 8023 | &base_key ) ); |
| 8024 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8025 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8026 | &operation, base_key, alg, |
| 8027 | input1->x, input1->len, |
| 8028 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8029 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8030 | goto exit; |
| 8031 | |
| 8032 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8033 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8034 | psa_set_key_type( &derived_attributes, key_type ); |
| 8035 | psa_set_key_bits( &derived_attributes, bits ); |
| 8036 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 8037 | &derived_key ) ); |
| 8038 | |
| 8039 | PSA_ASSERT( psa_export_key( derived_key, |
| 8040 | export_buffer, export_buffer_size, |
| 8041 | &export_length ) ); |
| 8042 | ASSERT_COMPARE( export_buffer, export_length, |
| 8043 | expected_export->x, expected_export->len ); |
| 8044 | |
| 8045 | exit: |
| 8046 | mbedtls_free( export_buffer ); |
| 8047 | psa_key_derivation_abort( &operation ); |
| 8048 | psa_destroy_key( base_key ); |
| 8049 | psa_destroy_key( derived_key ); |
| 8050 | PSA_DONE( ); |
| 8051 | } |
| 8052 | /* END_CASE */ |
| 8053 | |
| 8054 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8055 | void derive_key( int alg_arg, |
| 8056 | data_t *key_data, data_t *input1, data_t *input2, |
| 8057 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8058 | int expected_status_arg, |
| 8059 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8060 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8061 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8062 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8063 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8064 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8065 | size_t bits = bits_arg; |
| 8066 | psa_status_t expected_status = expected_status_arg; |
| 8067 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8068 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8069 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8070 | |
| 8071 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8072 | |
| 8073 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8074 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8075 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8076 | 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] | 8077 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8078 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8079 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8080 | input1->x, input1->len, |
| 8081 | input2->x, input2->len, |
| 8082 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8083 | goto exit; |
| 8084 | |
| 8085 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8086 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8087 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8088 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8089 | |
| 8090 | psa_status_t status = |
| 8091 | psa_key_derivation_output_key( &derived_attributes, |
| 8092 | &operation, |
| 8093 | &derived_key ); |
| 8094 | if( is_large_output > 0 ) |
| 8095 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8096 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8097 | |
| 8098 | exit: |
| 8099 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8100 | psa_destroy_key( base_key ); |
| 8101 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8102 | PSA_DONE( ); |
| 8103 | } |
| 8104 | /* END_CASE */ |
| 8105 | |
| 8106 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8107 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 8108 | int our_key_type_arg, int our_key_alg_arg, |
| 8109 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8110 | int expected_status_arg ) |
| 8111 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8112 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8113 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8114 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8115 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8116 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8117 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8118 | psa_status_t expected_status = expected_status_arg; |
| 8119 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8120 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8121 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8122 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8123 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8124 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8125 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8126 | PSA_ASSERT( psa_import_key( &attributes, |
| 8127 | our_key_data->x, our_key_data->len, |
| 8128 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8129 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8130 | /* The tests currently include inputs that should fail at either step. |
| 8131 | * Test cases that fail at the setup step should be changed to call |
| 8132 | * key_derivation_setup instead, and this function should be renamed |
| 8133 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8134 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8135 | if( status == PSA_SUCCESS ) |
| 8136 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8137 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 8138 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8139 | our_key, |
| 8140 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8141 | expected_status ); |
| 8142 | } |
| 8143 | else |
| 8144 | { |
| 8145 | TEST_ASSERT( status == expected_status ); |
| 8146 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8147 | |
| 8148 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8149 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8150 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8151 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8152 | } |
| 8153 | /* END_CASE */ |
| 8154 | |
| 8155 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8156 | void raw_key_agreement( int alg_arg, |
| 8157 | int our_key_type_arg, data_t *our_key_data, |
| 8158 | data_t *peer_key_data, |
| 8159 | data_t *expected_output ) |
| 8160 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8161 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8162 | psa_algorithm_t alg = alg_arg; |
| 8163 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8164 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8165 | unsigned char *output = NULL; |
| 8166 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8167 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8168 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8169 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8170 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8171 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8172 | psa_set_key_algorithm( &attributes, alg ); |
| 8173 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8174 | PSA_ASSERT( psa_import_key( &attributes, |
| 8175 | our_key_data->x, our_key_data->len, |
| 8176 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8177 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8178 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 8179 | key_bits = psa_get_key_bits( &attributes ); |
| 8180 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8181 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8182 | TEST_LE_U( expected_output->len, |
| 8183 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 8184 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 8185 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8186 | |
| 8187 | /* Good case with exact output size */ |
| 8188 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 8189 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8190 | peer_key_data->x, peer_key_data->len, |
| 8191 | output, expected_output->len, |
| 8192 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8193 | ASSERT_COMPARE( output, output_length, |
| 8194 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8195 | mbedtls_free( output ); |
| 8196 | output = NULL; |
| 8197 | output_length = ~0; |
| 8198 | |
| 8199 | /* Larger buffer */ |
| 8200 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 8201 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8202 | peer_key_data->x, peer_key_data->len, |
| 8203 | output, expected_output->len + 1, |
| 8204 | &output_length ) ); |
| 8205 | ASSERT_COMPARE( output, output_length, |
| 8206 | expected_output->x, expected_output->len ); |
| 8207 | mbedtls_free( output ); |
| 8208 | output = NULL; |
| 8209 | output_length = ~0; |
| 8210 | |
| 8211 | /* Buffer too small */ |
| 8212 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 8213 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 8214 | peer_key_data->x, peer_key_data->len, |
| 8215 | output, expected_output->len - 1, |
| 8216 | &output_length ), |
| 8217 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8218 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8219 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8220 | mbedtls_free( output ); |
| 8221 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8222 | |
| 8223 | exit: |
| 8224 | mbedtls_free( output ); |
| 8225 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8226 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8227 | } |
| 8228 | /* END_CASE */ |
| 8229 | |
| 8230 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8231 | void key_agreement_capacity( int alg_arg, |
| 8232 | int our_key_type_arg, data_t *our_key_data, |
| 8233 | data_t *peer_key_data, |
| 8234 | int expected_capacity_arg ) |
| 8235 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8236 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8237 | psa_algorithm_t alg = alg_arg; |
| 8238 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8239 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8240 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8241 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8242 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8243 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8244 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8245 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8246 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8247 | psa_set_key_algorithm( &attributes, alg ); |
| 8248 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8249 | PSA_ASSERT( psa_import_key( &attributes, |
| 8250 | our_key_data->x, our_key_data->len, |
| 8251 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8252 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8253 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8254 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8255 | &operation, |
| 8256 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8257 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8258 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8259 | { |
| 8260 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8261 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8262 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8263 | NULL, 0 ) ); |
| 8264 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8265 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8266 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8267 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8268 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8269 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8270 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8271 | /* Test the actual capacity by reading the output. */ |
| 8272 | while( actual_capacity > sizeof( output ) ) |
| 8273 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8274 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8275 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8276 | actual_capacity -= sizeof( output ); |
| 8277 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8278 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8279 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8280 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 8281 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8282 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8283 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8284 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8285 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8286 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8287 | } |
| 8288 | /* END_CASE */ |
| 8289 | |
| 8290 | /* BEGIN_CASE */ |
| 8291 | void key_agreement_output( int alg_arg, |
| 8292 | int our_key_type_arg, data_t *our_key_data, |
| 8293 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8294 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8295 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8296 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8297 | psa_algorithm_t alg = alg_arg; |
| 8298 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8299 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8300 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8301 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8302 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8303 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 8304 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8305 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8306 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8307 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8308 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8309 | psa_set_key_algorithm( &attributes, alg ); |
| 8310 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8311 | PSA_ASSERT( psa_import_key( &attributes, |
| 8312 | our_key_data->x, our_key_data->len, |
| 8313 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8314 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8315 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8316 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8317 | &operation, |
| 8318 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8319 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8320 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8321 | { |
| 8322 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8323 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8324 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8325 | NULL, 0 ) ); |
| 8326 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8327 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8328 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8329 | actual_output, |
| 8330 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8331 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8332 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8333 | if( expected_output2->len != 0 ) |
| 8334 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8335 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8336 | actual_output, |
| 8337 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8338 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8339 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8340 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8341 | |
| 8342 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8343 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8344 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8345 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8346 | mbedtls_free( actual_output ); |
| 8347 | } |
| 8348 | /* END_CASE */ |
| 8349 | |
| 8350 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8351 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8352 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8353 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8354 | unsigned char *output = NULL; |
| 8355 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8356 | size_t i; |
| 8357 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8358 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8359 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8360 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8361 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8362 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8363 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8364 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8365 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8366 | /* Run several times, to ensure that every output byte will be |
| 8367 | * nonzero at least once with overwhelming probability |
| 8368 | * (2^(-8*number_of_runs)). */ |
| 8369 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8370 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8371 | if( bytes != 0 ) |
| 8372 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8373 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8374 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8375 | for( i = 0; i < bytes; i++ ) |
| 8376 | { |
| 8377 | if( output[i] != 0 ) |
| 8378 | ++changed[i]; |
| 8379 | } |
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 | |
| 8382 | /* Check that every byte was changed to nonzero at least once. This |
| 8383 | * validates that psa_generate_random is overwriting every byte of |
| 8384 | * the output buffer. */ |
| 8385 | for( i = 0; i < bytes; i++ ) |
| 8386 | { |
| 8387 | TEST_ASSERT( changed[i] != 0 ); |
| 8388 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8389 | |
| 8390 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8391 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8392 | mbedtls_free( output ); |
| 8393 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8394 | } |
| 8395 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8396 | |
| 8397 | /* BEGIN_CASE */ |
| 8398 | void generate_key( int type_arg, |
| 8399 | int bits_arg, |
| 8400 | int usage_arg, |
| 8401 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8402 | int expected_status_arg, |
| 8403 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8404 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8405 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8406 | psa_key_type_t type = type_arg; |
| 8407 | psa_key_usage_t usage = usage_arg; |
| 8408 | size_t bits = bits_arg; |
| 8409 | psa_algorithm_t alg = alg_arg; |
| 8410 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8411 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8412 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8413 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8414 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8415 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8416 | psa_set_key_usage_flags( &attributes, usage ); |
| 8417 | psa_set_key_algorithm( &attributes, alg ); |
| 8418 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8419 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8420 | |
| 8421 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8422 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8423 | |
| 8424 | if( is_large_key > 0 ) |
| 8425 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8426 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8427 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8428 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8429 | |
| 8430 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8431 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8432 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8433 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8434 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8435 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8436 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8437 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8438 | |
| 8439 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8440 | /* |
| 8441 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8442 | * thus reset them as required. |
| 8443 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8444 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8445 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8446 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8447 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8448 | } |
| 8449 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8450 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8451 | /* 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] | 8452 | void generate_key_rsa( int bits_arg, |
| 8453 | data_t *e_arg, |
| 8454 | int expected_status_arg ) |
| 8455 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8456 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8457 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8458 | size_t bits = bits_arg; |
| 8459 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8460 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8461 | psa_status_t expected_status = expected_status_arg; |
| 8462 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8463 | uint8_t *exported = NULL; |
| 8464 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8465 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8466 | size_t exported_length = SIZE_MAX; |
| 8467 | uint8_t *e_read_buffer = NULL; |
| 8468 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8469 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8470 | size_t e_read_length = SIZE_MAX; |
| 8471 | |
| 8472 | if( e_arg->len == 0 || |
| 8473 | ( e_arg->len == 3 && |
| 8474 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8475 | { |
| 8476 | is_default_public_exponent = 1; |
| 8477 | e_read_size = 0; |
| 8478 | } |
| 8479 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8480 | ASSERT_ALLOC( exported, exported_size ); |
| 8481 | |
| 8482 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8483 | |
| 8484 | psa_set_key_usage_flags( &attributes, usage ); |
| 8485 | psa_set_key_algorithm( &attributes, alg ); |
| 8486 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8487 | e_arg->x, e_arg->len ) ); |
| 8488 | psa_set_key_bits( &attributes, bits ); |
| 8489 | |
| 8490 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8491 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8492 | if( expected_status != PSA_SUCCESS ) |
| 8493 | goto exit; |
| 8494 | |
| 8495 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8496 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8497 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8498 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8499 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8500 | e_read_buffer, e_read_size, |
| 8501 | &e_read_length ) ); |
| 8502 | if( is_default_public_exponent ) |
| 8503 | TEST_EQUAL( e_read_length, 0 ); |
| 8504 | else |
| 8505 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8506 | |
| 8507 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8508 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8509 | goto exit; |
| 8510 | |
| 8511 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8512 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8513 | exported, exported_size, |
| 8514 | &exported_length ) ); |
| 8515 | { |
| 8516 | uint8_t *p = exported; |
| 8517 | uint8_t *end = exported + exported_length; |
| 8518 | size_t len; |
| 8519 | /* RSAPublicKey ::= SEQUENCE { |
| 8520 | * modulus INTEGER, -- n |
| 8521 | * publicExponent INTEGER } -- e |
| 8522 | */ |
| 8523 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8524 | MBEDTLS_ASN1_SEQUENCE | |
| 8525 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8526 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8527 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8528 | MBEDTLS_ASN1_INTEGER ) ); |
| 8529 | if( len >= 1 && p[0] == 0 ) |
| 8530 | { |
| 8531 | ++p; |
| 8532 | --len; |
| 8533 | } |
| 8534 | if( e_arg->len == 0 ) |
| 8535 | { |
| 8536 | TEST_EQUAL( len, 3 ); |
| 8537 | TEST_EQUAL( p[0], 1 ); |
| 8538 | TEST_EQUAL( p[1], 0 ); |
| 8539 | TEST_EQUAL( p[2], 1 ); |
| 8540 | } |
| 8541 | else |
| 8542 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8543 | } |
| 8544 | |
| 8545 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8546 | /* |
| 8547 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8548 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8549 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8550 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8551 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8552 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8553 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8554 | mbedtls_free( e_read_buffer ); |
| 8555 | mbedtls_free( exported ); |
| 8556 | } |
| 8557 | /* END_CASE */ |
| 8558 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8559 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8560 | void persistent_key_load_key_from_storage( data_t *data, |
| 8561 | int type_arg, int bits_arg, |
| 8562 | int usage_flags_arg, int alg_arg, |
| 8563 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8564 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8565 | 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] | 8566 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8567 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8568 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8569 | psa_key_type_t type = type_arg; |
| 8570 | size_t bits = bits_arg; |
| 8571 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8572 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8573 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8574 | unsigned char *first_export = NULL; |
| 8575 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8576 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8577 | size_t first_exported_length; |
| 8578 | size_t second_exported_length; |
| 8579 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8580 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8581 | { |
| 8582 | ASSERT_ALLOC( first_export, export_size ); |
| 8583 | ASSERT_ALLOC( second_export, export_size ); |
| 8584 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8585 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8586 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8587 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8588 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8589 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8590 | psa_set_key_algorithm( &attributes, alg ); |
| 8591 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8592 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8593 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8594 | switch( generation_method ) |
| 8595 | { |
| 8596 | case IMPORT_KEY: |
| 8597 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8598 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8599 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8600 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8601 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8602 | case GENERATE_KEY: |
| 8603 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8604 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8605 | break; |
| 8606 | |
| 8607 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8608 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8609 | { |
| 8610 | /* Create base key */ |
| 8611 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8612 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8613 | psa_set_key_usage_flags( &base_attributes, |
| 8614 | PSA_KEY_USAGE_DERIVE ); |
| 8615 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8616 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8617 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8618 | data->x, data->len, |
| 8619 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8620 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8621 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8622 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8623 | &operation, |
| 8624 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8625 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8626 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8627 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8628 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8629 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8630 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8631 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8632 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8633 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8634 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8635 | #else |
| 8636 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8637 | #endif |
| 8638 | break; |
| 8639 | |
| 8640 | default: |
| 8641 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8642 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8643 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8644 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8645 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8646 | /* Export the key if permitted by the key policy. */ |
| 8647 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8648 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8649 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8650 | first_export, export_size, |
| 8651 | &first_exported_length ) ); |
| 8652 | if( generation_method == IMPORT_KEY ) |
| 8653 | ASSERT_COMPARE( data->x, data->len, |
| 8654 | first_export, first_exported_length ); |
| 8655 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8656 | |
| 8657 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8658 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8659 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8660 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8661 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8662 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8663 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8664 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8665 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8666 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8667 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8668 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8669 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8670 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8671 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8672 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
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 again if permitted by the key policy. */ |
| 8675 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 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 | second_export, export_size, |
| 8679 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8680 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8681 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8682 | } |
| 8683 | |
| 8684 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8685 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8686 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8687 | |
| 8688 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8689 | /* |
| 8690 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8691 | * thus reset them as required. |
| 8692 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8693 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8694 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8695 | mbedtls_free( first_export ); |
| 8696 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8697 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8698 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8699 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8700 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8701 | } |
| 8702 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8703 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8704 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8705 | void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 8706 | int primitive_arg, int hash_arg, int role_arg, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8707 | int input_first, data_t *pw_data, |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8708 | int expected_status_setup_arg, |
| 8709 | int expected_status_set_role_arg, |
| 8710 | int expected_status_set_password_key_arg, |
| 8711 | int expected_status_input_output_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8712 | { |
| 8713 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8714 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 8715 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8716 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 8717 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8718 | psa_algorithm_t hash_alg = hash_arg; |
| 8719 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8720 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8721 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8722 | psa_status_t expected_status_setup = expected_status_setup_arg; |
| 8723 | psa_status_t expected_status_set_role = expected_status_set_role_arg; |
| 8724 | psa_status_t expected_status_set_password_key = |
| 8725 | expected_status_set_password_key_arg; |
| 8726 | psa_status_t expected_status_input_output = |
| 8727 | expected_status_input_output_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8728 | unsigned char *output_buffer = NULL; |
| 8729 | size_t output_len = 0; |
| 8730 | |
| 8731 | PSA_INIT( ); |
| 8732 | |
| 8733 | ASSERT_ALLOC( output_buffer, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8734 | PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8735 | PSA_PAKE_STEP_KEY_SHARE) ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8736 | |
| 8737 | if( pw_data->len > 0 ) |
| 8738 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8739 | psa_set_key_usage_flags( &attributes, key_usage_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8740 | psa_set_key_algorithm( &attributes, alg ); |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8741 | psa_set_key_type( &attributes, key_type_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8742 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8743 | &key ) ); |
| 8744 | } |
| 8745 | |
| 8746 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8747 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8748 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8749 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8750 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8751 | |
| 8752 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8753 | PSA_ERROR_BAD_STATE ); |
| 8754 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8755 | PSA_ERROR_BAD_STATE ); |
| 8756 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8757 | PSA_ERROR_BAD_STATE ); |
| 8758 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8759 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8760 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8761 | NULL, 0, NULL ), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8762 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8763 | 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] | 8764 | PSA_ERROR_BAD_STATE ); |
| 8765 | |
| 8766 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8767 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8768 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8769 | expected_status_setup ); |
| 8770 | if( expected_status_setup != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8771 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8772 | |
Neil Armstrong | 50de0ae | 2022-06-08 17:46:24 +0200 | [diff] [blame] | 8773 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8774 | PSA_ERROR_BAD_STATE ); |
| 8775 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8776 | TEST_EQUAL( psa_pake_set_role( &operation, role), |
| 8777 | expected_status_set_role ); |
| 8778 | if( expected_status_set_role != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8779 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8780 | |
| 8781 | if( pw_data->len > 0 ) |
| 8782 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8783 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8784 | expected_status_set_password_key ); |
| 8785 | if( expected_status_set_password_key != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8786 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8787 | } |
| 8788 | |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8789 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8790 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8791 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8792 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8793 | |
| 8794 | const uint8_t unsupported_id[] = "abcd"; |
| 8795 | |
| 8796 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8797 | PSA_ERROR_NOT_SUPPORTED ); |
| 8798 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8799 | PSA_ERROR_NOT_SUPPORTED ); |
| 8800 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8801 | /* First round */ |
| 8802 | if( input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8803 | { |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8804 | /* Invalid parameters */ |
| 8805 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8806 | NULL, 0 ), |
| 8807 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8808 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8809 | output_buffer, 66 ), |
| 8810 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8811 | /* Invalid first step */ |
| 8812 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8813 | output_buffer, 66 ), |
| 8814 | PSA_ERROR_BAD_STATE ); |
| 8815 | |
| 8816 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8817 | output_buffer, 66 ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8818 | expected_status_input_output); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8819 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8820 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8821 | { |
| 8822 | /* Buffer too large */ |
| 8823 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8824 | output_buffer, 512 ), |
| 8825 | PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8826 | |
| 8827 | /* The operation should be aborted at this point */ |
| 8828 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8829 | output_buffer, 66 ), |
| 8830 | PSA_ERROR_BAD_STATE ); |
| 8831 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8832 | } |
| 8833 | else |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8834 | { |
| 8835 | /* Invalid parameters */ |
| 8836 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8837 | NULL, 0, NULL ), |
| 8838 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8839 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8840 | output_buffer, 512, &output_len ), |
| 8841 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8842 | /* Invalid first step */ |
| 8843 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8844 | output_buffer, 512, &output_len ), |
| 8845 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8846 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8847 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8848 | output_buffer, 512, &output_len ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8849 | expected_status_input_output ); |
Neil Armstrong | 98506ab | 2022-06-08 17:43:20 +0200 | [diff] [blame] | 8850 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8851 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8852 | { |
| 8853 | TEST_ASSERT( output_len > 0 ); |
| 8854 | |
| 8855 | /* Buffer too small */ |
| 8856 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8857 | output_buffer, 5, &output_len ), |
| 8858 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8859 | |
| 8860 | /* The operation should be aborted at this point */ |
| 8861 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8862 | output_buffer, 512, &output_len ), |
| 8863 | PSA_ERROR_BAD_STATE ); |
| 8864 | } |
| 8865 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8866 | |
| 8867 | exit: |
| 8868 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 8869 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8870 | mbedtls_free( output_buffer ); |
| 8871 | PSA_DONE( ); |
| 8872 | } |
| 8873 | /* END_CASE */ |
| 8874 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8875 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8876 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 8877 | int client_input_first, int inject_error, |
| 8878 | data_t *pw_data ) |
| 8879 | { |
| 8880 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8881 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8882 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8883 | psa_algorithm_t alg = alg_arg; |
| 8884 | psa_algorithm_t hash_alg = hash_arg; |
| 8885 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8886 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8887 | |
| 8888 | PSA_INIT( ); |
| 8889 | |
| 8890 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8891 | psa_set_key_algorithm( &attributes, alg ); |
| 8892 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8893 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8894 | &key ) ); |
| 8895 | |
| 8896 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8897 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8898 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8899 | |
| 8900 | |
| 8901 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8902 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8903 | |
| 8904 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8905 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8906 | |
| 8907 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8908 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8909 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8910 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8911 | client_input_first, 1, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8912 | |
| 8913 | if( inject_error == 1 || inject_error == 2 ) |
| 8914 | goto exit; |
| 8915 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8916 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8917 | client_input_first, 2, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8918 | |
| 8919 | exit: |
| 8920 | psa_destroy_key( key ); |
| 8921 | psa_pake_abort( &server ); |
| 8922 | psa_pake_abort( &client ); |
| 8923 | PSA_DONE( ); |
| 8924 | } |
| 8925 | /* END_CASE */ |
| 8926 | |
| 8927 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8928 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8929 | int derive_alg_arg, data_t *pw_data, |
| 8930 | int client_input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8931 | { |
| 8932 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8933 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8934 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8935 | psa_algorithm_t alg = alg_arg; |
| 8936 | psa_algorithm_t hash_alg = hash_arg; |
| 8937 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 8938 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8939 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8940 | psa_key_derivation_operation_t server_derive = |
| 8941 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8942 | psa_key_derivation_operation_t client_derive = |
| 8943 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8944 | |
| 8945 | PSA_INIT( ); |
| 8946 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8947 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8948 | psa_set_key_algorithm( &attributes, alg ); |
| 8949 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8950 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8951 | &key ) ); |
| 8952 | |
| 8953 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8954 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8955 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8956 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8957 | /* Get shared key */ |
| 8958 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 8959 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 8960 | |
| 8961 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 8962 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 8963 | { |
| 8964 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 8965 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8966 | (const uint8_t*) "", 0) ); |
| 8967 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 8968 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8969 | (const uint8_t*) "", 0) ); |
| 8970 | } |
| 8971 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8972 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8973 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8974 | |
| 8975 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8976 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8977 | |
| 8978 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8979 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8980 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8981 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8982 | PSA_ERROR_BAD_STATE ); |
| 8983 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8984 | PSA_ERROR_BAD_STATE ); |
| 8985 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8986 | /* First round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8987 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8988 | client_input_first, 1, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8989 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8990 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8991 | PSA_ERROR_BAD_STATE ); |
| 8992 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8993 | PSA_ERROR_BAD_STATE ); |
| 8994 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8995 | /* Second round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8996 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8997 | client_input_first, 2, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8998 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8999 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 9000 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 9001 | |
| 9002 | exit: |
| 9003 | psa_key_derivation_abort( &server_derive ); |
| 9004 | psa_key_derivation_abort( &client_derive ); |
| 9005 | psa_destroy_key( key ); |
| 9006 | psa_pake_abort( &server ); |
| 9007 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9008 | PSA_DONE( ); |
| 9009 | } |
| 9010 | /* END_CASE */ |