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 | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 709 | static int 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 ) |
| 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; |
| 736 | int ret = 0; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 737 | |
| 738 | ASSERT_ALLOC( buffer0, buffer_length ); |
| 739 | ASSERT_ALLOC( buffer1, buffer_length ); |
| 740 | |
| 741 | switch( round ) |
| 742 | { |
| 743 | case 1: |
| 744 | /* Server first round Output */ |
| 745 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 746 | buffer0 + buffer0_off, |
| 747 | 512 - buffer0_off, &s_g1_len ) ); |
| 748 | s_g1_off = buffer0_off; |
| 749 | buffer0_off += s_g1_len; |
| 750 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 751 | buffer0 + buffer0_off, |
| 752 | 512 - buffer0_off, &s_x1_pk_len ) ); |
| 753 | s_x1_pk_off = buffer0_off; |
| 754 | buffer0_off += s_x1_pk_len; |
| 755 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 756 | buffer0 + buffer0_off, |
| 757 | 512 - buffer0_off, &s_x1_pr_len ) ); |
| 758 | s_x1_pr_off = buffer0_off; |
| 759 | buffer0_off += s_x1_pr_len; |
| 760 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 761 | buffer0 + buffer0_off, |
| 762 | 512 - buffer0_off, &s_g2_len ) ); |
| 763 | s_g2_off = buffer0_off; |
| 764 | buffer0_off += s_g2_len; |
| 765 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 766 | buffer0 + buffer0_off, |
| 767 | 512 - buffer0_off, &s_x2_pk_len ) ); |
| 768 | s_x2_pk_off = buffer0_off; |
| 769 | buffer0_off += s_x2_pk_len; |
| 770 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 771 | buffer0 + buffer0_off, |
| 772 | 512 - buffer0_off, &s_x2_pr_len ) ); |
| 773 | s_x2_pr_off = buffer0_off; |
| 774 | buffer0_off += s_x2_pr_len; |
| 775 | |
| 776 | if( inject_error == 1 ) |
| 777 | { |
| 778 | buffer0[s_x1_pk_off + 12] >>= 4; |
| 779 | buffer0[s_x2_pk_off + 7] <<= 4; |
| 780 | expected_status = PSA_ERROR_DATA_INVALID; |
| 781 | } |
| 782 | |
| 783 | if( client_input_first == 1 ) |
| 784 | { |
| 785 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame^] | 786 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 787 | buffer0 + s_g1_off, s_g1_len ); |
| 788 | if( inject_error == 1 && status != PSA_SUCCESS ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 789 | { |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame^] | 790 | TEST_EQUAL( status, expected_status ); |
| 791 | break; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 792 | } |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame^] | 793 | else |
| 794 | { |
| 795 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 796 | } |
| 797 | |
| 798 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 799 | buffer0 + s_x1_pk_off, |
| 800 | s_x1_pk_len ); |
| 801 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 802 | { |
| 803 | TEST_EQUAL( status, expected_status ); |
| 804 | break; |
| 805 | } |
| 806 | else |
| 807 | { |
| 808 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 809 | } |
| 810 | |
| 811 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 812 | buffer0 + s_x1_pr_off, |
| 813 | s_x1_pr_len ); |
| 814 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 815 | { |
| 816 | TEST_EQUAL( status, expected_status ); |
| 817 | break; |
| 818 | } |
| 819 | else |
| 820 | { |
| 821 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 822 | } |
| 823 | |
| 824 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 825 | buffer0 + s_g2_off, |
| 826 | s_g2_len ); |
| 827 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 828 | { |
| 829 | TEST_EQUAL( status, expected_status ); |
| 830 | break; |
| 831 | } |
| 832 | else |
| 833 | { |
| 834 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 835 | } |
| 836 | |
| 837 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 838 | buffer0 + s_x2_pk_off, |
| 839 | s_x2_pk_len ); |
| 840 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 841 | { |
| 842 | TEST_EQUAL( status, expected_status ); |
| 843 | break; |
| 844 | } |
| 845 | else |
| 846 | { |
| 847 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 848 | } |
| 849 | |
| 850 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 851 | buffer0 + s_x2_pr_off, |
| 852 | s_x2_pr_len ); |
| 853 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 854 | { |
| 855 | TEST_EQUAL( status, expected_status ); |
| 856 | break; |
| 857 | } |
| 858 | else |
| 859 | { |
| 860 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 861 | } |
| 862 | |
| 863 | /* Error didn't trigger, exit with error */ |
| 864 | if( inject_error == 1 ) |
| 865 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /* Client first round Output */ |
| 869 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 870 | buffer1 + buffer1_off, |
| 871 | 512 - buffer1_off, &c_g1_len ) ); |
| 872 | c_g1_off = buffer1_off; |
| 873 | buffer1_off += c_g1_len; |
| 874 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 875 | buffer1 + buffer1_off, |
| 876 | 512 - buffer1_off, &c_x1_pk_len ) ); |
| 877 | c_x1_pk_off = buffer1_off; |
| 878 | buffer1_off += c_x1_pk_len; |
| 879 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 880 | buffer1 + buffer1_off, |
| 881 | 512 - buffer1_off, &c_x1_pr_len ) ); |
| 882 | c_x1_pr_off = buffer1_off; |
| 883 | buffer1_off += c_x1_pr_len; |
| 884 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 885 | buffer1 + buffer1_off, |
| 886 | 512 - buffer1_off, &c_g2_len ) ); |
| 887 | c_g2_off = buffer1_off; |
| 888 | buffer1_off += c_g2_len; |
| 889 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 890 | buffer1 + buffer1_off, |
| 891 | 512 - buffer1_off, &c_x2_pk_len ) ); |
| 892 | c_x2_pk_off = buffer1_off; |
| 893 | buffer1_off += c_x2_pk_len; |
| 894 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 895 | buffer1 + buffer1_off, |
| 896 | 512 - buffer1_off, &c_x2_pr_len ) ); |
| 897 | c_x2_pr_off = buffer1_off; |
| 898 | buffer1_off += c_x2_pr_len; |
| 899 | |
| 900 | if( client_input_first == 0 ) |
| 901 | { |
| 902 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame^] | 903 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 904 | buffer0 + s_g1_off, s_g1_len ); |
| 905 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 906 | { |
| 907 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 908 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame^] | 909 | } |
| 910 | else |
| 911 | { |
| 912 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 913 | } |
| 914 | |
| 915 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 916 | buffer0 + s_x1_pk_off, |
| 917 | s_x1_pk_len ); |
| 918 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 919 | { |
| 920 | TEST_EQUAL( status, expected_status ); |
| 921 | break; |
| 922 | } |
| 923 | else |
| 924 | { |
| 925 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 926 | } |
| 927 | |
| 928 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 929 | buffer0 + s_x1_pr_off, |
| 930 | s_x1_pr_len ); |
| 931 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 932 | { |
| 933 | TEST_EQUAL( status, expected_status ); |
| 934 | break; |
| 935 | } |
| 936 | else |
| 937 | { |
| 938 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 939 | } |
| 940 | |
| 941 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 942 | buffer0 + s_g2_off, |
| 943 | s_g2_len ); |
| 944 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 945 | { |
| 946 | TEST_EQUAL( status, expected_status ); |
| 947 | break; |
| 948 | } |
| 949 | else |
| 950 | { |
| 951 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 952 | } |
| 953 | |
| 954 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 955 | buffer0 + s_x2_pk_off, |
| 956 | s_x2_pk_len ); |
| 957 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 958 | { |
| 959 | TEST_EQUAL( status, expected_status ); |
| 960 | break; |
| 961 | } |
| 962 | else |
| 963 | { |
| 964 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 965 | } |
| 966 | |
| 967 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 968 | buffer0 + s_x2_pr_off, |
| 969 | s_x2_pr_len ); |
| 970 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 971 | { |
| 972 | TEST_EQUAL( status, expected_status ); |
| 973 | break; |
| 974 | } |
| 975 | else |
| 976 | { |
| 977 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 978 | } |
| 979 | |
| 980 | /* Error didn't trigger, exit with error */ |
| 981 | if( inject_error == 1 ) |
| 982 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | if( inject_error == 2 ) |
| 986 | { |
| 987 | buffer1[c_x1_pk_off + 12] >>= 4; |
| 988 | buffer1[c_x2_pk_off + 7] <<= 4; |
| 989 | expected_status = PSA_ERROR_DATA_INVALID; |
| 990 | } |
| 991 | |
| 992 | /* Server first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame^] | 993 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 994 | buffer1 + c_g1_off, c_g1_len ); |
| 995 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 996 | { |
| 997 | TEST_EQUAL( status, expected_status ); |
| 998 | break; |
| 999 | } |
| 1000 | else |
| 1001 | { |
| 1002 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1003 | } |
| 1004 | |
| 1005 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1006 | buffer1 + c_x1_pk_off, c_x1_pk_len ); |
| 1007 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1008 | { |
| 1009 | TEST_EQUAL( status, expected_status ); |
| 1010 | break; |
| 1011 | } |
| 1012 | else |
| 1013 | { |
| 1014 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1015 | } |
| 1016 | |
| 1017 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1018 | buffer1 + c_x1_pr_off, c_x1_pr_len ); |
| 1019 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1020 | { |
| 1021 | TEST_EQUAL( status, expected_status ); |
| 1022 | break; |
| 1023 | } |
| 1024 | else |
| 1025 | { |
| 1026 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1027 | } |
| 1028 | |
| 1029 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1030 | buffer1 + c_g2_off, c_g2_len ); |
| 1031 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1032 | { |
| 1033 | TEST_EQUAL( status, expected_status ); |
| 1034 | break; |
| 1035 | } |
| 1036 | else |
| 1037 | { |
| 1038 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1039 | } |
| 1040 | |
| 1041 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1042 | buffer1 + c_x2_pk_off, c_x2_pk_len ); |
| 1043 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1044 | { |
| 1045 | TEST_EQUAL( status, expected_status ); |
| 1046 | break; |
| 1047 | } |
| 1048 | else |
| 1049 | { |
| 1050 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1051 | } |
| 1052 | |
| 1053 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1054 | buffer1 + c_x2_pr_off, c_x2_pr_len ); |
| 1055 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1056 | { |
| 1057 | TEST_EQUAL( status, expected_status ); |
| 1058 | break; |
| 1059 | } |
| 1060 | else |
| 1061 | { |
| 1062 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1063 | } |
| 1064 | |
| 1065 | /* Error didn't trigger, exit with error */ |
| 1066 | if( inject_error == 2 ) |
| 1067 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1068 | |
| 1069 | break; |
| 1070 | |
| 1071 | case 2: |
| 1072 | /* Server second round Output */ |
| 1073 | buffer0_off = 0; |
| 1074 | |
| 1075 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1076 | buffer0 + buffer0_off, |
| 1077 | 512 - buffer0_off, &s_a_len ) ); |
| 1078 | s_a_off = buffer0_off; |
| 1079 | buffer0_off += s_a_len; |
| 1080 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1081 | buffer0 + buffer0_off, |
| 1082 | 512 - buffer0_off, &s_x2s_pk_len ) ); |
| 1083 | s_x2s_pk_off = buffer0_off; |
| 1084 | buffer0_off += s_x2s_pk_len; |
| 1085 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1086 | buffer0 + buffer0_off, |
| 1087 | 512 - buffer0_off, &s_x2s_pr_len ) ); |
| 1088 | s_x2s_pr_off = buffer0_off; |
| 1089 | buffer0_off += s_x2s_pr_len; |
| 1090 | |
| 1091 | if( inject_error == 3 ) |
| 1092 | { |
| 1093 | buffer0[s_x2s_pk_off + 12] >>= 4; |
| 1094 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1095 | } |
| 1096 | |
| 1097 | if( client_input_first == 1 ) |
| 1098 | { |
| 1099 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame^] | 1100 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1101 | buffer0 + s_a_off, s_a_len ); |
| 1102 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1103 | { |
| 1104 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1105 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame^] | 1106 | } |
| 1107 | else |
| 1108 | { |
| 1109 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1110 | } |
| 1111 | |
| 1112 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1113 | buffer0 + s_x2s_pk_off, |
| 1114 | s_x2s_pk_len ); |
| 1115 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1116 | { |
| 1117 | TEST_EQUAL( status, expected_status ); |
| 1118 | break; |
| 1119 | } |
| 1120 | else |
| 1121 | { |
| 1122 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1123 | } |
| 1124 | |
| 1125 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1126 | buffer0 + s_x2s_pr_off, |
| 1127 | s_x2s_pr_len ); |
| 1128 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1129 | { |
| 1130 | TEST_EQUAL( status, expected_status ); |
| 1131 | break; |
| 1132 | } |
| 1133 | else |
| 1134 | { |
| 1135 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1136 | } |
| 1137 | |
| 1138 | /* Error didn't trigger, exit with error */ |
| 1139 | if( inject_error == 3 ) |
| 1140 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /* Client second round Output */ |
| 1144 | buffer1_off = 0; |
| 1145 | |
| 1146 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1147 | buffer1 + buffer1_off, |
| 1148 | 512 - buffer1_off, &c_a_len ) ); |
| 1149 | c_a_off = buffer1_off; |
| 1150 | buffer1_off += c_a_len; |
| 1151 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1152 | buffer1 + buffer1_off, |
| 1153 | 512 - buffer1_off, &c_x2s_pk_len ) ); |
| 1154 | c_x2s_pk_off = buffer1_off; |
| 1155 | buffer1_off += c_x2s_pk_len; |
| 1156 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1157 | buffer1 + buffer1_off, |
| 1158 | 512 - buffer1_off, &c_x2s_pr_len ) ); |
| 1159 | c_x2s_pr_off = buffer1_off; |
| 1160 | buffer1_off += c_x2s_pr_len; |
| 1161 | |
| 1162 | if( client_input_first == 0 ) |
| 1163 | { |
| 1164 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame^] | 1165 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1166 | buffer0 + s_a_off, s_a_len ); |
| 1167 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1168 | { |
| 1169 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1170 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame^] | 1171 | } |
| 1172 | else |
| 1173 | { |
| 1174 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1175 | } |
| 1176 | |
| 1177 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1178 | buffer0 + s_x2s_pk_off, |
| 1179 | s_x2s_pk_len ); |
| 1180 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1181 | { |
| 1182 | TEST_EQUAL( status, expected_status ); |
| 1183 | break; |
| 1184 | } |
| 1185 | else |
| 1186 | { |
| 1187 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1188 | } |
| 1189 | |
| 1190 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1191 | buffer0 + s_x2s_pr_off, |
| 1192 | s_x2s_pr_len ); |
| 1193 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1194 | { |
| 1195 | TEST_EQUAL( status, expected_status ); |
| 1196 | break; |
| 1197 | } |
| 1198 | else |
| 1199 | { |
| 1200 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1201 | } |
| 1202 | |
| 1203 | /* Error didn't trigger, exit with error */ |
| 1204 | if( inject_error == 3 ) |
| 1205 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | if( inject_error == 4 ) |
| 1209 | { |
| 1210 | buffer1[c_x2s_pk_off + 12] >>= 4; |
| 1211 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1212 | } |
| 1213 | |
| 1214 | /* Server second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame^] | 1215 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1216 | buffer1 + c_a_off, c_a_len ); |
| 1217 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1218 | { |
| 1219 | TEST_EQUAL( status, expected_status ); |
| 1220 | break; |
| 1221 | } |
| 1222 | else |
| 1223 | { |
| 1224 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1225 | } |
| 1226 | |
| 1227 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1228 | buffer1 + c_x2s_pk_off, c_x2s_pk_len ); |
| 1229 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1230 | { |
| 1231 | TEST_EQUAL( status, expected_status ); |
| 1232 | break; |
| 1233 | } |
| 1234 | else |
| 1235 | { |
| 1236 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1237 | } |
| 1238 | |
| 1239 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1240 | buffer1 + c_x2s_pr_off, c_x2s_pr_len ); |
| 1241 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1242 | { |
| 1243 | TEST_EQUAL( status, expected_status ); |
| 1244 | break; |
| 1245 | } |
| 1246 | else |
| 1247 | { |
| 1248 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1249 | } |
| 1250 | |
| 1251 | /* Error didn't trigger, exit with error */ |
| 1252 | if( inject_error == 4 ) |
| 1253 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1254 | |
| 1255 | break; |
| 1256 | |
| 1257 | } |
| 1258 | |
| 1259 | ret = 1; |
| 1260 | |
| 1261 | exit: |
| 1262 | mbedtls_free( buffer0 ); |
| 1263 | mbedtls_free( buffer1 ); |
| 1264 | return( ret ); |
| 1265 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1266 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1267 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1268 | /* END_HEADER */ |
| 1269 | |
| 1270 | /* BEGIN_DEPENDENCIES |
| 1271 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1272 | * END_DEPENDENCIES |
| 1273 | */ |
| 1274 | |
| 1275 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1276 | void static_checks( ) |
| 1277 | { |
| 1278 | size_t max_truncated_mac_size = |
| 1279 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1280 | |
| 1281 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1282 | * encoding. The shifted mask is the maximum truncated value. The |
| 1283 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1284 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1285 | } |
| 1286 | /* END_CASE */ |
| 1287 | |
| 1288 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1289 | void import_with_policy( int type_arg, |
| 1290 | int usage_arg, int alg_arg, |
| 1291 | int expected_status_arg ) |
| 1292 | { |
| 1293 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1294 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1295 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1296 | psa_key_type_t type = type_arg; |
| 1297 | psa_key_usage_t usage = usage_arg; |
| 1298 | psa_algorithm_t alg = alg_arg; |
| 1299 | psa_status_t expected_status = expected_status_arg; |
| 1300 | const uint8_t key_material[16] = {0}; |
| 1301 | psa_status_t status; |
| 1302 | |
| 1303 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1304 | |
| 1305 | psa_set_key_type( &attributes, type ); |
| 1306 | psa_set_key_usage_flags( &attributes, usage ); |
| 1307 | psa_set_key_algorithm( &attributes, alg ); |
| 1308 | |
| 1309 | status = psa_import_key( &attributes, |
| 1310 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1311 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1312 | TEST_EQUAL( status, expected_status ); |
| 1313 | if( status != PSA_SUCCESS ) |
| 1314 | goto exit; |
| 1315 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1316 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1317 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1318 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1319 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1320 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1321 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1322 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1323 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1324 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1325 | |
| 1326 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1327 | /* |
| 1328 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1329 | * thus reset them as required. |
| 1330 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1331 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1332 | |
| 1333 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1334 | PSA_DONE( ); |
| 1335 | } |
| 1336 | /* END_CASE */ |
| 1337 | |
| 1338 | /* BEGIN_CASE */ |
| 1339 | void import_with_data( data_t *data, int type_arg, |
| 1340 | int attr_bits_arg, |
| 1341 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1342 | { |
| 1343 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1344 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1345 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1346 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1347 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1348 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1349 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1350 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1351 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1352 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1353 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1354 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1355 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1356 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1357 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1358 | if( status != PSA_SUCCESS ) |
| 1359 | goto exit; |
| 1360 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1361 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1362 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1363 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1364 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1365 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1366 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1367 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1368 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1369 | |
| 1370 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1371 | /* |
| 1372 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1373 | * thus reset them as required. |
| 1374 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1375 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1376 | |
| 1377 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1378 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1379 | } |
| 1380 | /* END_CASE */ |
| 1381 | |
| 1382 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1383 | void import_large_key( int type_arg, int byte_size_arg, |
| 1384 | int expected_status_arg ) |
| 1385 | { |
| 1386 | psa_key_type_t type = type_arg; |
| 1387 | size_t byte_size = byte_size_arg; |
| 1388 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1389 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1390 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1391 | psa_status_t status; |
| 1392 | uint8_t *buffer = NULL; |
| 1393 | size_t buffer_size = byte_size + 1; |
| 1394 | size_t n; |
| 1395 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1396 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1397 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1398 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1399 | memset( buffer, 'K', byte_size ); |
| 1400 | |
| 1401 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1402 | |
| 1403 | /* Try importing the key */ |
| 1404 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1405 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1406 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1407 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1408 | TEST_EQUAL( status, expected_status ); |
| 1409 | |
| 1410 | if( status == PSA_SUCCESS ) |
| 1411 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1412 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1413 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1414 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1415 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1416 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1417 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1418 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1419 | for( n = 0; n < byte_size; n++ ) |
| 1420 | TEST_EQUAL( buffer[n], 'K' ); |
| 1421 | for( n = byte_size; n < buffer_size; n++ ) |
| 1422 | TEST_EQUAL( buffer[n], 0 ); |
| 1423 | } |
| 1424 | |
| 1425 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1426 | /* |
| 1427 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1428 | * thus reset them as required. |
| 1429 | */ |
| 1430 | psa_reset_key_attributes( &attributes ); |
| 1431 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1432 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1433 | PSA_DONE( ); |
| 1434 | mbedtls_free( buffer ); |
| 1435 | } |
| 1436 | /* END_CASE */ |
| 1437 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1438 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1439 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1440 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1441 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1442 | size_t bits = bits_arg; |
| 1443 | psa_status_t expected_status = expected_status_arg; |
| 1444 | psa_status_t status; |
| 1445 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1446 | 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] | 1447 | size_t buffer_size = /* Slight overapproximations */ |
| 1448 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1449 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1450 | unsigned char *p; |
| 1451 | int ret; |
| 1452 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1453 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1454 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1455 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1456 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1457 | |
| 1458 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1459 | bits, keypair ) ) >= 0 ); |
| 1460 | length = ret; |
| 1461 | |
| 1462 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1463 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1464 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1465 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1466 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1467 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1468 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1469 | |
| 1470 | exit: |
| 1471 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1472 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1473 | } |
| 1474 | /* END_CASE */ |
| 1475 | |
| 1476 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1477 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1478 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1479 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1480 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1481 | int expected_bits, |
| 1482 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1483 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1484 | int canonical_input ) |
| 1485 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1486 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1487 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1488 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1489 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1490 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1491 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1492 | unsigned char *exported = NULL; |
| 1493 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1494 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1495 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1496 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1497 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1498 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1499 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1500 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1501 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1502 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1503 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1504 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1505 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1506 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1507 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1508 | psa_set_key_algorithm( &attributes, alg ); |
| 1509 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1510 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1511 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1512 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1513 | |
| 1514 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1515 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1516 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1517 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1518 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1519 | |
| 1520 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1521 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1522 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1523 | |
| 1524 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1525 | * and export_size. On errors, the exported length must be 0. */ |
| 1526 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1527 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1528 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1529 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1530 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1531 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1532 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1533 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1534 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1535 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1536 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1537 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1538 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1539 | * this validates the canonical representations. For canonical inputs, |
| 1540 | * this doesn't directly validate the implementation, but it still helps |
| 1541 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1542 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1543 | { |
| 1544 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1545 | goto exit; |
| 1546 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1547 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1548 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1549 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1550 | else |
| 1551 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1552 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1553 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1554 | &key2 ) ); |
| 1555 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1556 | reexported, |
| 1557 | export_size, |
| 1558 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1559 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1560 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1561 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1562 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1563 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1564 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1565 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1566 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1567 | |
| 1568 | destroy: |
| 1569 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1570 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1571 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1572 | |
| 1573 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1574 | /* |
| 1575 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1576 | * thus reset them as required. |
| 1577 | */ |
| 1578 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1579 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1580 | mbedtls_free( exported ); |
| 1581 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1582 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1583 | } |
| 1584 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1585 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1586 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1587 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1588 | int type_arg, |
| 1589 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1590 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1591 | int export_size_delta, |
| 1592 | int expected_export_status_arg, |
| 1593 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1594 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1595 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1596 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1597 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1598 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1599 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1600 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1601 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1602 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1603 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1604 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1605 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1606 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1607 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1608 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1609 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1610 | psa_set_key_algorithm( &attributes, alg ); |
| 1611 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1612 | |
| 1613 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1614 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1615 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1616 | /* Export the public key */ |
| 1617 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1618 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1619 | exported, export_size, |
| 1620 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1621 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1622 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1623 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1624 | 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] | 1625 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1626 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1627 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1628 | TEST_LE_U( expected_public_key->len, |
| 1629 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1630 | TEST_LE_U( expected_public_key->len, |
| 1631 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1632 | TEST_LE_U( expected_public_key->len, |
| 1633 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1634 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1635 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1636 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1637 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1638 | /* |
| 1639 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1640 | * thus reset them as required. |
| 1641 | */ |
| 1642 | psa_reset_key_attributes( &attributes ); |
| 1643 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1644 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1645 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1646 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1647 | } |
| 1648 | /* END_CASE */ |
| 1649 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1650 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1651 | void import_and_exercise_key( data_t *data, |
| 1652 | int type_arg, |
| 1653 | int bits_arg, |
| 1654 | int alg_arg ) |
| 1655 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1656 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1657 | psa_key_type_t type = type_arg; |
| 1658 | size_t bits = bits_arg; |
| 1659 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1660 | 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] | 1661 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1662 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1663 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1664 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1665 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1666 | psa_set_key_usage_flags( &attributes, usage ); |
| 1667 | psa_set_key_algorithm( &attributes, alg ); |
| 1668 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1669 | |
| 1670 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1671 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1672 | |
| 1673 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1674 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1675 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1676 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1677 | |
| 1678 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1679 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1680 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1681 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1682 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1683 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1684 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1685 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1686 | /* |
| 1687 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1688 | * thus reset them as required. |
| 1689 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1690 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1691 | |
| 1692 | psa_reset_key_attributes( &attributes ); |
| 1693 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1694 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1695 | } |
| 1696 | /* END_CASE */ |
| 1697 | |
| 1698 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1699 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1700 | int bits_arg, int expected_bits_arg, |
| 1701 | int usage_arg, int expected_usage_arg, |
| 1702 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1703 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1704 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1705 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1706 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1707 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1708 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1709 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1710 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1711 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1712 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1713 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1714 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1715 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1716 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1717 | psa_set_key_usage_flags( &attributes, usage ); |
| 1718 | psa_set_key_algorithm( &attributes, alg ); |
| 1719 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1720 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1721 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1722 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1723 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1724 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1725 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1726 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1727 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1728 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1729 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1730 | |
| 1731 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1732 | /* |
| 1733 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1734 | * thus reset them as required. |
| 1735 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1736 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1737 | |
| 1738 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1739 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1740 | } |
| 1741 | /* END_CASE */ |
| 1742 | |
| 1743 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1744 | void check_key_policy( int type_arg, int bits_arg, |
| 1745 | int usage_arg, int alg_arg ) |
| 1746 | { |
| 1747 | 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] | 1748 | usage_arg, |
| 1749 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1750 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1751 | goto exit; |
| 1752 | } |
| 1753 | /* END_CASE */ |
| 1754 | |
| 1755 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1756 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1757 | { |
| 1758 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1759 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1760 | * 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] | 1761 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1762 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1763 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1764 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1765 | |
| 1766 | memset( &zero, 0, sizeof( zero ) ); |
| 1767 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1768 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1769 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1770 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1771 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1772 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1773 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1774 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1775 | |
| 1776 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1777 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1778 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1779 | |
| 1780 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1781 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1782 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1783 | |
| 1784 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1785 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1786 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1787 | } |
| 1788 | /* END_CASE */ |
| 1789 | |
| 1790 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1791 | void mac_key_policy( int policy_usage_arg, |
| 1792 | int policy_alg_arg, |
| 1793 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1794 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1795 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1796 | int expected_status_sign_arg, |
| 1797 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1798 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1799 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1800 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1801 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1802 | psa_key_type_t key_type = key_type_arg; |
| 1803 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1804 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1805 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1806 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1807 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1808 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1809 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1810 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1811 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1812 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1813 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1814 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1815 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1816 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1817 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1818 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1819 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1820 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1821 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1822 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1823 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1824 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1825 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1826 | /* Calculate the MAC, one-shot case. */ |
| 1827 | uint8_t input[128] = {0}; |
| 1828 | size_t mac_len; |
| 1829 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1830 | input, 128, |
| 1831 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1832 | expected_status_sign ); |
| 1833 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1834 | /* Calculate the MAC, multi-part case. */ |
| 1835 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1836 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1837 | if( status == PSA_SUCCESS ) |
| 1838 | { |
| 1839 | status = psa_mac_update( &operation, input, 128 ); |
| 1840 | if( status == PSA_SUCCESS ) |
| 1841 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1842 | &mac_len ), |
| 1843 | expected_status_sign ); |
| 1844 | else |
| 1845 | TEST_EQUAL( status, expected_status_sign ); |
| 1846 | } |
| 1847 | else |
| 1848 | { |
| 1849 | TEST_EQUAL( status, expected_status_sign ); |
| 1850 | } |
| 1851 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1852 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1853 | /* Verify correct MAC, one-shot case. */ |
| 1854 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1855 | mac, mac_len ); |
| 1856 | |
| 1857 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1858 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1859 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1860 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1861 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1862 | /* Verify correct MAC, multi-part case. */ |
| 1863 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1864 | if( status == PSA_SUCCESS ) |
| 1865 | { |
| 1866 | status = psa_mac_update( &operation, input, 128 ); |
| 1867 | if( status == PSA_SUCCESS ) |
| 1868 | { |
| 1869 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1870 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1871 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1872 | else |
| 1873 | TEST_EQUAL( status, expected_status_verify ); |
| 1874 | } |
| 1875 | else |
| 1876 | { |
| 1877 | TEST_EQUAL( status, expected_status_verify ); |
| 1878 | } |
| 1879 | } |
| 1880 | else |
| 1881 | { |
| 1882 | TEST_EQUAL( status, expected_status_verify ); |
| 1883 | } |
| 1884 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1885 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1886 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1887 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1888 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1889 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1890 | |
| 1891 | exit: |
| 1892 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1893 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1894 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1895 | } |
| 1896 | /* END_CASE */ |
| 1897 | |
| 1898 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1899 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1900 | int policy_alg, |
| 1901 | int key_type, |
| 1902 | data_t *key_data, |
| 1903 | int exercise_alg ) |
| 1904 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1905 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1906 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1907 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1908 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1909 | size_t output_buffer_size = 0; |
| 1910 | size_t input_buffer_size = 0; |
| 1911 | size_t output_length = 0; |
| 1912 | uint8_t *output = NULL; |
| 1913 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1914 | psa_status_t status; |
| 1915 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1916 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1917 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1918 | input_buffer_size ); |
| 1919 | |
| 1920 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1921 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1922 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1923 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1924 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1925 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1926 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1927 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1928 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1929 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1930 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1931 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1932 | /* Check if no key usage flag implication is done */ |
| 1933 | TEST_EQUAL( policy_usage, |
| 1934 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1935 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1936 | /* Encrypt check, one-shot */ |
| 1937 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1938 | output, output_buffer_size, |
| 1939 | &output_length); |
| 1940 | if( policy_alg == exercise_alg && |
| 1941 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1942 | PSA_ASSERT( status ); |
| 1943 | else |
| 1944 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1945 | |
| 1946 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1947 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1948 | if( policy_alg == exercise_alg && |
| 1949 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1950 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1951 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1952 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1953 | psa_cipher_abort( &operation ); |
| 1954 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1955 | /* Decrypt check, one-shot */ |
| 1956 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 1957 | input, input_buffer_size, |
| 1958 | &output_length); |
| 1959 | if( policy_alg == exercise_alg && |
| 1960 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1961 | PSA_ASSERT( status ); |
| 1962 | else |
| 1963 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1964 | |
| 1965 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1966 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1967 | if( policy_alg == exercise_alg && |
| 1968 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1969 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1970 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1971 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1972 | |
| 1973 | exit: |
| 1974 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1975 | mbedtls_free( input ); |
| 1976 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1977 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1978 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1979 | } |
| 1980 | /* END_CASE */ |
| 1981 | |
| 1982 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1983 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1984 | int policy_alg, |
| 1985 | int key_type, |
| 1986 | data_t *key_data, |
| 1987 | int nonce_length_arg, |
| 1988 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1989 | int exercise_alg, |
| 1990 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1991 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1992 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1993 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1994 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1995 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1996 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1997 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1998 | unsigned char nonce[16] = {0}; |
| 1999 | size_t nonce_length = nonce_length_arg; |
| 2000 | unsigned char tag[16]; |
| 2001 | size_t tag_length = tag_length_arg; |
| 2002 | size_t output_length; |
| 2003 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2004 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 2005 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2006 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2007 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2008 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2009 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2010 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2011 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2012 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2013 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2014 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2015 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2016 | /* Check if no key usage implication is done */ |
| 2017 | TEST_EQUAL( policy_usage, |
| 2018 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2019 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2020 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2021 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2022 | nonce, nonce_length, |
| 2023 | NULL, 0, |
| 2024 | NULL, 0, |
| 2025 | tag, tag_length, |
| 2026 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2027 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2028 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2029 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2030 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2031 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2032 | /* Encrypt check, multi-part */ |
| 2033 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 2034 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2035 | TEST_EQUAL( status, expected_status ); |
| 2036 | else |
| 2037 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2038 | |
| 2039 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2040 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2041 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2042 | nonce, nonce_length, |
| 2043 | NULL, 0, |
| 2044 | tag, tag_length, |
| 2045 | NULL, 0, |
| 2046 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2047 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2048 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2049 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2050 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2051 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2052 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2053 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2054 | /* Decrypt check, multi-part */ |
| 2055 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 2056 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 2057 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2058 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2059 | else |
| 2060 | TEST_EQUAL( status, expected_status ); |
| 2061 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2062 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2063 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2064 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2065 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2066 | } |
| 2067 | /* END_CASE */ |
| 2068 | |
| 2069 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2070 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2071 | int policy_alg, |
| 2072 | int key_type, |
| 2073 | data_t *key_data, |
| 2074 | int exercise_alg ) |
| 2075 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2076 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2077 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2078 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2079 | psa_status_t status; |
| 2080 | size_t key_bits; |
| 2081 | size_t buffer_length; |
| 2082 | unsigned char *buffer = NULL; |
| 2083 | size_t output_length; |
| 2084 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2085 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2086 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2087 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2088 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2089 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2090 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2091 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2092 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2093 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2094 | /* Check if no key usage implication is done */ |
| 2095 | TEST_EQUAL( policy_usage, |
| 2096 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2097 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2098 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2099 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2100 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2101 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2102 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2103 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2104 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2105 | NULL, 0, |
| 2106 | NULL, 0, |
| 2107 | buffer, buffer_length, |
| 2108 | &output_length ); |
| 2109 | if( policy_alg == exercise_alg && |
| 2110 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2111 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2112 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2113 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2114 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2115 | if( buffer_length != 0 ) |
| 2116 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2117 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2118 | buffer, buffer_length, |
| 2119 | NULL, 0, |
| 2120 | buffer, buffer_length, |
| 2121 | &output_length ); |
| 2122 | if( policy_alg == exercise_alg && |
| 2123 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2124 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2125 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2126 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2127 | |
| 2128 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2129 | /* |
| 2130 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2131 | * thus reset them as required. |
| 2132 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2133 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2134 | |
| 2135 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2136 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2137 | mbedtls_free( buffer ); |
| 2138 | } |
| 2139 | /* END_CASE */ |
| 2140 | |
| 2141 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2142 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2143 | int policy_alg, |
| 2144 | int key_type, |
| 2145 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2146 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2147 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2148 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2149 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2150 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2151 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2152 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2153 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2154 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2155 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2156 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2157 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2158 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2159 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2160 | int compatible_alg = payload_length_arg > 0; |
| 2161 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2162 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2163 | size_t signature_length; |
| 2164 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2165 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2166 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2167 | TEST_EQUAL( expected_usage, |
| 2168 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2169 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2170 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2171 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2172 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2173 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2174 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2175 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2176 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2177 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2178 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2179 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 2180 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2181 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2182 | payload, payload_length, |
| 2183 | signature, sizeof( signature ), |
| 2184 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2185 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2186 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2187 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2188 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2189 | |
| 2190 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2191 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2192 | payload, payload_length, |
| 2193 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2194 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2195 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2196 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2197 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2198 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2199 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 2200 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2201 | { |
| 2202 | status = psa_sign_message( key, exercise_alg, |
| 2203 | payload, payload_length, |
| 2204 | signature, sizeof( signature ), |
| 2205 | &signature_length ); |
| 2206 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 2207 | PSA_ASSERT( status ); |
| 2208 | else |
| 2209 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2210 | |
| 2211 | memset( signature, 0, sizeof( signature ) ); |
| 2212 | status = psa_verify_message( key, exercise_alg, |
| 2213 | payload, payload_length, |
| 2214 | signature, sizeof( signature ) ); |
| 2215 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 2216 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 2217 | else |
| 2218 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2219 | } |
| 2220 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2221 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2222 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2223 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2224 | } |
| 2225 | /* END_CASE */ |
| 2226 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2227 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2228 | void derive_key_policy( int policy_usage, |
| 2229 | int policy_alg, |
| 2230 | int key_type, |
| 2231 | data_t *key_data, |
| 2232 | int exercise_alg ) |
| 2233 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2234 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2235 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2236 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2237 | psa_status_t status; |
| 2238 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2239 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2240 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2241 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2242 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2243 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2244 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2245 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2246 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2247 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2248 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2249 | |
| 2250 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2251 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2252 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2253 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2254 | &operation, |
| 2255 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2256 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2257 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2258 | |
| 2259 | status = psa_key_derivation_input_key( &operation, |
| 2260 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2261 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2262 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2263 | if( policy_alg == exercise_alg && |
| 2264 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2265 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2266 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2267 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2268 | |
| 2269 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2270 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2271 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2272 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2273 | } |
| 2274 | /* END_CASE */ |
| 2275 | |
| 2276 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2277 | void agreement_key_policy( int policy_usage, |
| 2278 | int policy_alg, |
| 2279 | int key_type_arg, |
| 2280 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2281 | int exercise_alg, |
| 2282 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2283 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2284 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2285 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2286 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2287 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2288 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2289 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2290 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2291 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2292 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2293 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2294 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2295 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2296 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2297 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2298 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2299 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2300 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2301 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2302 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2303 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2304 | |
| 2305 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2306 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2307 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2308 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2309 | } |
| 2310 | /* END_CASE */ |
| 2311 | |
| 2312 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2313 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2314 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2315 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2316 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2317 | psa_key_type_t key_type = key_type_arg; |
| 2318 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2319 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2320 | psa_key_usage_t usage = usage_arg; |
| 2321 | psa_algorithm_t alg = alg_arg; |
| 2322 | psa_algorithm_t alg2 = alg2_arg; |
| 2323 | |
| 2324 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2325 | |
| 2326 | psa_set_key_usage_flags( &attributes, usage ); |
| 2327 | psa_set_key_algorithm( &attributes, alg ); |
| 2328 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2329 | psa_set_key_type( &attributes, key_type ); |
| 2330 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2331 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2332 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2333 | /* Update the usage flags to obtain implicit usage flags */ |
| 2334 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2335 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2336 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2337 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2338 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2339 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2340 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2341 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2342 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2343 | goto exit; |
| 2344 | |
| 2345 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2346 | /* |
| 2347 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2348 | * thus reset them as required. |
| 2349 | */ |
| 2350 | psa_reset_key_attributes( &got_attributes ); |
| 2351 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2352 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2353 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2354 | } |
| 2355 | /* END_CASE */ |
| 2356 | |
| 2357 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2358 | void raw_agreement_key_policy( int policy_usage, |
| 2359 | int policy_alg, |
| 2360 | int key_type_arg, |
| 2361 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2362 | int exercise_alg, |
| 2363 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2364 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2365 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2366 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2367 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2368 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2369 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2370 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2371 | |
| 2372 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2373 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2374 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2375 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2376 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2377 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2378 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2379 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2380 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2381 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2382 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2383 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2384 | |
| 2385 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2386 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2387 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2388 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2389 | } |
| 2390 | /* END_CASE */ |
| 2391 | |
| 2392 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2393 | void copy_success( int source_usage_arg, |
| 2394 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2395 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2396 | int type_arg, data_t *material, |
| 2397 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2398 | int target_usage_arg, |
| 2399 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2400 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2401 | int expected_usage_arg, |
| 2402 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2403 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2404 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2405 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2406 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2407 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2408 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2409 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2410 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2411 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2412 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2413 | uint8_t *export_buffer = NULL; |
| 2414 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2415 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2416 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2417 | /* Prepare the source key. */ |
| 2418 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2419 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2420 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2421 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2422 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2423 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2424 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2425 | &source_key ) ); |
| 2426 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2427 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2428 | /* Prepare the target attributes. */ |
| 2429 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2430 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2431 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2432 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2433 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
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 | if( target_usage_arg != -1 ) |
| 2436 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2437 | if( target_alg_arg != -1 ) |
| 2438 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2439 | if( target_alg2_arg != -1 ) |
| 2440 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2441 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2442 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2443 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2444 | PSA_ASSERT( psa_copy_key( source_key, |
| 2445 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2446 | |
| 2447 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2448 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2449 | |
| 2450 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2451 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2452 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2453 | psa_get_key_type( &target_attributes ) ); |
| 2454 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2455 | psa_get_key_bits( &target_attributes ) ); |
| 2456 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2457 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2458 | TEST_EQUAL( expected_alg2, |
| 2459 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2460 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2461 | { |
| 2462 | size_t length; |
| 2463 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2464 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2465 | material->len, &length ) ); |
| 2466 | ASSERT_COMPARE( material->x, material->len, |
| 2467 | export_buffer, length ); |
| 2468 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2469 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2470 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2471 | { |
| 2472 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2473 | goto exit; |
| 2474 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2475 | goto exit; |
| 2476 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2477 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2478 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2479 | |
| 2480 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2481 | /* |
| 2482 | * Source and target key attributes may have been returned by |
| 2483 | * psa_get_key_attributes() thus reset them as required. |
| 2484 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2485 | psa_reset_key_attributes( &source_attributes ); |
| 2486 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2487 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2488 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2489 | mbedtls_free( export_buffer ); |
| 2490 | } |
| 2491 | /* END_CASE */ |
| 2492 | |
| 2493 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2494 | void copy_fail( int source_usage_arg, |
| 2495 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2496 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2497 | int type_arg, data_t *material, |
| 2498 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2499 | int target_usage_arg, |
| 2500 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2501 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2502 | int expected_status_arg ) |
| 2503 | { |
| 2504 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2505 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2506 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2507 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2508 | 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] | 2509 | |
| 2510 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2511 | |
| 2512 | /* Prepare the source key. */ |
| 2513 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2514 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2515 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2516 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2517 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2518 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2519 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2520 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2521 | |
| 2522 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2523 | psa_set_key_id( &target_attributes, key_id ); |
| 2524 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2525 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2526 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2527 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2528 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2529 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2530 | |
| 2531 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2532 | TEST_EQUAL( psa_copy_key( source_key, |
| 2533 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2534 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2535 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2536 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2537 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2538 | exit: |
| 2539 | psa_reset_key_attributes( &source_attributes ); |
| 2540 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2541 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2542 | } |
| 2543 | /* END_CASE */ |
| 2544 | |
| 2545 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2546 | void hash_operation_init( ) |
| 2547 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2548 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2549 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2550 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2551 | * 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] | 2552 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2553 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2554 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2555 | psa_hash_operation_t zero; |
| 2556 | |
| 2557 | memset( &zero, 0, sizeof( zero ) ); |
| 2558 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2559 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2560 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2561 | PSA_ERROR_BAD_STATE ); |
| 2562 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2563 | PSA_ERROR_BAD_STATE ); |
| 2564 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2565 | PSA_ERROR_BAD_STATE ); |
| 2566 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2567 | /* A default hash operation should be abortable without error. */ |
| 2568 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2569 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2570 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2571 | } |
| 2572 | /* END_CASE */ |
| 2573 | |
| 2574 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2575 | void hash_setup( int alg_arg, |
| 2576 | int expected_status_arg ) |
| 2577 | { |
| 2578 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2579 | uint8_t *output = NULL; |
| 2580 | size_t output_size = 0; |
| 2581 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2582 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2583 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2584 | psa_status_t status; |
| 2585 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2586 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2587 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2588 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2589 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2590 | ASSERT_ALLOC( output, output_size ); |
| 2591 | |
| 2592 | status = psa_hash_compute( alg, NULL, 0, |
| 2593 | output, output_size, &output_length ); |
| 2594 | TEST_EQUAL( status, expected_status ); |
| 2595 | |
| 2596 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2597 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2598 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2599 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2600 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2601 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2602 | |
| 2603 | /* If setup failed, reproduce the failure, so as to |
| 2604 | * test the resulting state of the operation object. */ |
| 2605 | if( status != PSA_SUCCESS ) |
| 2606 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2607 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2608 | /* Now the operation object should be reusable. */ |
| 2609 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2610 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2611 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2612 | #endif |
| 2613 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2614 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2615 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2616 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2617 | } |
| 2618 | /* END_CASE */ |
| 2619 | |
| 2620 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2621 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2622 | int output_size_arg, int expected_status_arg ) |
| 2623 | { |
| 2624 | psa_algorithm_t alg = alg_arg; |
| 2625 | uint8_t *output = NULL; |
| 2626 | size_t output_size = output_size_arg; |
| 2627 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2628 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2629 | psa_status_t expected_status = expected_status_arg; |
| 2630 | psa_status_t status; |
| 2631 | |
| 2632 | ASSERT_ALLOC( output, output_size ); |
| 2633 | |
| 2634 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2635 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2636 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2637 | status = psa_hash_compute( alg, input->x, input->len, |
| 2638 | output, output_size, &output_length ); |
| 2639 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2640 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2641 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2642 | /* Hash Compute, multi-part */ |
| 2643 | status = psa_hash_setup( &operation, alg ); |
| 2644 | if( status == PSA_SUCCESS ) |
| 2645 | { |
| 2646 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2647 | if( status == PSA_SUCCESS ) |
| 2648 | { |
| 2649 | status = psa_hash_finish( &operation, output, output_size, |
| 2650 | &output_length ); |
| 2651 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2652 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2653 | else |
| 2654 | TEST_EQUAL( status, expected_status ); |
| 2655 | } |
| 2656 | else |
| 2657 | { |
| 2658 | TEST_EQUAL( status, expected_status ); |
| 2659 | } |
| 2660 | } |
| 2661 | else |
| 2662 | { |
| 2663 | TEST_EQUAL( status, expected_status ); |
| 2664 | } |
| 2665 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2666 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2667 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2668 | mbedtls_free( output ); |
| 2669 | PSA_DONE( ); |
| 2670 | } |
| 2671 | /* END_CASE */ |
| 2672 | |
| 2673 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2674 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2675 | data_t *reference_hash, |
| 2676 | int expected_status_arg ) |
| 2677 | { |
| 2678 | psa_algorithm_t alg = alg_arg; |
| 2679 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2680 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2681 | psa_status_t status; |
| 2682 | |
| 2683 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2684 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2685 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2686 | status = psa_hash_compare( alg, input->x, input->len, |
| 2687 | reference_hash->x, reference_hash->len ); |
| 2688 | TEST_EQUAL( status, expected_status ); |
| 2689 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2690 | /* Hash Compare, multi-part */ |
| 2691 | status = psa_hash_setup( &operation, alg ); |
| 2692 | if( status == PSA_SUCCESS ) |
| 2693 | { |
| 2694 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2695 | if( status == PSA_SUCCESS ) |
| 2696 | { |
| 2697 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2698 | reference_hash->len ); |
| 2699 | TEST_EQUAL( status, expected_status ); |
| 2700 | } |
| 2701 | else |
| 2702 | { |
| 2703 | TEST_EQUAL( status, expected_status ); |
| 2704 | } |
| 2705 | } |
| 2706 | else |
| 2707 | { |
| 2708 | TEST_EQUAL( status, expected_status ); |
| 2709 | } |
| 2710 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2711 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2712 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2713 | PSA_DONE( ); |
| 2714 | } |
| 2715 | /* END_CASE */ |
| 2716 | |
| 2717 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2718 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2719 | data_t *expected_output ) |
| 2720 | { |
| 2721 | psa_algorithm_t alg = alg_arg; |
| 2722 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2723 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2724 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2725 | size_t i; |
| 2726 | |
| 2727 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2728 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2729 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2730 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2731 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2732 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2733 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2734 | ASSERT_COMPARE( output, output_length, |
| 2735 | expected_output->x, expected_output->len ); |
| 2736 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2737 | /* Compute with tight buffer, multi-part */ |
| 2738 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2739 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2740 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2741 | PSA_HASH_LENGTH( alg ), |
| 2742 | &output_length ) ); |
| 2743 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2744 | ASSERT_COMPARE( output, output_length, |
| 2745 | expected_output->x, expected_output->len ); |
| 2746 | |
| 2747 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2748 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2749 | output, sizeof( output ), |
| 2750 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2751 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2752 | ASSERT_COMPARE( output, output_length, |
| 2753 | expected_output->x, expected_output->len ); |
| 2754 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2755 | /* Compute with larger buffer, multi-part */ |
| 2756 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2757 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2758 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2759 | sizeof( output ), &output_length ) ); |
| 2760 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2761 | ASSERT_COMPARE( output, output_length, |
| 2762 | expected_output->x, expected_output->len ); |
| 2763 | |
| 2764 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2765 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2766 | output, output_length ) ); |
| 2767 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2768 | /* Compare with correct hash, multi-part */ |
| 2769 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2770 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2771 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2772 | output_length ) ); |
| 2773 | |
| 2774 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2775 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2776 | output, output_length + 1 ), |
| 2777 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2778 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2779 | /* Compare with trailing garbage, multi-part */ |
| 2780 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2781 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2782 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2783 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2784 | |
| 2785 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2786 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2787 | output, output_length - 1 ), |
| 2788 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2789 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2790 | /* Compare with truncated hash, multi-part */ |
| 2791 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2792 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2793 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2794 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2795 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2796 | /* Compare with corrupted value */ |
| 2797 | for( i = 0; i < output_length; i++ ) |
| 2798 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2799 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2800 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2801 | |
| 2802 | /* One-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2803 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2804 | output, output_length ), |
| 2805 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2806 | |
| 2807 | /* Multi-Part */ |
| 2808 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2809 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2810 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length ), |
| 2811 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2812 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2813 | output[i] ^= 1; |
| 2814 | } |
| 2815 | |
| 2816 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2817 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2818 | PSA_DONE( ); |
| 2819 | } |
| 2820 | /* END_CASE */ |
| 2821 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2822 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2823 | void hash_bad_order( ) |
| 2824 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2825 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2826 | unsigned char input[] = ""; |
| 2827 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2828 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2829 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2830 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2831 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2832 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2833 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2834 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2835 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2836 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2837 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2838 | /* Call setup twice in a row. */ |
| 2839 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2840 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2841 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2842 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2843 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2844 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2845 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2846 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2847 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2848 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2849 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2850 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2851 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2852 | /* Check that update calls abort on error. */ |
| 2853 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2854 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2855 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2856 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2857 | PSA_ERROR_BAD_STATE ); |
| 2858 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2859 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2860 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2861 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2862 | /* Call update after finish. */ |
| 2863 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2864 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2865 | hash, sizeof( hash ), &hash_len ) ); |
| 2866 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2867 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2868 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2869 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2870 | /* Call verify without calling setup beforehand. */ |
| 2871 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2872 | valid_hash, sizeof( valid_hash ) ), |
| 2873 | PSA_ERROR_BAD_STATE ); |
| 2874 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2875 | |
| 2876 | /* Call verify after finish. */ |
| 2877 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2878 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2879 | hash, sizeof( hash ), &hash_len ) ); |
| 2880 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2881 | valid_hash, sizeof( valid_hash ) ), |
| 2882 | PSA_ERROR_BAD_STATE ); |
| 2883 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2884 | |
| 2885 | /* Call verify twice in a row. */ |
| 2886 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2887 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2888 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2889 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2890 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2891 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2892 | valid_hash, sizeof( valid_hash ) ), |
| 2893 | PSA_ERROR_BAD_STATE ); |
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 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2896 | |
| 2897 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2898 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2899 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2900 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2901 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2902 | |
| 2903 | /* Call finish twice in a row. */ |
| 2904 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2905 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2906 | hash, sizeof( hash ), &hash_len ) ); |
| 2907 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2908 | hash, sizeof( hash ), &hash_len ), |
| 2909 | PSA_ERROR_BAD_STATE ); |
| 2910 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2911 | |
| 2912 | /* Call finish after calling verify. */ |
| 2913 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2914 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2915 | valid_hash, sizeof( valid_hash ) ) ); |
| 2916 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2917 | hash, sizeof( hash ), &hash_len ), |
| 2918 | PSA_ERROR_BAD_STATE ); |
| 2919 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2920 | |
| 2921 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2922 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2923 | } |
| 2924 | /* END_CASE */ |
| 2925 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2926 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2927 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2928 | { |
| 2929 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2930 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2931 | * appended to it */ |
| 2932 | unsigned char hash[] = { |
| 2933 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2934 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2935 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2936 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2937 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2938 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2939 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2940 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2941 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2942 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2943 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2944 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2945 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2946 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2947 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2948 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2949 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2950 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2951 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2952 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2953 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2954 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2955 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2956 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2957 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2958 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2959 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2960 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2961 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2962 | } |
| 2963 | /* END_CASE */ |
| 2964 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2965 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2966 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2967 | { |
| 2968 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2969 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2970 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2971 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2972 | size_t hash_len; |
| 2973 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2974 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2975 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2976 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2977 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2978 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2979 | hash, expected_size - 1, &hash_len ), |
| 2980 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2981 | |
| 2982 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2983 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2984 | } |
| 2985 | /* END_CASE */ |
| 2986 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2987 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2988 | void hash_clone_source_state( ) |
| 2989 | { |
| 2990 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2991 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2992 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2993 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2994 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2995 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2996 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2997 | size_t hash_len; |
| 2998 | |
| 2999 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3000 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 3001 | |
| 3002 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3003 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3004 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3005 | hash, sizeof( hash ), &hash_len ) ); |
| 3006 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3007 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3008 | |
| 3009 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 3010 | PSA_ERROR_BAD_STATE ); |
| 3011 | |
| 3012 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 3013 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 3014 | hash, sizeof( hash ), &hash_len ) ); |
| 3015 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 3016 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3017 | hash, sizeof( hash ), &hash_len ) ); |
| 3018 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 3019 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 3020 | hash, sizeof( hash ), &hash_len ) ); |
| 3021 | |
| 3022 | exit: |
| 3023 | psa_hash_abort( &op_source ); |
| 3024 | psa_hash_abort( &op_init ); |
| 3025 | psa_hash_abort( &op_setup ); |
| 3026 | psa_hash_abort( &op_finished ); |
| 3027 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3028 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3029 | } |
| 3030 | /* END_CASE */ |
| 3031 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3032 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3033 | void hash_clone_target_state( ) |
| 3034 | { |
| 3035 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3036 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3037 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3038 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3039 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3040 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3041 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3042 | size_t hash_len; |
| 3043 | |
| 3044 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3045 | |
| 3046 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3047 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3048 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3049 | hash, sizeof( hash ), &hash_len ) ); |
| 3050 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3051 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3052 | |
| 3053 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 3054 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 3055 | hash, sizeof( hash ), &hash_len ) ); |
| 3056 | |
| 3057 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 3058 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 3059 | PSA_ERROR_BAD_STATE ); |
| 3060 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 3061 | PSA_ERROR_BAD_STATE ); |
| 3062 | |
| 3063 | exit: |
| 3064 | psa_hash_abort( &op_target ); |
| 3065 | psa_hash_abort( &op_init ); |
| 3066 | psa_hash_abort( &op_setup ); |
| 3067 | psa_hash_abort( &op_finished ); |
| 3068 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3069 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3070 | } |
| 3071 | /* END_CASE */ |
| 3072 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3073 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3074 | void mac_operation_init( ) |
| 3075 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3076 | const uint8_t input[1] = { 0 }; |
| 3077 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3078 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3079 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3080 | * 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] | 3081 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3082 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 3083 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3084 | psa_mac_operation_t zero; |
| 3085 | |
| 3086 | memset( &zero, 0, sizeof( zero ) ); |
| 3087 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3088 | /* A freshly-initialized MAC operation should not be usable. */ |
| 3089 | TEST_EQUAL( psa_mac_update( &func, |
| 3090 | input, sizeof( input ) ), |
| 3091 | PSA_ERROR_BAD_STATE ); |
| 3092 | TEST_EQUAL( psa_mac_update( &init, |
| 3093 | input, sizeof( input ) ), |
| 3094 | PSA_ERROR_BAD_STATE ); |
| 3095 | TEST_EQUAL( psa_mac_update( &zero, |
| 3096 | input, sizeof( input ) ), |
| 3097 | PSA_ERROR_BAD_STATE ); |
| 3098 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3099 | /* A default MAC operation should be abortable without error. */ |
| 3100 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 3101 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 3102 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3103 | } |
| 3104 | /* END_CASE */ |
| 3105 | |
| 3106 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3107 | void mac_setup( int key_type_arg, |
| 3108 | data_t *key, |
| 3109 | int alg_arg, |
| 3110 | int expected_status_arg ) |
| 3111 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3112 | psa_key_type_t key_type = key_type_arg; |
| 3113 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3114 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3115 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3116 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3117 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3118 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3119 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3120 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3121 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3122 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3123 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 3124 | &operation, &status ) ) |
| 3125 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3126 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3127 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3128 | /* The operation object should be reusable. */ |
| 3129 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3130 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3131 | smoke_test_key_data, |
| 3132 | sizeof( smoke_test_key_data ), |
| 3133 | KNOWN_SUPPORTED_MAC_ALG, |
| 3134 | &operation, &status ) ) |
| 3135 | goto exit; |
| 3136 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3137 | #endif |
| 3138 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3139 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3140 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3141 | } |
| 3142 | /* END_CASE */ |
| 3143 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3144 | /* 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] | 3145 | void mac_bad_order( ) |
| 3146 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3147 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3148 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3149 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3150 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3151 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3152 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3153 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3154 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3155 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3156 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3157 | size_t sign_mac_length = 0; |
| 3158 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3159 | const uint8_t verify_mac[] = { |
| 3160 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3161 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 3162 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 3163 | |
| 3164 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3165 | 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] | 3166 | psa_set_key_algorithm( &attributes, alg ); |
| 3167 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3168 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3169 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3170 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3171 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3172 | /* Call update without calling setup beforehand. */ |
| 3173 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3174 | PSA_ERROR_BAD_STATE ); |
| 3175 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3176 | |
| 3177 | /* Call sign finish without calling setup beforehand. */ |
| 3178 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 3179 | &sign_mac_length), |
| 3180 | PSA_ERROR_BAD_STATE ); |
| 3181 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3182 | |
| 3183 | /* Call verify finish without calling setup beforehand. */ |
| 3184 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3185 | verify_mac, sizeof( verify_mac ) ), |
| 3186 | PSA_ERROR_BAD_STATE ); |
| 3187 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3188 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3189 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3190 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3191 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3192 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3193 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3194 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3195 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3196 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3197 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3198 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3199 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3200 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3201 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3202 | sign_mac, sizeof( sign_mac ), |
| 3203 | &sign_mac_length ) ); |
| 3204 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3205 | PSA_ERROR_BAD_STATE ); |
| 3206 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3207 | |
| 3208 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3209 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3210 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3211 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3212 | verify_mac, sizeof( verify_mac ) ) ); |
| 3213 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3214 | PSA_ERROR_BAD_STATE ); |
| 3215 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3216 | |
| 3217 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3218 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3219 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3220 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3221 | sign_mac, sizeof( sign_mac ), |
| 3222 | &sign_mac_length ) ); |
| 3223 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3224 | sign_mac, sizeof( sign_mac ), |
| 3225 | &sign_mac_length ), |
| 3226 | PSA_ERROR_BAD_STATE ); |
| 3227 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3228 | |
| 3229 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3230 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3231 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3232 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3233 | verify_mac, sizeof( verify_mac ) ) ); |
| 3234 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3235 | verify_mac, sizeof( verify_mac ) ), |
| 3236 | PSA_ERROR_BAD_STATE ); |
| 3237 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3238 | |
| 3239 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3240 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3241 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3242 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3243 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3244 | verify_mac, sizeof( verify_mac ) ), |
| 3245 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3246 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3247 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3248 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3249 | |
| 3250 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3251 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3252 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3253 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3254 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3255 | sign_mac, sizeof( sign_mac ), |
| 3256 | &sign_mac_length ), |
| 3257 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3258 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3259 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3260 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3261 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3262 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3263 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3264 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3265 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3266 | } |
| 3267 | /* END_CASE */ |
| 3268 | |
| 3269 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3270 | void mac_sign_verify_multi( int key_type_arg, |
| 3271 | data_t *key_data, |
| 3272 | int alg_arg, |
| 3273 | data_t *input, |
| 3274 | int is_verify, |
| 3275 | data_t *expected_mac ) |
| 3276 | { |
| 3277 | size_t data_part_len = 0; |
| 3278 | |
| 3279 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3280 | { |
| 3281 | /* Split data into length(data_part_len) parts. */ |
| 3282 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3283 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3284 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3285 | alg_arg, |
| 3286 | input, data_part_len, |
| 3287 | expected_mac, |
| 3288 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3289 | break; |
| 3290 | |
| 3291 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3292 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3293 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3294 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3295 | alg_arg, |
| 3296 | input, data_part_len, |
| 3297 | expected_mac, |
| 3298 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3299 | break; |
| 3300 | } |
| 3301 | |
| 3302 | /* Goto is required to silence warnings about unused labels, as we |
| 3303 | * don't actually do any test assertions in this function. */ |
| 3304 | goto exit; |
| 3305 | } |
| 3306 | /* END_CASE */ |
| 3307 | |
| 3308 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3309 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3310 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3311 | int alg_arg, |
| 3312 | data_t *input, |
| 3313 | data_t *expected_mac ) |
| 3314 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3315 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3316 | psa_key_type_t key_type = key_type_arg; |
| 3317 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3318 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3319 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3320 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3321 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3322 | 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] | 3323 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3324 | const size_t output_sizes_to_test[] = { |
| 3325 | 0, |
| 3326 | 1, |
| 3327 | expected_mac->len - 1, |
| 3328 | expected_mac->len, |
| 3329 | expected_mac->len + 1, |
| 3330 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3331 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3332 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3333 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3334 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3335 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3336 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3337 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3338 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3339 | psa_set_key_algorithm( &attributes, alg ); |
| 3340 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3341 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3342 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3343 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3344 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3345 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3346 | { |
| 3347 | const size_t output_size = output_sizes_to_test[i]; |
| 3348 | psa_status_t expected_status = |
| 3349 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3350 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3351 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3352 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3353 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3354 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3355 | /* Calculate the MAC, one-shot case. */ |
| 3356 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3357 | input->x, input->len, |
| 3358 | actual_mac, output_size, &mac_length ), |
| 3359 | expected_status ); |
| 3360 | if( expected_status == PSA_SUCCESS ) |
| 3361 | { |
| 3362 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3363 | actual_mac, mac_length ); |
| 3364 | } |
| 3365 | |
| 3366 | if( output_size > 0 ) |
| 3367 | memset( actual_mac, 0, output_size ); |
| 3368 | |
| 3369 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3370 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3371 | PSA_ASSERT( psa_mac_update( &operation, |
| 3372 | input->x, input->len ) ); |
| 3373 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3374 | actual_mac, output_size, |
| 3375 | &mac_length ), |
| 3376 | expected_status ); |
| 3377 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3378 | |
| 3379 | if( expected_status == PSA_SUCCESS ) |
| 3380 | { |
| 3381 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3382 | actual_mac, mac_length ); |
| 3383 | } |
| 3384 | mbedtls_free( actual_mac ); |
| 3385 | actual_mac = NULL; |
| 3386 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3387 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3388 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3389 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3390 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3391 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3392 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3393 | } |
| 3394 | /* END_CASE */ |
| 3395 | |
| 3396 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3397 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3398 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3399 | int alg_arg, |
| 3400 | data_t *input, |
| 3401 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3402 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3403 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3404 | psa_key_type_t key_type = key_type_arg; |
| 3405 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3406 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3407 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3408 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3409 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3410 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3411 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3412 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3413 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3414 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3415 | psa_set_key_algorithm( &attributes, alg ); |
| 3416 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3417 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3418 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3419 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3420 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3421 | /* Verify correct MAC, one-shot case. */ |
| 3422 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3423 | expected_mac->x, expected_mac->len ) ); |
| 3424 | |
| 3425 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3426 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3427 | PSA_ASSERT( psa_mac_update( &operation, |
| 3428 | input->x, input->len ) ); |
| 3429 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3430 | expected_mac->x, |
| 3431 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3432 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3433 | /* Test a MAC that's too short, one-shot case. */ |
| 3434 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3435 | input->x, input->len, |
| 3436 | expected_mac->x, |
| 3437 | expected_mac->len - 1 ), |
| 3438 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3439 | |
| 3440 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3441 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3442 | PSA_ASSERT( psa_mac_update( &operation, |
| 3443 | input->x, input->len ) ); |
| 3444 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3445 | expected_mac->x, |
| 3446 | expected_mac->len - 1 ), |
| 3447 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3448 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3449 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3450 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3451 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3452 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3453 | input->x, input->len, |
| 3454 | perturbed_mac, expected_mac->len + 1 ), |
| 3455 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3456 | |
| 3457 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3458 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3459 | PSA_ASSERT( psa_mac_update( &operation, |
| 3460 | input->x, input->len ) ); |
| 3461 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3462 | perturbed_mac, |
| 3463 | expected_mac->len + 1 ), |
| 3464 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3465 | |
| 3466 | /* Test changing one byte. */ |
| 3467 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3468 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3469 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3470 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3471 | |
| 3472 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3473 | input->x, input->len, |
| 3474 | perturbed_mac, expected_mac->len ), |
| 3475 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3476 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3477 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3478 | PSA_ASSERT( psa_mac_update( &operation, |
| 3479 | input->x, input->len ) ); |
| 3480 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3481 | perturbed_mac, |
| 3482 | expected_mac->len ), |
| 3483 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3484 | perturbed_mac[i] ^= 1; |
| 3485 | } |
| 3486 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3487 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3488 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3489 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3490 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3491 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3492 | } |
| 3493 | /* END_CASE */ |
| 3494 | |
| 3495 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3496 | void cipher_operation_init( ) |
| 3497 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3498 | const uint8_t input[1] = { 0 }; |
| 3499 | unsigned char output[1] = { 0 }; |
| 3500 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3501 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3502 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3503 | * 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] | 3504 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3505 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3506 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3507 | psa_cipher_operation_t zero; |
| 3508 | |
| 3509 | memset( &zero, 0, sizeof( zero ) ); |
| 3510 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3511 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3512 | TEST_EQUAL( psa_cipher_update( &func, |
| 3513 | input, sizeof( input ), |
| 3514 | output, sizeof( output ), |
| 3515 | &output_length ), |
| 3516 | PSA_ERROR_BAD_STATE ); |
| 3517 | TEST_EQUAL( psa_cipher_update( &init, |
| 3518 | input, sizeof( input ), |
| 3519 | output, sizeof( output ), |
| 3520 | &output_length ), |
| 3521 | PSA_ERROR_BAD_STATE ); |
| 3522 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3523 | input, sizeof( input ), |
| 3524 | output, sizeof( output ), |
| 3525 | &output_length ), |
| 3526 | PSA_ERROR_BAD_STATE ); |
| 3527 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3528 | /* A default cipher operation should be abortable without error. */ |
| 3529 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3530 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3531 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3532 | } |
| 3533 | /* END_CASE */ |
| 3534 | |
| 3535 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3536 | void cipher_setup( int key_type_arg, |
| 3537 | data_t *key, |
| 3538 | int alg_arg, |
| 3539 | int expected_status_arg ) |
| 3540 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3541 | psa_key_type_t key_type = key_type_arg; |
| 3542 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3543 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3544 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3545 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3546 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3547 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3548 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3549 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3550 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3551 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3552 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3553 | &operation, &status ) ) |
| 3554 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3555 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3556 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3557 | /* The operation object should be reusable. */ |
| 3558 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3559 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3560 | smoke_test_key_data, |
| 3561 | sizeof( smoke_test_key_data ), |
| 3562 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3563 | &operation, &status ) ) |
| 3564 | goto exit; |
| 3565 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3566 | #endif |
| 3567 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3568 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3569 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3570 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3571 | } |
| 3572 | /* END_CASE */ |
| 3573 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3574 | /* 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] | 3575 | void cipher_bad_order( ) |
| 3576 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3577 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3578 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3579 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3580 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3581 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3582 | 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] | 3583 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3584 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3585 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3586 | const uint8_t text[] = { |
| 3587 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3588 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3589 | 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] | 3590 | size_t length = 0; |
| 3591 | |
| 3592 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3593 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3594 | psa_set_key_algorithm( &attributes, alg ); |
| 3595 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3596 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3597 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3598 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3599 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3600 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3601 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3602 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3603 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3604 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3605 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3606 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3607 | |
| 3608 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3609 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3610 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3611 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3612 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3613 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3614 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3615 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3616 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3617 | /* Generate an IV without calling setup beforehand. */ |
| 3618 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3619 | buffer, sizeof( buffer ), |
| 3620 | &length ), |
| 3621 | PSA_ERROR_BAD_STATE ); |
| 3622 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3623 | |
| 3624 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3625 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3626 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3627 | buffer, sizeof( buffer ), |
| 3628 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3629 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3630 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3631 | buffer, sizeof( buffer ), |
| 3632 | &length ), |
| 3633 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3634 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3635 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3636 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3637 | |
| 3638 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3639 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3640 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3641 | iv, sizeof( iv ) ) ); |
| 3642 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3643 | buffer, sizeof( buffer ), |
| 3644 | &length ), |
| 3645 | PSA_ERROR_BAD_STATE ); |
| 3646 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3647 | |
| 3648 | /* Set an IV without calling setup beforehand. */ |
| 3649 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3650 | iv, sizeof( iv ) ), |
| 3651 | PSA_ERROR_BAD_STATE ); |
| 3652 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3653 | |
| 3654 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3655 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3656 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3657 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3658 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3659 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3660 | iv, sizeof( iv ) ), |
| 3661 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3662 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3663 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3664 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3665 | |
| 3666 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3667 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3668 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3669 | buffer, sizeof( buffer ), |
| 3670 | &length ) ); |
| 3671 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3672 | iv, sizeof( iv ) ), |
| 3673 | PSA_ERROR_BAD_STATE ); |
| 3674 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3675 | |
| 3676 | /* Call update without calling setup beforehand. */ |
| 3677 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3678 | text, sizeof( text ), |
| 3679 | buffer, sizeof( buffer ), |
| 3680 | &length ), |
| 3681 | PSA_ERROR_BAD_STATE ); |
| 3682 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3683 | |
| 3684 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3685 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3686 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3687 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3688 | text, sizeof( text ), |
| 3689 | buffer, sizeof( buffer ), |
| 3690 | &length ), |
| 3691 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3692 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3693 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3694 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3695 | |
| 3696 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3697 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3698 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3699 | iv, sizeof( iv ) ) ); |
| 3700 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3701 | buffer, sizeof( buffer ), &length ) ); |
| 3702 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3703 | text, sizeof( text ), |
| 3704 | buffer, sizeof( buffer ), |
| 3705 | &length ), |
| 3706 | PSA_ERROR_BAD_STATE ); |
| 3707 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3708 | |
| 3709 | /* Call finish without calling setup beforehand. */ |
| 3710 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3711 | buffer, sizeof( buffer ), &length ), |
| 3712 | PSA_ERROR_BAD_STATE ); |
| 3713 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3714 | |
| 3715 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3716 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3717 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3718 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3719 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3720 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3721 | buffer, sizeof( buffer ), &length ), |
| 3722 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3723 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3724 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3725 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3726 | |
| 3727 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3728 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3729 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3730 | iv, sizeof( iv ) ) ); |
| 3731 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3732 | buffer, sizeof( buffer ), &length ) ); |
| 3733 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3734 | buffer, sizeof( buffer ), &length ), |
| 3735 | PSA_ERROR_BAD_STATE ); |
| 3736 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3737 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3738 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3739 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3740 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3741 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3742 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3743 | } |
| 3744 | /* END_CASE */ |
| 3745 | |
| 3746 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3747 | void cipher_encrypt_fail( int alg_arg, |
| 3748 | int key_type_arg, |
| 3749 | data_t *key_data, |
| 3750 | data_t *input, |
| 3751 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3752 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3753 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3754 | psa_status_t status; |
| 3755 | psa_key_type_t key_type = key_type_arg; |
| 3756 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3757 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3758 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3759 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3760 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3761 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3762 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3763 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3764 | size_t function_output_length; |
| 3765 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3766 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3767 | |
| 3768 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3769 | { |
| 3770 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3771 | |
| 3772 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3773 | psa_set_key_algorithm( &attributes, alg ); |
| 3774 | psa_set_key_type( &attributes, key_type ); |
| 3775 | |
| 3776 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3777 | input->len ); |
| 3778 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3779 | |
| 3780 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3781 | &key ) ); |
| 3782 | } |
| 3783 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3784 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3785 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3786 | output_buffer_size, &output_length ); |
| 3787 | |
| 3788 | TEST_EQUAL( status, expected_status ); |
| 3789 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3790 | /* Encrypt, multi-part */ |
| 3791 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3792 | if( status == PSA_SUCCESS ) |
| 3793 | { |
| 3794 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3795 | { |
| 3796 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3797 | iv, iv_size, |
| 3798 | &iv_length ) ); |
| 3799 | } |
| 3800 | |
| 3801 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3802 | output, output_buffer_size, |
| 3803 | &function_output_length ); |
| 3804 | if( status == PSA_SUCCESS ) |
| 3805 | { |
| 3806 | output_length += function_output_length; |
| 3807 | |
| 3808 | status = psa_cipher_finish( &operation, output + output_length, |
| 3809 | output_buffer_size - output_length, |
| 3810 | &function_output_length ); |
| 3811 | |
| 3812 | TEST_EQUAL( status, expected_status ); |
| 3813 | } |
| 3814 | else |
| 3815 | { |
| 3816 | TEST_EQUAL( status, expected_status ); |
| 3817 | } |
| 3818 | } |
| 3819 | else |
| 3820 | { |
| 3821 | TEST_EQUAL( status, expected_status ); |
| 3822 | } |
| 3823 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3824 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3825 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3826 | mbedtls_free( output ); |
| 3827 | psa_destroy_key( key ); |
| 3828 | PSA_DONE( ); |
| 3829 | } |
| 3830 | /* END_CASE */ |
| 3831 | |
| 3832 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3833 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3834 | data_t *input, int iv_length, |
| 3835 | int expected_result ) |
| 3836 | { |
| 3837 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3838 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3839 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3840 | size_t output_buffer_size = 0; |
| 3841 | unsigned char *output = NULL; |
| 3842 | |
| 3843 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3844 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3845 | |
| 3846 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3847 | |
| 3848 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3849 | psa_set_key_algorithm( &attributes, alg ); |
| 3850 | psa_set_key_type( &attributes, key_type ); |
| 3851 | |
| 3852 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3853 | &key ) ); |
| 3854 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3855 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3856 | iv_length ) ); |
| 3857 | |
| 3858 | exit: |
| 3859 | psa_cipher_abort( &operation ); |
| 3860 | mbedtls_free( output ); |
| 3861 | psa_destroy_key( key ); |
| 3862 | PSA_DONE( ); |
| 3863 | } |
| 3864 | /* END_CASE */ |
| 3865 | |
| 3866 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3867 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3868 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3869 | { |
| 3870 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3871 | psa_key_type_t key_type = key_type_arg; |
| 3872 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3873 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3874 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3875 | unsigned char *output = NULL; |
| 3876 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3877 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3878 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3879 | |
| 3880 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3881 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3882 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3883 | TEST_LE_U( ciphertext->len, |
| 3884 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3885 | 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] | 3886 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3887 | TEST_LE_U( plaintext->len, |
| 3888 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3889 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3890 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3891 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3892 | |
| 3893 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3894 | psa_set_key_usage_flags( &attributes, |
| 3895 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3896 | psa_set_key_algorithm( &attributes, alg ); |
| 3897 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3898 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3899 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3900 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3901 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3902 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3903 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3904 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3905 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3906 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3907 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3908 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3909 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3910 | PSA_ERROR_BAD_STATE ); |
| 3911 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3912 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3913 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3914 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3915 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3916 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3917 | PSA_ASSERT( psa_cipher_decrypt_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 ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3920 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3921 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3922 | /* Multipart encryption */ |
| 3923 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3924 | output_length = 0; |
| 3925 | length = ~0; |
| 3926 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3927 | plaintext->x, plaintext->len, |
| 3928 | output, output_buffer_size, |
| 3929 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3930 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3931 | output_length += length; |
| 3932 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3933 | output + output_length, |
| 3934 | output_buffer_size - output_length, |
| 3935 | &length ) ); |
| 3936 | output_length += length; |
| 3937 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3938 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3939 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3940 | /* Multipart encryption */ |
| 3941 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3942 | output_length = 0; |
| 3943 | length = ~0; |
| 3944 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3945 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3946 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3947 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3948 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3949 | output_length += length; |
| 3950 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3951 | output + output_length, |
| 3952 | output_buffer_size - output_length, |
| 3953 | &length ) ); |
| 3954 | output_length += length; |
| 3955 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 3956 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3957 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3958 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3959 | output_length = ~0; |
| 3960 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 3961 | output, output_buffer_size, |
| 3962 | &output_length ) ); |
| 3963 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 3964 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3965 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3966 | /* One-shot decryption */ |
| 3967 | output_length = ~0; |
| 3968 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 3969 | output, output_buffer_size, |
| 3970 | &output_length ) ); |
| 3971 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3972 | output, output_length ); |
| 3973 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3974 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3975 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3976 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3977 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3978 | psa_destroy_key( key ); |
| 3979 | PSA_DONE( ); |
| 3980 | } |
| 3981 | /* END_CASE */ |
| 3982 | |
| 3983 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3984 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 3985 | { |
| 3986 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3987 | psa_algorithm_t alg = alg_arg; |
| 3988 | psa_key_type_t key_type = key_type_arg; |
| 3989 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3990 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3991 | psa_status_t status; |
| 3992 | |
| 3993 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3994 | |
| 3995 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3996 | psa_set_key_algorithm( &attributes, alg ); |
| 3997 | psa_set_key_type( &attributes, key_type ); |
| 3998 | |
| 3999 | /* Usage of either of these two size macros would cause divide by zero |
| 4000 | * with incorrect key types previously. Input length should be irrelevant |
| 4001 | * here. */ |
| 4002 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 4003 | 0 ); |
| 4004 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 4005 | |
| 4006 | |
| 4007 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4008 | &key ) ); |
| 4009 | |
| 4010 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4011 | * Encrypt or decrypt will end up in the same place. */ |
| 4012 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 4013 | |
| 4014 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 4015 | |
| 4016 | exit: |
| 4017 | psa_cipher_abort( &operation ); |
| 4018 | psa_destroy_key( key ); |
| 4019 | PSA_DONE( ); |
| 4020 | } |
| 4021 | /* END_CASE */ |
| 4022 | |
| 4023 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4024 | void cipher_encrypt_validation( int alg_arg, |
| 4025 | int key_type_arg, |
| 4026 | data_t *key_data, |
| 4027 | data_t *input ) |
| 4028 | { |
| 4029 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4030 | psa_key_type_t key_type = key_type_arg; |
| 4031 | psa_algorithm_t alg = alg_arg; |
| 4032 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 4033 | unsigned char *output1 = NULL; |
| 4034 | size_t output1_buffer_size = 0; |
| 4035 | size_t output1_length = 0; |
| 4036 | unsigned char *output2 = NULL; |
| 4037 | size_t output2_buffer_size = 0; |
| 4038 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4039 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4040 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4041 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4042 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4043 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4044 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4045 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4046 | psa_set_key_algorithm( &attributes, alg ); |
| 4047 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4048 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4049 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 4050 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4051 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4052 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 4053 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 4054 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4055 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4056 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4057 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4058 | /* The one-shot cipher encryption uses generated iv so validating |
| 4059 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4060 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 4061 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4062 | TEST_LE_U( output1_length, |
| 4063 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4064 | TEST_LE_U( output1_length, |
| 4065 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4066 | |
| 4067 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 4068 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4069 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4070 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4071 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4072 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4073 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4074 | TEST_LE_U( function_output_length, |
| 4075 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4076 | TEST_LE_U( function_output_length, |
| 4077 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4078 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4079 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4080 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4081 | output2 + output2_length, |
| 4082 | output2_buffer_size - output2_length, |
| 4083 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4084 | TEST_LE_U( function_output_length, |
| 4085 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4086 | TEST_LE_U( function_output_length, |
| 4087 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4088 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4089 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4090 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4091 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 4092 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4093 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4094 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4095 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4096 | mbedtls_free( output1 ); |
| 4097 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4098 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4099 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4100 | } |
| 4101 | /* END_CASE */ |
| 4102 | |
| 4103 | /* BEGIN_CASE */ |
| 4104 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4105 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4106 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4107 | int first_part_size_arg, |
| 4108 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4109 | data_t *expected_output, |
| 4110 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4111 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4112 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4113 | psa_key_type_t key_type = key_type_arg; |
| 4114 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4115 | psa_status_t status; |
| 4116 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4117 | size_t first_part_size = first_part_size_arg; |
| 4118 | size_t output1_length = output1_length_arg; |
| 4119 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4120 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4121 | size_t output_buffer_size = 0; |
| 4122 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4123 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4124 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4125 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4126 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4127 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4128 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4129 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4130 | psa_set_key_algorithm( &attributes, alg ); |
| 4131 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4132 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4133 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4134 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4135 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4136 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4137 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4138 | if( iv->len > 0 ) |
| 4139 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4140 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4141 | } |
| 4142 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4143 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4144 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4145 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4146 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4147 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4148 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 4149 | output, output_buffer_size, |
| 4150 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4151 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4152 | TEST_LE_U( function_output_length, |
| 4153 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4154 | TEST_LE_U( function_output_length, |
| 4155 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4156 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4157 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4158 | if( first_part_size < input->len ) |
| 4159 | { |
| 4160 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4161 | input->x + first_part_size, |
| 4162 | input->len - first_part_size, |
| 4163 | ( output_buffer_size == 0 ? NULL : |
| 4164 | output + total_output_length ), |
| 4165 | output_buffer_size - total_output_length, |
| 4166 | &function_output_length ) ); |
| 4167 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4168 | TEST_LE_U( function_output_length, |
| 4169 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4170 | alg, |
| 4171 | input->len - first_part_size ) ); |
| 4172 | TEST_LE_U( function_output_length, |
| 4173 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4174 | total_output_length += function_output_length; |
| 4175 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4176 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4177 | status = psa_cipher_finish( &operation, |
| 4178 | ( output_buffer_size == 0 ? NULL : |
| 4179 | output + total_output_length ), |
| 4180 | output_buffer_size - total_output_length, |
| 4181 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4182 | TEST_LE_U( function_output_length, |
| 4183 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4184 | TEST_LE_U( function_output_length, |
| 4185 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4186 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4187 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4188 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4189 | if( expected_status == PSA_SUCCESS ) |
| 4190 | { |
| 4191 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4192 | |
| 4193 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4194 | output, total_output_length ); |
| 4195 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4196 | |
| 4197 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4198 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4199 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4200 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4201 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4202 | } |
| 4203 | /* END_CASE */ |
| 4204 | |
| 4205 | /* BEGIN_CASE */ |
| 4206 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4207 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4208 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4209 | int first_part_size_arg, |
| 4210 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4211 | data_t *expected_output, |
| 4212 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4213 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4214 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4215 | psa_key_type_t key_type = key_type_arg; |
| 4216 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4217 | psa_status_t status; |
| 4218 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4219 | size_t first_part_size = first_part_size_arg; |
| 4220 | size_t output1_length = output1_length_arg; |
| 4221 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4222 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4223 | size_t output_buffer_size = 0; |
| 4224 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4225 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4226 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4227 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4228 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4229 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4230 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4231 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4232 | psa_set_key_algorithm( &attributes, alg ); |
| 4233 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4234 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4235 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4236 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4237 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4238 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4239 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4240 | if( iv->len > 0 ) |
| 4241 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4242 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4243 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4244 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4245 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4246 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4247 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4248 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4249 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4250 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4251 | input->x, first_part_size, |
| 4252 | output, output_buffer_size, |
| 4253 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4254 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4255 | TEST_LE_U( function_output_length, |
| 4256 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4257 | TEST_LE_U( function_output_length, |
| 4258 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4259 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4260 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4261 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4262 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4263 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4264 | input->x + first_part_size, |
| 4265 | input->len - first_part_size, |
| 4266 | ( output_buffer_size == 0 ? NULL : |
| 4267 | output + total_output_length ), |
| 4268 | output_buffer_size - total_output_length, |
| 4269 | &function_output_length ) ); |
| 4270 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4271 | TEST_LE_U( function_output_length, |
| 4272 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4273 | alg, |
| 4274 | input->len - first_part_size ) ); |
| 4275 | TEST_LE_U( function_output_length, |
| 4276 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4277 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4278 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4279 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4280 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4281 | ( output_buffer_size == 0 ? NULL : |
| 4282 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4283 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4284 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4285 | TEST_LE_U( function_output_length, |
| 4286 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4287 | TEST_LE_U( function_output_length, |
| 4288 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4289 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4290 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4291 | |
| 4292 | if( expected_status == PSA_SUCCESS ) |
| 4293 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4294 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4295 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4296 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4297 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4298 | } |
| 4299 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4300 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4301 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4302 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4303 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4304 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4305 | } |
| 4306 | /* END_CASE */ |
| 4307 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4308 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4309 | void cipher_decrypt_fail( int alg_arg, |
| 4310 | int key_type_arg, |
| 4311 | data_t *key_data, |
| 4312 | data_t *iv, |
| 4313 | data_t *input_arg, |
| 4314 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4315 | { |
| 4316 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4317 | psa_status_t status; |
| 4318 | psa_key_type_t key_type = key_type_arg; |
| 4319 | psa_algorithm_t alg = alg_arg; |
| 4320 | psa_status_t expected_status = expected_status_arg; |
| 4321 | unsigned char *input = NULL; |
| 4322 | size_t input_buffer_size = 0; |
| 4323 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4324 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4325 | size_t output_buffer_size = 0; |
| 4326 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4327 | size_t function_output_length; |
| 4328 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4329 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4330 | |
| 4331 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4332 | { |
| 4333 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4334 | |
| 4335 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4336 | psa_set_key_algorithm( &attributes, alg ); |
| 4337 | psa_set_key_type( &attributes, key_type ); |
| 4338 | |
| 4339 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4340 | &key ) ); |
| 4341 | } |
| 4342 | |
| 4343 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4344 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4345 | if ( input_buffer_size > 0 ) |
| 4346 | { |
| 4347 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4348 | memcpy( input, iv->x, iv->len ); |
| 4349 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4350 | } |
| 4351 | |
| 4352 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4353 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4354 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4355 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4356 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4357 | output_buffer_size, &output_length ); |
| 4358 | TEST_EQUAL( status, expected_status ); |
| 4359 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4360 | /* Decrypt, multi-part */ |
| 4361 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4362 | if( status == PSA_SUCCESS ) |
| 4363 | { |
| 4364 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4365 | input_arg->len ) + |
| 4366 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4367 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4368 | |
| 4369 | if( iv->len > 0 ) |
| 4370 | { |
| 4371 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4372 | |
| 4373 | if( status != PSA_SUCCESS ) |
| 4374 | TEST_EQUAL( status, expected_status ); |
| 4375 | } |
| 4376 | |
| 4377 | if( status == PSA_SUCCESS ) |
| 4378 | { |
| 4379 | status = psa_cipher_update( &operation, |
| 4380 | input_arg->x, input_arg->len, |
| 4381 | output_multi, output_buffer_size, |
| 4382 | &function_output_length ); |
| 4383 | if( status == PSA_SUCCESS ) |
| 4384 | { |
| 4385 | output_length = function_output_length; |
| 4386 | |
| 4387 | status = psa_cipher_finish( &operation, |
| 4388 | output_multi + output_length, |
| 4389 | output_buffer_size - output_length, |
| 4390 | &function_output_length ); |
| 4391 | |
| 4392 | TEST_EQUAL( status, expected_status ); |
| 4393 | } |
| 4394 | else |
| 4395 | { |
| 4396 | TEST_EQUAL( status, expected_status ); |
| 4397 | } |
| 4398 | } |
| 4399 | else |
| 4400 | { |
| 4401 | TEST_EQUAL( status, expected_status ); |
| 4402 | } |
| 4403 | } |
| 4404 | else |
| 4405 | { |
| 4406 | TEST_EQUAL( status, expected_status ); |
| 4407 | } |
| 4408 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4409 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4410 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4411 | mbedtls_free( input ); |
| 4412 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4413 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4414 | psa_destroy_key( key ); |
| 4415 | PSA_DONE( ); |
| 4416 | } |
| 4417 | /* END_CASE */ |
| 4418 | |
| 4419 | /* BEGIN_CASE */ |
| 4420 | void cipher_decrypt( int alg_arg, |
| 4421 | int key_type_arg, |
| 4422 | data_t *key_data, |
| 4423 | data_t *iv, |
| 4424 | data_t *input_arg, |
| 4425 | data_t *expected_output ) |
| 4426 | { |
| 4427 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4428 | psa_key_type_t key_type = key_type_arg; |
| 4429 | psa_algorithm_t alg = alg_arg; |
| 4430 | unsigned char *input = NULL; |
| 4431 | size_t input_buffer_size = 0; |
| 4432 | unsigned char *output = NULL; |
| 4433 | size_t output_buffer_size = 0; |
| 4434 | size_t output_length = 0; |
| 4435 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4436 | |
| 4437 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4438 | |
| 4439 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4440 | psa_set_key_algorithm( &attributes, alg ); |
| 4441 | psa_set_key_type( &attributes, key_type ); |
| 4442 | |
| 4443 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4444 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4445 | if ( input_buffer_size > 0 ) |
| 4446 | { |
| 4447 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4448 | memcpy( input, iv->x, iv->len ); |
| 4449 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4450 | } |
| 4451 | |
| 4452 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4453 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4454 | |
| 4455 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4456 | &key ) ); |
| 4457 | |
| 4458 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4459 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4460 | TEST_LE_U( output_length, |
| 4461 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4462 | TEST_LE_U( output_length, |
| 4463 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4464 | |
| 4465 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4466 | output, output_length ); |
| 4467 | exit: |
| 4468 | mbedtls_free( input ); |
| 4469 | mbedtls_free( output ); |
| 4470 | psa_destroy_key( key ); |
| 4471 | PSA_DONE( ); |
| 4472 | } |
| 4473 | /* END_CASE */ |
| 4474 | |
| 4475 | /* BEGIN_CASE */ |
| 4476 | void cipher_verify_output( int alg_arg, |
| 4477 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4478 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4479 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4480 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4481 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4482 | psa_key_type_t key_type = key_type_arg; |
| 4483 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4484 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4485 | size_t output1_size = 0; |
| 4486 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4487 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4488 | size_t output2_size = 0; |
| 4489 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4490 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4491 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4492 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4493 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4494 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4495 | psa_set_key_algorithm( &attributes, alg ); |
| 4496 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4497 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4498 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4499 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4500 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4501 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4502 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4503 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4504 | output1, output1_size, |
| 4505 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4506 | TEST_LE_U( output1_length, |
| 4507 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4508 | TEST_LE_U( output1_length, |
| 4509 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4510 | |
| 4511 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4512 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4513 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4514 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4515 | output2, output2_size, |
| 4516 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4517 | TEST_LE_U( output2_length, |
| 4518 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4519 | TEST_LE_U( output2_length, |
| 4520 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4521 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4522 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4523 | |
| 4524 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4525 | mbedtls_free( output1 ); |
| 4526 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4527 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4528 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4529 | } |
| 4530 | /* END_CASE */ |
| 4531 | |
| 4532 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4533 | void cipher_verify_output_multipart( int alg_arg, |
| 4534 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4535 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4536 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4537 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4538 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4539 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4540 | psa_key_type_t key_type = key_type_arg; |
| 4541 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4542 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4543 | unsigned char iv[16] = {0}; |
| 4544 | size_t iv_size = 16; |
| 4545 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4546 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4547 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4548 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4549 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4550 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4551 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4552 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4553 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4554 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4555 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4556 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4557 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4558 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4559 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4560 | psa_set_key_algorithm( &attributes, alg ); |
| 4561 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4562 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4563 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4564 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4565 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4566 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4567 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4568 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4569 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4570 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4571 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4572 | iv, iv_size, |
| 4573 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4574 | } |
| 4575 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4576 | 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] | 4577 | TEST_LE_U( output1_buffer_size, |
| 4578 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4579 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4580 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4581 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4582 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4583 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4584 | output1, output1_buffer_size, |
| 4585 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4586 | TEST_LE_U( function_output_length, |
| 4587 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4588 | TEST_LE_U( function_output_length, |
| 4589 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4590 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4591 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4592 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4593 | input->x + first_part_size, |
| 4594 | input->len - first_part_size, |
| 4595 | output1, output1_buffer_size, |
| 4596 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4597 | TEST_LE_U( function_output_length, |
| 4598 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4599 | alg, |
| 4600 | input->len - first_part_size ) ); |
| 4601 | TEST_LE_U( function_output_length, |
| 4602 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4603 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4604 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4605 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4606 | output1 + output1_length, |
| 4607 | output1_buffer_size - output1_length, |
| 4608 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4609 | TEST_LE_U( function_output_length, |
| 4610 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4611 | TEST_LE_U( function_output_length, |
| 4612 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4613 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4614 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4615 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4616 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4617 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4618 | TEST_LE_U( output2_buffer_size, |
| 4619 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4620 | TEST_LE_U( output2_buffer_size, |
| 4621 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4622 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4623 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4624 | if( iv_length > 0 ) |
| 4625 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4626 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4627 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4628 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4629 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4630 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4631 | output2, output2_buffer_size, |
| 4632 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4633 | TEST_LE_U( function_output_length, |
| 4634 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4635 | TEST_LE_U( function_output_length, |
| 4636 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4637 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4638 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4639 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4640 | output1 + first_part_size, |
| 4641 | output1_length - first_part_size, |
| 4642 | output2, output2_buffer_size, |
| 4643 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4644 | TEST_LE_U( function_output_length, |
| 4645 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4646 | alg, |
| 4647 | output1_length - first_part_size ) ); |
| 4648 | TEST_LE_U( function_output_length, |
| 4649 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4650 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4651 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4652 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4653 | output2 + output2_length, |
| 4654 | output2_buffer_size - output2_length, |
| 4655 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4656 | TEST_LE_U( function_output_length, |
| 4657 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4658 | TEST_LE_U( function_output_length, |
| 4659 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4660 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4661 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4662 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4663 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4664 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4665 | |
| 4666 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4667 | psa_cipher_abort( &operation1 ); |
| 4668 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4669 | mbedtls_free( output1 ); |
| 4670 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4671 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4672 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4673 | } |
| 4674 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4675 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4676 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4677 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4678 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4679 | data_t *nonce, |
| 4680 | data_t *additional_data, |
| 4681 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4682 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4683 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4684 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4685 | psa_key_type_t key_type = key_type_arg; |
| 4686 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4687 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4688 | unsigned char *output_data = NULL; |
| 4689 | size_t output_size = 0; |
| 4690 | size_t output_length = 0; |
| 4691 | unsigned char *output_data2 = NULL; |
| 4692 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4693 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4694 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4695 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4696 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4697 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4698 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4699 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4700 | psa_set_key_algorithm( &attributes, alg ); |
| 4701 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4702 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4703 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4704 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4705 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4706 | key_bits = psa_get_key_bits( &attributes ); |
| 4707 | |
| 4708 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4709 | alg ); |
| 4710 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4711 | * should be exact. */ |
| 4712 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4713 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4714 | { |
| 4715 | TEST_EQUAL( output_size, |
| 4716 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4717 | TEST_LE_U( output_size, |
| 4718 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4719 | } |
| 4720 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4721 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4722 | status = psa_aead_encrypt( key, alg, |
| 4723 | nonce->x, nonce->len, |
| 4724 | additional_data->x, |
| 4725 | additional_data->len, |
| 4726 | input_data->x, input_data->len, |
| 4727 | output_data, output_size, |
| 4728 | &output_length ); |
| 4729 | |
| 4730 | /* If the operation is not supported, just skip and not fail in case the |
| 4731 | * encryption involves a common limitation of cryptography hardwares and |
| 4732 | * an alternative implementation. */ |
| 4733 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4734 | { |
| 4735 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4736 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4737 | } |
| 4738 | |
| 4739 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4740 | |
| 4741 | if( PSA_SUCCESS == expected_result ) |
| 4742 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4743 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4744 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4745 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4746 | * should be exact. */ |
| 4747 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4748 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4749 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4750 | TEST_LE_U( input_data->len, |
| 4751 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4752 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4753 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4754 | nonce->x, nonce->len, |
| 4755 | additional_data->x, |
| 4756 | additional_data->len, |
| 4757 | output_data, output_length, |
| 4758 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4759 | &output_length2 ), |
| 4760 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4761 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4762 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4763 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4764 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4765 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4766 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4767 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4768 | mbedtls_free( output_data ); |
| 4769 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4770 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4771 | } |
| 4772 | /* END_CASE */ |
| 4773 | |
| 4774 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4775 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4776 | int alg_arg, |
| 4777 | data_t *nonce, |
| 4778 | data_t *additional_data, |
| 4779 | data_t *input_data, |
| 4780 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4781 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4782 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4783 | psa_key_type_t key_type = key_type_arg; |
| 4784 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4785 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4786 | unsigned char *output_data = NULL; |
| 4787 | size_t output_size = 0; |
| 4788 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4789 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4790 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4791 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4792 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4793 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4794 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4795 | psa_set_key_algorithm( &attributes, alg ); |
| 4796 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4797 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4798 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4799 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4800 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4801 | key_bits = psa_get_key_bits( &attributes ); |
| 4802 | |
| 4803 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4804 | alg ); |
| 4805 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4806 | * should be exact. */ |
| 4807 | TEST_EQUAL( output_size, |
| 4808 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4809 | TEST_LE_U( output_size, |
| 4810 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4811 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4812 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4813 | status = psa_aead_encrypt( key, alg, |
| 4814 | nonce->x, nonce->len, |
| 4815 | additional_data->x, additional_data->len, |
| 4816 | input_data->x, input_data->len, |
| 4817 | output_data, output_size, |
| 4818 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4819 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4820 | /* If the operation is not supported, just skip and not fail in case the |
| 4821 | * encryption involves a common limitation of cryptography hardwares and |
| 4822 | * an alternative implementation. */ |
| 4823 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4824 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4825 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4826 | 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] | 4827 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4828 | |
| 4829 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4830 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4831 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4832 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4833 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4834 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4835 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4836 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4837 | } |
| 4838 | /* END_CASE */ |
| 4839 | |
| 4840 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4841 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4842 | int alg_arg, |
| 4843 | data_t *nonce, |
| 4844 | data_t *additional_data, |
| 4845 | data_t *input_data, |
| 4846 | data_t *expected_data, |
| 4847 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4848 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4849 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4850 | psa_key_type_t key_type = key_type_arg; |
| 4851 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4852 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4853 | unsigned char *output_data = NULL; |
| 4854 | size_t output_size = 0; |
| 4855 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4856 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4857 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4858 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4859 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4860 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4861 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4862 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4863 | psa_set_key_algorithm( &attributes, alg ); |
| 4864 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4865 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4866 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4867 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4868 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4869 | key_bits = psa_get_key_bits( &attributes ); |
| 4870 | |
| 4871 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4872 | alg ); |
| 4873 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4874 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4875 | { |
| 4876 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4877 | * should be exact. */ |
| 4878 | TEST_EQUAL( output_size, |
| 4879 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4880 | TEST_LE_U( output_size, |
| 4881 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4882 | } |
| 4883 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4884 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4885 | status = psa_aead_decrypt( key, alg, |
| 4886 | nonce->x, nonce->len, |
| 4887 | additional_data->x, |
| 4888 | additional_data->len, |
| 4889 | input_data->x, input_data->len, |
| 4890 | output_data, output_size, |
| 4891 | &output_length ); |
| 4892 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4893 | /* If the operation is not supported, just skip and not fail in case the |
| 4894 | * decryption involves a common limitation of cryptography hardwares and |
| 4895 | * an alternative implementation. */ |
| 4896 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4897 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4898 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4899 | 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] | 4900 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4901 | |
| 4902 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4903 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4904 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4905 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4906 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4907 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4908 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4909 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4910 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4911 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4912 | } |
| 4913 | /* END_CASE */ |
| 4914 | |
| 4915 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4916 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4917 | int alg_arg, |
| 4918 | data_t *nonce, |
| 4919 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4920 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4921 | int do_set_lengths, |
| 4922 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4923 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4924 | size_t ad_part_len = 0; |
| 4925 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4926 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4927 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4928 | 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] | 4929 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4930 | mbedtls_test_set_step( ad_part_len ); |
| 4931 | |
| 4932 | if( do_set_lengths ) |
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 | if( ad_part_len & 0x01 ) |
| 4935 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4936 | else |
| 4937 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4938 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4939 | |
| 4940 | /* Split ad into length(ad_part_len) parts. */ |
| 4941 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4942 | alg_arg, nonce, |
| 4943 | additional_data, |
| 4944 | ad_part_len, |
| 4945 | input_data, -1, |
| 4946 | set_lengths_method, |
| 4947 | expected_output, |
| 4948 | 1, 0 ) ) |
| 4949 | break; |
| 4950 | |
| 4951 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4952 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4953 | |
| 4954 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4955 | alg_arg, nonce, |
| 4956 | additional_data, |
| 4957 | ad_part_len, |
| 4958 | input_data, -1, |
| 4959 | set_lengths_method, |
| 4960 | expected_output, |
| 4961 | 1, 1 ) ) |
| 4962 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4963 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4964 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4965 | 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] | 4966 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4967 | /* Split data into length(data_part_len) parts. */ |
| 4968 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 4969 | |
| 4970 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4971 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4972 | if( data_part_len & 0x01 ) |
| 4973 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4974 | else |
| 4975 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4976 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4977 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4978 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4979 | alg_arg, nonce, |
| 4980 | additional_data, -1, |
| 4981 | input_data, data_part_len, |
| 4982 | set_lengths_method, |
| 4983 | expected_output, |
| 4984 | 1, 0 ) ) |
| 4985 | break; |
| 4986 | |
| 4987 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 4988 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 4989 | |
| 4990 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4991 | alg_arg, nonce, |
| 4992 | additional_data, -1, |
| 4993 | input_data, data_part_len, |
| 4994 | set_lengths_method, |
| 4995 | expected_output, |
| 4996 | 1, 1 ) ) |
| 4997 | break; |
| 4998 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4999 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5000 | /* Goto is required to silence warnings about unused labels, as we |
| 5001 | * don't actually do any test assertions in this function. */ |
| 5002 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5003 | } |
| 5004 | /* END_CASE */ |
| 5005 | |
| 5006 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5007 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 5008 | int alg_arg, |
| 5009 | data_t *nonce, |
| 5010 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5011 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 5012 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5013 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5014 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5015 | size_t ad_part_len = 0; |
| 5016 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5017 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5018 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5019 | 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] | 5020 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5021 | /* Split ad into length(ad_part_len) parts. */ |
| 5022 | mbedtls_test_set_step( ad_part_len ); |
| 5023 | |
| 5024 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5025 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5026 | if( ad_part_len & 0x01 ) |
| 5027 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5028 | else |
| 5029 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5030 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5031 | |
| 5032 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5033 | alg_arg, nonce, |
| 5034 | additional_data, |
| 5035 | ad_part_len, |
| 5036 | input_data, -1, |
| 5037 | set_lengths_method, |
| 5038 | expected_output, |
| 5039 | 0, 0 ) ) |
| 5040 | break; |
| 5041 | |
| 5042 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5043 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5044 | |
| 5045 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5046 | alg_arg, nonce, |
| 5047 | additional_data, |
| 5048 | ad_part_len, |
| 5049 | input_data, -1, |
| 5050 | set_lengths_method, |
| 5051 | expected_output, |
| 5052 | 0, 1 ) ) |
| 5053 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5054 | } |
| 5055 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5056 | 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] | 5057 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5058 | /* Split data into length(data_part_len) parts. */ |
| 5059 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5060 | |
| 5061 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5062 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5063 | if( data_part_len & 0x01 ) |
| 5064 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5065 | else |
| 5066 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5067 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5068 | |
| 5069 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5070 | alg_arg, nonce, |
| 5071 | additional_data, -1, |
| 5072 | input_data, data_part_len, |
| 5073 | set_lengths_method, |
| 5074 | expected_output, |
| 5075 | 0, 0 ) ) |
| 5076 | break; |
| 5077 | |
| 5078 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5079 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5080 | |
| 5081 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5082 | alg_arg, nonce, |
| 5083 | additional_data, -1, |
| 5084 | input_data, data_part_len, |
| 5085 | set_lengths_method, |
| 5086 | expected_output, |
| 5087 | 0, 1 ) ) |
| 5088 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5089 | } |
| 5090 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5091 | /* Goto is required to silence warnings about unused labels, as we |
| 5092 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5093 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5094 | } |
| 5095 | /* END_CASE */ |
| 5096 | |
| 5097 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5098 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 5099 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5100 | int nonce_length, |
| 5101 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5102 | data_t *additional_data, |
| 5103 | data_t *input_data, |
| 5104 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5105 | { |
| 5106 | |
| 5107 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5108 | psa_key_type_t key_type = key_type_arg; |
| 5109 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5110 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5111 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5112 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5113 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5114 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5115 | size_t actual_nonce_length = 0; |
| 5116 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5117 | unsigned char *output = NULL; |
| 5118 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5119 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5120 | size_t ciphertext_size = 0; |
| 5121 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5122 | size_t tag_length = 0; |
| 5123 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5124 | |
| 5125 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5126 | |
| 5127 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5128 | psa_set_key_algorithm( & attributes, alg ); |
| 5129 | psa_set_key_type( & attributes, key_type ); |
| 5130 | |
| 5131 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5132 | &key ) ); |
| 5133 | |
| 5134 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5135 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5136 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5137 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5138 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5139 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5140 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5141 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5142 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_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 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5145 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5146 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5147 | |
| 5148 | /* If the operation is not supported, just skip and not fail in case the |
| 5149 | * encryption involves a common limitation of cryptography hardwares and |
| 5150 | * an alternative implementation. */ |
| 5151 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5152 | { |
| 5153 | 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] | 5154 | 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] | 5155 | } |
| 5156 | |
| 5157 | PSA_ASSERT( status ); |
| 5158 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5159 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5160 | nonce_length, |
| 5161 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5162 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5163 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5164 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5165 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5166 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5167 | if( expected_status == PSA_SUCCESS ) |
| 5168 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 5169 | alg ) ); |
| 5170 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5171 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5172 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5173 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5174 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5175 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 5176 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5177 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5178 | |
| 5179 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5180 | additional_data->len ) ); |
| 5181 | |
| 5182 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5183 | output, output_size, |
| 5184 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5185 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5186 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5187 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5188 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5189 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5190 | |
| 5191 | exit: |
| 5192 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5193 | mbedtls_free( output ); |
| 5194 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5195 | psa_aead_abort( &operation ); |
| 5196 | PSA_DONE( ); |
| 5197 | } |
| 5198 | /* END_CASE */ |
| 5199 | |
| 5200 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5201 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 5202 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5203 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5204 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5205 | data_t *additional_data, |
| 5206 | data_t *input_data, |
| 5207 | int expected_status_arg ) |
| 5208 | { |
| 5209 | |
| 5210 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5211 | psa_key_type_t key_type = key_type_arg; |
| 5212 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5213 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5214 | uint8_t *nonce_buffer = NULL; |
| 5215 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5216 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5217 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5218 | unsigned char *output = NULL; |
| 5219 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5220 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5221 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5222 | size_t ciphertext_size = 0; |
| 5223 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5224 | size_t tag_length = 0; |
| 5225 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5226 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5227 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5228 | |
| 5229 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5230 | |
| 5231 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5232 | psa_set_key_algorithm( &attributes, alg ); |
| 5233 | psa_set_key_type( &attributes, key_type ); |
| 5234 | |
| 5235 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5236 | &key ) ); |
| 5237 | |
| 5238 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5239 | |
| 5240 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5241 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5242 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5243 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5244 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5245 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5246 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_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 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5249 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5250 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5251 | |
| 5252 | /* If the operation is not supported, just skip and not fail in case the |
| 5253 | * encryption involves a common limitation of cryptography hardwares and |
| 5254 | * an alternative implementation. */ |
| 5255 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5256 | { |
| 5257 | 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] | 5258 | 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] | 5259 | } |
| 5260 | |
| 5261 | PSA_ASSERT( status ); |
| 5262 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5263 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 5264 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5265 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 5266 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5267 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5268 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5269 | } |
| 5270 | else |
| 5271 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5272 | /* If length is zero, then this will return NULL. */ |
| 5273 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5274 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5275 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5276 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5277 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5278 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5279 | { |
| 5280 | nonce_buffer[index] = 'a' + index; |
| 5281 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5282 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5283 | } |
| 5284 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5285 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5286 | { |
| 5287 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5288 | input_data->len ) ); |
| 5289 | } |
| 5290 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5291 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5292 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5293 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5294 | |
| 5295 | if( expected_status == PSA_SUCCESS ) |
| 5296 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5297 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5298 | { |
| 5299 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5300 | input_data->len ) ); |
| 5301 | } |
| 5302 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5303 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5304 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5305 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5306 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5307 | additional_data->len ), |
| 5308 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5309 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5310 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5311 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5312 | &ciphertext_length ), |
| 5313 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5314 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5315 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5316 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5317 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5318 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5319 | } |
| 5320 | |
| 5321 | exit: |
| 5322 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5323 | mbedtls_free( output ); |
| 5324 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5325 | mbedtls_free( nonce_buffer ); |
| 5326 | psa_aead_abort( &operation ); |
| 5327 | PSA_DONE( ); |
| 5328 | } |
| 5329 | /* END_CASE */ |
| 5330 | |
| 5331 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5332 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5333 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5334 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5335 | data_t *nonce, |
| 5336 | data_t *additional_data, |
| 5337 | data_t *input_data, |
| 5338 | int expected_status_arg ) |
| 5339 | { |
| 5340 | |
| 5341 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5342 | psa_key_type_t key_type = key_type_arg; |
| 5343 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5344 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5345 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5346 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5347 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5348 | unsigned char *output = NULL; |
| 5349 | unsigned char *ciphertext = NULL; |
| 5350 | size_t output_size = output_size_arg; |
| 5351 | size_t ciphertext_size = 0; |
| 5352 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5353 | size_t tag_length = 0; |
| 5354 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5355 | |
| 5356 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5357 | |
| 5358 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5359 | psa_set_key_algorithm( &attributes, alg ); |
| 5360 | psa_set_key_type( &attributes, key_type ); |
| 5361 | |
| 5362 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5363 | &key ) ); |
| 5364 | |
| 5365 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5366 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5367 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5368 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5369 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5370 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5371 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5372 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5373 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5374 | |
| 5375 | /* If the operation is not supported, just skip and not fail in case the |
| 5376 | * encryption involves a common limitation of cryptography hardwares and |
| 5377 | * an alternative implementation. */ |
| 5378 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5379 | { |
| 5380 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5381 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5382 | } |
| 5383 | |
| 5384 | PSA_ASSERT( status ); |
| 5385 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5386 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5387 | input_data->len ) ); |
| 5388 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5389 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5390 | |
| 5391 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5392 | additional_data->len ) ); |
| 5393 | |
| 5394 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5395 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5396 | |
| 5397 | TEST_EQUAL( status, expected_status ); |
| 5398 | |
| 5399 | if( expected_status == PSA_SUCCESS ) |
| 5400 | { |
| 5401 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5402 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5403 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5404 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5405 | } |
| 5406 | |
| 5407 | exit: |
| 5408 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5409 | mbedtls_free( output ); |
| 5410 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5411 | psa_aead_abort( &operation ); |
| 5412 | PSA_DONE( ); |
| 5413 | } |
| 5414 | /* END_CASE */ |
| 5415 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5416 | /* BEGIN_CASE */ |
| 5417 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5418 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5419 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5420 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5421 | data_t *nonce, |
| 5422 | data_t *additional_data, |
| 5423 | data_t *input_data, |
| 5424 | int expected_status_arg ) |
| 5425 | { |
| 5426 | |
| 5427 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5428 | psa_key_type_t key_type = key_type_arg; |
| 5429 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5430 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5431 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5432 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5433 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5434 | unsigned char *ciphertext = NULL; |
| 5435 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5436 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5437 | size_t ciphertext_size = 0; |
| 5438 | size_t ciphertext_length = 0; |
| 5439 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5440 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5441 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5442 | |
| 5443 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5444 | |
| 5445 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5446 | psa_set_key_algorithm( &attributes, alg ); |
| 5447 | psa_set_key_type( &attributes, key_type ); |
| 5448 | |
| 5449 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5450 | &key ) ); |
| 5451 | |
| 5452 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5453 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5454 | 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] | 5455 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5456 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5457 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5458 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5459 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5460 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5461 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5462 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5463 | |
| 5464 | /* If the operation is not supported, just skip and not fail in case the |
| 5465 | * encryption involves a common limitation of cryptography hardwares and |
| 5466 | * an alternative implementation. */ |
| 5467 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5468 | { |
| 5469 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5470 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5471 | } |
| 5472 | |
| 5473 | PSA_ASSERT( status ); |
| 5474 | |
| 5475 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5476 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5477 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5478 | input_data->len ) ); |
| 5479 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5480 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5481 | additional_data->len ) ); |
| 5482 | |
| 5483 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5484 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5485 | |
| 5486 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5487 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5488 | finish_ciphertext_size, |
| 5489 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5490 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5491 | |
| 5492 | TEST_EQUAL( status, expected_status ); |
| 5493 | |
| 5494 | exit: |
| 5495 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5496 | mbedtls_free( ciphertext ); |
| 5497 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5498 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5499 | psa_aead_abort( &operation ); |
| 5500 | PSA_DONE( ); |
| 5501 | } |
| 5502 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5503 | |
| 5504 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5505 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5506 | int alg_arg, |
| 5507 | data_t *nonce, |
| 5508 | data_t *additional_data, |
| 5509 | data_t *input_data, |
| 5510 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5511 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5512 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5513 | int expected_status_arg ) |
| 5514 | { |
| 5515 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5516 | psa_key_type_t key_type = key_type_arg; |
| 5517 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5518 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5519 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5520 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5521 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5522 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5523 | unsigned char *plaintext = NULL; |
| 5524 | unsigned char *finish_plaintext = NULL; |
| 5525 | size_t plaintext_size = 0; |
| 5526 | size_t plaintext_length = 0; |
| 5527 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5528 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5529 | unsigned char *tag_buffer = NULL; |
| 5530 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5531 | |
| 5532 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5533 | |
| 5534 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5535 | psa_set_key_algorithm( &attributes, alg ); |
| 5536 | psa_set_key_type( &attributes, key_type ); |
| 5537 | |
| 5538 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5539 | &key ) ); |
| 5540 | |
| 5541 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5542 | |
| 5543 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5544 | input_data->len ); |
| 5545 | |
| 5546 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5547 | |
| 5548 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5549 | |
| 5550 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5551 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5552 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5553 | |
| 5554 | /* If the operation is not supported, just skip and not fail in case the |
| 5555 | * encryption involves a common limitation of cryptography hardwares and |
| 5556 | * an alternative implementation. */ |
| 5557 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5558 | { |
| 5559 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5560 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5561 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5562 | TEST_EQUAL( status, expected_setup_status ); |
| 5563 | |
| 5564 | if( status != PSA_SUCCESS ) |
| 5565 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5566 | |
| 5567 | PSA_ASSERT( status ); |
| 5568 | |
| 5569 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5570 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5571 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5572 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5573 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5574 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5575 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5576 | additional_data->len ) ); |
| 5577 | |
| 5578 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5579 | input_data->len, |
| 5580 | plaintext, plaintext_size, |
| 5581 | &plaintext_length ) ); |
| 5582 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5583 | if( tag_usage == USE_GIVEN_TAG ) |
| 5584 | { |
| 5585 | tag_buffer = tag->x; |
| 5586 | tag_size = tag->len; |
| 5587 | } |
| 5588 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5589 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5590 | verify_plaintext_size, |
| 5591 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5592 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5593 | |
| 5594 | TEST_EQUAL( status, expected_status ); |
| 5595 | |
| 5596 | exit: |
| 5597 | psa_destroy_key( key ); |
| 5598 | mbedtls_free( plaintext ); |
| 5599 | mbedtls_free( finish_plaintext ); |
| 5600 | psa_aead_abort( &operation ); |
| 5601 | PSA_DONE( ); |
| 5602 | } |
| 5603 | /* END_CASE */ |
| 5604 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5605 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5606 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5607 | int alg_arg, int expected_status_arg ) |
| 5608 | { |
| 5609 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5610 | psa_key_type_t key_type = key_type_arg; |
| 5611 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5612 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5613 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5614 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5615 | psa_status_t expected_status = expected_status_arg; |
| 5616 | |
| 5617 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5618 | |
| 5619 | psa_set_key_usage_flags( &attributes, |
| 5620 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5621 | psa_set_key_algorithm( &attributes, alg ); |
| 5622 | psa_set_key_type( &attributes, key_type ); |
| 5623 | |
| 5624 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5625 | &key ) ); |
| 5626 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5627 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5628 | |
| 5629 | TEST_EQUAL( status, expected_status ); |
| 5630 | |
| 5631 | psa_aead_abort( &operation ); |
| 5632 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5633 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5634 | |
| 5635 | TEST_EQUAL(status, expected_status ); |
| 5636 | |
| 5637 | exit: |
| 5638 | psa_destroy_key( key ); |
| 5639 | psa_aead_abort( &operation ); |
| 5640 | PSA_DONE( ); |
| 5641 | } |
| 5642 | /* END_CASE */ |
| 5643 | |
| 5644 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5645 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5646 | int alg_arg, |
| 5647 | data_t *nonce, |
| 5648 | data_t *additional_data, |
| 5649 | data_t *input_data ) |
| 5650 | { |
| 5651 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5652 | psa_key_type_t key_type = key_type_arg; |
| 5653 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5654 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5655 | unsigned char *output_data = NULL; |
| 5656 | unsigned char *final_data = NULL; |
| 5657 | size_t output_size = 0; |
| 5658 | size_t finish_output_size = 0; |
| 5659 | size_t output_length = 0; |
| 5660 | size_t key_bits = 0; |
| 5661 | size_t tag_length = 0; |
| 5662 | size_t tag_size = 0; |
| 5663 | size_t nonce_length = 0; |
| 5664 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5665 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5666 | size_t output_part_length = 0; |
| 5667 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5668 | |
| 5669 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5670 | |
| 5671 | psa_set_key_usage_flags( & attributes, |
| 5672 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5673 | psa_set_key_algorithm( & attributes, alg ); |
| 5674 | psa_set_key_type( & attributes, key_type ); |
| 5675 | |
| 5676 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5677 | &key ) ); |
| 5678 | |
| 5679 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5680 | key_bits = psa_get_key_bits( &attributes ); |
| 5681 | |
| 5682 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5683 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5684 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5685 | |
| 5686 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5687 | |
| 5688 | ASSERT_ALLOC( output_data, output_size ); |
| 5689 | |
| 5690 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5691 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5692 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5693 | |
| 5694 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5695 | |
| 5696 | /* Test all operations error without calling setup first. */ |
| 5697 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5698 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5699 | PSA_ERROR_BAD_STATE ); |
| 5700 | |
| 5701 | psa_aead_abort( &operation ); |
| 5702 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5703 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5704 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5705 | &nonce_length ), |
| 5706 | PSA_ERROR_BAD_STATE ); |
| 5707 | |
| 5708 | psa_aead_abort( &operation ); |
| 5709 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5710 | /* ------------------------------------------------------- */ |
| 5711 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5712 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5713 | input_data->len ), |
| 5714 | PSA_ERROR_BAD_STATE ); |
| 5715 | |
| 5716 | psa_aead_abort( &operation ); |
| 5717 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5718 | /* ------------------------------------------------------- */ |
| 5719 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5720 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5721 | additional_data->len ), |
| 5722 | PSA_ERROR_BAD_STATE ); |
| 5723 | |
| 5724 | psa_aead_abort( &operation ); |
| 5725 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5726 | /* ------------------------------------------------------- */ |
| 5727 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5728 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5729 | input_data->len, output_data, |
| 5730 | output_size, &output_length ), |
| 5731 | PSA_ERROR_BAD_STATE ); |
| 5732 | |
| 5733 | psa_aead_abort( &operation ); |
| 5734 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5735 | /* ------------------------------------------------------- */ |
| 5736 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5737 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5738 | finish_output_size, |
| 5739 | &output_part_length, |
| 5740 | tag_buffer, tag_length, |
| 5741 | &tag_size ), |
| 5742 | PSA_ERROR_BAD_STATE ); |
| 5743 | |
| 5744 | psa_aead_abort( &operation ); |
| 5745 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5746 | /* ------------------------------------------------------- */ |
| 5747 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5748 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5749 | finish_output_size, |
| 5750 | &output_part_length, |
| 5751 | tag_buffer, |
| 5752 | tag_length ), |
| 5753 | PSA_ERROR_BAD_STATE ); |
| 5754 | |
| 5755 | psa_aead_abort( &operation ); |
| 5756 | |
| 5757 | /* Test for double setups. */ |
| 5758 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5759 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5760 | |
| 5761 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5762 | PSA_ERROR_BAD_STATE ); |
| 5763 | |
| 5764 | psa_aead_abort( &operation ); |
| 5765 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5766 | /* ------------------------------------------------------- */ |
| 5767 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5768 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5769 | |
| 5770 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5771 | PSA_ERROR_BAD_STATE ); |
| 5772 | |
| 5773 | psa_aead_abort( &operation ); |
| 5774 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5775 | /* ------------------------------------------------------- */ |
| 5776 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5777 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5778 | |
| 5779 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5780 | PSA_ERROR_BAD_STATE ); |
| 5781 | |
| 5782 | psa_aead_abort( &operation ); |
| 5783 | |
| 5784 | /* ------------------------------------------------------- */ |
| 5785 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5786 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5787 | |
| 5788 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5789 | PSA_ERROR_BAD_STATE ); |
| 5790 | |
| 5791 | psa_aead_abort( &operation ); |
| 5792 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5793 | /* Test for not setting a nonce. */ |
| 5794 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5795 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5796 | |
| 5797 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5798 | additional_data->len ), |
| 5799 | PSA_ERROR_BAD_STATE ); |
| 5800 | |
| 5801 | psa_aead_abort( &operation ); |
| 5802 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5803 | /* ------------------------------------------------------- */ |
| 5804 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5805 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5806 | |
| 5807 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5808 | input_data->len, output_data, |
| 5809 | output_size, &output_length ), |
| 5810 | PSA_ERROR_BAD_STATE ); |
| 5811 | |
| 5812 | psa_aead_abort( &operation ); |
| 5813 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5814 | /* ------------------------------------------------------- */ |
| 5815 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5816 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5817 | |
| 5818 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5819 | finish_output_size, |
| 5820 | &output_part_length, |
| 5821 | tag_buffer, tag_length, |
| 5822 | &tag_size ), |
| 5823 | PSA_ERROR_BAD_STATE ); |
| 5824 | |
| 5825 | psa_aead_abort( &operation ); |
| 5826 | |
| 5827 | /* ------------------------------------------------------- */ |
| 5828 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5829 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5830 | |
| 5831 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5832 | finish_output_size, |
| 5833 | &output_part_length, |
| 5834 | tag_buffer, |
| 5835 | tag_length ), |
| 5836 | PSA_ERROR_BAD_STATE ); |
| 5837 | |
| 5838 | psa_aead_abort( &operation ); |
| 5839 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5840 | /* Test for double setting nonce. */ |
| 5841 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5842 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5843 | |
| 5844 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5845 | |
| 5846 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5847 | PSA_ERROR_BAD_STATE ); |
| 5848 | |
| 5849 | psa_aead_abort( &operation ); |
| 5850 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5851 | /* Test for double generating nonce. */ |
| 5852 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5853 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5854 | |
| 5855 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5856 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5857 | &nonce_length ) ); |
| 5858 | |
| 5859 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5860 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5861 | &nonce_length ), |
| 5862 | PSA_ERROR_BAD_STATE ); |
| 5863 | |
| 5864 | |
| 5865 | psa_aead_abort( &operation ); |
| 5866 | |
| 5867 | /* Test for generate nonce then set and vice versa */ |
| 5868 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5869 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5870 | |
| 5871 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5872 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5873 | &nonce_length ) ); |
| 5874 | |
| 5875 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5876 | PSA_ERROR_BAD_STATE ); |
| 5877 | |
| 5878 | psa_aead_abort( &operation ); |
| 5879 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5880 | /* Test for generating nonce after calling set lengths */ |
| 5881 | |
| 5882 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5883 | |
| 5884 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5885 | input_data->len ) ); |
| 5886 | |
| 5887 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5888 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5889 | &nonce_length ) ); |
| 5890 | |
| 5891 | psa_aead_abort( &operation ); |
| 5892 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5893 | /* 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] | 5894 | |
| 5895 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5896 | |
| 5897 | if( operation.alg == PSA_ALG_CCM ) |
| 5898 | { |
| 5899 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5900 | input_data->len ), |
| 5901 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5902 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5903 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5904 | &nonce_length ), |
| 5905 | PSA_ERROR_BAD_STATE ); |
| 5906 | } |
| 5907 | else |
| 5908 | { |
| 5909 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5910 | input_data->len ) ); |
| 5911 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5912 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5913 | &nonce_length ) ); |
| 5914 | } |
| 5915 | |
| 5916 | psa_aead_abort( &operation ); |
| 5917 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5918 | /* 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] | 5919 | #if SIZE_MAX > UINT32_MAX |
| 5920 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5921 | |
| 5922 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5923 | { |
| 5924 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5925 | input_data->len ), |
| 5926 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5927 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5928 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5929 | &nonce_length ), |
| 5930 | PSA_ERROR_BAD_STATE ); |
| 5931 | } |
| 5932 | else |
| 5933 | { |
| 5934 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5935 | input_data->len ) ); |
| 5936 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5937 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5938 | &nonce_length ) ); |
| 5939 | } |
| 5940 | |
| 5941 | psa_aead_abort( &operation ); |
| 5942 | #endif |
| 5943 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5944 | /* 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] | 5945 | |
| 5946 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5947 | |
| 5948 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5949 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5950 | &nonce_length ) ); |
| 5951 | |
| 5952 | if( operation.alg == PSA_ALG_CCM ) |
| 5953 | { |
| 5954 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5955 | input_data->len ), |
| 5956 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5957 | } |
| 5958 | else |
| 5959 | { |
| 5960 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5961 | input_data->len ) ); |
| 5962 | } |
| 5963 | |
| 5964 | psa_aead_abort( &operation ); |
| 5965 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5966 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5967 | /* Test for setting nonce after calling set lengths */ |
| 5968 | |
| 5969 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5970 | |
| 5971 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5972 | input_data->len ) ); |
| 5973 | |
| 5974 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5975 | |
| 5976 | psa_aead_abort( &operation ); |
| 5977 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5978 | /* 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] | 5979 | |
| 5980 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5981 | |
| 5982 | if( operation.alg == PSA_ALG_CCM ) |
| 5983 | { |
| 5984 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5985 | input_data->len ), |
| 5986 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5987 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5988 | PSA_ERROR_BAD_STATE ); |
| 5989 | } |
| 5990 | else |
| 5991 | { |
| 5992 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5993 | input_data->len ) ); |
| 5994 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5995 | } |
| 5996 | |
| 5997 | psa_aead_abort( &operation ); |
| 5998 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5999 | /* 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] | 6000 | #if SIZE_MAX > UINT32_MAX |
| 6001 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6002 | |
| 6003 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 6004 | { |
| 6005 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6006 | input_data->len ), |
| 6007 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6008 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6009 | PSA_ERROR_BAD_STATE ); |
| 6010 | } |
| 6011 | else |
| 6012 | { |
| 6013 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6014 | input_data->len ) ); |
| 6015 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6016 | } |
| 6017 | |
| 6018 | psa_aead_abort( &operation ); |
| 6019 | #endif |
| 6020 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6021 | /* 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] | 6022 | |
| 6023 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6024 | |
| 6025 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6026 | |
| 6027 | if( operation.alg == PSA_ALG_CCM ) |
| 6028 | { |
| 6029 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6030 | input_data->len ), |
| 6031 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6032 | } |
| 6033 | else |
| 6034 | { |
| 6035 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6036 | input_data->len ) ); |
| 6037 | } |
| 6038 | |
| 6039 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6040 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6041 | /* 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] | 6042 | #if SIZE_MAX > UINT32_MAX |
| 6043 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6044 | |
| 6045 | if( operation.alg == PSA_ALG_GCM ) |
| 6046 | { |
| 6047 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6048 | SIZE_MAX ), |
| 6049 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6050 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6051 | PSA_ERROR_BAD_STATE ); |
| 6052 | } |
| 6053 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6054 | { |
| 6055 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6056 | SIZE_MAX ) ); |
| 6057 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6058 | } |
| 6059 | |
| 6060 | psa_aead_abort( &operation ); |
| 6061 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6062 | /* 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] | 6063 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6064 | |
| 6065 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6066 | |
| 6067 | if( operation.alg == PSA_ALG_GCM ) |
| 6068 | { |
| 6069 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6070 | SIZE_MAX ), |
| 6071 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6072 | } |
| 6073 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6074 | { |
| 6075 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6076 | SIZE_MAX ) ); |
| 6077 | } |
| 6078 | |
| 6079 | psa_aead_abort( &operation ); |
| 6080 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6081 | |
| 6082 | /* ------------------------------------------------------- */ |
| 6083 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6084 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6085 | |
| 6086 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6087 | |
| 6088 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6089 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6090 | &nonce_length ), |
| 6091 | PSA_ERROR_BAD_STATE ); |
| 6092 | |
| 6093 | psa_aead_abort( &operation ); |
| 6094 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6095 | /* Test for generating nonce in decrypt setup. */ |
| 6096 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6097 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6098 | |
| 6099 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6100 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6101 | &nonce_length ), |
| 6102 | PSA_ERROR_BAD_STATE ); |
| 6103 | |
| 6104 | psa_aead_abort( &operation ); |
| 6105 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6106 | /* Test for setting lengths twice. */ |
| 6107 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6108 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6109 | |
| 6110 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6111 | |
| 6112 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6113 | input_data->len ) ); |
| 6114 | |
| 6115 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6116 | input_data->len ), |
| 6117 | PSA_ERROR_BAD_STATE ); |
| 6118 | |
| 6119 | psa_aead_abort( &operation ); |
| 6120 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6121 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6122 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6123 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6124 | |
| 6125 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6126 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6127 | if( operation.alg == PSA_ALG_CCM ) |
| 6128 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6129 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6130 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6131 | additional_data->len ), |
| 6132 | PSA_ERROR_BAD_STATE ); |
| 6133 | } |
| 6134 | else |
| 6135 | { |
| 6136 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6137 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6138 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6139 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6140 | input_data->len ), |
| 6141 | PSA_ERROR_BAD_STATE ); |
| 6142 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6143 | psa_aead_abort( &operation ); |
| 6144 | |
| 6145 | /* ------------------------------------------------------- */ |
| 6146 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6147 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6148 | |
| 6149 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6150 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6151 | if( operation.alg == PSA_ALG_CCM ) |
| 6152 | { |
| 6153 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6154 | input_data->len, output_data, |
| 6155 | output_size, &output_length ), |
| 6156 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6157 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6158 | } |
| 6159 | else |
| 6160 | { |
| 6161 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6162 | input_data->len, output_data, |
| 6163 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6164 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6165 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6166 | input_data->len ), |
| 6167 | PSA_ERROR_BAD_STATE ); |
| 6168 | } |
| 6169 | psa_aead_abort( &operation ); |
| 6170 | |
| 6171 | /* ------------------------------------------------------- */ |
| 6172 | |
| 6173 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6174 | |
| 6175 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6176 | |
| 6177 | if( operation.alg == PSA_ALG_CCM ) |
| 6178 | { |
| 6179 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6180 | finish_output_size, |
| 6181 | &output_part_length, |
| 6182 | tag_buffer, tag_length, |
| 6183 | &tag_size ) ); |
| 6184 | } |
| 6185 | else |
| 6186 | { |
| 6187 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6188 | finish_output_size, |
| 6189 | &output_part_length, |
| 6190 | tag_buffer, tag_length, |
| 6191 | &tag_size ) ); |
| 6192 | |
| 6193 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6194 | input_data->len ), |
| 6195 | PSA_ERROR_BAD_STATE ); |
| 6196 | } |
| 6197 | psa_aead_abort( &operation ); |
| 6198 | |
| 6199 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6200 | |
| 6201 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6202 | |
| 6203 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6204 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6205 | &nonce_length ) ); |
| 6206 | if( operation.alg == PSA_ALG_CCM ) |
| 6207 | { |
| 6208 | |
| 6209 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6210 | additional_data->len ), |
| 6211 | PSA_ERROR_BAD_STATE ); |
| 6212 | } |
| 6213 | else |
| 6214 | { |
| 6215 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6216 | additional_data->len ) ); |
| 6217 | |
| 6218 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6219 | input_data->len ), |
| 6220 | PSA_ERROR_BAD_STATE ); |
| 6221 | } |
| 6222 | psa_aead_abort( &operation ); |
| 6223 | |
| 6224 | /* ------------------------------------------------------- */ |
| 6225 | |
| 6226 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6227 | |
| 6228 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6229 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6230 | &nonce_length ) ); |
| 6231 | if( operation.alg == PSA_ALG_CCM ) |
| 6232 | { |
| 6233 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6234 | input_data->len, output_data, |
| 6235 | output_size, &output_length ), |
| 6236 | PSA_ERROR_BAD_STATE ); |
| 6237 | |
| 6238 | } |
| 6239 | else |
| 6240 | { |
| 6241 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6242 | input_data->len, output_data, |
| 6243 | output_size, &output_length ) ); |
| 6244 | |
| 6245 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6246 | input_data->len ), |
| 6247 | PSA_ERROR_BAD_STATE ); |
| 6248 | } |
| 6249 | psa_aead_abort( &operation ); |
| 6250 | |
| 6251 | /* ------------------------------------------------------- */ |
| 6252 | |
| 6253 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6254 | |
| 6255 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6256 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6257 | &nonce_length ) ); |
| 6258 | if( operation.alg == PSA_ALG_CCM ) |
| 6259 | { |
| 6260 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6261 | finish_output_size, |
| 6262 | &output_part_length, |
| 6263 | tag_buffer, tag_length, |
| 6264 | &tag_size ) ); |
| 6265 | } |
| 6266 | else |
| 6267 | { |
| 6268 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6269 | finish_output_size, |
| 6270 | &output_part_length, |
| 6271 | tag_buffer, tag_length, |
| 6272 | &tag_size ) ); |
| 6273 | |
| 6274 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6275 | input_data->len ), |
| 6276 | PSA_ERROR_BAD_STATE ); |
| 6277 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6278 | psa_aead_abort( &operation ); |
| 6279 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6280 | /* Test for not sending any additional data or data after setting non zero |
| 6281 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6282 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6283 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6284 | |
| 6285 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6286 | |
| 6287 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6288 | input_data->len ) ); |
| 6289 | |
| 6290 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6291 | finish_output_size, |
| 6292 | &output_part_length, |
| 6293 | tag_buffer, tag_length, |
| 6294 | &tag_size ), |
| 6295 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6296 | |
| 6297 | psa_aead_abort( &operation ); |
| 6298 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6299 | /* Test for not sending any additional data or data after setting non-zero |
| 6300 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6301 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6302 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6303 | |
| 6304 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6305 | |
| 6306 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6307 | input_data->len ) ); |
| 6308 | |
| 6309 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6310 | finish_output_size, |
| 6311 | &output_part_length, |
| 6312 | tag_buffer, |
| 6313 | tag_length ), |
| 6314 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6315 | |
| 6316 | psa_aead_abort( &operation ); |
| 6317 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6318 | /* Test for not sending any additional data after setting a non-zero length |
| 6319 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6320 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6321 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6322 | |
| 6323 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6324 | |
| 6325 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6326 | input_data->len ) ); |
| 6327 | |
| 6328 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6329 | input_data->len, output_data, |
| 6330 | output_size, &output_length ), |
| 6331 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6332 | |
| 6333 | psa_aead_abort( &operation ); |
| 6334 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6335 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6336 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6337 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6338 | |
| 6339 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6340 | |
| 6341 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6342 | input_data->len ) ); |
| 6343 | |
| 6344 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6345 | additional_data->len ) ); |
| 6346 | |
| 6347 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6348 | finish_output_size, |
| 6349 | &output_part_length, |
| 6350 | tag_buffer, tag_length, |
| 6351 | &tag_size ), |
| 6352 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6353 | |
| 6354 | psa_aead_abort( &operation ); |
| 6355 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6356 | /* Test for sending too much additional data after setting lengths. */ |
| 6357 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6358 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6359 | |
| 6360 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6361 | |
| 6362 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6363 | |
| 6364 | |
| 6365 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6366 | additional_data->len ), |
| 6367 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6368 | |
| 6369 | psa_aead_abort( &operation ); |
| 6370 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6371 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6372 | |
| 6373 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6374 | |
| 6375 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6376 | |
| 6377 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6378 | input_data->len ) ); |
| 6379 | |
| 6380 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6381 | additional_data->len ) ); |
| 6382 | |
| 6383 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6384 | 1 ), |
| 6385 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6386 | |
| 6387 | psa_aead_abort( &operation ); |
| 6388 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6389 | /* Test for sending too much data after setting lengths. */ |
| 6390 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6391 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6392 | |
| 6393 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6394 | |
| 6395 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6396 | |
| 6397 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6398 | input_data->len, output_data, |
| 6399 | output_size, &output_length ), |
| 6400 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6401 | |
| 6402 | psa_aead_abort( &operation ); |
| 6403 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6404 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6405 | |
| 6406 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6407 | |
| 6408 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6409 | |
| 6410 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6411 | input_data->len ) ); |
| 6412 | |
| 6413 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6414 | additional_data->len ) ); |
| 6415 | |
| 6416 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6417 | input_data->len, output_data, |
| 6418 | output_size, &output_length ) ); |
| 6419 | |
| 6420 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6421 | 1, output_data, |
| 6422 | output_size, &output_length ), |
| 6423 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6424 | |
| 6425 | psa_aead_abort( &operation ); |
| 6426 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6427 | /* Test sending additional data after data. */ |
| 6428 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6429 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6430 | |
| 6431 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6432 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6433 | if( operation.alg != PSA_ALG_CCM ) |
| 6434 | { |
| 6435 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6436 | input_data->len, output_data, |
| 6437 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6438 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6439 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6440 | additional_data->len ), |
| 6441 | PSA_ERROR_BAD_STATE ); |
| 6442 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6443 | psa_aead_abort( &operation ); |
| 6444 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6445 | /* Test calling finish on decryption. */ |
| 6446 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6447 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6448 | |
| 6449 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6450 | |
| 6451 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6452 | finish_output_size, |
| 6453 | &output_part_length, |
| 6454 | tag_buffer, tag_length, |
| 6455 | &tag_size ), |
| 6456 | PSA_ERROR_BAD_STATE ); |
| 6457 | |
| 6458 | psa_aead_abort( &operation ); |
| 6459 | |
| 6460 | /* Test calling verify on encryption. */ |
| 6461 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6462 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6463 | |
| 6464 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6465 | |
| 6466 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6467 | finish_output_size, |
| 6468 | &output_part_length, |
| 6469 | tag_buffer, |
| 6470 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6471 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6472 | |
| 6473 | psa_aead_abort( &operation ); |
| 6474 | |
| 6475 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6476 | exit: |
| 6477 | psa_destroy_key( key ); |
| 6478 | psa_aead_abort( &operation ); |
| 6479 | mbedtls_free( output_data ); |
| 6480 | mbedtls_free( final_data ); |
| 6481 | PSA_DONE( ); |
| 6482 | } |
| 6483 | /* END_CASE */ |
| 6484 | |
| 6485 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6486 | void signature_size( int type_arg, |
| 6487 | int bits, |
| 6488 | int alg_arg, |
| 6489 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6490 | { |
| 6491 | psa_key_type_t type = type_arg; |
| 6492 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6493 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6494 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6495 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6496 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6497 | exit: |
| 6498 | ; |
| 6499 | } |
| 6500 | /* END_CASE */ |
| 6501 | |
| 6502 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6503 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6504 | int alg_arg, data_t *input_data, |
| 6505 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6506 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6507 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6508 | psa_key_type_t key_type = key_type_arg; |
| 6509 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6510 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6511 | unsigned char *signature = NULL; |
| 6512 | size_t signature_size; |
| 6513 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6514 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6515 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6516 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6517 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6518 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6519 | psa_set_key_algorithm( &attributes, alg ); |
| 6520 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6521 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6522 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6523 | &key ) ); |
| 6524 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6525 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6526 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6527 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6528 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6529 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6530 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6531 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6532 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6533 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6534 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6535 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6536 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6537 | input_data->x, input_data->len, |
| 6538 | signature, signature_size, |
| 6539 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6540 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6541 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6542 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6543 | |
| 6544 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6545 | /* |
| 6546 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6547 | * thus reset them as required. |
| 6548 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6549 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6550 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6551 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6552 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6553 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6554 | } |
| 6555 | /* END_CASE */ |
| 6556 | |
| 6557 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6558 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6559 | int alg_arg, data_t *input_data, |
| 6560 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6561 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6562 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6563 | psa_key_type_t key_type = key_type_arg; |
| 6564 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6565 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6566 | psa_status_t actual_status; |
| 6567 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6568 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6569 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6570 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6571 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6572 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6573 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6574 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6575 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6576 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6577 | psa_set_key_algorithm( &attributes, alg ); |
| 6578 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6579 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6580 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6581 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6582 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6583 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6584 | input_data->x, input_data->len, |
| 6585 | signature, signature_size, |
| 6586 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6587 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6588 | /* The value of *signature_length is unspecified on error, but |
| 6589 | * whatever it is, it should be less than signature_size, so that |
| 6590 | * if the caller tries to read *signature_length bytes without |
| 6591 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6592 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6593 | |
| 6594 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6595 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6596 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6597 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6598 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6599 | } |
| 6600 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6601 | |
| 6602 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6603 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6604 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6605 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6606 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6607 | psa_key_type_t key_type = key_type_arg; |
| 6608 | psa_algorithm_t alg = alg_arg; |
| 6609 | size_t key_bits; |
| 6610 | unsigned char *signature = NULL; |
| 6611 | size_t signature_size; |
| 6612 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6613 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6614 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6615 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6616 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6617 | 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] | 6618 | psa_set_key_algorithm( &attributes, alg ); |
| 6619 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6620 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6621 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6622 | &key ) ); |
| 6623 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6624 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6625 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6626 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6627 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6628 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6629 | key_bits, alg ); |
| 6630 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6631 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6632 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6633 | |
| 6634 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6635 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6636 | input_data->x, input_data->len, |
| 6637 | signature, signature_size, |
| 6638 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6639 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6640 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6641 | TEST_ASSERT( signature_length > 0 ); |
| 6642 | |
| 6643 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6644 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6645 | input_data->x, input_data->len, |
| 6646 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6647 | |
| 6648 | if( input_data->len != 0 ) |
| 6649 | { |
| 6650 | /* Flip a bit in the input and verify that the signature is now |
| 6651 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6652 | * because ECDSA may ignore the last few bits of the input. */ |
| 6653 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6654 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6655 | input_data->x, input_data->len, |
| 6656 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6657 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6658 | } |
| 6659 | |
| 6660 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6661 | /* |
| 6662 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6663 | * thus reset them as required. |
| 6664 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6665 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6666 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6667 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6668 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6669 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6670 | } |
| 6671 | /* END_CASE */ |
| 6672 | |
| 6673 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6674 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6675 | int alg_arg, data_t *hash_data, |
| 6676 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6677 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6678 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6679 | psa_key_type_t key_type = key_type_arg; |
| 6680 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6681 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6682 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6683 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6684 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6685 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6686 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6687 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6688 | psa_set_key_algorithm( &attributes, alg ); |
| 6689 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6690 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6691 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6692 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6693 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6694 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6695 | hash_data->x, hash_data->len, |
| 6696 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6697 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6698 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6699 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6700 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6701 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6702 | } |
| 6703 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6704 | |
| 6705 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6706 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6707 | int alg_arg, data_t *hash_data, |
| 6708 | data_t *signature_data, |
| 6709 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6710 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6711 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6712 | psa_key_type_t key_type = key_type_arg; |
| 6713 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6714 | psa_status_t actual_status; |
| 6715 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6716 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6717 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6718 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6719 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6720 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6721 | psa_set_key_algorithm( &attributes, alg ); |
| 6722 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6723 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6724 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6725 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6726 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6727 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6728 | hash_data->x, hash_data->len, |
| 6729 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6730 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6731 | |
| 6732 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6733 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6734 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6735 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6736 | } |
| 6737 | /* END_CASE */ |
| 6738 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6739 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6740 | void sign_message_deterministic( int key_type_arg, |
| 6741 | data_t *key_data, |
| 6742 | int alg_arg, |
| 6743 | data_t *input_data, |
| 6744 | data_t *output_data ) |
| 6745 | { |
| 6746 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6747 | psa_key_type_t key_type = key_type_arg; |
| 6748 | psa_algorithm_t alg = alg_arg; |
| 6749 | size_t key_bits; |
| 6750 | unsigned char *signature = NULL; |
| 6751 | size_t signature_size; |
| 6752 | size_t signature_length = 0xdeadbeef; |
| 6753 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6754 | |
| 6755 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6756 | |
| 6757 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6758 | psa_set_key_algorithm( &attributes, alg ); |
| 6759 | psa_set_key_type( &attributes, key_type ); |
| 6760 | |
| 6761 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6762 | &key ) ); |
| 6763 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6764 | key_bits = psa_get_key_bits( &attributes ); |
| 6765 | |
| 6766 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6767 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6768 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6769 | ASSERT_ALLOC( signature, signature_size ); |
| 6770 | |
| 6771 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6772 | input_data->x, input_data->len, |
| 6773 | signature, signature_size, |
| 6774 | &signature_length ) ); |
| 6775 | |
| 6776 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6777 | signature, signature_length ); |
| 6778 | |
| 6779 | exit: |
| 6780 | psa_reset_key_attributes( &attributes ); |
| 6781 | |
| 6782 | psa_destroy_key( key ); |
| 6783 | mbedtls_free( signature ); |
| 6784 | PSA_DONE( ); |
| 6785 | |
| 6786 | } |
| 6787 | /* END_CASE */ |
| 6788 | |
| 6789 | /* BEGIN_CASE */ |
| 6790 | void sign_message_fail( int key_type_arg, |
| 6791 | data_t *key_data, |
| 6792 | int alg_arg, |
| 6793 | data_t *input_data, |
| 6794 | int signature_size_arg, |
| 6795 | int expected_status_arg ) |
| 6796 | { |
| 6797 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6798 | psa_key_type_t key_type = key_type_arg; |
| 6799 | psa_algorithm_t alg = alg_arg; |
| 6800 | size_t signature_size = signature_size_arg; |
| 6801 | psa_status_t actual_status; |
| 6802 | psa_status_t expected_status = expected_status_arg; |
| 6803 | unsigned char *signature = NULL; |
| 6804 | size_t signature_length = 0xdeadbeef; |
| 6805 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6806 | |
| 6807 | ASSERT_ALLOC( signature, signature_size ); |
| 6808 | |
| 6809 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6810 | |
| 6811 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6812 | psa_set_key_algorithm( &attributes, alg ); |
| 6813 | psa_set_key_type( &attributes, key_type ); |
| 6814 | |
| 6815 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6816 | &key ) ); |
| 6817 | |
| 6818 | actual_status = psa_sign_message( key, alg, |
| 6819 | input_data->x, input_data->len, |
| 6820 | signature, signature_size, |
| 6821 | &signature_length ); |
| 6822 | TEST_EQUAL( actual_status, expected_status ); |
| 6823 | /* The value of *signature_length is unspecified on error, but |
| 6824 | * whatever it is, it should be less than signature_size, so that |
| 6825 | * if the caller tries to read *signature_length bytes without |
| 6826 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6827 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6828 | |
| 6829 | exit: |
| 6830 | psa_reset_key_attributes( &attributes ); |
| 6831 | psa_destroy_key( key ); |
| 6832 | mbedtls_free( signature ); |
| 6833 | PSA_DONE( ); |
| 6834 | } |
| 6835 | /* END_CASE */ |
| 6836 | |
| 6837 | /* BEGIN_CASE */ |
| 6838 | void sign_verify_message( int key_type_arg, |
| 6839 | data_t *key_data, |
| 6840 | int alg_arg, |
| 6841 | data_t *input_data ) |
| 6842 | { |
| 6843 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6844 | psa_key_type_t key_type = key_type_arg; |
| 6845 | psa_algorithm_t alg = alg_arg; |
| 6846 | size_t key_bits; |
| 6847 | unsigned char *signature = NULL; |
| 6848 | size_t signature_size; |
| 6849 | size_t signature_length = 0xdeadbeef; |
| 6850 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6851 | |
| 6852 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6853 | |
| 6854 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6855 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6856 | psa_set_key_algorithm( &attributes, alg ); |
| 6857 | psa_set_key_type( &attributes, key_type ); |
| 6858 | |
| 6859 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6860 | &key ) ); |
| 6861 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6862 | key_bits = psa_get_key_bits( &attributes ); |
| 6863 | |
| 6864 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6865 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6866 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6867 | ASSERT_ALLOC( signature, signature_size ); |
| 6868 | |
| 6869 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6870 | input_data->x, input_data->len, |
| 6871 | signature, signature_size, |
| 6872 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6873 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6874 | TEST_ASSERT( signature_length > 0 ); |
| 6875 | |
| 6876 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6877 | input_data->x, input_data->len, |
| 6878 | signature, signature_length ) ); |
| 6879 | |
| 6880 | if( input_data->len != 0 ) |
| 6881 | { |
| 6882 | /* Flip a bit in the input and verify that the signature is now |
| 6883 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6884 | * because ECDSA may ignore the last few bits of the input. */ |
| 6885 | input_data->x[0] ^= 1; |
| 6886 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6887 | input_data->x, input_data->len, |
| 6888 | signature, signature_length ), |
| 6889 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6890 | } |
| 6891 | |
| 6892 | exit: |
| 6893 | psa_reset_key_attributes( &attributes ); |
| 6894 | |
| 6895 | psa_destroy_key( key ); |
| 6896 | mbedtls_free( signature ); |
| 6897 | PSA_DONE( ); |
| 6898 | } |
| 6899 | /* END_CASE */ |
| 6900 | |
| 6901 | /* BEGIN_CASE */ |
| 6902 | void verify_message( int key_type_arg, |
| 6903 | data_t *key_data, |
| 6904 | int alg_arg, |
| 6905 | data_t *input_data, |
| 6906 | data_t *signature_data ) |
| 6907 | { |
| 6908 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6909 | psa_key_type_t key_type = key_type_arg; |
| 6910 | psa_algorithm_t alg = alg_arg; |
| 6911 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6912 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6913 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6914 | |
| 6915 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6916 | |
| 6917 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6918 | psa_set_key_algorithm( &attributes, alg ); |
| 6919 | psa_set_key_type( &attributes, key_type ); |
| 6920 | |
| 6921 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6922 | &key ) ); |
| 6923 | |
| 6924 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6925 | input_data->x, input_data->len, |
| 6926 | signature_data->x, signature_data->len ) ); |
| 6927 | |
| 6928 | exit: |
| 6929 | psa_reset_key_attributes( &attributes ); |
| 6930 | psa_destroy_key( key ); |
| 6931 | PSA_DONE( ); |
| 6932 | } |
| 6933 | /* END_CASE */ |
| 6934 | |
| 6935 | /* BEGIN_CASE */ |
| 6936 | void verify_message_fail( int key_type_arg, |
| 6937 | data_t *key_data, |
| 6938 | int alg_arg, |
| 6939 | data_t *hash_data, |
| 6940 | data_t *signature_data, |
| 6941 | int expected_status_arg ) |
| 6942 | { |
| 6943 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6944 | psa_key_type_t key_type = key_type_arg; |
| 6945 | psa_algorithm_t alg = alg_arg; |
| 6946 | psa_status_t actual_status; |
| 6947 | psa_status_t expected_status = expected_status_arg; |
| 6948 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6949 | |
| 6950 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6951 | |
| 6952 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6953 | psa_set_key_algorithm( &attributes, alg ); |
| 6954 | psa_set_key_type( &attributes, key_type ); |
| 6955 | |
| 6956 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6957 | &key ) ); |
| 6958 | |
| 6959 | actual_status = psa_verify_message( key, alg, |
| 6960 | hash_data->x, hash_data->len, |
| 6961 | signature_data->x, |
| 6962 | signature_data->len ); |
| 6963 | TEST_EQUAL( actual_status, expected_status ); |
| 6964 | |
| 6965 | exit: |
| 6966 | psa_reset_key_attributes( &attributes ); |
| 6967 | psa_destroy_key( key ); |
| 6968 | PSA_DONE( ); |
| 6969 | } |
| 6970 | /* END_CASE */ |
| 6971 | |
| 6972 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6973 | void asymmetric_encrypt( int key_type_arg, |
| 6974 | data_t *key_data, |
| 6975 | int alg_arg, |
| 6976 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6977 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6978 | int expected_output_length_arg, |
| 6979 | int expected_status_arg ) |
| 6980 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6981 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6982 | psa_key_type_t key_type = key_type_arg; |
| 6983 | psa_algorithm_t alg = alg_arg; |
| 6984 | size_t expected_output_length = expected_output_length_arg; |
| 6985 | size_t key_bits; |
| 6986 | unsigned char *output = NULL; |
| 6987 | size_t output_size; |
| 6988 | size_t output_length = ~0; |
| 6989 | psa_status_t actual_status; |
| 6990 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6991 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6992 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6993 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 6994 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6995 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6996 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 6997 | psa_set_key_algorithm( &attributes, alg ); |
| 6998 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6999 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7000 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7001 | |
| 7002 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7003 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7004 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7005 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7006 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7007 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7008 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7009 | |
| 7010 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7011 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7012 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7013 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7014 | output, output_size, |
| 7015 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7016 | TEST_EQUAL( actual_status, expected_status ); |
| 7017 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7018 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7019 | /* If the label is empty, the test framework puts a non-null pointer |
| 7020 | * in label->x. Test that a null pointer works as well. */ |
| 7021 | if( label->len == 0 ) |
| 7022 | { |
| 7023 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7024 | if( output_size != 0 ) |
| 7025 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7026 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7027 | input_data->x, input_data->len, |
| 7028 | NULL, label->len, |
| 7029 | output, output_size, |
| 7030 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7031 | TEST_EQUAL( actual_status, expected_status ); |
| 7032 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7033 | } |
| 7034 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7035 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7036 | /* |
| 7037 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7038 | * thus reset them as required. |
| 7039 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7040 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7041 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7042 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7043 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7044 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7045 | } |
| 7046 | /* END_CASE */ |
| 7047 | |
| 7048 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7049 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 7050 | data_t *key_data, |
| 7051 | int alg_arg, |
| 7052 | data_t *input_data, |
| 7053 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7054 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7055 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7056 | psa_key_type_t key_type = key_type_arg; |
| 7057 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7058 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7059 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7060 | size_t output_size; |
| 7061 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7062 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7063 | size_t output2_size; |
| 7064 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7065 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7066 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7067 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7068 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7069 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 7070 | psa_set_key_algorithm( &attributes, alg ); |
| 7071 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7072 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7073 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7074 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7075 | |
| 7076 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7077 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7078 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7079 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7080 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7081 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7082 | ASSERT_ALLOC( output, output_size ); |
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 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7085 | TEST_LE_U( output2_size, |
| 7086 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 7087 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7088 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7089 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7090 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7091 | * the original plaintext because of the non-optional random |
| 7092 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7093 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7094 | input_data->x, input_data->len, |
| 7095 | label->x, label->len, |
| 7096 | output, output_size, |
| 7097 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7098 | /* We don't know what ciphertext length to expect, but check that |
| 7099 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7100 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7101 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7102 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7103 | output, output_length, |
| 7104 | label->x, label->len, |
| 7105 | output2, output2_size, |
| 7106 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7107 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 7108 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7109 | |
| 7110 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7111 | /* |
| 7112 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7113 | * thus reset them as required. |
| 7114 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7115 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7116 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7117 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7118 | mbedtls_free( output ); |
| 7119 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7120 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7121 | } |
| 7122 | /* END_CASE */ |
| 7123 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7124 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7125 | void asymmetric_decrypt( int key_type_arg, |
| 7126 | data_t *key_data, |
| 7127 | int alg_arg, |
| 7128 | data_t *input_data, |
| 7129 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 7130 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7131 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7132 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7133 | psa_key_type_t key_type = key_type_arg; |
| 7134 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7135 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7136 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7137 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7138 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7139 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7140 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7141 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7142 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7143 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7144 | psa_set_key_algorithm( &attributes, alg ); |
| 7145 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7146 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7147 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7148 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7149 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7150 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 7151 | key_bits = psa_get_key_bits( &attributes ); |
| 7152 | |
| 7153 | /* Determine the maximum ciphertext length */ |
| 7154 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7155 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7156 | ASSERT_ALLOC( output, output_size ); |
| 7157 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7158 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7159 | input_data->x, input_data->len, |
| 7160 | label->x, label->len, |
| 7161 | output, |
| 7162 | output_size, |
| 7163 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7164 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7165 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7166 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7167 | /* If the label is empty, the test framework puts a non-null pointer |
| 7168 | * in label->x. Test that a null pointer works as well. */ |
| 7169 | if( label->len == 0 ) |
| 7170 | { |
| 7171 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7172 | if( output_size != 0 ) |
| 7173 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7174 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7175 | input_data->x, input_data->len, |
| 7176 | NULL, label->len, |
| 7177 | output, |
| 7178 | output_size, |
| 7179 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7180 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7181 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7182 | } |
| 7183 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7184 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7185 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7186 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7187 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7188 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7189 | } |
| 7190 | /* END_CASE */ |
| 7191 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7192 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7193 | void asymmetric_decrypt_fail( int key_type_arg, |
| 7194 | data_t *key_data, |
| 7195 | int alg_arg, |
| 7196 | data_t *input_data, |
| 7197 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7198 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7199 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7200 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7201 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7202 | psa_key_type_t key_type = key_type_arg; |
| 7203 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7204 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7205 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7206 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7207 | psa_status_t actual_status; |
| 7208 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7209 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7210 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7211 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7212 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7213 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7214 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7215 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7216 | psa_set_key_algorithm( &attributes, alg ); |
| 7217 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7218 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7219 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7220 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7221 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7222 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7223 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7224 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7225 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7226 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7227 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7228 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7229 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7230 | /* If the label is empty, the test framework puts a non-null pointer |
| 7231 | * in label->x. Test that a null pointer works as well. */ |
| 7232 | if( label->len == 0 ) |
| 7233 | { |
| 7234 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7235 | if( output_size != 0 ) |
| 7236 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7237 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7238 | input_data->x, input_data->len, |
| 7239 | NULL, label->len, |
| 7240 | output, output_size, |
| 7241 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7242 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7243 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7244 | } |
| 7245 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7246 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7247 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7248 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7249 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7250 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7251 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7252 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7253 | |
| 7254 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 7255 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7256 | { |
| 7257 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7258 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7259 | * 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] | 7260 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7261 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7262 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 7263 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7264 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7265 | |
| 7266 | memset( &zero, 0, sizeof( zero ) ); |
| 7267 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7268 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7269 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7270 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7271 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7272 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7273 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7274 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7275 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7276 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7277 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7278 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7279 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7280 | } |
| 7281 | /* END_CASE */ |
| 7282 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7283 | /* BEGIN_CASE */ |
| 7284 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7285 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7286 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7287 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7288 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7289 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7290 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7291 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7292 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7293 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7294 | |
| 7295 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7296 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7297 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7298 | } |
| 7299 | /* END_CASE */ |
| 7300 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7301 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7302 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7303 | int expected_status_arg ) |
| 7304 | { |
| 7305 | psa_algorithm_t alg = alg_arg; |
| 7306 | size_t capacity = capacity_arg; |
| 7307 | psa_status_t expected_status = expected_status_arg; |
| 7308 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7309 | |
| 7310 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7311 | |
| 7312 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7313 | |
| 7314 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7315 | expected_status ); |
| 7316 | |
| 7317 | exit: |
| 7318 | psa_key_derivation_abort( &operation ); |
| 7319 | PSA_DONE( ); |
| 7320 | } |
| 7321 | /* END_CASE */ |
| 7322 | |
| 7323 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7324 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7325 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7326 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7327 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7328 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7329 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7330 | int expected_status_arg3, |
| 7331 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7332 | { |
| 7333 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7334 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7335 | 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] | 7336 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7337 | expected_status_arg2, |
| 7338 | expected_status_arg3}; |
| 7339 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7340 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7341 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7342 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7343 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7344 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7345 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7346 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7347 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7348 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7349 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7350 | |
| 7351 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7352 | |
| 7353 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7354 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7355 | |
| 7356 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7357 | |
| 7358 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7359 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7360 | mbedtls_test_set_step( i ); |
| 7361 | if( steps[i] == 0 ) |
| 7362 | { |
| 7363 | /* Skip this step */ |
| 7364 | } |
| 7365 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7366 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7367 | psa_set_key_type( &attributes, key_types[i] ); |
| 7368 | PSA_ASSERT( psa_import_key( &attributes, |
| 7369 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7370 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7371 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7372 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7373 | { |
| 7374 | // When taking a private key as secret input, use key agreement |
| 7375 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7376 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7377 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7378 | expected_statuses[i] ); |
| 7379 | } |
| 7380 | else |
| 7381 | { |
| 7382 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7383 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7384 | expected_statuses[i] ); |
| 7385 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7386 | } |
| 7387 | else |
| 7388 | { |
| 7389 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7390 | &operation, steps[i], |
| 7391 | inputs[i]->x, inputs[i]->len ), |
| 7392 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7393 | } |
| 7394 | } |
| 7395 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7396 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7397 | { |
| 7398 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7399 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7400 | psa_set_key_bits( &attributes, 8 ); |
| 7401 | actual_output_status = |
| 7402 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7403 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7404 | } |
| 7405 | else |
| 7406 | { |
| 7407 | uint8_t buffer[1]; |
| 7408 | actual_output_status = |
| 7409 | psa_key_derivation_output_bytes( &operation, |
| 7410 | buffer, sizeof( buffer ) ); |
| 7411 | } |
| 7412 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7413 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7414 | exit: |
| 7415 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7416 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7417 | psa_destroy_key( keys[i] ); |
| 7418 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7419 | PSA_DONE( ); |
| 7420 | } |
| 7421 | /* END_CASE */ |
| 7422 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7423 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7424 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7425 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7426 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7427 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7428 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7429 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7430 | unsigned char input1[] = "Input 1"; |
| 7431 | size_t input1_length = sizeof( input1 ); |
| 7432 | unsigned char input2[] = "Input 2"; |
| 7433 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7434 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7435 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7436 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7437 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7438 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7439 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7440 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7441 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7442 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7443 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7444 | psa_set_key_algorithm( &attributes, alg ); |
| 7445 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7446 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7447 | PSA_ASSERT( psa_import_key( &attributes, |
| 7448 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7449 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7450 | |
| 7451 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7452 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7453 | input1, input1_length, |
| 7454 | input2, input2_length, |
| 7455 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7456 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7457 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7458 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7459 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7460 | PSA_ERROR_BAD_STATE ); |
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 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7463 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7464 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7465 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7466 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7467 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7468 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7469 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7470 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7471 | } |
| 7472 | /* END_CASE */ |
| 7473 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7474 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7475 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7476 | { |
| 7477 | uint8_t output_buffer[16]; |
| 7478 | size_t buffer_size = 16; |
| 7479 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7480 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7481 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7482 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7483 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7484 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7485 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7486 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7487 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7488 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7489 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7490 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7491 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7492 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7493 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7494 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7495 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7496 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7497 | |
| 7498 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7499 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7500 | } |
| 7501 | /* END_CASE */ |
| 7502 | |
| 7503 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7504 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7505 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7506 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7507 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7508 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7509 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7510 | int requested_capacity_arg, |
| 7511 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7512 | data_t *expected_output2, |
| 7513 | int other_key_input_type, |
| 7514 | int key_input_type, |
| 7515 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7516 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7517 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7518 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7519 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7520 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7521 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7522 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7523 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7524 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7525 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7526 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7527 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7528 | uint8_t *expected_outputs[2] = |
| 7529 | {expected_output1->x, expected_output2->x}; |
| 7530 | size_t output_sizes[2] = |
| 7531 | {expected_output1->len, expected_output2->len}; |
| 7532 | size_t output_buffer_size = 0; |
| 7533 | uint8_t *output_buffer = NULL; |
| 7534 | size_t expected_capacity; |
| 7535 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7536 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7537 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7538 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7539 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7540 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7541 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7542 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7543 | |
| 7544 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7545 | { |
| 7546 | if( output_sizes[i] > output_buffer_size ) |
| 7547 | output_buffer_size = output_sizes[i]; |
| 7548 | if( output_sizes[i] == 0 ) |
| 7549 | expected_outputs[i] = NULL; |
| 7550 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7551 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7552 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7553 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7554 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7555 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7556 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7557 | requested_capacity ) ); |
| 7558 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7559 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7560 | switch( steps[i] ) |
| 7561 | { |
| 7562 | case 0: |
| 7563 | break; |
| 7564 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7565 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7566 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7567 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7568 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7569 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7570 | inputs[i]->x, inputs[i]->len ), |
| 7571 | statuses[i] ); |
| 7572 | |
| 7573 | if( statuses[i] != PSA_SUCCESS ) |
| 7574 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7575 | break; |
| 7576 | case 1: // input key |
| 7577 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7578 | psa_set_key_algorithm( &attributes1, alg ); |
| 7579 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7580 | |
| 7581 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7582 | inputs[i]->x, inputs[i]->len, |
| 7583 | &keys[i] ) ); |
| 7584 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7585 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7586 | { |
| 7587 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7588 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7589 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7590 | } |
| 7591 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7592 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7593 | steps[i], |
| 7594 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7595 | break; |
| 7596 | default: |
| 7597 | TEST_ASSERT( ! "default case not supported" ); |
| 7598 | break; |
| 7599 | } |
| 7600 | break; |
| 7601 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7602 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7603 | { |
| 7604 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7605 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7606 | steps[i], |
| 7607 | inputs[i]->x, |
| 7608 | inputs[i]->len ), |
| 7609 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7610 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7611 | case 1: // input key, type DERIVE |
| 7612 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7613 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7614 | psa_set_key_algorithm( &attributes2, alg ); |
| 7615 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7616 | |
| 7617 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7618 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7619 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7620 | |
| 7621 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7622 | inputs[i]->x, inputs[i]->len, |
| 7623 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7624 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7625 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7626 | steps[i], |
| 7627 | keys[i] ), |
| 7628 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7629 | break; |
| 7630 | case 2: // key agreement |
| 7631 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7632 | psa_set_key_algorithm( &attributes3, alg ); |
| 7633 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7634 | |
| 7635 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7636 | inputs[i]->x, inputs[i]->len, |
| 7637 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7638 | |
| 7639 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7640 | &operation, |
| 7641 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7642 | keys[i], key_agreement_peer_key->x, |
| 7643 | key_agreement_peer_key->len ), statuses[i] ); |
| 7644 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7645 | default: |
| 7646 | TEST_ASSERT( ! "default case not supported" ); |
| 7647 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7648 | } |
| 7649 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7650 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7651 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7652 | break; |
| 7653 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7654 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7655 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7656 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7657 | |
| 7658 | if( statuses[i] != PSA_SUCCESS ) |
| 7659 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7660 | break; |
| 7661 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7662 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7663 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7664 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7665 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7666 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7667 | expected_capacity = requested_capacity; |
| 7668 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7669 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7670 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7671 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7672 | |
| 7673 | /* For output key derivation secret must be provided using |
| 7674 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7675 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7676 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7677 | |
| 7678 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7679 | psa_set_key_algorithm( &attributes4, alg ); |
| 7680 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7681 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7682 | |
| 7683 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7684 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7685 | } |
| 7686 | else // output bytes |
| 7687 | { |
| 7688 | /* Expansion phase. */ |
| 7689 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7690 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7691 | /* Read some bytes. */ |
| 7692 | status = psa_key_derivation_output_bytes( &operation, |
| 7693 | output_buffer, output_sizes[i] ); |
| 7694 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7695 | { |
| 7696 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7697 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7698 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7699 | continue; |
| 7700 | } |
| 7701 | else if( expected_capacity == 0 || |
| 7702 | output_sizes[i] > expected_capacity ) |
| 7703 | { |
| 7704 | /* Capacity exceeded. */ |
| 7705 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7706 | expected_capacity = 0; |
| 7707 | continue; |
| 7708 | } |
| 7709 | /* Success. Check the read data. */ |
| 7710 | PSA_ASSERT( status ); |
| 7711 | if( output_sizes[i] != 0 ) |
| 7712 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7713 | expected_outputs[i], output_sizes[i] ); |
| 7714 | /* Check the operation status. */ |
| 7715 | expected_capacity -= output_sizes[i]; |
| 7716 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7717 | ¤t_capacity ) ); |
| 7718 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7719 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7720 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7721 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7722 | |
| 7723 | exit: |
| 7724 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7725 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7726 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7727 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7728 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7729 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7730 | } |
| 7731 | /* END_CASE */ |
| 7732 | |
| 7733 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7734 | void derive_full( int alg_arg, |
| 7735 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7736 | data_t *input1, |
| 7737 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7738 | int requested_capacity_arg ) |
| 7739 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7740 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7741 | psa_algorithm_t alg = alg_arg; |
| 7742 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7743 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7744 | unsigned char output_buffer[16]; |
| 7745 | size_t expected_capacity = requested_capacity; |
| 7746 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7747 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7748 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7749 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7750 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7751 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7752 | psa_set_key_algorithm( &attributes, alg ); |
| 7753 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7754 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7755 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7756 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7757 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7758 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7759 | input1->x, input1->len, |
| 7760 | input2->x, input2->len, |
| 7761 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7762 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7763 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7764 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7765 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7766 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7767 | |
| 7768 | /* Expansion phase. */ |
| 7769 | while( current_capacity > 0 ) |
| 7770 | { |
| 7771 | size_t read_size = sizeof( output_buffer ); |
| 7772 | if( read_size > current_capacity ) |
| 7773 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7774 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7775 | output_buffer, |
| 7776 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7777 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7778 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7779 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7780 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7781 | } |
| 7782 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7783 | /* Check that the operation refuses to go over capacity. */ |
| 7784 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7785 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7786 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7787 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7788 | |
| 7789 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7790 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7791 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7792 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7793 | } |
| 7794 | /* END_CASE */ |
| 7795 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7796 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7797 | void derive_key_exercise( int alg_arg, |
| 7798 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7799 | data_t *input1, |
| 7800 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7801 | int derived_type_arg, |
| 7802 | int derived_bits_arg, |
| 7803 | int derived_usage_arg, |
| 7804 | int derived_alg_arg ) |
| 7805 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7806 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7807 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7808 | psa_algorithm_t alg = alg_arg; |
| 7809 | psa_key_type_t derived_type = derived_type_arg; |
| 7810 | size_t derived_bits = derived_bits_arg; |
| 7811 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7812 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7813 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7814 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7815 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7816 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7817 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7818 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7819 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7820 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7821 | psa_set_key_algorithm( &attributes, alg ); |
| 7822 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7823 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7824 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7825 | |
| 7826 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7827 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7828 | input1->x, input1->len, |
| 7829 | input2->x, input2->len, |
| 7830 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7831 | goto exit; |
| 7832 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7833 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7834 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7835 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7836 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7837 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7838 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7839 | |
| 7840 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7841 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7842 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7843 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7844 | |
| 7845 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7846 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7847 | goto exit; |
| 7848 | |
| 7849 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7850 | /* |
| 7851 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7852 | * thus reset them as required. |
| 7853 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7854 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7855 | |
| 7856 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7857 | psa_destroy_key( base_key ); |
| 7858 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7859 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7860 | } |
| 7861 | /* END_CASE */ |
| 7862 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7863 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7864 | void derive_key_export( int alg_arg, |
| 7865 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7866 | data_t *input1, |
| 7867 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7868 | int bytes1_arg, |
| 7869 | int bytes2_arg ) |
| 7870 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7871 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7872 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7873 | psa_algorithm_t alg = alg_arg; |
| 7874 | size_t bytes1 = bytes1_arg; |
| 7875 | size_t bytes2 = bytes2_arg; |
| 7876 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7877 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7878 | uint8_t *output_buffer = NULL; |
| 7879 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7880 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7881 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7882 | size_t length; |
| 7883 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7884 | ASSERT_ALLOC( output_buffer, capacity ); |
| 7885 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7886 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7887 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7888 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7889 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7890 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7891 | 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] | 7892 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7893 | |
| 7894 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7895 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7896 | input1->x, input1->len, |
| 7897 | input2->x, input2->len, |
| 7898 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7899 | goto exit; |
| 7900 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7901 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7902 | output_buffer, |
| 7903 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7904 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7905 | |
| 7906 | /* 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] | 7907 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7908 | input1->x, input1->len, |
| 7909 | input2->x, input2->len, |
| 7910 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7911 | goto exit; |
| 7912 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7913 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7914 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7915 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7916 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7917 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7918 | &derived_key ) ); |
| 7919 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7920 | export_buffer, bytes1, |
| 7921 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7922 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7923 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7924 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7925 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7926 | &derived_key ) ); |
| 7927 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7928 | export_buffer + bytes1, bytes2, |
| 7929 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7930 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7931 | |
| 7932 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 7933 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 7934 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7935 | |
| 7936 | exit: |
| 7937 | mbedtls_free( output_buffer ); |
| 7938 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7939 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7940 | psa_destroy_key( base_key ); |
| 7941 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7942 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7943 | } |
| 7944 | /* END_CASE */ |
| 7945 | |
| 7946 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7947 | void derive_key_type( int alg_arg, |
| 7948 | data_t *key_data, |
| 7949 | data_t *input1, |
| 7950 | data_t *input2, |
| 7951 | int key_type_arg, int bits_arg, |
| 7952 | data_t *expected_export ) |
| 7953 | { |
| 7954 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7955 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7956 | const psa_algorithm_t alg = alg_arg; |
| 7957 | const psa_key_type_t key_type = key_type_arg; |
| 7958 | const size_t bits = bits_arg; |
| 7959 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7960 | const size_t export_buffer_size = |
| 7961 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 7962 | uint8_t *export_buffer = NULL; |
| 7963 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7964 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7965 | size_t export_length; |
| 7966 | |
| 7967 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 7968 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7969 | |
| 7970 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7971 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7972 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 7973 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 7974 | &base_key ) ); |
| 7975 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 7976 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7977 | &operation, base_key, alg, |
| 7978 | input1->x, input1->len, |
| 7979 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 7980 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7981 | goto exit; |
| 7982 | |
| 7983 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7984 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7985 | psa_set_key_type( &derived_attributes, key_type ); |
| 7986 | psa_set_key_bits( &derived_attributes, bits ); |
| 7987 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 7988 | &derived_key ) ); |
| 7989 | |
| 7990 | PSA_ASSERT( psa_export_key( derived_key, |
| 7991 | export_buffer, export_buffer_size, |
| 7992 | &export_length ) ); |
| 7993 | ASSERT_COMPARE( export_buffer, export_length, |
| 7994 | expected_export->x, expected_export->len ); |
| 7995 | |
| 7996 | exit: |
| 7997 | mbedtls_free( export_buffer ); |
| 7998 | psa_key_derivation_abort( &operation ); |
| 7999 | psa_destroy_key( base_key ); |
| 8000 | psa_destroy_key( derived_key ); |
| 8001 | PSA_DONE( ); |
| 8002 | } |
| 8003 | /* END_CASE */ |
| 8004 | |
| 8005 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8006 | void derive_key( int alg_arg, |
| 8007 | data_t *key_data, data_t *input1, data_t *input2, |
| 8008 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8009 | int expected_status_arg, |
| 8010 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8011 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8012 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8013 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8014 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8015 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8016 | size_t bits = bits_arg; |
| 8017 | psa_status_t expected_status = expected_status_arg; |
| 8018 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8019 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8020 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8021 | |
| 8022 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8023 | |
| 8024 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8025 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8026 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8027 | 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] | 8028 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8029 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8030 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8031 | input1->x, input1->len, |
| 8032 | input2->x, input2->len, |
| 8033 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8034 | goto exit; |
| 8035 | |
| 8036 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8037 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8038 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8039 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8040 | |
| 8041 | psa_status_t status = |
| 8042 | psa_key_derivation_output_key( &derived_attributes, |
| 8043 | &operation, |
| 8044 | &derived_key ); |
| 8045 | if( is_large_output > 0 ) |
| 8046 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8047 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8048 | |
| 8049 | exit: |
| 8050 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8051 | psa_destroy_key( base_key ); |
| 8052 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8053 | PSA_DONE( ); |
| 8054 | } |
| 8055 | /* END_CASE */ |
| 8056 | |
| 8057 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8058 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 8059 | int our_key_type_arg, int our_key_alg_arg, |
| 8060 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8061 | int expected_status_arg ) |
| 8062 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8063 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8064 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8065 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8066 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8067 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8068 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8069 | psa_status_t expected_status = expected_status_arg; |
| 8070 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8071 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8072 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8073 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8074 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8075 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8076 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8077 | PSA_ASSERT( psa_import_key( &attributes, |
| 8078 | our_key_data->x, our_key_data->len, |
| 8079 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8080 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8081 | /* The tests currently include inputs that should fail at either step. |
| 8082 | * Test cases that fail at the setup step should be changed to call |
| 8083 | * key_derivation_setup instead, and this function should be renamed |
| 8084 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8085 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8086 | if( status == PSA_SUCCESS ) |
| 8087 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8088 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 8089 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8090 | our_key, |
| 8091 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8092 | expected_status ); |
| 8093 | } |
| 8094 | else |
| 8095 | { |
| 8096 | TEST_ASSERT( status == expected_status ); |
| 8097 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8098 | |
| 8099 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8100 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8101 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8102 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8103 | } |
| 8104 | /* END_CASE */ |
| 8105 | |
| 8106 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8107 | void raw_key_agreement( int alg_arg, |
| 8108 | int our_key_type_arg, data_t *our_key_data, |
| 8109 | data_t *peer_key_data, |
| 8110 | data_t *expected_output ) |
| 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 | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8113 | psa_algorithm_t alg = alg_arg; |
| 8114 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8115 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8116 | unsigned char *output = NULL; |
| 8117 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8118 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8119 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8120 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8121 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8122 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8123 | psa_set_key_algorithm( &attributes, alg ); |
| 8124 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8125 | PSA_ASSERT( psa_import_key( &attributes, |
| 8126 | our_key_data->x, our_key_data->len, |
| 8127 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8128 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8129 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 8130 | key_bits = psa_get_key_bits( &attributes ); |
| 8131 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8132 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8133 | TEST_LE_U( expected_output->len, |
| 8134 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 8135 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 8136 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8137 | |
| 8138 | /* Good case with exact output size */ |
| 8139 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 8140 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8141 | peer_key_data->x, peer_key_data->len, |
| 8142 | output, expected_output->len, |
| 8143 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8144 | ASSERT_COMPARE( output, output_length, |
| 8145 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8146 | mbedtls_free( output ); |
| 8147 | output = NULL; |
| 8148 | output_length = ~0; |
| 8149 | |
| 8150 | /* Larger buffer */ |
| 8151 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 8152 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8153 | peer_key_data->x, peer_key_data->len, |
| 8154 | output, expected_output->len + 1, |
| 8155 | &output_length ) ); |
| 8156 | ASSERT_COMPARE( output, output_length, |
| 8157 | expected_output->x, expected_output->len ); |
| 8158 | mbedtls_free( output ); |
| 8159 | output = NULL; |
| 8160 | output_length = ~0; |
| 8161 | |
| 8162 | /* Buffer too small */ |
| 8163 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 8164 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 8165 | peer_key_data->x, peer_key_data->len, |
| 8166 | output, expected_output->len - 1, |
| 8167 | &output_length ), |
| 8168 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8169 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8170 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8171 | mbedtls_free( output ); |
| 8172 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8173 | |
| 8174 | exit: |
| 8175 | mbedtls_free( output ); |
| 8176 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8177 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8178 | } |
| 8179 | /* END_CASE */ |
| 8180 | |
| 8181 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8182 | void key_agreement_capacity( int alg_arg, |
| 8183 | int our_key_type_arg, data_t *our_key_data, |
| 8184 | data_t *peer_key_data, |
| 8185 | int expected_capacity_arg ) |
| 8186 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8187 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8188 | psa_algorithm_t alg = alg_arg; |
| 8189 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8190 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8191 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8192 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8193 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8194 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8195 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8196 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8197 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8198 | psa_set_key_algorithm( &attributes, alg ); |
| 8199 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8200 | PSA_ASSERT( psa_import_key( &attributes, |
| 8201 | our_key_data->x, our_key_data->len, |
| 8202 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8203 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8204 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8205 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8206 | &operation, |
| 8207 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8208 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8209 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8210 | { |
| 8211 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8212 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8213 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8214 | NULL, 0 ) ); |
| 8215 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8216 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8217 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8218 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8219 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8220 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8221 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8222 | /* Test the actual capacity by reading the output. */ |
| 8223 | while( actual_capacity > sizeof( output ) ) |
| 8224 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8225 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8226 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8227 | actual_capacity -= sizeof( output ); |
| 8228 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8229 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8230 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8231 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 8232 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8233 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8234 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8235 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8236 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8237 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8238 | } |
| 8239 | /* END_CASE */ |
| 8240 | |
| 8241 | /* BEGIN_CASE */ |
| 8242 | void key_agreement_output( int alg_arg, |
| 8243 | int our_key_type_arg, data_t *our_key_data, |
| 8244 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8245 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8246 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8247 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8248 | psa_algorithm_t alg = alg_arg; |
| 8249 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8250 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8251 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8252 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8253 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8254 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 8255 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8256 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8257 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8258 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8259 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8260 | psa_set_key_algorithm( &attributes, alg ); |
| 8261 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8262 | PSA_ASSERT( psa_import_key( &attributes, |
| 8263 | our_key_data->x, our_key_data->len, |
| 8264 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8265 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8266 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8267 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8268 | &operation, |
| 8269 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8270 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8271 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8272 | { |
| 8273 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8274 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8275 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8276 | NULL, 0 ) ); |
| 8277 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8278 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8279 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8280 | actual_output, |
| 8281 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8282 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8283 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8284 | if( expected_output2->len != 0 ) |
| 8285 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8286 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8287 | actual_output, |
| 8288 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8289 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8290 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8291 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8292 | |
| 8293 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8294 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8295 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8296 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8297 | mbedtls_free( actual_output ); |
| 8298 | } |
| 8299 | /* END_CASE */ |
| 8300 | |
| 8301 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8302 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8303 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8304 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8305 | unsigned char *output = NULL; |
| 8306 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8307 | size_t i; |
| 8308 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8309 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8310 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8311 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8312 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8313 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8314 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8315 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8316 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8317 | /* Run several times, to ensure that every output byte will be |
| 8318 | * nonzero at least once with overwhelming probability |
| 8319 | * (2^(-8*number_of_runs)). */ |
| 8320 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8321 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8322 | if( bytes != 0 ) |
| 8323 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8324 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8325 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8326 | for( i = 0; i < bytes; i++ ) |
| 8327 | { |
| 8328 | if( output[i] != 0 ) |
| 8329 | ++changed[i]; |
| 8330 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8331 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8332 | |
| 8333 | /* Check that every byte was changed to nonzero at least once. This |
| 8334 | * validates that psa_generate_random is overwriting every byte of |
| 8335 | * the output buffer. */ |
| 8336 | for( i = 0; i < bytes; i++ ) |
| 8337 | { |
| 8338 | TEST_ASSERT( changed[i] != 0 ); |
| 8339 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8340 | |
| 8341 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8342 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8343 | mbedtls_free( output ); |
| 8344 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8345 | } |
| 8346 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8347 | |
| 8348 | /* BEGIN_CASE */ |
| 8349 | void generate_key( int type_arg, |
| 8350 | int bits_arg, |
| 8351 | int usage_arg, |
| 8352 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8353 | int expected_status_arg, |
| 8354 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8355 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8356 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8357 | psa_key_type_t type = type_arg; |
| 8358 | psa_key_usage_t usage = usage_arg; |
| 8359 | size_t bits = bits_arg; |
| 8360 | psa_algorithm_t alg = alg_arg; |
| 8361 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8362 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8363 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8364 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8365 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8366 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8367 | psa_set_key_usage_flags( &attributes, usage ); |
| 8368 | psa_set_key_algorithm( &attributes, alg ); |
| 8369 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8370 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8371 | |
| 8372 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8373 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8374 | |
| 8375 | if( is_large_key > 0 ) |
| 8376 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8377 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8378 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8379 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8380 | |
| 8381 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8382 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8383 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8384 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8385 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8386 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8387 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8388 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8389 | |
| 8390 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8391 | /* |
| 8392 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8393 | * thus reset them as required. |
| 8394 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8395 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8396 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8397 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8398 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8399 | } |
| 8400 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8401 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8402 | /* 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] | 8403 | void generate_key_rsa( int bits_arg, |
| 8404 | data_t *e_arg, |
| 8405 | int expected_status_arg ) |
| 8406 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8407 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8408 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8409 | size_t bits = bits_arg; |
| 8410 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8411 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8412 | psa_status_t expected_status = expected_status_arg; |
| 8413 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8414 | uint8_t *exported = NULL; |
| 8415 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8416 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8417 | size_t exported_length = SIZE_MAX; |
| 8418 | uint8_t *e_read_buffer = NULL; |
| 8419 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8420 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8421 | size_t e_read_length = SIZE_MAX; |
| 8422 | |
| 8423 | if( e_arg->len == 0 || |
| 8424 | ( e_arg->len == 3 && |
| 8425 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8426 | { |
| 8427 | is_default_public_exponent = 1; |
| 8428 | e_read_size = 0; |
| 8429 | } |
| 8430 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8431 | ASSERT_ALLOC( exported, exported_size ); |
| 8432 | |
| 8433 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8434 | |
| 8435 | psa_set_key_usage_flags( &attributes, usage ); |
| 8436 | psa_set_key_algorithm( &attributes, alg ); |
| 8437 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8438 | e_arg->x, e_arg->len ) ); |
| 8439 | psa_set_key_bits( &attributes, bits ); |
| 8440 | |
| 8441 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8442 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8443 | if( expected_status != PSA_SUCCESS ) |
| 8444 | goto exit; |
| 8445 | |
| 8446 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8447 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8448 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8449 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8450 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8451 | e_read_buffer, e_read_size, |
| 8452 | &e_read_length ) ); |
| 8453 | if( is_default_public_exponent ) |
| 8454 | TEST_EQUAL( e_read_length, 0 ); |
| 8455 | else |
| 8456 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8457 | |
| 8458 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8459 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8460 | goto exit; |
| 8461 | |
| 8462 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8463 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8464 | exported, exported_size, |
| 8465 | &exported_length ) ); |
| 8466 | { |
| 8467 | uint8_t *p = exported; |
| 8468 | uint8_t *end = exported + exported_length; |
| 8469 | size_t len; |
| 8470 | /* RSAPublicKey ::= SEQUENCE { |
| 8471 | * modulus INTEGER, -- n |
| 8472 | * publicExponent INTEGER } -- e |
| 8473 | */ |
| 8474 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8475 | MBEDTLS_ASN1_SEQUENCE | |
| 8476 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8477 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8478 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8479 | MBEDTLS_ASN1_INTEGER ) ); |
| 8480 | if( len >= 1 && p[0] == 0 ) |
| 8481 | { |
| 8482 | ++p; |
| 8483 | --len; |
| 8484 | } |
| 8485 | if( e_arg->len == 0 ) |
| 8486 | { |
| 8487 | TEST_EQUAL( len, 3 ); |
| 8488 | TEST_EQUAL( p[0], 1 ); |
| 8489 | TEST_EQUAL( p[1], 0 ); |
| 8490 | TEST_EQUAL( p[2], 1 ); |
| 8491 | } |
| 8492 | else |
| 8493 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8494 | } |
| 8495 | |
| 8496 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8497 | /* |
| 8498 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8499 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8500 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8501 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8502 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8503 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8504 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8505 | mbedtls_free( e_read_buffer ); |
| 8506 | mbedtls_free( exported ); |
| 8507 | } |
| 8508 | /* END_CASE */ |
| 8509 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8510 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8511 | void persistent_key_load_key_from_storage( data_t *data, |
| 8512 | int type_arg, int bits_arg, |
| 8513 | int usage_flags_arg, int alg_arg, |
| 8514 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8515 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8516 | 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] | 8517 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8518 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8519 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8520 | psa_key_type_t type = type_arg; |
| 8521 | size_t bits = bits_arg; |
| 8522 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8523 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8524 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8525 | unsigned char *first_export = NULL; |
| 8526 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8527 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8528 | size_t first_exported_length; |
| 8529 | size_t second_exported_length; |
| 8530 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8531 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8532 | { |
| 8533 | ASSERT_ALLOC( first_export, export_size ); |
| 8534 | ASSERT_ALLOC( second_export, export_size ); |
| 8535 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8536 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8537 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8538 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8539 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8540 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8541 | psa_set_key_algorithm( &attributes, alg ); |
| 8542 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8543 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8544 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8545 | switch( generation_method ) |
| 8546 | { |
| 8547 | case IMPORT_KEY: |
| 8548 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8549 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8550 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8551 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8552 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8553 | case GENERATE_KEY: |
| 8554 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8555 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8556 | break; |
| 8557 | |
| 8558 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8559 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8560 | { |
| 8561 | /* Create base key */ |
| 8562 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8563 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8564 | psa_set_key_usage_flags( &base_attributes, |
| 8565 | PSA_KEY_USAGE_DERIVE ); |
| 8566 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8567 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8568 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8569 | data->x, data->len, |
| 8570 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8571 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8572 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8573 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8574 | &operation, |
| 8575 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8576 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8577 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8578 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8579 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8580 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8581 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8582 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8583 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8584 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8585 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8586 | #else |
| 8587 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8588 | #endif |
| 8589 | break; |
| 8590 | |
| 8591 | default: |
| 8592 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8593 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8594 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8595 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8596 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8597 | /* Export the key if permitted by the key policy. */ |
| 8598 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8599 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8600 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8601 | first_export, export_size, |
| 8602 | &first_exported_length ) ); |
| 8603 | if( generation_method == IMPORT_KEY ) |
| 8604 | ASSERT_COMPARE( data->x, data->len, |
| 8605 | first_export, first_exported_length ); |
| 8606 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8607 | |
| 8608 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8609 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8610 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8611 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8612 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8613 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8614 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8615 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8616 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8617 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8618 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8619 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8620 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8621 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8622 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8623 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8624 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8625 | /* Export the key again if permitted by the key policy. */ |
| 8626 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8627 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8628 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8629 | second_export, export_size, |
| 8630 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8631 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8632 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8633 | } |
| 8634 | |
| 8635 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8636 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8637 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8638 | |
| 8639 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8640 | /* |
| 8641 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8642 | * thus reset them as required. |
| 8643 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8644 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8645 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8646 | mbedtls_free( first_export ); |
| 8647 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8648 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8649 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8650 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8651 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8652 | } |
| 8653 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8654 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8655 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8656 | void ecjpake_setup( int alg_arg, int primitive_arg, int hash_arg, int role_arg, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8657 | int input_first, data_t *pw_data, |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8658 | int expected_status_arg ) |
| 8659 | { |
| 8660 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8661 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 8662 | psa_algorithm_t alg = alg_arg; |
| 8663 | psa_algorithm_t hash_alg = hash_arg; |
| 8664 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8665 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8666 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8667 | psa_status_t expected_status = expected_status_arg; |
| 8668 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8669 | unsigned char *output_buffer = NULL; |
| 8670 | size_t output_len = 0; |
| 8671 | |
| 8672 | PSA_INIT( ); |
| 8673 | |
| 8674 | ASSERT_ALLOC( output_buffer, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8675 | PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8676 | PSA_PAKE_STEP_KEY_SHARE) ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8677 | |
| 8678 | if( pw_data->len > 0 ) |
| 8679 | { |
| 8680 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8681 | psa_set_key_algorithm( &attributes, alg ); |
| 8682 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8683 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8684 | &key ) ); |
| 8685 | } |
| 8686 | |
| 8687 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8688 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8689 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8690 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8691 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8692 | |
| 8693 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8694 | PSA_ERROR_BAD_STATE ); |
| 8695 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8696 | PSA_ERROR_BAD_STATE ); |
| 8697 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8698 | PSA_ERROR_BAD_STATE ); |
| 8699 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8700 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8701 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8702 | NULL, 0, NULL ), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8703 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8704 | 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] | 8705 | PSA_ERROR_BAD_STATE ); |
| 8706 | |
| 8707 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8708 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8709 | status = psa_pake_setup( &operation, &cipher_suite ); |
| 8710 | if( status != PSA_SUCCESS ) |
| 8711 | { |
| 8712 | TEST_EQUAL( status, expected_status ); |
| 8713 | goto exit; |
| 8714 | } |
| 8715 | else |
| 8716 | PSA_ASSERT( status ); |
| 8717 | |
Neil Armstrong | 50de0ae | 2022-06-08 17:46:24 +0200 | [diff] [blame] | 8718 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8719 | PSA_ERROR_BAD_STATE ); |
| 8720 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8721 | status = psa_pake_set_role( &operation, role ); |
| 8722 | if( status != PSA_SUCCESS ) |
| 8723 | { |
| 8724 | TEST_EQUAL( status, expected_status ); |
| 8725 | goto exit; |
| 8726 | } |
| 8727 | else |
| 8728 | PSA_ASSERT( status ); |
| 8729 | |
| 8730 | if( pw_data->len > 0 ) |
| 8731 | { |
| 8732 | status = psa_pake_set_password_key( &operation, key ); |
| 8733 | if( status != PSA_SUCCESS ) |
| 8734 | { |
| 8735 | TEST_EQUAL( status, expected_status ); |
| 8736 | goto exit; |
| 8737 | } |
| 8738 | else |
| 8739 | PSA_ASSERT( status ); |
| 8740 | } |
| 8741 | |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8742 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8743 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8744 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8745 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8746 | |
| 8747 | const uint8_t unsupported_id[] = "abcd"; |
| 8748 | |
| 8749 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8750 | PSA_ERROR_NOT_SUPPORTED ); |
| 8751 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8752 | PSA_ERROR_NOT_SUPPORTED ); |
| 8753 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8754 | /* First round */ |
| 8755 | if( input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8756 | { |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8757 | /* Invalid parameters */ |
| 8758 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8759 | NULL, 0 ), |
| 8760 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8761 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8762 | output_buffer, 66 ), |
| 8763 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8764 | /* Invalid first step */ |
| 8765 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8766 | output_buffer, 66 ), |
| 8767 | PSA_ERROR_BAD_STATE ); |
| 8768 | |
| 8769 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8770 | output_buffer, 66 ), |
| 8771 | expected_status); |
| 8772 | |
| 8773 | if( expected_status == PSA_SUCCESS ) |
| 8774 | { |
| 8775 | /* Buffer too large */ |
| 8776 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8777 | output_buffer, 512 ), |
| 8778 | PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8779 | |
| 8780 | /* The operation should be aborted at this point */ |
| 8781 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8782 | output_buffer, 66 ), |
| 8783 | PSA_ERROR_BAD_STATE ); |
| 8784 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8785 | } |
| 8786 | else |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8787 | { |
| 8788 | /* Invalid parameters */ |
| 8789 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8790 | NULL, 0, NULL ), |
| 8791 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8792 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8793 | output_buffer, 512, &output_len ), |
| 8794 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8795 | /* Invalid first step */ |
| 8796 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8797 | output_buffer, 512, &output_len ), |
| 8798 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8799 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8800 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8801 | output_buffer, 512, &output_len ), |
| 8802 | expected_status ); |
Neil Armstrong | 98506ab | 2022-06-08 17:43:20 +0200 | [diff] [blame] | 8803 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8804 | if( expected_status == PSA_SUCCESS ) |
| 8805 | { |
| 8806 | TEST_ASSERT( output_len > 0 ); |
| 8807 | |
| 8808 | /* Buffer too small */ |
| 8809 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8810 | output_buffer, 5, &output_len ), |
| 8811 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8812 | |
| 8813 | /* The operation should be aborted at this point */ |
| 8814 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8815 | output_buffer, 512, &output_len ), |
| 8816 | PSA_ERROR_BAD_STATE ); |
| 8817 | } |
| 8818 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8819 | |
| 8820 | exit: |
| 8821 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 8822 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8823 | mbedtls_free( output_buffer ); |
| 8824 | PSA_DONE( ); |
| 8825 | } |
| 8826 | /* END_CASE */ |
| 8827 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8828 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8829 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 8830 | int client_input_first, int inject_error, |
| 8831 | data_t *pw_data ) |
| 8832 | { |
| 8833 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8834 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8835 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8836 | psa_algorithm_t alg = alg_arg; |
| 8837 | psa_algorithm_t hash_alg = hash_arg; |
| 8838 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8839 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8840 | |
| 8841 | PSA_INIT( ); |
| 8842 | |
| 8843 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8844 | psa_set_key_algorithm( &attributes, alg ); |
| 8845 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8846 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8847 | &key ) ); |
| 8848 | |
| 8849 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8850 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8851 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8852 | |
| 8853 | |
| 8854 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8855 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8856 | |
| 8857 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8858 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8859 | |
| 8860 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8861 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8862 | |
| 8863 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8864 | client_input_first, 1, |
| 8865 | inject_error ), 1 ); |
| 8866 | |
| 8867 | if( inject_error == 1 || inject_error == 2 ) |
| 8868 | goto exit; |
| 8869 | |
| 8870 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8871 | client_input_first, 2, |
| 8872 | inject_error ), 1 ); |
| 8873 | |
| 8874 | exit: |
| 8875 | psa_destroy_key( key ); |
| 8876 | psa_pake_abort( &server ); |
| 8877 | psa_pake_abort( &client ); |
| 8878 | PSA_DONE( ); |
| 8879 | } |
| 8880 | /* END_CASE */ |
| 8881 | |
| 8882 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8883 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8884 | int derive_alg_arg, data_t *pw_data, |
| 8885 | int client_input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8886 | { |
| 8887 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8888 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8889 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8890 | psa_algorithm_t alg = alg_arg; |
| 8891 | psa_algorithm_t hash_alg = hash_arg; |
| 8892 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 8893 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8894 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8895 | psa_key_derivation_operation_t server_derive = |
| 8896 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8897 | psa_key_derivation_operation_t client_derive = |
| 8898 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8899 | |
| 8900 | PSA_INIT( ); |
| 8901 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8902 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8903 | psa_set_key_algorithm( &attributes, alg ); |
| 8904 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8905 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8906 | &key ) ); |
| 8907 | |
| 8908 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8909 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8910 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8911 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8912 | /* Get shared key */ |
| 8913 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 8914 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 8915 | |
| 8916 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 8917 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 8918 | { |
| 8919 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 8920 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8921 | (const uint8_t*) "", 0) ); |
| 8922 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 8923 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8924 | (const uint8_t*) "", 0) ); |
| 8925 | } |
| 8926 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8927 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8928 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8929 | |
| 8930 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8931 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8932 | |
| 8933 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8934 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8935 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8936 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8937 | PSA_ERROR_BAD_STATE ); |
| 8938 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8939 | PSA_ERROR_BAD_STATE ); |
| 8940 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8941 | /* First round */ |
| 8942 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8943 | client_input_first, 1, 0 ), 1 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8944 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8945 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8946 | PSA_ERROR_BAD_STATE ); |
| 8947 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8948 | PSA_ERROR_BAD_STATE ); |
| 8949 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8950 | /* Second round */ |
| 8951 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8952 | client_input_first, 2, 0 ), 1 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8953 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8954 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 8955 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 8956 | |
| 8957 | exit: |
| 8958 | psa_key_derivation_abort( &server_derive ); |
| 8959 | psa_key_derivation_abort( &client_derive ); |
| 8960 | psa_destroy_key( key ); |
| 8961 | psa_pake_abort( &server ); |
| 8962 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8963 | PSA_DONE( ); |
| 8964 | } |
| 8965 | /* END_CASE */ |