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; |
| 735 | int ret; |
| 736 | |
| 737 | ASSERT_ALLOC( buffer0, buffer_length ); |
| 738 | ASSERT_ALLOC( buffer1, buffer_length ); |
| 739 | |
| 740 | switch( round ) |
| 741 | { |
| 742 | case 1: |
| 743 | /* Server first round Output */ |
| 744 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 745 | buffer0 + buffer0_off, |
| 746 | 512 - buffer0_off, &s_g1_len ) ); |
| 747 | s_g1_off = buffer0_off; |
| 748 | buffer0_off += s_g1_len; |
| 749 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 750 | buffer0 + buffer0_off, |
| 751 | 512 - buffer0_off, &s_x1_pk_len ) ); |
| 752 | s_x1_pk_off = buffer0_off; |
| 753 | buffer0_off += s_x1_pk_len; |
| 754 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 755 | buffer0 + buffer0_off, |
| 756 | 512 - buffer0_off, &s_x1_pr_len ) ); |
| 757 | s_x1_pr_off = buffer0_off; |
| 758 | buffer0_off += s_x1_pr_len; |
| 759 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 760 | buffer0 + buffer0_off, |
| 761 | 512 - buffer0_off, &s_g2_len ) ); |
| 762 | s_g2_off = buffer0_off; |
| 763 | buffer0_off += s_g2_len; |
| 764 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 765 | buffer0 + buffer0_off, |
| 766 | 512 - buffer0_off, &s_x2_pk_len ) ); |
| 767 | s_x2_pk_off = buffer0_off; |
| 768 | buffer0_off += s_x2_pk_len; |
| 769 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 770 | buffer0 + buffer0_off, |
| 771 | 512 - buffer0_off, &s_x2_pr_len ) ); |
| 772 | s_x2_pr_off = buffer0_off; |
| 773 | buffer0_off += s_x2_pr_len; |
| 774 | |
| 775 | if( inject_error == 1 ) |
| 776 | { |
| 777 | buffer0[s_x1_pk_off + 12] >>= 4; |
| 778 | buffer0[s_x2_pk_off + 7] <<= 4; |
| 779 | expected_status = PSA_ERROR_DATA_INVALID; |
| 780 | } |
| 781 | |
| 782 | if( client_input_first == 1 ) |
| 783 | { |
| 784 | /* Client first round Input */ |
| 785 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 786 | buffer0 + s_g1_off, s_g1_len ) ); |
| 787 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 788 | buffer0 + s_x1_pk_off, |
| 789 | s_x1_pk_len ) ); |
| 790 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 791 | buffer0 + s_x1_pr_off, |
| 792 | s_x1_pr_len ) ); |
| 793 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 794 | buffer0 + s_g2_off, |
| 795 | s_g2_len ) ); |
| 796 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 797 | buffer0 + s_x2_pk_off, |
| 798 | s_x2_pk_len ) ); |
| 799 | TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 800 | buffer0 + s_x2_pr_off, |
| 801 | s_x2_pr_len ), |
| 802 | expected_status ); |
| 803 | |
| 804 | if( inject_error == 1 ) |
| 805 | { |
| 806 | ret = 1; |
| 807 | goto exit; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /* Client first round Output */ |
| 812 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 813 | buffer1 + buffer1_off, |
| 814 | 512 - buffer1_off, &c_g1_len ) ); |
| 815 | c_g1_off = buffer1_off; |
| 816 | buffer1_off += c_g1_len; |
| 817 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 818 | buffer1 + buffer1_off, |
| 819 | 512 - buffer1_off, &c_x1_pk_len ) ); |
| 820 | c_x1_pk_off = buffer1_off; |
| 821 | buffer1_off += c_x1_pk_len; |
| 822 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 823 | buffer1 + buffer1_off, |
| 824 | 512 - buffer1_off, &c_x1_pr_len ) ); |
| 825 | c_x1_pr_off = buffer1_off; |
| 826 | buffer1_off += c_x1_pr_len; |
| 827 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 828 | buffer1 + buffer1_off, |
| 829 | 512 - buffer1_off, &c_g2_len ) ); |
| 830 | c_g2_off = buffer1_off; |
| 831 | buffer1_off += c_g2_len; |
| 832 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 833 | buffer1 + buffer1_off, |
| 834 | 512 - buffer1_off, &c_x2_pk_len ) ); |
| 835 | c_x2_pk_off = buffer1_off; |
| 836 | buffer1_off += c_x2_pk_len; |
| 837 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 838 | buffer1 + buffer1_off, |
| 839 | 512 - buffer1_off, &c_x2_pr_len ) ); |
| 840 | c_x2_pr_off = buffer1_off; |
| 841 | buffer1_off += c_x2_pr_len; |
| 842 | |
| 843 | if( client_input_first == 0 ) |
| 844 | { |
| 845 | /* Client first round Input */ |
| 846 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 847 | buffer0 + s_g1_off, s_g1_len ) ); |
| 848 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 849 | buffer0 + s_x1_pk_off, |
| 850 | s_x1_pk_len ) ); |
| 851 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 852 | buffer0 + s_x1_pr_off, |
| 853 | s_x1_pr_len ) ); |
| 854 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 855 | buffer0 + s_g2_off, |
| 856 | s_g2_len ) ); |
| 857 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 858 | buffer0 + s_x2_pk_off, |
| 859 | s_x2_pk_len ) ); |
| 860 | TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 861 | buffer0 + s_x2_pr_off, |
| 862 | s_x2_pr_len ), |
| 863 | expected_status ); |
| 864 | |
| 865 | if( inject_error == 1 ) |
| 866 | break; |
| 867 | } |
| 868 | |
| 869 | if( inject_error == 2 ) |
| 870 | { |
| 871 | buffer1[c_x1_pk_off + 12] >>= 4; |
| 872 | buffer1[c_x2_pk_off + 7] <<= 4; |
| 873 | expected_status = PSA_ERROR_DATA_INVALID; |
| 874 | } |
| 875 | |
| 876 | /* Server first round Input */ |
| 877 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 878 | buffer1 + c_g1_off, c_g1_len ) ); |
| 879 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 880 | buffer1 + c_x1_pk_off, c_x1_pk_len ) ); |
| 881 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 882 | buffer1 + c_x1_pr_off, c_x1_pr_len ) ); |
| 883 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 884 | buffer1 + c_g2_off, c_g2_len ) ); |
| 885 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 886 | buffer1 + c_x2_pk_off, c_x2_pk_len ) ); |
| 887 | TEST_EQUAL( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 888 | buffer1 + c_x2_pr_off, c_x2_pr_len ), |
| 889 | expected_status ); |
| 890 | |
| 891 | break; |
| 892 | |
| 893 | case 2: |
| 894 | /* Server second round Output */ |
| 895 | buffer0_off = 0; |
| 896 | |
| 897 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 898 | buffer0 + buffer0_off, |
| 899 | 512 - buffer0_off, &s_a_len ) ); |
| 900 | s_a_off = buffer0_off; |
| 901 | buffer0_off += s_a_len; |
| 902 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 903 | buffer0 + buffer0_off, |
| 904 | 512 - buffer0_off, &s_x2s_pk_len ) ); |
| 905 | s_x2s_pk_off = buffer0_off; |
| 906 | buffer0_off += s_x2s_pk_len; |
| 907 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 908 | buffer0 + buffer0_off, |
| 909 | 512 - buffer0_off, &s_x2s_pr_len ) ); |
| 910 | s_x2s_pr_off = buffer0_off; |
| 911 | buffer0_off += s_x2s_pr_len; |
| 912 | |
| 913 | if( inject_error == 3 ) |
| 914 | { |
| 915 | buffer0[s_x2s_pk_off + 12] >>= 4; |
| 916 | expected_status = PSA_ERROR_DATA_INVALID; |
| 917 | } |
| 918 | |
| 919 | if( client_input_first == 1 ) |
| 920 | { |
| 921 | /* Client second round Input */ |
| 922 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 923 | buffer0 + s_a_off, s_a_len ) ); |
| 924 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 925 | buffer0 + s_x2s_pk_off, |
| 926 | s_x2s_pk_len ) ); |
| 927 | TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 928 | buffer0 + s_x2s_pr_off, |
| 929 | s_x2s_pr_len ), |
| 930 | expected_status ); |
| 931 | |
| 932 | if( inject_error == 3 ) |
| 933 | break; |
| 934 | } |
| 935 | |
| 936 | /* Client second round Output */ |
| 937 | buffer1_off = 0; |
| 938 | |
| 939 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 940 | buffer1 + buffer1_off, |
| 941 | 512 - buffer1_off, &c_a_len ) ); |
| 942 | c_a_off = buffer1_off; |
| 943 | buffer1_off += c_a_len; |
| 944 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 945 | buffer1 + buffer1_off, |
| 946 | 512 - buffer1_off, &c_x2s_pk_len ) ); |
| 947 | c_x2s_pk_off = buffer1_off; |
| 948 | buffer1_off += c_x2s_pk_len; |
| 949 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 950 | buffer1 + buffer1_off, |
| 951 | 512 - buffer1_off, &c_x2s_pr_len ) ); |
| 952 | c_x2s_pr_off = buffer1_off; |
| 953 | buffer1_off += c_x2s_pr_len; |
| 954 | |
| 955 | if( client_input_first == 0 ) |
| 956 | { |
| 957 | /* Client second round Input */ |
| 958 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 959 | buffer0 + s_a_off, s_a_len ) ); |
| 960 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 961 | buffer0 + s_x2s_pk_off, |
| 962 | s_x2s_pk_len ) ); |
| 963 | TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 964 | buffer0 + s_x2s_pr_off, |
| 965 | s_x2s_pr_len ), |
| 966 | expected_status ); |
| 967 | |
| 968 | if( inject_error == 3 ) |
| 969 | break; |
| 970 | } |
| 971 | |
| 972 | if( inject_error == 4 ) |
| 973 | { |
| 974 | buffer1[c_x2s_pk_off + 12] >>= 4; |
| 975 | expected_status = PSA_ERROR_DATA_INVALID; |
| 976 | } |
| 977 | |
| 978 | /* Server second round Input */ |
| 979 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 980 | buffer1 + c_a_off, c_a_len ) ); |
| 981 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 982 | buffer1 + c_x2s_pk_off, c_x2s_pk_len ) ); |
| 983 | TEST_EQUAL( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 984 | buffer1 + c_x2s_pr_off, c_x2s_pr_len ), |
| 985 | expected_status ); |
| 986 | |
| 987 | break; |
| 988 | |
| 989 | } |
| 990 | |
| 991 | ret = 1; |
| 992 | |
| 993 | exit: |
| 994 | mbedtls_free( buffer0 ); |
| 995 | mbedtls_free( buffer1 ); |
| 996 | return( ret ); |
| 997 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame^] | 998 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 999 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1000 | /* END_HEADER */ |
| 1001 | |
| 1002 | /* BEGIN_DEPENDENCIES |
| 1003 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1004 | * END_DEPENDENCIES |
| 1005 | */ |
| 1006 | |
| 1007 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1008 | void static_checks( ) |
| 1009 | { |
| 1010 | size_t max_truncated_mac_size = |
| 1011 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1012 | |
| 1013 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1014 | * encoding. The shifted mask is the maximum truncated value. The |
| 1015 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1016 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1017 | } |
| 1018 | /* END_CASE */ |
| 1019 | |
| 1020 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1021 | void import_with_policy( int type_arg, |
| 1022 | int usage_arg, int alg_arg, |
| 1023 | int expected_status_arg ) |
| 1024 | { |
| 1025 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1026 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1027 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1028 | psa_key_type_t type = type_arg; |
| 1029 | psa_key_usage_t usage = usage_arg; |
| 1030 | psa_algorithm_t alg = alg_arg; |
| 1031 | psa_status_t expected_status = expected_status_arg; |
| 1032 | const uint8_t key_material[16] = {0}; |
| 1033 | psa_status_t status; |
| 1034 | |
| 1035 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1036 | |
| 1037 | psa_set_key_type( &attributes, type ); |
| 1038 | psa_set_key_usage_flags( &attributes, usage ); |
| 1039 | psa_set_key_algorithm( &attributes, alg ); |
| 1040 | |
| 1041 | status = psa_import_key( &attributes, |
| 1042 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1043 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1044 | TEST_EQUAL( status, expected_status ); |
| 1045 | if( status != PSA_SUCCESS ) |
| 1046 | goto exit; |
| 1047 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1048 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1049 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1050 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1051 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1052 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1053 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1054 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1055 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1056 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1057 | |
| 1058 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1059 | /* |
| 1060 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1061 | * thus reset them as required. |
| 1062 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1063 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1064 | |
| 1065 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1066 | PSA_DONE( ); |
| 1067 | } |
| 1068 | /* END_CASE */ |
| 1069 | |
| 1070 | /* BEGIN_CASE */ |
| 1071 | void import_with_data( data_t *data, int type_arg, |
| 1072 | int attr_bits_arg, |
| 1073 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1074 | { |
| 1075 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1076 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1077 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1078 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1079 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1080 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1081 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1082 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1083 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1084 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1085 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1086 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1087 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1088 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1089 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1090 | if( status != PSA_SUCCESS ) |
| 1091 | goto exit; |
| 1092 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1093 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1094 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1095 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1096 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1097 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1098 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1099 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1100 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1101 | |
| 1102 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1103 | /* |
| 1104 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1105 | * thus reset them as required. |
| 1106 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1107 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1108 | |
| 1109 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1110 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1111 | } |
| 1112 | /* END_CASE */ |
| 1113 | |
| 1114 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1115 | void import_large_key( int type_arg, int byte_size_arg, |
| 1116 | int expected_status_arg ) |
| 1117 | { |
| 1118 | psa_key_type_t type = type_arg; |
| 1119 | size_t byte_size = byte_size_arg; |
| 1120 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1121 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1122 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1123 | psa_status_t status; |
| 1124 | uint8_t *buffer = NULL; |
| 1125 | size_t buffer_size = byte_size + 1; |
| 1126 | size_t n; |
| 1127 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1128 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1129 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1130 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1131 | memset( buffer, 'K', byte_size ); |
| 1132 | |
| 1133 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1134 | |
| 1135 | /* Try importing the key */ |
| 1136 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1137 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1138 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1139 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1140 | TEST_EQUAL( status, expected_status ); |
| 1141 | |
| 1142 | if( status == PSA_SUCCESS ) |
| 1143 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1144 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1145 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1146 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1147 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1148 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1149 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1150 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1151 | for( n = 0; n < byte_size; n++ ) |
| 1152 | TEST_EQUAL( buffer[n], 'K' ); |
| 1153 | for( n = byte_size; n < buffer_size; n++ ) |
| 1154 | TEST_EQUAL( buffer[n], 0 ); |
| 1155 | } |
| 1156 | |
| 1157 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1158 | /* |
| 1159 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1160 | * thus reset them as required. |
| 1161 | */ |
| 1162 | psa_reset_key_attributes( &attributes ); |
| 1163 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1164 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1165 | PSA_DONE( ); |
| 1166 | mbedtls_free( buffer ); |
| 1167 | } |
| 1168 | /* END_CASE */ |
| 1169 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1170 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1171 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1172 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1173 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1174 | size_t bits = bits_arg; |
| 1175 | psa_status_t expected_status = expected_status_arg; |
| 1176 | psa_status_t status; |
| 1177 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1178 | 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] | 1179 | size_t buffer_size = /* Slight overapproximations */ |
| 1180 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1181 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1182 | unsigned char *p; |
| 1183 | int ret; |
| 1184 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1185 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1186 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1187 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1188 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1189 | |
| 1190 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1191 | bits, keypair ) ) >= 0 ); |
| 1192 | length = ret; |
| 1193 | |
| 1194 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1195 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1196 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1197 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1198 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1199 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1200 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1201 | |
| 1202 | exit: |
| 1203 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1204 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1205 | } |
| 1206 | /* END_CASE */ |
| 1207 | |
| 1208 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1209 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1210 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1211 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1212 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1213 | int expected_bits, |
| 1214 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1215 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1216 | int canonical_input ) |
| 1217 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1218 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1219 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1220 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1221 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1222 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1223 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1224 | unsigned char *exported = NULL; |
| 1225 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1226 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1227 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1228 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1229 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1230 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1231 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1232 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1233 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1234 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1235 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1236 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1237 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1238 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1239 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1240 | psa_set_key_algorithm( &attributes, alg ); |
| 1241 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1242 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1243 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1244 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1245 | |
| 1246 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1247 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1248 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1249 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1250 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1251 | |
| 1252 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1253 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1254 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1255 | |
| 1256 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1257 | * and export_size. On errors, the exported length must be 0. */ |
| 1258 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1259 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1260 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1261 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1262 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1263 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1264 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1265 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1266 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1267 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1268 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1269 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1270 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1271 | * this validates the canonical representations. For canonical inputs, |
| 1272 | * this doesn't directly validate the implementation, but it still helps |
| 1273 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1274 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1275 | { |
| 1276 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1277 | goto exit; |
| 1278 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1279 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1280 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1281 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1282 | else |
| 1283 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1284 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1285 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1286 | &key2 ) ); |
| 1287 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1288 | reexported, |
| 1289 | export_size, |
| 1290 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1291 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1292 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1293 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1294 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1295 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1296 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1297 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1298 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1299 | |
| 1300 | destroy: |
| 1301 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1302 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1303 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1304 | |
| 1305 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1306 | /* |
| 1307 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1308 | * thus reset them as required. |
| 1309 | */ |
| 1310 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1311 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1312 | mbedtls_free( exported ); |
| 1313 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1314 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1315 | } |
| 1316 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1317 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1318 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1319 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1320 | int type_arg, |
| 1321 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1322 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1323 | int export_size_delta, |
| 1324 | int expected_export_status_arg, |
| 1325 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1326 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1327 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1328 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1329 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1330 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1331 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1332 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1333 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1334 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1335 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1336 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1337 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1338 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1339 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1340 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1341 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1342 | psa_set_key_algorithm( &attributes, alg ); |
| 1343 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1344 | |
| 1345 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1346 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1347 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1348 | /* Export the public key */ |
| 1349 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1350 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1351 | exported, export_size, |
| 1352 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1353 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1354 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1355 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1356 | 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] | 1357 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1358 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1359 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1360 | TEST_LE_U( expected_public_key->len, |
| 1361 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1362 | TEST_LE_U( expected_public_key->len, |
| 1363 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1364 | TEST_LE_U( expected_public_key->len, |
| 1365 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1366 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1367 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1368 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1369 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1370 | /* |
| 1371 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1372 | * thus reset them as required. |
| 1373 | */ |
| 1374 | psa_reset_key_attributes( &attributes ); |
| 1375 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1376 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1377 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1378 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1379 | } |
| 1380 | /* END_CASE */ |
| 1381 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1382 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1383 | void import_and_exercise_key( data_t *data, |
| 1384 | int type_arg, |
| 1385 | int bits_arg, |
| 1386 | int alg_arg ) |
| 1387 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1388 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1389 | psa_key_type_t type = type_arg; |
| 1390 | size_t bits = bits_arg; |
| 1391 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1392 | 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] | 1393 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1394 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1395 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1396 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1397 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1398 | psa_set_key_usage_flags( &attributes, usage ); |
| 1399 | psa_set_key_algorithm( &attributes, alg ); |
| 1400 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1401 | |
| 1402 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1403 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1404 | |
| 1405 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1406 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1407 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1408 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1409 | |
| 1410 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1411 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1412 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1413 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1414 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1415 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1416 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1417 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1418 | /* |
| 1419 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1420 | * thus reset them as required. |
| 1421 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1422 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1423 | |
| 1424 | psa_reset_key_attributes( &attributes ); |
| 1425 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1426 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1427 | } |
| 1428 | /* END_CASE */ |
| 1429 | |
| 1430 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1431 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1432 | int bits_arg, int expected_bits_arg, |
| 1433 | int usage_arg, int expected_usage_arg, |
| 1434 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1435 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1436 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1437 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1438 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1439 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1440 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1441 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1442 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1443 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1444 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1445 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1446 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1447 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1448 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1449 | psa_set_key_usage_flags( &attributes, usage ); |
| 1450 | psa_set_key_algorithm( &attributes, alg ); |
| 1451 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1452 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1453 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1454 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1455 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1456 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1457 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1458 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1459 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1460 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1461 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1462 | |
| 1463 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1464 | /* |
| 1465 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1466 | * thus reset them as required. |
| 1467 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1468 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1469 | |
| 1470 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1471 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1472 | } |
| 1473 | /* END_CASE */ |
| 1474 | |
| 1475 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1476 | void check_key_policy( int type_arg, int bits_arg, |
| 1477 | int usage_arg, int alg_arg ) |
| 1478 | { |
| 1479 | 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] | 1480 | usage_arg, |
| 1481 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1482 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1483 | goto exit; |
| 1484 | } |
| 1485 | /* END_CASE */ |
| 1486 | |
| 1487 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1488 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1489 | { |
| 1490 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1491 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1492 | * 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] | 1493 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1494 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1495 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1496 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1497 | |
| 1498 | memset( &zero, 0, sizeof( zero ) ); |
| 1499 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1500 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1501 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1502 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1503 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1504 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1505 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1506 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1507 | |
| 1508 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1509 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1510 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1511 | |
| 1512 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1513 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1514 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1515 | |
| 1516 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1517 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1518 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1519 | } |
| 1520 | /* END_CASE */ |
| 1521 | |
| 1522 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1523 | void mac_key_policy( int policy_usage_arg, |
| 1524 | int policy_alg_arg, |
| 1525 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1526 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1527 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1528 | int expected_status_sign_arg, |
| 1529 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1530 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1531 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1532 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1533 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1534 | psa_key_type_t key_type = key_type_arg; |
| 1535 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1536 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1537 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1538 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1539 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1540 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1541 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1542 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1543 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1544 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1545 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1546 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1547 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1548 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1549 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1550 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1551 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1552 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1553 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1554 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1555 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1556 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1557 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1558 | /* Calculate the MAC, one-shot case. */ |
| 1559 | uint8_t input[128] = {0}; |
| 1560 | size_t mac_len; |
| 1561 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1562 | input, 128, |
| 1563 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1564 | expected_status_sign ); |
| 1565 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1566 | /* Calculate the MAC, multi-part case. */ |
| 1567 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1568 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1569 | if( status == PSA_SUCCESS ) |
| 1570 | { |
| 1571 | status = psa_mac_update( &operation, input, 128 ); |
| 1572 | if( status == PSA_SUCCESS ) |
| 1573 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1574 | &mac_len ), |
| 1575 | expected_status_sign ); |
| 1576 | else |
| 1577 | TEST_EQUAL( status, expected_status_sign ); |
| 1578 | } |
| 1579 | else |
| 1580 | { |
| 1581 | TEST_EQUAL( status, expected_status_sign ); |
| 1582 | } |
| 1583 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1584 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1585 | /* Verify correct MAC, one-shot case. */ |
| 1586 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1587 | mac, mac_len ); |
| 1588 | |
| 1589 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1590 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1591 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1592 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1593 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1594 | /* Verify correct MAC, multi-part case. */ |
| 1595 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1596 | if( status == PSA_SUCCESS ) |
| 1597 | { |
| 1598 | status = psa_mac_update( &operation, input, 128 ); |
| 1599 | if( status == PSA_SUCCESS ) |
| 1600 | { |
| 1601 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1602 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1603 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1604 | else |
| 1605 | TEST_EQUAL( status, expected_status_verify ); |
| 1606 | } |
| 1607 | else |
| 1608 | { |
| 1609 | TEST_EQUAL( status, expected_status_verify ); |
| 1610 | } |
| 1611 | } |
| 1612 | else |
| 1613 | { |
| 1614 | TEST_EQUAL( status, expected_status_verify ); |
| 1615 | } |
| 1616 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1617 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1618 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1619 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1620 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1621 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1622 | |
| 1623 | exit: |
| 1624 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1625 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1626 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1627 | } |
| 1628 | /* END_CASE */ |
| 1629 | |
| 1630 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1631 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1632 | int policy_alg, |
| 1633 | int key_type, |
| 1634 | data_t *key_data, |
| 1635 | int exercise_alg ) |
| 1636 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1637 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1638 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1639 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1640 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1641 | size_t output_buffer_size = 0; |
| 1642 | size_t input_buffer_size = 0; |
| 1643 | size_t output_length = 0; |
| 1644 | uint8_t *output = NULL; |
| 1645 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1646 | psa_status_t status; |
| 1647 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1648 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1649 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1650 | input_buffer_size ); |
| 1651 | |
| 1652 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1653 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1654 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1655 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1656 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1657 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1658 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1659 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1660 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1661 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1662 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1663 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1664 | /* Check if no key usage flag implication is done */ |
| 1665 | TEST_EQUAL( policy_usage, |
| 1666 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1667 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1668 | /* Encrypt check, one-shot */ |
| 1669 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1670 | output, output_buffer_size, |
| 1671 | &output_length); |
| 1672 | if( policy_alg == exercise_alg && |
| 1673 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1674 | PSA_ASSERT( status ); |
| 1675 | else |
| 1676 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1677 | |
| 1678 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1679 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1680 | if( policy_alg == exercise_alg && |
| 1681 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1682 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1683 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1684 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1685 | psa_cipher_abort( &operation ); |
| 1686 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1687 | /* Decrypt check, one-shot */ |
| 1688 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 1689 | input, input_buffer_size, |
| 1690 | &output_length); |
| 1691 | if( policy_alg == exercise_alg && |
| 1692 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1693 | PSA_ASSERT( status ); |
| 1694 | else |
| 1695 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1696 | |
| 1697 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1698 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1699 | if( policy_alg == exercise_alg && |
| 1700 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1701 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1702 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1703 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1704 | |
| 1705 | exit: |
| 1706 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1707 | mbedtls_free( input ); |
| 1708 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1709 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1710 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1711 | } |
| 1712 | /* END_CASE */ |
| 1713 | |
| 1714 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1715 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1716 | int policy_alg, |
| 1717 | int key_type, |
| 1718 | data_t *key_data, |
| 1719 | int nonce_length_arg, |
| 1720 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1721 | int exercise_alg, |
| 1722 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1723 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1724 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1725 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1726 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1727 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1728 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1729 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1730 | unsigned char nonce[16] = {0}; |
| 1731 | size_t nonce_length = nonce_length_arg; |
| 1732 | unsigned char tag[16]; |
| 1733 | size_t tag_length = tag_length_arg; |
| 1734 | size_t output_length; |
| 1735 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1736 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 1737 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1738 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1739 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1740 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1741 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1742 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1743 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1744 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1745 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1746 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1747 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1748 | /* Check if no key usage implication is done */ |
| 1749 | TEST_EQUAL( policy_usage, |
| 1750 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1751 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1752 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1753 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1754 | nonce, nonce_length, |
| 1755 | NULL, 0, |
| 1756 | NULL, 0, |
| 1757 | tag, tag_length, |
| 1758 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1759 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1760 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1761 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1762 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1763 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1764 | /* Encrypt check, multi-part */ |
| 1765 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 1766 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1767 | TEST_EQUAL( status, expected_status ); |
| 1768 | else |
| 1769 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1770 | |
| 1771 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1772 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1773 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1774 | nonce, nonce_length, |
| 1775 | NULL, 0, |
| 1776 | tag, tag_length, |
| 1777 | NULL, 0, |
| 1778 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1779 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1780 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1781 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1782 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1783 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1784 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1785 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1786 | /* Decrypt check, multi-part */ |
| 1787 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 1788 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 1789 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1790 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1791 | else |
| 1792 | TEST_EQUAL( status, expected_status ); |
| 1793 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1794 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1795 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1796 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1797 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1798 | } |
| 1799 | /* END_CASE */ |
| 1800 | |
| 1801 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1802 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1803 | int policy_alg, |
| 1804 | int key_type, |
| 1805 | data_t *key_data, |
| 1806 | int exercise_alg ) |
| 1807 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1808 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1809 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1810 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1811 | psa_status_t status; |
| 1812 | size_t key_bits; |
| 1813 | size_t buffer_length; |
| 1814 | unsigned char *buffer = NULL; |
| 1815 | size_t output_length; |
| 1816 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1817 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1818 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1819 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1820 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1821 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1822 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1823 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1824 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1825 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1826 | /* Check if no key usage implication is done */ |
| 1827 | TEST_EQUAL( policy_usage, |
| 1828 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1829 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1830 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1831 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1832 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1833 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1834 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1835 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1836 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1837 | NULL, 0, |
| 1838 | NULL, 0, |
| 1839 | buffer, buffer_length, |
| 1840 | &output_length ); |
| 1841 | if( policy_alg == exercise_alg && |
| 1842 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1843 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1844 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1845 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1846 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1847 | if( buffer_length != 0 ) |
| 1848 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1849 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1850 | buffer, buffer_length, |
| 1851 | NULL, 0, |
| 1852 | buffer, buffer_length, |
| 1853 | &output_length ); |
| 1854 | if( policy_alg == exercise_alg && |
| 1855 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1856 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1857 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1858 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1859 | |
| 1860 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1861 | /* |
| 1862 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1863 | * thus reset them as required. |
| 1864 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1865 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1866 | |
| 1867 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1868 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1869 | mbedtls_free( buffer ); |
| 1870 | } |
| 1871 | /* END_CASE */ |
| 1872 | |
| 1873 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1874 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1875 | int policy_alg, |
| 1876 | int key_type, |
| 1877 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1878 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1879 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1880 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1881 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1882 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1883 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1884 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 1885 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1886 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1887 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1888 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1889 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1890 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1891 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1892 | int compatible_alg = payload_length_arg > 0; |
| 1893 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1894 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1895 | size_t signature_length; |
| 1896 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1897 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1898 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1899 | TEST_EQUAL( expected_usage, |
| 1900 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1901 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1902 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1903 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1904 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1905 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1906 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1907 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1908 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1909 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1910 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1911 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1912 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1913 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1914 | payload, payload_length, |
| 1915 | signature, sizeof( signature ), |
| 1916 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1917 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1918 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1919 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1920 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1921 | |
| 1922 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1923 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1924 | payload, payload_length, |
| 1925 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1926 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1927 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1928 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1929 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1930 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 1931 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 1932 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1933 | { |
| 1934 | status = psa_sign_message( key, exercise_alg, |
| 1935 | payload, payload_length, |
| 1936 | signature, sizeof( signature ), |
| 1937 | &signature_length ); |
| 1938 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 1939 | PSA_ASSERT( status ); |
| 1940 | else |
| 1941 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1942 | |
| 1943 | memset( signature, 0, sizeof( signature ) ); |
| 1944 | status = psa_verify_message( key, exercise_alg, |
| 1945 | payload, payload_length, |
| 1946 | signature, sizeof( signature ) ); |
| 1947 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 1948 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1949 | else |
| 1950 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1951 | } |
| 1952 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1953 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1954 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1955 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1956 | } |
| 1957 | /* END_CASE */ |
| 1958 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1959 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1960 | void derive_key_policy( int policy_usage, |
| 1961 | int policy_alg, |
| 1962 | int key_type, |
| 1963 | data_t *key_data, |
| 1964 | int exercise_alg ) |
| 1965 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1966 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1967 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1968 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1969 | psa_status_t status; |
| 1970 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1971 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1972 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1973 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1974 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1975 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1976 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1977 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1978 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1979 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1980 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1981 | |
| 1982 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1983 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1984 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1985 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1986 | &operation, |
| 1987 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1988 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1989 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1990 | |
| 1991 | status = psa_key_derivation_input_key( &operation, |
| 1992 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1993 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1994 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1995 | if( policy_alg == exercise_alg && |
| 1996 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1997 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1998 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1999 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2000 | |
| 2001 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2002 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2003 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2004 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2005 | } |
| 2006 | /* END_CASE */ |
| 2007 | |
| 2008 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2009 | void agreement_key_policy( int policy_usage, |
| 2010 | int policy_alg, |
| 2011 | int key_type_arg, |
| 2012 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2013 | int exercise_alg, |
| 2014 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2015 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2016 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2017 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2018 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2019 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2020 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2021 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2022 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2023 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2024 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2025 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2026 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2027 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2028 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2029 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2030 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2031 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2032 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2033 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2034 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2035 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2036 | |
| 2037 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2038 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2039 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2040 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2041 | } |
| 2042 | /* END_CASE */ |
| 2043 | |
| 2044 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2045 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2046 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2047 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2048 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2049 | psa_key_type_t key_type = key_type_arg; |
| 2050 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2051 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2052 | psa_key_usage_t usage = usage_arg; |
| 2053 | psa_algorithm_t alg = alg_arg; |
| 2054 | psa_algorithm_t alg2 = alg2_arg; |
| 2055 | |
| 2056 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2057 | |
| 2058 | psa_set_key_usage_flags( &attributes, usage ); |
| 2059 | psa_set_key_algorithm( &attributes, alg ); |
| 2060 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2061 | psa_set_key_type( &attributes, key_type ); |
| 2062 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2063 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2064 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2065 | /* Update the usage flags to obtain implicit usage flags */ |
| 2066 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2067 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2068 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2069 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2070 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2071 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2072 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2073 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2074 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2075 | goto exit; |
| 2076 | |
| 2077 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2078 | /* |
| 2079 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2080 | * thus reset them as required. |
| 2081 | */ |
| 2082 | psa_reset_key_attributes( &got_attributes ); |
| 2083 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2084 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2085 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2086 | } |
| 2087 | /* END_CASE */ |
| 2088 | |
| 2089 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2090 | void raw_agreement_key_policy( int policy_usage, |
| 2091 | int policy_alg, |
| 2092 | int key_type_arg, |
| 2093 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2094 | int exercise_alg, |
| 2095 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2096 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2097 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2098 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2099 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2100 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2101 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2102 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2103 | |
| 2104 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2105 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2106 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2107 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2108 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2109 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2110 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2111 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2112 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2113 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2114 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2115 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2116 | |
| 2117 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2118 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2119 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2120 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2121 | } |
| 2122 | /* END_CASE */ |
| 2123 | |
| 2124 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2125 | void copy_success( int source_usage_arg, |
| 2126 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2127 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2128 | int type_arg, data_t *material, |
| 2129 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2130 | int target_usage_arg, |
| 2131 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2132 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2133 | int expected_usage_arg, |
| 2134 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2135 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2136 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2137 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2138 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2139 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2140 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2141 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2142 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2143 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2144 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2145 | uint8_t *export_buffer = NULL; |
| 2146 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2147 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2148 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2149 | /* Prepare the source key. */ |
| 2150 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2151 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2152 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2153 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2154 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2155 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2156 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2157 | &source_key ) ); |
| 2158 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2159 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2160 | /* Prepare the target attributes. */ |
| 2161 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2162 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2163 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2164 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2165 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2166 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2167 | if( target_usage_arg != -1 ) |
| 2168 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2169 | if( target_alg_arg != -1 ) |
| 2170 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2171 | if( target_alg2_arg != -1 ) |
| 2172 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2173 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2174 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2175 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2176 | PSA_ASSERT( psa_copy_key( source_key, |
| 2177 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2178 | |
| 2179 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2180 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2181 | |
| 2182 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2183 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2184 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2185 | psa_get_key_type( &target_attributes ) ); |
| 2186 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2187 | psa_get_key_bits( &target_attributes ) ); |
| 2188 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2189 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2190 | TEST_EQUAL( expected_alg2, |
| 2191 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2192 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2193 | { |
| 2194 | size_t length; |
| 2195 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2196 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2197 | material->len, &length ) ); |
| 2198 | ASSERT_COMPARE( material->x, material->len, |
| 2199 | export_buffer, length ); |
| 2200 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2201 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2202 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2203 | { |
| 2204 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2205 | goto exit; |
| 2206 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2207 | goto exit; |
| 2208 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2209 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2210 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2211 | |
| 2212 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2213 | /* |
| 2214 | * Source and target key attributes may have been returned by |
| 2215 | * psa_get_key_attributes() thus reset them as required. |
| 2216 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2217 | psa_reset_key_attributes( &source_attributes ); |
| 2218 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2219 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2220 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2221 | mbedtls_free( export_buffer ); |
| 2222 | } |
| 2223 | /* END_CASE */ |
| 2224 | |
| 2225 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2226 | void copy_fail( int source_usage_arg, |
| 2227 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2228 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2229 | int type_arg, data_t *material, |
| 2230 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2231 | int target_usage_arg, |
| 2232 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2233 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2234 | int expected_status_arg ) |
| 2235 | { |
| 2236 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2237 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2238 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2239 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2240 | 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] | 2241 | |
| 2242 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2243 | |
| 2244 | /* Prepare the source key. */ |
| 2245 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2246 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2247 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2248 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2249 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2250 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2251 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2252 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2253 | |
| 2254 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2255 | psa_set_key_id( &target_attributes, key_id ); |
| 2256 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2257 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2258 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2259 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2260 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2261 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2262 | |
| 2263 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2264 | TEST_EQUAL( psa_copy_key( source_key, |
| 2265 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2266 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2267 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2268 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2269 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2270 | exit: |
| 2271 | psa_reset_key_attributes( &source_attributes ); |
| 2272 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2273 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2274 | } |
| 2275 | /* END_CASE */ |
| 2276 | |
| 2277 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2278 | void hash_operation_init( ) |
| 2279 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2280 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2281 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2282 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2283 | * 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] | 2284 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2285 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2286 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2287 | psa_hash_operation_t zero; |
| 2288 | |
| 2289 | memset( &zero, 0, sizeof( zero ) ); |
| 2290 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2291 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2292 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2293 | PSA_ERROR_BAD_STATE ); |
| 2294 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2295 | PSA_ERROR_BAD_STATE ); |
| 2296 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2297 | PSA_ERROR_BAD_STATE ); |
| 2298 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2299 | /* A default hash operation should be abortable without error. */ |
| 2300 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2301 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2302 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2303 | } |
| 2304 | /* END_CASE */ |
| 2305 | |
| 2306 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2307 | void hash_setup( int alg_arg, |
| 2308 | int expected_status_arg ) |
| 2309 | { |
| 2310 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2311 | uint8_t *output = NULL; |
| 2312 | size_t output_size = 0; |
| 2313 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2314 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2315 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2316 | psa_status_t status; |
| 2317 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2318 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2319 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2320 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2321 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2322 | ASSERT_ALLOC( output, output_size ); |
| 2323 | |
| 2324 | status = psa_hash_compute( alg, NULL, 0, |
| 2325 | output, output_size, &output_length ); |
| 2326 | TEST_EQUAL( status, expected_status ); |
| 2327 | |
| 2328 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2329 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2330 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2331 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2332 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2333 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2334 | |
| 2335 | /* If setup failed, reproduce the failure, so as to |
| 2336 | * test the resulting state of the operation object. */ |
| 2337 | if( status != PSA_SUCCESS ) |
| 2338 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2339 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2340 | /* Now the operation object should be reusable. */ |
| 2341 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2342 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2343 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2344 | #endif |
| 2345 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2346 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2347 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2348 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2349 | } |
| 2350 | /* END_CASE */ |
| 2351 | |
| 2352 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2353 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2354 | int output_size_arg, int expected_status_arg ) |
| 2355 | { |
| 2356 | psa_algorithm_t alg = alg_arg; |
| 2357 | uint8_t *output = NULL; |
| 2358 | size_t output_size = output_size_arg; |
| 2359 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2360 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2361 | psa_status_t expected_status = expected_status_arg; |
| 2362 | psa_status_t status; |
| 2363 | |
| 2364 | ASSERT_ALLOC( output, output_size ); |
| 2365 | |
| 2366 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2367 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2368 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2369 | status = psa_hash_compute( alg, input->x, input->len, |
| 2370 | output, output_size, &output_length ); |
| 2371 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2372 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2373 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2374 | /* Hash Compute, multi-part */ |
| 2375 | status = psa_hash_setup( &operation, alg ); |
| 2376 | if( status == PSA_SUCCESS ) |
| 2377 | { |
| 2378 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2379 | if( status == PSA_SUCCESS ) |
| 2380 | { |
| 2381 | status = psa_hash_finish( &operation, output, output_size, |
| 2382 | &output_length ); |
| 2383 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2384 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2385 | else |
| 2386 | TEST_EQUAL( status, expected_status ); |
| 2387 | } |
| 2388 | else |
| 2389 | { |
| 2390 | TEST_EQUAL( status, expected_status ); |
| 2391 | } |
| 2392 | } |
| 2393 | else |
| 2394 | { |
| 2395 | TEST_EQUAL( status, expected_status ); |
| 2396 | } |
| 2397 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2398 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2399 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2400 | mbedtls_free( output ); |
| 2401 | PSA_DONE( ); |
| 2402 | } |
| 2403 | /* END_CASE */ |
| 2404 | |
| 2405 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2406 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2407 | data_t *reference_hash, |
| 2408 | int expected_status_arg ) |
| 2409 | { |
| 2410 | psa_algorithm_t alg = alg_arg; |
| 2411 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2412 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2413 | psa_status_t status; |
| 2414 | |
| 2415 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2416 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2417 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2418 | status = psa_hash_compare( alg, input->x, input->len, |
| 2419 | reference_hash->x, reference_hash->len ); |
| 2420 | TEST_EQUAL( status, expected_status ); |
| 2421 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2422 | /* Hash Compare, multi-part */ |
| 2423 | status = psa_hash_setup( &operation, alg ); |
| 2424 | if( status == PSA_SUCCESS ) |
| 2425 | { |
| 2426 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2427 | if( status == PSA_SUCCESS ) |
| 2428 | { |
| 2429 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2430 | reference_hash->len ); |
| 2431 | TEST_EQUAL( status, expected_status ); |
| 2432 | } |
| 2433 | else |
| 2434 | { |
| 2435 | TEST_EQUAL( status, expected_status ); |
| 2436 | } |
| 2437 | } |
| 2438 | else |
| 2439 | { |
| 2440 | TEST_EQUAL( status, expected_status ); |
| 2441 | } |
| 2442 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2443 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2444 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2445 | PSA_DONE( ); |
| 2446 | } |
| 2447 | /* END_CASE */ |
| 2448 | |
| 2449 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2450 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2451 | data_t *expected_output ) |
| 2452 | { |
| 2453 | psa_algorithm_t alg = alg_arg; |
| 2454 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2455 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2456 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2457 | size_t i; |
| 2458 | |
| 2459 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2460 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2461 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2462 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2463 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2464 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2465 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2466 | ASSERT_COMPARE( output, output_length, |
| 2467 | expected_output->x, expected_output->len ); |
| 2468 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2469 | /* Compute with tight buffer, multi-part */ |
| 2470 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2471 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2472 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2473 | PSA_HASH_LENGTH( alg ), |
| 2474 | &output_length ) ); |
| 2475 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2476 | ASSERT_COMPARE( output, output_length, |
| 2477 | expected_output->x, expected_output->len ); |
| 2478 | |
| 2479 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2480 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2481 | output, sizeof( output ), |
| 2482 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2483 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2484 | ASSERT_COMPARE( output, output_length, |
| 2485 | expected_output->x, expected_output->len ); |
| 2486 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2487 | /* Compute with larger buffer, multi-part */ |
| 2488 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2489 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2490 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2491 | sizeof( output ), &output_length ) ); |
| 2492 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2493 | ASSERT_COMPARE( output, output_length, |
| 2494 | expected_output->x, expected_output->len ); |
| 2495 | |
| 2496 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2497 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2498 | output, output_length ) ); |
| 2499 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2500 | /* Compare with correct hash, multi-part */ |
| 2501 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2502 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2503 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2504 | output_length ) ); |
| 2505 | |
| 2506 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2507 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2508 | output, output_length + 1 ), |
| 2509 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2510 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2511 | /* Compare with trailing garbage, multi-part */ |
| 2512 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2513 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2514 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2515 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2516 | |
| 2517 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2518 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2519 | output, output_length - 1 ), |
| 2520 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2521 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2522 | /* Compare with truncated hash, multi-part */ |
| 2523 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2524 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2525 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2526 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2527 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2528 | /* Compare with corrupted value */ |
| 2529 | for( i = 0; i < output_length; i++ ) |
| 2530 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2531 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2532 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2533 | |
| 2534 | /* One-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2535 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2536 | output, output_length ), |
| 2537 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2538 | |
| 2539 | /* Multi-Part */ |
| 2540 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2541 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2542 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length ), |
| 2543 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2544 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2545 | output[i] ^= 1; |
| 2546 | } |
| 2547 | |
| 2548 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2549 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2550 | PSA_DONE( ); |
| 2551 | } |
| 2552 | /* END_CASE */ |
| 2553 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2554 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2555 | void hash_bad_order( ) |
| 2556 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2557 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2558 | unsigned char input[] = ""; |
| 2559 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2560 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2561 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2562 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2563 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2564 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2565 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2566 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2567 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2568 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2569 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2570 | /* Call setup twice in a row. */ |
| 2571 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2572 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2573 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2574 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2575 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2576 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2577 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2578 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2579 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2580 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2581 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2582 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2583 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2584 | /* Check that update calls abort on error. */ |
| 2585 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2586 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2587 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2588 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2589 | PSA_ERROR_BAD_STATE ); |
| 2590 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2591 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2592 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2593 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2594 | /* Call update after finish. */ |
| 2595 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2596 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2597 | hash, sizeof( hash ), &hash_len ) ); |
| 2598 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2599 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2600 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2601 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2602 | /* Call verify without calling setup beforehand. */ |
| 2603 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2604 | valid_hash, sizeof( valid_hash ) ), |
| 2605 | PSA_ERROR_BAD_STATE ); |
| 2606 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2607 | |
| 2608 | /* Call verify after finish. */ |
| 2609 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2610 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2611 | hash, sizeof( hash ), &hash_len ) ); |
| 2612 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2613 | valid_hash, sizeof( valid_hash ) ), |
| 2614 | PSA_ERROR_BAD_STATE ); |
| 2615 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2616 | |
| 2617 | /* Call verify twice in a row. */ |
| 2618 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2619 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2620 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2621 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2622 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2623 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2624 | valid_hash, sizeof( valid_hash ) ), |
| 2625 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2626 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2627 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2628 | |
| 2629 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2630 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2631 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2632 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2633 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2634 | |
| 2635 | /* Call finish twice in a row. */ |
| 2636 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2637 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2638 | hash, sizeof( hash ), &hash_len ) ); |
| 2639 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2640 | hash, sizeof( hash ), &hash_len ), |
| 2641 | PSA_ERROR_BAD_STATE ); |
| 2642 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2643 | |
| 2644 | /* Call finish after calling verify. */ |
| 2645 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2646 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2647 | valid_hash, sizeof( valid_hash ) ) ); |
| 2648 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2649 | hash, sizeof( hash ), &hash_len ), |
| 2650 | PSA_ERROR_BAD_STATE ); |
| 2651 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2652 | |
| 2653 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2654 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2655 | } |
| 2656 | /* END_CASE */ |
| 2657 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2658 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2659 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2660 | { |
| 2661 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2662 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2663 | * appended to it */ |
| 2664 | unsigned char hash[] = { |
| 2665 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2666 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2667 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2668 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2669 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2670 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2671 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2672 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2673 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2674 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2675 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2676 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2677 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2678 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2679 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2680 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2681 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2682 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2683 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2684 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2685 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2686 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2687 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2688 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2689 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2690 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2691 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2692 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2693 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2694 | } |
| 2695 | /* END_CASE */ |
| 2696 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2697 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2698 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2699 | { |
| 2700 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2701 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2702 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2703 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2704 | size_t hash_len; |
| 2705 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2706 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2707 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2708 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2709 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2710 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2711 | hash, expected_size - 1, &hash_len ), |
| 2712 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2713 | |
| 2714 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2715 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2716 | } |
| 2717 | /* END_CASE */ |
| 2718 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2719 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2720 | void hash_clone_source_state( ) |
| 2721 | { |
| 2722 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2723 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2724 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2725 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2726 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2727 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2728 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2729 | size_t hash_len; |
| 2730 | |
| 2731 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2732 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2733 | |
| 2734 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2735 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2736 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2737 | hash, sizeof( hash ), &hash_len ) ); |
| 2738 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2739 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2740 | |
| 2741 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2742 | PSA_ERROR_BAD_STATE ); |
| 2743 | |
| 2744 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2745 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2746 | hash, sizeof( hash ), &hash_len ) ); |
| 2747 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2748 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2749 | hash, sizeof( hash ), &hash_len ) ); |
| 2750 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2751 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2752 | hash, sizeof( hash ), &hash_len ) ); |
| 2753 | |
| 2754 | exit: |
| 2755 | psa_hash_abort( &op_source ); |
| 2756 | psa_hash_abort( &op_init ); |
| 2757 | psa_hash_abort( &op_setup ); |
| 2758 | psa_hash_abort( &op_finished ); |
| 2759 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2760 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2761 | } |
| 2762 | /* END_CASE */ |
| 2763 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2764 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2765 | void hash_clone_target_state( ) |
| 2766 | { |
| 2767 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2768 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2769 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2770 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2771 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2772 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2773 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2774 | size_t hash_len; |
| 2775 | |
| 2776 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2777 | |
| 2778 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2779 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2780 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2781 | hash, sizeof( hash ), &hash_len ) ); |
| 2782 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2783 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2784 | |
| 2785 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2786 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2787 | hash, sizeof( hash ), &hash_len ) ); |
| 2788 | |
| 2789 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2790 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2791 | PSA_ERROR_BAD_STATE ); |
| 2792 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2793 | PSA_ERROR_BAD_STATE ); |
| 2794 | |
| 2795 | exit: |
| 2796 | psa_hash_abort( &op_target ); |
| 2797 | psa_hash_abort( &op_init ); |
| 2798 | psa_hash_abort( &op_setup ); |
| 2799 | psa_hash_abort( &op_finished ); |
| 2800 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2801 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2802 | } |
| 2803 | /* END_CASE */ |
| 2804 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2805 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2806 | void mac_operation_init( ) |
| 2807 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2808 | const uint8_t input[1] = { 0 }; |
| 2809 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2810 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2811 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2812 | * 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] | 2813 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2814 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2815 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2816 | psa_mac_operation_t zero; |
| 2817 | |
| 2818 | memset( &zero, 0, sizeof( zero ) ); |
| 2819 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2820 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2821 | TEST_EQUAL( psa_mac_update( &func, |
| 2822 | input, sizeof( input ) ), |
| 2823 | PSA_ERROR_BAD_STATE ); |
| 2824 | TEST_EQUAL( psa_mac_update( &init, |
| 2825 | input, sizeof( input ) ), |
| 2826 | PSA_ERROR_BAD_STATE ); |
| 2827 | TEST_EQUAL( psa_mac_update( &zero, |
| 2828 | input, sizeof( input ) ), |
| 2829 | PSA_ERROR_BAD_STATE ); |
| 2830 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2831 | /* A default MAC operation should be abortable without error. */ |
| 2832 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2833 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2834 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2835 | } |
| 2836 | /* END_CASE */ |
| 2837 | |
| 2838 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2839 | void mac_setup( int key_type_arg, |
| 2840 | data_t *key, |
| 2841 | int alg_arg, |
| 2842 | int expected_status_arg ) |
| 2843 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2844 | psa_key_type_t key_type = key_type_arg; |
| 2845 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2846 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2847 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2848 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2849 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2850 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2851 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2852 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2853 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2854 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2855 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2856 | &operation, &status ) ) |
| 2857 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2858 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2859 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2860 | /* The operation object should be reusable. */ |
| 2861 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2862 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2863 | smoke_test_key_data, |
| 2864 | sizeof( smoke_test_key_data ), |
| 2865 | KNOWN_SUPPORTED_MAC_ALG, |
| 2866 | &operation, &status ) ) |
| 2867 | goto exit; |
| 2868 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2869 | #endif |
| 2870 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2871 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2872 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2873 | } |
| 2874 | /* END_CASE */ |
| 2875 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2876 | /* 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] | 2877 | void mac_bad_order( ) |
| 2878 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2879 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2880 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2881 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2882 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2883 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2884 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2885 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2886 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2887 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2888 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2889 | size_t sign_mac_length = 0; |
| 2890 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2891 | const uint8_t verify_mac[] = { |
| 2892 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2893 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2894 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2895 | |
| 2896 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2897 | 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] | 2898 | psa_set_key_algorithm( &attributes, alg ); |
| 2899 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2900 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2901 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2902 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2903 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2904 | /* Call update without calling setup beforehand. */ |
| 2905 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2906 | PSA_ERROR_BAD_STATE ); |
| 2907 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2908 | |
| 2909 | /* Call sign finish without calling setup beforehand. */ |
| 2910 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2911 | &sign_mac_length), |
| 2912 | PSA_ERROR_BAD_STATE ); |
| 2913 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2914 | |
| 2915 | /* Call verify finish without calling setup beforehand. */ |
| 2916 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2917 | verify_mac, sizeof( verify_mac ) ), |
| 2918 | PSA_ERROR_BAD_STATE ); |
| 2919 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2920 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2921 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2922 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2923 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2924 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2925 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2926 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2927 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2928 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2929 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2930 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2931 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2932 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2933 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2934 | sign_mac, sizeof( sign_mac ), |
| 2935 | &sign_mac_length ) ); |
| 2936 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2937 | PSA_ERROR_BAD_STATE ); |
| 2938 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2939 | |
| 2940 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2941 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2942 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2943 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2944 | verify_mac, sizeof( verify_mac ) ) ); |
| 2945 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2946 | PSA_ERROR_BAD_STATE ); |
| 2947 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2948 | |
| 2949 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2950 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2951 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2952 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2953 | sign_mac, sizeof( sign_mac ), |
| 2954 | &sign_mac_length ) ); |
| 2955 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2956 | sign_mac, sizeof( sign_mac ), |
| 2957 | &sign_mac_length ), |
| 2958 | PSA_ERROR_BAD_STATE ); |
| 2959 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2960 | |
| 2961 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2962 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2963 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2964 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2965 | verify_mac, sizeof( verify_mac ) ) ); |
| 2966 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2967 | verify_mac, sizeof( verify_mac ) ), |
| 2968 | PSA_ERROR_BAD_STATE ); |
| 2969 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2970 | |
| 2971 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2972 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2973 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2974 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2975 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2976 | verify_mac, sizeof( verify_mac ) ), |
| 2977 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2978 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2979 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2980 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2981 | |
| 2982 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2983 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2984 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2985 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2986 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2987 | sign_mac, sizeof( sign_mac ), |
| 2988 | &sign_mac_length ), |
| 2989 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2990 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2991 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2992 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2993 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2994 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2995 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2996 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2997 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2998 | } |
| 2999 | /* END_CASE */ |
| 3000 | |
| 3001 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3002 | void mac_sign_verify_multi( int key_type_arg, |
| 3003 | data_t *key_data, |
| 3004 | int alg_arg, |
| 3005 | data_t *input, |
| 3006 | int is_verify, |
| 3007 | data_t *expected_mac ) |
| 3008 | { |
| 3009 | size_t data_part_len = 0; |
| 3010 | |
| 3011 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3012 | { |
| 3013 | /* Split data into length(data_part_len) parts. */ |
| 3014 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3015 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3016 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3017 | alg_arg, |
| 3018 | input, data_part_len, |
| 3019 | expected_mac, |
| 3020 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3021 | break; |
| 3022 | |
| 3023 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3024 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3025 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3026 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3027 | alg_arg, |
| 3028 | input, data_part_len, |
| 3029 | expected_mac, |
| 3030 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3031 | break; |
| 3032 | } |
| 3033 | |
| 3034 | /* Goto is required to silence warnings about unused labels, as we |
| 3035 | * don't actually do any test assertions in this function. */ |
| 3036 | goto exit; |
| 3037 | } |
| 3038 | /* END_CASE */ |
| 3039 | |
| 3040 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3041 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3042 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3043 | int alg_arg, |
| 3044 | data_t *input, |
| 3045 | data_t *expected_mac ) |
| 3046 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3047 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3048 | psa_key_type_t key_type = key_type_arg; |
| 3049 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3050 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3051 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3052 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3053 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3054 | 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] | 3055 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3056 | const size_t output_sizes_to_test[] = { |
| 3057 | 0, |
| 3058 | 1, |
| 3059 | expected_mac->len - 1, |
| 3060 | expected_mac->len, |
| 3061 | expected_mac->len + 1, |
| 3062 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3063 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3064 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3065 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3066 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3067 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3068 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3069 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3070 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3071 | psa_set_key_algorithm( &attributes, alg ); |
| 3072 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3073 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3074 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3075 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3076 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3077 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3078 | { |
| 3079 | const size_t output_size = output_sizes_to_test[i]; |
| 3080 | psa_status_t expected_status = |
| 3081 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3082 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3083 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3084 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3085 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3086 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3087 | /* Calculate the MAC, one-shot case. */ |
| 3088 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3089 | input->x, input->len, |
| 3090 | actual_mac, output_size, &mac_length ), |
| 3091 | expected_status ); |
| 3092 | if( expected_status == PSA_SUCCESS ) |
| 3093 | { |
| 3094 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3095 | actual_mac, mac_length ); |
| 3096 | } |
| 3097 | |
| 3098 | if( output_size > 0 ) |
| 3099 | memset( actual_mac, 0, output_size ); |
| 3100 | |
| 3101 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3102 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3103 | PSA_ASSERT( psa_mac_update( &operation, |
| 3104 | input->x, input->len ) ); |
| 3105 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3106 | actual_mac, output_size, |
| 3107 | &mac_length ), |
| 3108 | expected_status ); |
| 3109 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3110 | |
| 3111 | if( expected_status == PSA_SUCCESS ) |
| 3112 | { |
| 3113 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3114 | actual_mac, mac_length ); |
| 3115 | } |
| 3116 | mbedtls_free( actual_mac ); |
| 3117 | actual_mac = NULL; |
| 3118 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3119 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3120 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3121 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3122 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3123 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3124 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3125 | } |
| 3126 | /* END_CASE */ |
| 3127 | |
| 3128 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3129 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3130 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3131 | int alg_arg, |
| 3132 | data_t *input, |
| 3133 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3134 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3135 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3136 | psa_key_type_t key_type = key_type_arg; |
| 3137 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3138 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3139 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3140 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3141 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3142 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3143 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3144 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3145 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3146 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3147 | psa_set_key_algorithm( &attributes, alg ); |
| 3148 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3149 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3150 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3151 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3152 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3153 | /* Verify correct MAC, one-shot case. */ |
| 3154 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3155 | expected_mac->x, expected_mac->len ) ); |
| 3156 | |
| 3157 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3158 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3159 | PSA_ASSERT( psa_mac_update( &operation, |
| 3160 | input->x, input->len ) ); |
| 3161 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3162 | expected_mac->x, |
| 3163 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3164 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3165 | /* Test a MAC that's too short, one-shot case. */ |
| 3166 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3167 | input->x, input->len, |
| 3168 | expected_mac->x, |
| 3169 | expected_mac->len - 1 ), |
| 3170 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3171 | |
| 3172 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3173 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3174 | PSA_ASSERT( psa_mac_update( &operation, |
| 3175 | input->x, input->len ) ); |
| 3176 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3177 | expected_mac->x, |
| 3178 | expected_mac->len - 1 ), |
| 3179 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3180 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3181 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3182 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3183 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3184 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3185 | input->x, input->len, |
| 3186 | perturbed_mac, expected_mac->len + 1 ), |
| 3187 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3188 | |
| 3189 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3190 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3191 | PSA_ASSERT( psa_mac_update( &operation, |
| 3192 | input->x, input->len ) ); |
| 3193 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3194 | perturbed_mac, |
| 3195 | expected_mac->len + 1 ), |
| 3196 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3197 | |
| 3198 | /* Test changing one byte. */ |
| 3199 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3200 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3201 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3202 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3203 | |
| 3204 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3205 | input->x, input->len, |
| 3206 | perturbed_mac, expected_mac->len ), |
| 3207 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3208 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3209 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3210 | PSA_ASSERT( psa_mac_update( &operation, |
| 3211 | input->x, input->len ) ); |
| 3212 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3213 | perturbed_mac, |
| 3214 | expected_mac->len ), |
| 3215 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3216 | perturbed_mac[i] ^= 1; |
| 3217 | } |
| 3218 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3219 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3220 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3221 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3222 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3223 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3224 | } |
| 3225 | /* END_CASE */ |
| 3226 | |
| 3227 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3228 | void cipher_operation_init( ) |
| 3229 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3230 | const uint8_t input[1] = { 0 }; |
| 3231 | unsigned char output[1] = { 0 }; |
| 3232 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3233 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3234 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3235 | * 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] | 3236 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3237 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3238 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3239 | psa_cipher_operation_t zero; |
| 3240 | |
| 3241 | memset( &zero, 0, sizeof( zero ) ); |
| 3242 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3243 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3244 | TEST_EQUAL( psa_cipher_update( &func, |
| 3245 | input, sizeof( input ), |
| 3246 | output, sizeof( output ), |
| 3247 | &output_length ), |
| 3248 | PSA_ERROR_BAD_STATE ); |
| 3249 | TEST_EQUAL( psa_cipher_update( &init, |
| 3250 | input, sizeof( input ), |
| 3251 | output, sizeof( output ), |
| 3252 | &output_length ), |
| 3253 | PSA_ERROR_BAD_STATE ); |
| 3254 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3255 | input, sizeof( input ), |
| 3256 | output, sizeof( output ), |
| 3257 | &output_length ), |
| 3258 | PSA_ERROR_BAD_STATE ); |
| 3259 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3260 | /* A default cipher operation should be abortable without error. */ |
| 3261 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3262 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3263 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3264 | } |
| 3265 | /* END_CASE */ |
| 3266 | |
| 3267 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3268 | void cipher_setup( int key_type_arg, |
| 3269 | data_t *key, |
| 3270 | int alg_arg, |
| 3271 | int expected_status_arg ) |
| 3272 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3273 | psa_key_type_t key_type = key_type_arg; |
| 3274 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3275 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3276 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3277 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3278 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3279 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3280 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3281 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3282 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3283 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3284 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3285 | &operation, &status ) ) |
| 3286 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3287 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3288 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3289 | /* The operation object should be reusable. */ |
| 3290 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3291 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3292 | smoke_test_key_data, |
| 3293 | sizeof( smoke_test_key_data ), |
| 3294 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3295 | &operation, &status ) ) |
| 3296 | goto exit; |
| 3297 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3298 | #endif |
| 3299 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3300 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3301 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3302 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3303 | } |
| 3304 | /* END_CASE */ |
| 3305 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3306 | /* 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] | 3307 | void cipher_bad_order( ) |
| 3308 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3309 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3310 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3311 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3312 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3313 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3314 | 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] | 3315 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3316 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3317 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3318 | const uint8_t text[] = { |
| 3319 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3320 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3321 | 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] | 3322 | size_t length = 0; |
| 3323 | |
| 3324 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3325 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3326 | psa_set_key_algorithm( &attributes, alg ); |
| 3327 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3328 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3329 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3330 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3331 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3332 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3333 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3334 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3335 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3336 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3337 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3338 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3339 | |
| 3340 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3341 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3342 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3343 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3344 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3345 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3346 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3347 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3348 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3349 | /* Generate an IV without calling setup beforehand. */ |
| 3350 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3351 | buffer, sizeof( buffer ), |
| 3352 | &length ), |
| 3353 | PSA_ERROR_BAD_STATE ); |
| 3354 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3355 | |
| 3356 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3357 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3358 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3359 | buffer, sizeof( buffer ), |
| 3360 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3361 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3362 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3363 | buffer, sizeof( buffer ), |
| 3364 | &length ), |
| 3365 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3366 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3367 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3368 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3369 | |
| 3370 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3371 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3372 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3373 | iv, sizeof( iv ) ) ); |
| 3374 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3375 | buffer, sizeof( buffer ), |
| 3376 | &length ), |
| 3377 | PSA_ERROR_BAD_STATE ); |
| 3378 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3379 | |
| 3380 | /* Set an IV without calling setup beforehand. */ |
| 3381 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3382 | iv, sizeof( iv ) ), |
| 3383 | PSA_ERROR_BAD_STATE ); |
| 3384 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3385 | |
| 3386 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3387 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3388 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3389 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3390 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3391 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3392 | iv, sizeof( iv ) ), |
| 3393 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3394 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3395 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3396 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3397 | |
| 3398 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3399 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3400 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3401 | buffer, sizeof( buffer ), |
| 3402 | &length ) ); |
| 3403 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3404 | iv, sizeof( iv ) ), |
| 3405 | PSA_ERROR_BAD_STATE ); |
| 3406 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3407 | |
| 3408 | /* Call update without calling setup beforehand. */ |
| 3409 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3410 | text, sizeof( text ), |
| 3411 | buffer, sizeof( buffer ), |
| 3412 | &length ), |
| 3413 | PSA_ERROR_BAD_STATE ); |
| 3414 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3415 | |
| 3416 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3417 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3418 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3419 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3420 | text, sizeof( text ), |
| 3421 | buffer, sizeof( buffer ), |
| 3422 | &length ), |
| 3423 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3424 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3425 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3426 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3427 | |
| 3428 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3429 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3430 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3431 | iv, sizeof( iv ) ) ); |
| 3432 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3433 | buffer, sizeof( buffer ), &length ) ); |
| 3434 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3435 | text, sizeof( text ), |
| 3436 | buffer, sizeof( buffer ), |
| 3437 | &length ), |
| 3438 | PSA_ERROR_BAD_STATE ); |
| 3439 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3440 | |
| 3441 | /* Call finish without calling setup beforehand. */ |
| 3442 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3443 | buffer, sizeof( buffer ), &length ), |
| 3444 | PSA_ERROR_BAD_STATE ); |
| 3445 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3446 | |
| 3447 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3448 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3449 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3450 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3451 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3452 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3453 | buffer, sizeof( buffer ), &length ), |
| 3454 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3455 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3456 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3457 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3458 | |
| 3459 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3460 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3461 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3462 | iv, sizeof( iv ) ) ); |
| 3463 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3464 | buffer, sizeof( buffer ), &length ) ); |
| 3465 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3466 | buffer, sizeof( buffer ), &length ), |
| 3467 | PSA_ERROR_BAD_STATE ); |
| 3468 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3469 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3470 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3471 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3472 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3473 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3474 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3475 | } |
| 3476 | /* END_CASE */ |
| 3477 | |
| 3478 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3479 | void cipher_encrypt_fail( int alg_arg, |
| 3480 | int key_type_arg, |
| 3481 | data_t *key_data, |
| 3482 | data_t *input, |
| 3483 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3484 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3485 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3486 | psa_status_t status; |
| 3487 | psa_key_type_t key_type = key_type_arg; |
| 3488 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3489 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3490 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3491 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3492 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3493 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3494 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3495 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3496 | size_t function_output_length; |
| 3497 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3498 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3499 | |
| 3500 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3501 | { |
| 3502 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3503 | |
| 3504 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3505 | psa_set_key_algorithm( &attributes, alg ); |
| 3506 | psa_set_key_type( &attributes, key_type ); |
| 3507 | |
| 3508 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3509 | input->len ); |
| 3510 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3511 | |
| 3512 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3513 | &key ) ); |
| 3514 | } |
| 3515 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3516 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3517 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3518 | output_buffer_size, &output_length ); |
| 3519 | |
| 3520 | TEST_EQUAL( status, expected_status ); |
| 3521 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3522 | /* Encrypt, multi-part */ |
| 3523 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3524 | if( status == PSA_SUCCESS ) |
| 3525 | { |
| 3526 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3527 | { |
| 3528 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3529 | iv, iv_size, |
| 3530 | &iv_length ) ); |
| 3531 | } |
| 3532 | |
| 3533 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3534 | output, output_buffer_size, |
| 3535 | &function_output_length ); |
| 3536 | if( status == PSA_SUCCESS ) |
| 3537 | { |
| 3538 | output_length += function_output_length; |
| 3539 | |
| 3540 | status = psa_cipher_finish( &operation, output + output_length, |
| 3541 | output_buffer_size - output_length, |
| 3542 | &function_output_length ); |
| 3543 | |
| 3544 | TEST_EQUAL( status, expected_status ); |
| 3545 | } |
| 3546 | else |
| 3547 | { |
| 3548 | TEST_EQUAL( status, expected_status ); |
| 3549 | } |
| 3550 | } |
| 3551 | else |
| 3552 | { |
| 3553 | TEST_EQUAL( status, expected_status ); |
| 3554 | } |
| 3555 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3556 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3557 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3558 | mbedtls_free( output ); |
| 3559 | psa_destroy_key( key ); |
| 3560 | PSA_DONE( ); |
| 3561 | } |
| 3562 | /* END_CASE */ |
| 3563 | |
| 3564 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3565 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3566 | data_t *input, int iv_length, |
| 3567 | int expected_result ) |
| 3568 | { |
| 3569 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3570 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3571 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3572 | size_t output_buffer_size = 0; |
| 3573 | unsigned char *output = NULL; |
| 3574 | |
| 3575 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3576 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3577 | |
| 3578 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3579 | |
| 3580 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3581 | psa_set_key_algorithm( &attributes, alg ); |
| 3582 | psa_set_key_type( &attributes, key_type ); |
| 3583 | |
| 3584 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3585 | &key ) ); |
| 3586 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3587 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3588 | iv_length ) ); |
| 3589 | |
| 3590 | exit: |
| 3591 | psa_cipher_abort( &operation ); |
| 3592 | mbedtls_free( output ); |
| 3593 | psa_destroy_key( key ); |
| 3594 | PSA_DONE( ); |
| 3595 | } |
| 3596 | /* END_CASE */ |
| 3597 | |
| 3598 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3599 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3600 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3601 | { |
| 3602 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3603 | psa_key_type_t key_type = key_type_arg; |
| 3604 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3605 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3606 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3607 | unsigned char *output = NULL; |
| 3608 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3609 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3610 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3611 | |
| 3612 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3613 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3614 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3615 | TEST_LE_U( ciphertext->len, |
| 3616 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3617 | 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] | 3618 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3619 | TEST_LE_U( plaintext->len, |
| 3620 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3621 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3622 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3623 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3624 | |
| 3625 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3626 | psa_set_key_usage_flags( &attributes, |
| 3627 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3628 | psa_set_key_algorithm( &attributes, alg ); |
| 3629 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3630 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3631 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3632 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3633 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3634 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3635 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3636 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3637 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3638 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3639 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3640 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3641 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3642 | PSA_ERROR_BAD_STATE ); |
| 3643 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3644 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3645 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3646 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3647 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3648 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3649 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3650 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3651 | &length ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3652 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3653 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3654 | /* Multipart encryption */ |
| 3655 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3656 | output_length = 0; |
| 3657 | length = ~0; |
| 3658 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3659 | plaintext->x, plaintext->len, |
| 3660 | output, output_buffer_size, |
| 3661 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3662 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3663 | output_length += length; |
| 3664 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3665 | output + output_length, |
| 3666 | output_buffer_size - output_length, |
| 3667 | &length ) ); |
| 3668 | output_length += length; |
| 3669 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3670 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3671 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3672 | /* Multipart encryption */ |
| 3673 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3674 | output_length = 0; |
| 3675 | length = ~0; |
| 3676 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3677 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3678 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3679 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3680 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3681 | output_length += length; |
| 3682 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3683 | output + output_length, |
| 3684 | output_buffer_size - output_length, |
| 3685 | &length ) ); |
| 3686 | output_length += length; |
| 3687 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 3688 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3689 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3690 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3691 | output_length = ~0; |
| 3692 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 3693 | output, output_buffer_size, |
| 3694 | &output_length ) ); |
| 3695 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 3696 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3697 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3698 | /* One-shot decryption */ |
| 3699 | output_length = ~0; |
| 3700 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 3701 | output, output_buffer_size, |
| 3702 | &output_length ) ); |
| 3703 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3704 | output, output_length ); |
| 3705 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3706 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3707 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3708 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3709 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3710 | psa_destroy_key( key ); |
| 3711 | PSA_DONE( ); |
| 3712 | } |
| 3713 | /* END_CASE */ |
| 3714 | |
| 3715 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3716 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 3717 | { |
| 3718 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3719 | psa_algorithm_t alg = alg_arg; |
| 3720 | psa_key_type_t key_type = key_type_arg; |
| 3721 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3722 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3723 | psa_status_t status; |
| 3724 | |
| 3725 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3726 | |
| 3727 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3728 | psa_set_key_algorithm( &attributes, alg ); |
| 3729 | psa_set_key_type( &attributes, key_type ); |
| 3730 | |
| 3731 | /* Usage of either of these two size macros would cause divide by zero |
| 3732 | * with incorrect key types previously. Input length should be irrelevant |
| 3733 | * here. */ |
| 3734 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 3735 | 0 ); |
| 3736 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 3737 | |
| 3738 | |
| 3739 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3740 | &key ) ); |
| 3741 | |
| 3742 | /* Should fail due to invalid alg type (to support invalid key type). |
| 3743 | * Encrypt or decrypt will end up in the same place. */ |
| 3744 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3745 | |
| 3746 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 3747 | |
| 3748 | exit: |
| 3749 | psa_cipher_abort( &operation ); |
| 3750 | psa_destroy_key( key ); |
| 3751 | PSA_DONE( ); |
| 3752 | } |
| 3753 | /* END_CASE */ |
| 3754 | |
| 3755 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3756 | void cipher_encrypt_validation( int alg_arg, |
| 3757 | int key_type_arg, |
| 3758 | data_t *key_data, |
| 3759 | data_t *input ) |
| 3760 | { |
| 3761 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3762 | psa_key_type_t key_type = key_type_arg; |
| 3763 | psa_algorithm_t alg = alg_arg; |
| 3764 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 3765 | unsigned char *output1 = NULL; |
| 3766 | size_t output1_buffer_size = 0; |
| 3767 | size_t output1_length = 0; |
| 3768 | unsigned char *output2 = NULL; |
| 3769 | size_t output2_buffer_size = 0; |
| 3770 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3771 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3772 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3773 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3774 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3775 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3776 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3777 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3778 | psa_set_key_algorithm( &attributes, alg ); |
| 3779 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3780 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3781 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3782 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3783 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 3784 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 3785 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 3786 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3787 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3788 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3789 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 3790 | /* The one-shot cipher encryption uses generated iv so validating |
| 3791 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3792 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 3793 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3794 | TEST_LE_U( output1_length, |
| 3795 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3796 | TEST_LE_U( output1_length, |
| 3797 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3798 | |
| 3799 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3800 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3801 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3802 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3803 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3804 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3805 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3806 | TEST_LE_U( function_output_length, |
| 3807 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3808 | TEST_LE_U( function_output_length, |
| 3809 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3810 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3811 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3812 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3813 | output2 + output2_length, |
| 3814 | output2_buffer_size - output2_length, |
| 3815 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3816 | TEST_LE_U( function_output_length, |
| 3817 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3818 | TEST_LE_U( function_output_length, |
| 3819 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3820 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3821 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3822 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3823 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 3824 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3825 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3826 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3827 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3828 | mbedtls_free( output1 ); |
| 3829 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3830 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3831 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3832 | } |
| 3833 | /* END_CASE */ |
| 3834 | |
| 3835 | /* BEGIN_CASE */ |
| 3836 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3837 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3838 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3839 | int first_part_size_arg, |
| 3840 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3841 | data_t *expected_output, |
| 3842 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3843 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3844 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3845 | psa_key_type_t key_type = key_type_arg; |
| 3846 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3847 | psa_status_t status; |
| 3848 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3849 | size_t first_part_size = first_part_size_arg; |
| 3850 | size_t output1_length = output1_length_arg; |
| 3851 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3852 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3853 | size_t output_buffer_size = 0; |
| 3854 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3855 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3856 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3857 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3858 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3859 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3860 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3861 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3862 | psa_set_key_algorithm( &attributes, alg ); |
| 3863 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3864 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3865 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3866 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3867 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3868 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3869 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3870 | if( iv->len > 0 ) |
| 3871 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3872 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3873 | } |
| 3874 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3875 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3876 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3877 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3878 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3879 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3880 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3881 | output, output_buffer_size, |
| 3882 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3883 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3884 | TEST_LE_U( function_output_length, |
| 3885 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3886 | TEST_LE_U( function_output_length, |
| 3887 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3888 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3889 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3890 | if( first_part_size < input->len ) |
| 3891 | { |
| 3892 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3893 | input->x + first_part_size, |
| 3894 | input->len - first_part_size, |
| 3895 | ( output_buffer_size == 0 ? NULL : |
| 3896 | output + total_output_length ), |
| 3897 | output_buffer_size - total_output_length, |
| 3898 | &function_output_length ) ); |
| 3899 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3900 | TEST_LE_U( function_output_length, |
| 3901 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3902 | alg, |
| 3903 | input->len - first_part_size ) ); |
| 3904 | TEST_LE_U( function_output_length, |
| 3905 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3906 | total_output_length += function_output_length; |
| 3907 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3908 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3909 | status = psa_cipher_finish( &operation, |
| 3910 | ( output_buffer_size == 0 ? NULL : |
| 3911 | output + total_output_length ), |
| 3912 | output_buffer_size - total_output_length, |
| 3913 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3914 | TEST_LE_U( function_output_length, |
| 3915 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3916 | TEST_LE_U( function_output_length, |
| 3917 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3918 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3919 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3920 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3921 | if( expected_status == PSA_SUCCESS ) |
| 3922 | { |
| 3923 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3924 | |
| 3925 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3926 | output, total_output_length ); |
| 3927 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3928 | |
| 3929 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3930 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3931 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3932 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3933 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3934 | } |
| 3935 | /* END_CASE */ |
| 3936 | |
| 3937 | /* BEGIN_CASE */ |
| 3938 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3939 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3940 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3941 | int first_part_size_arg, |
| 3942 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3943 | data_t *expected_output, |
| 3944 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3945 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3946 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3947 | psa_key_type_t key_type = key_type_arg; |
| 3948 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3949 | psa_status_t status; |
| 3950 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3951 | size_t first_part_size = first_part_size_arg; |
| 3952 | size_t output1_length = output1_length_arg; |
| 3953 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3954 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3955 | size_t output_buffer_size = 0; |
| 3956 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3957 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3958 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3959 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3960 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3961 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3962 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3963 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3964 | psa_set_key_algorithm( &attributes, alg ); |
| 3965 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3966 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3967 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3968 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3969 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3970 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3971 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3972 | if( iv->len > 0 ) |
| 3973 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3974 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3975 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3976 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3977 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3978 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3979 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3980 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3981 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3982 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3983 | input->x, first_part_size, |
| 3984 | output, output_buffer_size, |
| 3985 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3986 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3987 | TEST_LE_U( function_output_length, |
| 3988 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3989 | TEST_LE_U( function_output_length, |
| 3990 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3991 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3992 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3993 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3994 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3995 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3996 | input->x + first_part_size, |
| 3997 | input->len - first_part_size, |
| 3998 | ( output_buffer_size == 0 ? NULL : |
| 3999 | output + total_output_length ), |
| 4000 | output_buffer_size - total_output_length, |
| 4001 | &function_output_length ) ); |
| 4002 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4003 | TEST_LE_U( function_output_length, |
| 4004 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4005 | alg, |
| 4006 | input->len - first_part_size ) ); |
| 4007 | TEST_LE_U( function_output_length, |
| 4008 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4009 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4010 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4011 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4012 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4013 | ( output_buffer_size == 0 ? NULL : |
| 4014 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4015 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4016 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4017 | TEST_LE_U( function_output_length, |
| 4018 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4019 | TEST_LE_U( function_output_length, |
| 4020 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4021 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4022 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4023 | |
| 4024 | if( expected_status == PSA_SUCCESS ) |
| 4025 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4026 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4027 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4028 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4029 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4030 | } |
| 4031 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4032 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4033 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4034 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4035 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4036 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4037 | } |
| 4038 | /* END_CASE */ |
| 4039 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4040 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4041 | void cipher_decrypt_fail( int alg_arg, |
| 4042 | int key_type_arg, |
| 4043 | data_t *key_data, |
| 4044 | data_t *iv, |
| 4045 | data_t *input_arg, |
| 4046 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4047 | { |
| 4048 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4049 | psa_status_t status; |
| 4050 | psa_key_type_t key_type = key_type_arg; |
| 4051 | psa_algorithm_t alg = alg_arg; |
| 4052 | psa_status_t expected_status = expected_status_arg; |
| 4053 | unsigned char *input = NULL; |
| 4054 | size_t input_buffer_size = 0; |
| 4055 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4056 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4057 | size_t output_buffer_size = 0; |
| 4058 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4059 | size_t function_output_length; |
| 4060 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4061 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4062 | |
| 4063 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4064 | { |
| 4065 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4066 | |
| 4067 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4068 | psa_set_key_algorithm( &attributes, alg ); |
| 4069 | psa_set_key_type( &attributes, key_type ); |
| 4070 | |
| 4071 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4072 | &key ) ); |
| 4073 | } |
| 4074 | |
| 4075 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4076 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4077 | if ( input_buffer_size > 0 ) |
| 4078 | { |
| 4079 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4080 | memcpy( input, iv->x, iv->len ); |
| 4081 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4082 | } |
| 4083 | |
| 4084 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4085 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4086 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4087 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4088 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4089 | output_buffer_size, &output_length ); |
| 4090 | TEST_EQUAL( status, expected_status ); |
| 4091 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4092 | /* Decrypt, multi-part */ |
| 4093 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4094 | if( status == PSA_SUCCESS ) |
| 4095 | { |
| 4096 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4097 | input_arg->len ) + |
| 4098 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4099 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4100 | |
| 4101 | if( iv->len > 0 ) |
| 4102 | { |
| 4103 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4104 | |
| 4105 | if( status != PSA_SUCCESS ) |
| 4106 | TEST_EQUAL( status, expected_status ); |
| 4107 | } |
| 4108 | |
| 4109 | if( status == PSA_SUCCESS ) |
| 4110 | { |
| 4111 | status = psa_cipher_update( &operation, |
| 4112 | input_arg->x, input_arg->len, |
| 4113 | output_multi, output_buffer_size, |
| 4114 | &function_output_length ); |
| 4115 | if( status == PSA_SUCCESS ) |
| 4116 | { |
| 4117 | output_length = function_output_length; |
| 4118 | |
| 4119 | status = psa_cipher_finish( &operation, |
| 4120 | output_multi + output_length, |
| 4121 | output_buffer_size - output_length, |
| 4122 | &function_output_length ); |
| 4123 | |
| 4124 | TEST_EQUAL( status, expected_status ); |
| 4125 | } |
| 4126 | else |
| 4127 | { |
| 4128 | TEST_EQUAL( status, expected_status ); |
| 4129 | } |
| 4130 | } |
| 4131 | else |
| 4132 | { |
| 4133 | TEST_EQUAL( status, expected_status ); |
| 4134 | } |
| 4135 | } |
| 4136 | else |
| 4137 | { |
| 4138 | TEST_EQUAL( status, expected_status ); |
| 4139 | } |
| 4140 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4141 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4142 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4143 | mbedtls_free( input ); |
| 4144 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4145 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4146 | psa_destroy_key( key ); |
| 4147 | PSA_DONE( ); |
| 4148 | } |
| 4149 | /* END_CASE */ |
| 4150 | |
| 4151 | /* BEGIN_CASE */ |
| 4152 | void cipher_decrypt( int alg_arg, |
| 4153 | int key_type_arg, |
| 4154 | data_t *key_data, |
| 4155 | data_t *iv, |
| 4156 | data_t *input_arg, |
| 4157 | data_t *expected_output ) |
| 4158 | { |
| 4159 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4160 | psa_key_type_t key_type = key_type_arg; |
| 4161 | psa_algorithm_t alg = alg_arg; |
| 4162 | unsigned char *input = NULL; |
| 4163 | size_t input_buffer_size = 0; |
| 4164 | unsigned char *output = NULL; |
| 4165 | size_t output_buffer_size = 0; |
| 4166 | size_t output_length = 0; |
| 4167 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4168 | |
| 4169 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4170 | |
| 4171 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4172 | psa_set_key_algorithm( &attributes, alg ); |
| 4173 | psa_set_key_type( &attributes, key_type ); |
| 4174 | |
| 4175 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4176 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4177 | if ( input_buffer_size > 0 ) |
| 4178 | { |
| 4179 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4180 | memcpy( input, iv->x, iv->len ); |
| 4181 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4182 | } |
| 4183 | |
| 4184 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4185 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4186 | |
| 4187 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4188 | &key ) ); |
| 4189 | |
| 4190 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4191 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4192 | TEST_LE_U( output_length, |
| 4193 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4194 | TEST_LE_U( output_length, |
| 4195 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4196 | |
| 4197 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4198 | output, output_length ); |
| 4199 | exit: |
| 4200 | mbedtls_free( input ); |
| 4201 | mbedtls_free( output ); |
| 4202 | psa_destroy_key( key ); |
| 4203 | PSA_DONE( ); |
| 4204 | } |
| 4205 | /* END_CASE */ |
| 4206 | |
| 4207 | /* BEGIN_CASE */ |
| 4208 | void cipher_verify_output( int alg_arg, |
| 4209 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4210 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4211 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4212 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4213 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4214 | psa_key_type_t key_type = key_type_arg; |
| 4215 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4216 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4217 | size_t output1_size = 0; |
| 4218 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4219 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4220 | size_t output2_size = 0; |
| 4221 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4222 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4223 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4224 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4225 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4226 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4227 | psa_set_key_algorithm( &attributes, alg ); |
| 4228 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4229 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4230 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4231 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4232 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4233 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4234 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4235 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4236 | output1, output1_size, |
| 4237 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4238 | TEST_LE_U( output1_length, |
| 4239 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4240 | TEST_LE_U( output1_length, |
| 4241 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4242 | |
| 4243 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4244 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4245 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4246 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4247 | output2, output2_size, |
| 4248 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4249 | TEST_LE_U( output2_length, |
| 4250 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4251 | TEST_LE_U( output2_length, |
| 4252 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4253 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4254 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4255 | |
| 4256 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4257 | mbedtls_free( output1 ); |
| 4258 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4259 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4260 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4261 | } |
| 4262 | /* END_CASE */ |
| 4263 | |
| 4264 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4265 | void cipher_verify_output_multipart( int alg_arg, |
| 4266 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4267 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4268 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4269 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4270 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4271 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4272 | psa_key_type_t key_type = key_type_arg; |
| 4273 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4274 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4275 | unsigned char iv[16] = {0}; |
| 4276 | size_t iv_size = 16; |
| 4277 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4278 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4279 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4280 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4281 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4282 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4283 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4284 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4285 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4286 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4287 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4288 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4289 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4290 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4291 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4292 | psa_set_key_algorithm( &attributes, alg ); |
| 4293 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4294 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4295 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4296 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4297 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4298 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4299 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4300 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4301 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4302 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4303 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4304 | iv, iv_size, |
| 4305 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4308 | 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] | 4309 | TEST_LE_U( output1_buffer_size, |
| 4310 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4311 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4312 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4313 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4314 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4315 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4316 | output1, output1_buffer_size, |
| 4317 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4318 | TEST_LE_U( function_output_length, |
| 4319 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4320 | TEST_LE_U( function_output_length, |
| 4321 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4322 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4323 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4324 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4325 | input->x + first_part_size, |
| 4326 | input->len - first_part_size, |
| 4327 | output1, output1_buffer_size, |
| 4328 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4329 | TEST_LE_U( function_output_length, |
| 4330 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4331 | alg, |
| 4332 | input->len - first_part_size ) ); |
| 4333 | TEST_LE_U( function_output_length, |
| 4334 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4335 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4336 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4337 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4338 | output1 + output1_length, |
| 4339 | output1_buffer_size - output1_length, |
| 4340 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4341 | TEST_LE_U( function_output_length, |
| 4342 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4343 | TEST_LE_U( function_output_length, |
| 4344 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4345 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4346 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4347 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4348 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4349 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4350 | TEST_LE_U( output2_buffer_size, |
| 4351 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4352 | TEST_LE_U( output2_buffer_size, |
| 4353 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4354 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4355 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4356 | if( iv_length > 0 ) |
| 4357 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4358 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4359 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4360 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4361 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4362 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4363 | output2, output2_buffer_size, |
| 4364 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4365 | TEST_LE_U( function_output_length, |
| 4366 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4367 | TEST_LE_U( function_output_length, |
| 4368 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4369 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4370 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4371 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4372 | output1 + first_part_size, |
| 4373 | output1_length - first_part_size, |
| 4374 | output2, output2_buffer_size, |
| 4375 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4376 | TEST_LE_U( function_output_length, |
| 4377 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4378 | alg, |
| 4379 | output1_length - first_part_size ) ); |
| 4380 | TEST_LE_U( function_output_length, |
| 4381 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4382 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4383 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4384 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4385 | output2 + output2_length, |
| 4386 | output2_buffer_size - output2_length, |
| 4387 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4388 | TEST_LE_U( function_output_length, |
| 4389 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4390 | TEST_LE_U( function_output_length, |
| 4391 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4392 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4393 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4394 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4395 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4396 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4397 | |
| 4398 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4399 | psa_cipher_abort( &operation1 ); |
| 4400 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4401 | mbedtls_free( output1 ); |
| 4402 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4403 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4404 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4405 | } |
| 4406 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4407 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4408 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4409 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4410 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4411 | data_t *nonce, |
| 4412 | data_t *additional_data, |
| 4413 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4414 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4415 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4416 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4417 | psa_key_type_t key_type = key_type_arg; |
| 4418 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4419 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4420 | unsigned char *output_data = NULL; |
| 4421 | size_t output_size = 0; |
| 4422 | size_t output_length = 0; |
| 4423 | unsigned char *output_data2 = NULL; |
| 4424 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4425 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4426 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4427 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4428 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4429 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4430 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4431 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4432 | psa_set_key_algorithm( &attributes, alg ); |
| 4433 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4434 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4435 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4436 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4437 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4438 | key_bits = psa_get_key_bits( &attributes ); |
| 4439 | |
| 4440 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4441 | alg ); |
| 4442 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4443 | * should be exact. */ |
| 4444 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4445 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4446 | { |
| 4447 | TEST_EQUAL( output_size, |
| 4448 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4449 | TEST_LE_U( output_size, |
| 4450 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4451 | } |
| 4452 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4453 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4454 | status = psa_aead_encrypt( key, alg, |
| 4455 | nonce->x, nonce->len, |
| 4456 | additional_data->x, |
| 4457 | additional_data->len, |
| 4458 | input_data->x, input_data->len, |
| 4459 | output_data, output_size, |
| 4460 | &output_length ); |
| 4461 | |
| 4462 | /* If the operation is not supported, just skip and not fail in case the |
| 4463 | * encryption involves a common limitation of cryptography hardwares and |
| 4464 | * an alternative implementation. */ |
| 4465 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4466 | { |
| 4467 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4468 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4469 | } |
| 4470 | |
| 4471 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4472 | |
| 4473 | if( PSA_SUCCESS == expected_result ) |
| 4474 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4475 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4476 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4477 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4478 | * should be exact. */ |
| 4479 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4480 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4481 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4482 | TEST_LE_U( input_data->len, |
| 4483 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4484 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4485 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4486 | nonce->x, nonce->len, |
| 4487 | additional_data->x, |
| 4488 | additional_data->len, |
| 4489 | output_data, output_length, |
| 4490 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4491 | &output_length2 ), |
| 4492 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4493 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4494 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4495 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4496 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4497 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4498 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4499 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4500 | mbedtls_free( output_data ); |
| 4501 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4502 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4503 | } |
| 4504 | /* END_CASE */ |
| 4505 | |
| 4506 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4507 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4508 | int alg_arg, |
| 4509 | data_t *nonce, |
| 4510 | data_t *additional_data, |
| 4511 | data_t *input_data, |
| 4512 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4513 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4514 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4515 | psa_key_type_t key_type = key_type_arg; |
| 4516 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4517 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4518 | unsigned char *output_data = NULL; |
| 4519 | size_t output_size = 0; |
| 4520 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4521 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4522 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4523 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4524 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4525 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4526 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4527 | psa_set_key_algorithm( &attributes, alg ); |
| 4528 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4529 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4530 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4531 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4532 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4533 | key_bits = psa_get_key_bits( &attributes ); |
| 4534 | |
| 4535 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4536 | alg ); |
| 4537 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4538 | * should be exact. */ |
| 4539 | TEST_EQUAL( output_size, |
| 4540 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4541 | TEST_LE_U( output_size, |
| 4542 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4543 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4544 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4545 | status = psa_aead_encrypt( key, alg, |
| 4546 | nonce->x, nonce->len, |
| 4547 | additional_data->x, additional_data->len, |
| 4548 | input_data->x, input_data->len, |
| 4549 | output_data, output_size, |
| 4550 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4551 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4552 | /* If the operation is not supported, just skip and not fail in case the |
| 4553 | * encryption involves a common limitation of cryptography hardwares and |
| 4554 | * an alternative implementation. */ |
| 4555 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4556 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4557 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4558 | 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] | 4559 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4560 | |
| 4561 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4562 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4563 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4564 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4565 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4566 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4567 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4568 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4569 | } |
| 4570 | /* END_CASE */ |
| 4571 | |
| 4572 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4573 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4574 | int alg_arg, |
| 4575 | data_t *nonce, |
| 4576 | data_t *additional_data, |
| 4577 | data_t *input_data, |
| 4578 | data_t *expected_data, |
| 4579 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4580 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4581 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4582 | psa_key_type_t key_type = key_type_arg; |
| 4583 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4584 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4585 | unsigned char *output_data = NULL; |
| 4586 | size_t output_size = 0; |
| 4587 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4588 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4589 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4590 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4591 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4592 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4593 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4594 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4595 | psa_set_key_algorithm( &attributes, alg ); |
| 4596 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4597 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4598 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4599 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4600 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4601 | key_bits = psa_get_key_bits( &attributes ); |
| 4602 | |
| 4603 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4604 | alg ); |
| 4605 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4606 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4607 | { |
| 4608 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4609 | * should be exact. */ |
| 4610 | TEST_EQUAL( output_size, |
| 4611 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4612 | TEST_LE_U( output_size, |
| 4613 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4614 | } |
| 4615 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4616 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4617 | status = psa_aead_decrypt( key, alg, |
| 4618 | nonce->x, nonce->len, |
| 4619 | additional_data->x, |
| 4620 | additional_data->len, |
| 4621 | input_data->x, input_data->len, |
| 4622 | output_data, output_size, |
| 4623 | &output_length ); |
| 4624 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4625 | /* If the operation is not supported, just skip and not fail in case the |
| 4626 | * decryption involves a common limitation of cryptography hardwares and |
| 4627 | * an alternative implementation. */ |
| 4628 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4629 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4630 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4631 | 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] | 4632 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4633 | |
| 4634 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4635 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4636 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4637 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4638 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4639 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4640 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4641 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4642 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4643 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4644 | } |
| 4645 | /* END_CASE */ |
| 4646 | |
| 4647 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4648 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4649 | int alg_arg, |
| 4650 | data_t *nonce, |
| 4651 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4652 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4653 | int do_set_lengths, |
| 4654 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4655 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4656 | size_t ad_part_len = 0; |
| 4657 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4658 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4659 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4660 | 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] | 4661 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4662 | mbedtls_test_set_step( ad_part_len ); |
| 4663 | |
| 4664 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4665 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4666 | if( ad_part_len & 0x01 ) |
| 4667 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4668 | else |
| 4669 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4670 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4671 | |
| 4672 | /* Split ad into length(ad_part_len) parts. */ |
| 4673 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4674 | alg_arg, nonce, |
| 4675 | additional_data, |
| 4676 | ad_part_len, |
| 4677 | input_data, -1, |
| 4678 | set_lengths_method, |
| 4679 | expected_output, |
| 4680 | 1, 0 ) ) |
| 4681 | break; |
| 4682 | |
| 4683 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4684 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4685 | |
| 4686 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4687 | alg_arg, nonce, |
| 4688 | additional_data, |
| 4689 | ad_part_len, |
| 4690 | input_data, -1, |
| 4691 | set_lengths_method, |
| 4692 | expected_output, |
| 4693 | 1, 1 ) ) |
| 4694 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4695 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4696 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4697 | 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] | 4698 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4699 | /* Split data into length(data_part_len) parts. */ |
| 4700 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 4701 | |
| 4702 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4703 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4704 | if( data_part_len & 0x01 ) |
| 4705 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4706 | else |
| 4707 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4708 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4709 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4710 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4711 | alg_arg, nonce, |
| 4712 | additional_data, -1, |
| 4713 | input_data, data_part_len, |
| 4714 | set_lengths_method, |
| 4715 | expected_output, |
| 4716 | 1, 0 ) ) |
| 4717 | break; |
| 4718 | |
| 4719 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 4720 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 4721 | |
| 4722 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4723 | alg_arg, nonce, |
| 4724 | additional_data, -1, |
| 4725 | input_data, data_part_len, |
| 4726 | set_lengths_method, |
| 4727 | expected_output, |
| 4728 | 1, 1 ) ) |
| 4729 | break; |
| 4730 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4731 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4732 | /* Goto is required to silence warnings about unused labels, as we |
| 4733 | * don't actually do any test assertions in this function. */ |
| 4734 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4735 | } |
| 4736 | /* END_CASE */ |
| 4737 | |
| 4738 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4739 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 4740 | int alg_arg, |
| 4741 | data_t *nonce, |
| 4742 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4743 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4744 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4745 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4746 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4747 | size_t ad_part_len = 0; |
| 4748 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4749 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4750 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4751 | 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] | 4752 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4753 | /* Split ad into length(ad_part_len) parts. */ |
| 4754 | mbedtls_test_set_step( ad_part_len ); |
| 4755 | |
| 4756 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4757 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4758 | if( ad_part_len & 0x01 ) |
| 4759 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4760 | else |
| 4761 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4762 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4763 | |
| 4764 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4765 | alg_arg, nonce, |
| 4766 | additional_data, |
| 4767 | ad_part_len, |
| 4768 | input_data, -1, |
| 4769 | set_lengths_method, |
| 4770 | expected_output, |
| 4771 | 0, 0 ) ) |
| 4772 | break; |
| 4773 | |
| 4774 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4775 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4776 | |
| 4777 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4778 | alg_arg, nonce, |
| 4779 | additional_data, |
| 4780 | ad_part_len, |
| 4781 | input_data, -1, |
| 4782 | set_lengths_method, |
| 4783 | expected_output, |
| 4784 | 0, 1 ) ) |
| 4785 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4786 | } |
| 4787 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4788 | 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] | 4789 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4790 | /* Split data into length(data_part_len) parts. */ |
| 4791 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 4792 | |
| 4793 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4794 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4795 | if( data_part_len & 0x01 ) |
| 4796 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4797 | else |
| 4798 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4799 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4800 | |
| 4801 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4802 | alg_arg, nonce, |
| 4803 | additional_data, -1, |
| 4804 | input_data, data_part_len, |
| 4805 | set_lengths_method, |
| 4806 | expected_output, |
| 4807 | 0, 0 ) ) |
| 4808 | break; |
| 4809 | |
| 4810 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 4811 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 4812 | |
| 4813 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4814 | alg_arg, nonce, |
| 4815 | additional_data, -1, |
| 4816 | input_data, data_part_len, |
| 4817 | set_lengths_method, |
| 4818 | expected_output, |
| 4819 | 0, 1 ) ) |
| 4820 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4821 | } |
| 4822 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4823 | /* Goto is required to silence warnings about unused labels, as we |
| 4824 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4825 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4826 | } |
| 4827 | /* END_CASE */ |
| 4828 | |
| 4829 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4830 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 4831 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4832 | int nonce_length, |
| 4833 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4834 | data_t *additional_data, |
| 4835 | data_t *input_data, |
| 4836 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4837 | { |
| 4838 | |
| 4839 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4840 | psa_key_type_t key_type = key_type_arg; |
| 4841 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4842 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4843 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 4844 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4845 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4846 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4847 | size_t actual_nonce_length = 0; |
| 4848 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 4849 | unsigned char *output = NULL; |
| 4850 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4851 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4852 | size_t ciphertext_size = 0; |
| 4853 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4854 | size_t tag_length = 0; |
| 4855 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4856 | |
| 4857 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4858 | |
| 4859 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4860 | psa_set_key_algorithm( & attributes, alg ); |
| 4861 | psa_set_key_type( & attributes, key_type ); |
| 4862 | |
| 4863 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4864 | &key ) ); |
| 4865 | |
| 4866 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4867 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4868 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4869 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4870 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4871 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4872 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4873 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4874 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4875 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4876 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4877 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4878 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4879 | |
| 4880 | /* If the operation is not supported, just skip and not fail in case the |
| 4881 | * encryption involves a common limitation of cryptography hardwares and |
| 4882 | * an alternative implementation. */ |
| 4883 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4884 | { |
| 4885 | 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] | 4886 | 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] | 4887 | } |
| 4888 | |
| 4889 | PSA_ASSERT( status ); |
| 4890 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4891 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4892 | nonce_length, |
| 4893 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4894 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4895 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4896 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4897 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 4898 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 4899 | if( expected_status == PSA_SUCCESS ) |
| 4900 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 4901 | alg ) ); |
| 4902 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4903 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 4904 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4905 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4906 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4907 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 4908 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4909 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4910 | |
| 4911 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4912 | additional_data->len ) ); |
| 4913 | |
| 4914 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4915 | output, output_size, |
| 4916 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4917 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4918 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 4919 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4920 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 4921 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4922 | |
| 4923 | exit: |
| 4924 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4925 | mbedtls_free( output ); |
| 4926 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4927 | psa_aead_abort( &operation ); |
| 4928 | PSA_DONE( ); |
| 4929 | } |
| 4930 | /* END_CASE */ |
| 4931 | |
| 4932 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4933 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 4934 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4935 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4936 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4937 | data_t *additional_data, |
| 4938 | data_t *input_data, |
| 4939 | int expected_status_arg ) |
| 4940 | { |
| 4941 | |
| 4942 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4943 | psa_key_type_t key_type = key_type_arg; |
| 4944 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4945 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4946 | uint8_t *nonce_buffer = NULL; |
| 4947 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4948 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4949 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4950 | unsigned char *output = NULL; |
| 4951 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4952 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4953 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4954 | size_t ciphertext_size = 0; |
| 4955 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4956 | size_t tag_length = 0; |
| 4957 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4958 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4959 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4960 | |
| 4961 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4962 | |
| 4963 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4964 | psa_set_key_algorithm( &attributes, alg ); |
| 4965 | psa_set_key_type( &attributes, key_type ); |
| 4966 | |
| 4967 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4968 | &key ) ); |
| 4969 | |
| 4970 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4971 | |
| 4972 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4973 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4974 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4975 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4976 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4977 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4978 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4979 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4980 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4981 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4982 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4983 | |
| 4984 | /* If the operation is not supported, just skip and not fail in case the |
| 4985 | * encryption involves a common limitation of cryptography hardwares and |
| 4986 | * an alternative implementation. */ |
| 4987 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4988 | { |
| 4989 | 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] | 4990 | 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] | 4991 | } |
| 4992 | |
| 4993 | PSA_ASSERT( status ); |
| 4994 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4995 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 4996 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4997 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 4998 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 4999 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5000 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5001 | } |
| 5002 | else |
| 5003 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5004 | /* If length is zero, then this will return NULL. */ |
| 5005 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5006 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5007 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5008 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5009 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5010 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5011 | { |
| 5012 | nonce_buffer[index] = 'a' + index; |
| 5013 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5014 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5015 | } |
| 5016 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5017 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5018 | { |
| 5019 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5020 | input_data->len ) ); |
| 5021 | } |
| 5022 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5023 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5024 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5025 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5026 | |
| 5027 | if( expected_status == PSA_SUCCESS ) |
| 5028 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5029 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5030 | { |
| 5031 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5032 | input_data->len ) ); |
| 5033 | } |
| 5034 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5035 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5036 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5037 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5038 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5039 | additional_data->len ), |
| 5040 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5041 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5042 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5043 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5044 | &ciphertext_length ), |
| 5045 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5046 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5047 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5048 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5049 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5050 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | exit: |
| 5054 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5055 | mbedtls_free( output ); |
| 5056 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5057 | mbedtls_free( nonce_buffer ); |
| 5058 | psa_aead_abort( &operation ); |
| 5059 | PSA_DONE( ); |
| 5060 | } |
| 5061 | /* END_CASE */ |
| 5062 | |
| 5063 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5064 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5065 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5066 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5067 | data_t *nonce, |
| 5068 | data_t *additional_data, |
| 5069 | data_t *input_data, |
| 5070 | int expected_status_arg ) |
| 5071 | { |
| 5072 | |
| 5073 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5074 | psa_key_type_t key_type = key_type_arg; |
| 5075 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5076 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5077 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5078 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5079 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5080 | unsigned char *output = NULL; |
| 5081 | unsigned char *ciphertext = NULL; |
| 5082 | size_t output_size = output_size_arg; |
| 5083 | size_t ciphertext_size = 0; |
| 5084 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5085 | size_t tag_length = 0; |
| 5086 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5087 | |
| 5088 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5089 | |
| 5090 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5091 | psa_set_key_algorithm( &attributes, alg ); |
| 5092 | psa_set_key_type( &attributes, key_type ); |
| 5093 | |
| 5094 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5095 | &key ) ); |
| 5096 | |
| 5097 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5098 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5099 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5100 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5101 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5102 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5103 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5104 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5105 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5106 | |
| 5107 | /* If the operation is not supported, just skip and not fail in case the |
| 5108 | * encryption involves a common limitation of cryptography hardwares and |
| 5109 | * an alternative implementation. */ |
| 5110 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5111 | { |
| 5112 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5113 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5114 | } |
| 5115 | |
| 5116 | PSA_ASSERT( status ); |
| 5117 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5118 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5119 | input_data->len ) ); |
| 5120 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5121 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5122 | |
| 5123 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5124 | additional_data->len ) ); |
| 5125 | |
| 5126 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5127 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5128 | |
| 5129 | TEST_EQUAL( status, expected_status ); |
| 5130 | |
| 5131 | if( expected_status == PSA_SUCCESS ) |
| 5132 | { |
| 5133 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5134 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5135 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5136 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5137 | } |
| 5138 | |
| 5139 | exit: |
| 5140 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5141 | mbedtls_free( output ); |
| 5142 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5143 | psa_aead_abort( &operation ); |
| 5144 | PSA_DONE( ); |
| 5145 | } |
| 5146 | /* END_CASE */ |
| 5147 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5148 | /* BEGIN_CASE */ |
| 5149 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5150 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5151 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5152 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5153 | data_t *nonce, |
| 5154 | data_t *additional_data, |
| 5155 | data_t *input_data, |
| 5156 | int expected_status_arg ) |
| 5157 | { |
| 5158 | |
| 5159 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5160 | psa_key_type_t key_type = key_type_arg; |
| 5161 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5162 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5163 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5164 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5165 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5166 | unsigned char *ciphertext = NULL; |
| 5167 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5168 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5169 | size_t ciphertext_size = 0; |
| 5170 | size_t ciphertext_length = 0; |
| 5171 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5172 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5173 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5174 | |
| 5175 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5176 | |
| 5177 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5178 | psa_set_key_algorithm( &attributes, alg ); |
| 5179 | psa_set_key_type( &attributes, key_type ); |
| 5180 | |
| 5181 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5182 | &key ) ); |
| 5183 | |
| 5184 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5185 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5186 | 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] | 5187 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5188 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5189 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5190 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5191 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5192 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5193 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5194 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5195 | |
| 5196 | /* If the operation is not supported, just skip and not fail in case the |
| 5197 | * encryption involves a common limitation of cryptography hardwares and |
| 5198 | * an alternative implementation. */ |
| 5199 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5200 | { |
| 5201 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5202 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5203 | } |
| 5204 | |
| 5205 | PSA_ASSERT( status ); |
| 5206 | |
| 5207 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5208 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5209 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5210 | input_data->len ) ); |
| 5211 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5212 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5213 | additional_data->len ) ); |
| 5214 | |
| 5215 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5216 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5217 | |
| 5218 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5219 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5220 | finish_ciphertext_size, |
| 5221 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5222 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5223 | |
| 5224 | TEST_EQUAL( status, expected_status ); |
| 5225 | |
| 5226 | exit: |
| 5227 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5228 | mbedtls_free( ciphertext ); |
| 5229 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5230 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5231 | psa_aead_abort( &operation ); |
| 5232 | PSA_DONE( ); |
| 5233 | } |
| 5234 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5235 | |
| 5236 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5237 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5238 | int alg_arg, |
| 5239 | data_t *nonce, |
| 5240 | data_t *additional_data, |
| 5241 | data_t *input_data, |
| 5242 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5243 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5244 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5245 | int expected_status_arg ) |
| 5246 | { |
| 5247 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5248 | psa_key_type_t key_type = key_type_arg; |
| 5249 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5250 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5251 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5252 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5253 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5254 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5255 | unsigned char *plaintext = NULL; |
| 5256 | unsigned char *finish_plaintext = NULL; |
| 5257 | size_t plaintext_size = 0; |
| 5258 | size_t plaintext_length = 0; |
| 5259 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5260 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5261 | unsigned char *tag_buffer = NULL; |
| 5262 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5263 | |
| 5264 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5265 | |
| 5266 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5267 | psa_set_key_algorithm( &attributes, alg ); |
| 5268 | psa_set_key_type( &attributes, key_type ); |
| 5269 | |
| 5270 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5271 | &key ) ); |
| 5272 | |
| 5273 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5274 | |
| 5275 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5276 | input_data->len ); |
| 5277 | |
| 5278 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5279 | |
| 5280 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5281 | |
| 5282 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5283 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5284 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5285 | |
| 5286 | /* If the operation is not supported, just skip and not fail in case the |
| 5287 | * encryption involves a common limitation of cryptography hardwares and |
| 5288 | * an alternative implementation. */ |
| 5289 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5290 | { |
| 5291 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5292 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5293 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5294 | TEST_EQUAL( status, expected_setup_status ); |
| 5295 | |
| 5296 | if( status != PSA_SUCCESS ) |
| 5297 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5298 | |
| 5299 | PSA_ASSERT( status ); |
| 5300 | |
| 5301 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5302 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5303 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5304 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5305 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5306 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5307 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5308 | additional_data->len ) ); |
| 5309 | |
| 5310 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5311 | input_data->len, |
| 5312 | plaintext, plaintext_size, |
| 5313 | &plaintext_length ) ); |
| 5314 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5315 | if( tag_usage == USE_GIVEN_TAG ) |
| 5316 | { |
| 5317 | tag_buffer = tag->x; |
| 5318 | tag_size = tag->len; |
| 5319 | } |
| 5320 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5321 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5322 | verify_plaintext_size, |
| 5323 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5324 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5325 | |
| 5326 | TEST_EQUAL( status, expected_status ); |
| 5327 | |
| 5328 | exit: |
| 5329 | psa_destroy_key( key ); |
| 5330 | mbedtls_free( plaintext ); |
| 5331 | mbedtls_free( finish_plaintext ); |
| 5332 | psa_aead_abort( &operation ); |
| 5333 | PSA_DONE( ); |
| 5334 | } |
| 5335 | /* END_CASE */ |
| 5336 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5337 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5338 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5339 | int alg_arg, int expected_status_arg ) |
| 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 | 5221ef6 | 2021-09-19 17:33:03 +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; |
| 5348 | |
| 5349 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5350 | |
| 5351 | psa_set_key_usage_flags( &attributes, |
| 5352 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5353 | psa_set_key_algorithm( &attributes, alg ); |
| 5354 | psa_set_key_type( &attributes, key_type ); |
| 5355 | |
| 5356 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5357 | &key ) ); |
| 5358 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5359 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5360 | |
| 5361 | TEST_EQUAL( status, expected_status ); |
| 5362 | |
| 5363 | psa_aead_abort( &operation ); |
| 5364 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5365 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5366 | |
| 5367 | TEST_EQUAL(status, expected_status ); |
| 5368 | |
| 5369 | exit: |
| 5370 | psa_destroy_key( key ); |
| 5371 | psa_aead_abort( &operation ); |
| 5372 | PSA_DONE( ); |
| 5373 | } |
| 5374 | /* END_CASE */ |
| 5375 | |
| 5376 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5377 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5378 | int alg_arg, |
| 5379 | data_t *nonce, |
| 5380 | data_t *additional_data, |
| 5381 | data_t *input_data ) |
| 5382 | { |
| 5383 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5384 | psa_key_type_t key_type = key_type_arg; |
| 5385 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5386 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5387 | unsigned char *output_data = NULL; |
| 5388 | unsigned char *final_data = NULL; |
| 5389 | size_t output_size = 0; |
| 5390 | size_t finish_output_size = 0; |
| 5391 | size_t output_length = 0; |
| 5392 | size_t key_bits = 0; |
| 5393 | size_t tag_length = 0; |
| 5394 | size_t tag_size = 0; |
| 5395 | size_t nonce_length = 0; |
| 5396 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5397 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5398 | size_t output_part_length = 0; |
| 5399 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5400 | |
| 5401 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5402 | |
| 5403 | psa_set_key_usage_flags( & attributes, |
| 5404 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5405 | psa_set_key_algorithm( & attributes, alg ); |
| 5406 | psa_set_key_type( & attributes, key_type ); |
| 5407 | |
| 5408 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5409 | &key ) ); |
| 5410 | |
| 5411 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5412 | key_bits = psa_get_key_bits( &attributes ); |
| 5413 | |
| 5414 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5415 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5416 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5417 | |
| 5418 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5419 | |
| 5420 | ASSERT_ALLOC( output_data, output_size ); |
| 5421 | |
| 5422 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5423 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5424 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5425 | |
| 5426 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5427 | |
| 5428 | /* Test all operations error without calling setup first. */ |
| 5429 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5430 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5431 | PSA_ERROR_BAD_STATE ); |
| 5432 | |
| 5433 | psa_aead_abort( &operation ); |
| 5434 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5435 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5436 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5437 | &nonce_length ), |
| 5438 | PSA_ERROR_BAD_STATE ); |
| 5439 | |
| 5440 | psa_aead_abort( &operation ); |
| 5441 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5442 | /* ------------------------------------------------------- */ |
| 5443 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5444 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5445 | input_data->len ), |
| 5446 | PSA_ERROR_BAD_STATE ); |
| 5447 | |
| 5448 | psa_aead_abort( &operation ); |
| 5449 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5450 | /* ------------------------------------------------------- */ |
| 5451 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5452 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5453 | additional_data->len ), |
| 5454 | PSA_ERROR_BAD_STATE ); |
| 5455 | |
| 5456 | psa_aead_abort( &operation ); |
| 5457 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5458 | /* ------------------------------------------------------- */ |
| 5459 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5460 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5461 | input_data->len, output_data, |
| 5462 | output_size, &output_length ), |
| 5463 | PSA_ERROR_BAD_STATE ); |
| 5464 | |
| 5465 | psa_aead_abort( &operation ); |
| 5466 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5467 | /* ------------------------------------------------------- */ |
| 5468 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5469 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5470 | finish_output_size, |
| 5471 | &output_part_length, |
| 5472 | tag_buffer, tag_length, |
| 5473 | &tag_size ), |
| 5474 | PSA_ERROR_BAD_STATE ); |
| 5475 | |
| 5476 | psa_aead_abort( &operation ); |
| 5477 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5478 | /* ------------------------------------------------------- */ |
| 5479 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5480 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5481 | finish_output_size, |
| 5482 | &output_part_length, |
| 5483 | tag_buffer, |
| 5484 | tag_length ), |
| 5485 | PSA_ERROR_BAD_STATE ); |
| 5486 | |
| 5487 | psa_aead_abort( &operation ); |
| 5488 | |
| 5489 | /* Test for double setups. */ |
| 5490 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5491 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5492 | |
| 5493 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5494 | PSA_ERROR_BAD_STATE ); |
| 5495 | |
| 5496 | psa_aead_abort( &operation ); |
| 5497 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5498 | /* ------------------------------------------------------- */ |
| 5499 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5500 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5501 | |
| 5502 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5503 | PSA_ERROR_BAD_STATE ); |
| 5504 | |
| 5505 | psa_aead_abort( &operation ); |
| 5506 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5507 | /* ------------------------------------------------------- */ |
| 5508 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5509 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5510 | |
| 5511 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5512 | PSA_ERROR_BAD_STATE ); |
| 5513 | |
| 5514 | psa_aead_abort( &operation ); |
| 5515 | |
| 5516 | /* ------------------------------------------------------- */ |
| 5517 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5518 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5519 | |
| 5520 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5521 | PSA_ERROR_BAD_STATE ); |
| 5522 | |
| 5523 | psa_aead_abort( &operation ); |
| 5524 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5525 | /* Test for not setting a nonce. */ |
| 5526 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5527 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5528 | |
| 5529 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5530 | additional_data->len ), |
| 5531 | PSA_ERROR_BAD_STATE ); |
| 5532 | |
| 5533 | psa_aead_abort( &operation ); |
| 5534 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5535 | /* ------------------------------------------------------- */ |
| 5536 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5537 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5538 | |
| 5539 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5540 | input_data->len, output_data, |
| 5541 | output_size, &output_length ), |
| 5542 | PSA_ERROR_BAD_STATE ); |
| 5543 | |
| 5544 | psa_aead_abort( &operation ); |
| 5545 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5546 | /* ------------------------------------------------------- */ |
| 5547 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5548 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5549 | |
| 5550 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5551 | finish_output_size, |
| 5552 | &output_part_length, |
| 5553 | tag_buffer, tag_length, |
| 5554 | &tag_size ), |
| 5555 | PSA_ERROR_BAD_STATE ); |
| 5556 | |
| 5557 | psa_aead_abort( &operation ); |
| 5558 | |
| 5559 | /* ------------------------------------------------------- */ |
| 5560 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5561 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5562 | |
| 5563 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5564 | finish_output_size, |
| 5565 | &output_part_length, |
| 5566 | tag_buffer, |
| 5567 | tag_length ), |
| 5568 | PSA_ERROR_BAD_STATE ); |
| 5569 | |
| 5570 | psa_aead_abort( &operation ); |
| 5571 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5572 | /* Test for double setting nonce. */ |
| 5573 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5574 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5575 | |
| 5576 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5577 | |
| 5578 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5579 | PSA_ERROR_BAD_STATE ); |
| 5580 | |
| 5581 | psa_aead_abort( &operation ); |
| 5582 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5583 | /* Test for double generating nonce. */ |
| 5584 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5585 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5586 | |
| 5587 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5588 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5589 | &nonce_length ) ); |
| 5590 | |
| 5591 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5592 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5593 | &nonce_length ), |
| 5594 | PSA_ERROR_BAD_STATE ); |
| 5595 | |
| 5596 | |
| 5597 | psa_aead_abort( &operation ); |
| 5598 | |
| 5599 | /* Test for generate nonce then set and vice versa */ |
| 5600 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5601 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5602 | |
| 5603 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5604 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5605 | &nonce_length ) ); |
| 5606 | |
| 5607 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5608 | PSA_ERROR_BAD_STATE ); |
| 5609 | |
| 5610 | psa_aead_abort( &operation ); |
| 5611 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5612 | /* Test for generating nonce after calling set lengths */ |
| 5613 | |
| 5614 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5615 | |
| 5616 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5617 | input_data->len ) ); |
| 5618 | |
| 5619 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5620 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5621 | &nonce_length ) ); |
| 5622 | |
| 5623 | psa_aead_abort( &operation ); |
| 5624 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5625 | /* 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] | 5626 | |
| 5627 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5628 | |
| 5629 | if( operation.alg == PSA_ALG_CCM ) |
| 5630 | { |
| 5631 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5632 | input_data->len ), |
| 5633 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5634 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5635 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5636 | &nonce_length ), |
| 5637 | PSA_ERROR_BAD_STATE ); |
| 5638 | } |
| 5639 | else |
| 5640 | { |
| 5641 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5642 | input_data->len ) ); |
| 5643 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5644 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5645 | &nonce_length ) ); |
| 5646 | } |
| 5647 | |
| 5648 | psa_aead_abort( &operation ); |
| 5649 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5650 | /* 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] | 5651 | #if SIZE_MAX > UINT32_MAX |
| 5652 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5653 | |
| 5654 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5655 | { |
| 5656 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5657 | input_data->len ), |
| 5658 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5659 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5660 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5661 | &nonce_length ), |
| 5662 | PSA_ERROR_BAD_STATE ); |
| 5663 | } |
| 5664 | else |
| 5665 | { |
| 5666 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5667 | input_data->len ) ); |
| 5668 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5669 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5670 | &nonce_length ) ); |
| 5671 | } |
| 5672 | |
| 5673 | psa_aead_abort( &operation ); |
| 5674 | #endif |
| 5675 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5676 | /* 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] | 5677 | |
| 5678 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5679 | |
| 5680 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5681 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5682 | &nonce_length ) ); |
| 5683 | |
| 5684 | if( operation.alg == PSA_ALG_CCM ) |
| 5685 | { |
| 5686 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5687 | input_data->len ), |
| 5688 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5689 | } |
| 5690 | else |
| 5691 | { |
| 5692 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5693 | input_data->len ) ); |
| 5694 | } |
| 5695 | |
| 5696 | psa_aead_abort( &operation ); |
| 5697 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5698 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5699 | /* Test for setting nonce after calling set lengths */ |
| 5700 | |
| 5701 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5702 | |
| 5703 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5704 | input_data->len ) ); |
| 5705 | |
| 5706 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5707 | |
| 5708 | psa_aead_abort( &operation ); |
| 5709 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5710 | /* 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] | 5711 | |
| 5712 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5713 | |
| 5714 | if( operation.alg == PSA_ALG_CCM ) |
| 5715 | { |
| 5716 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5717 | input_data->len ), |
| 5718 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5719 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5720 | PSA_ERROR_BAD_STATE ); |
| 5721 | } |
| 5722 | else |
| 5723 | { |
| 5724 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5725 | input_data->len ) ); |
| 5726 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5727 | } |
| 5728 | |
| 5729 | psa_aead_abort( &operation ); |
| 5730 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5731 | /* 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] | 5732 | #if SIZE_MAX > UINT32_MAX |
| 5733 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5734 | |
| 5735 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5736 | { |
| 5737 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5738 | input_data->len ), |
| 5739 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5740 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5741 | PSA_ERROR_BAD_STATE ); |
| 5742 | } |
| 5743 | else |
| 5744 | { |
| 5745 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5746 | input_data->len ) ); |
| 5747 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5748 | } |
| 5749 | |
| 5750 | psa_aead_abort( &operation ); |
| 5751 | #endif |
| 5752 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5753 | /* 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] | 5754 | |
| 5755 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5756 | |
| 5757 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5758 | |
| 5759 | if( operation.alg == PSA_ALG_CCM ) |
| 5760 | { |
| 5761 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5762 | input_data->len ), |
| 5763 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5764 | } |
| 5765 | else |
| 5766 | { |
| 5767 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5768 | input_data->len ) ); |
| 5769 | } |
| 5770 | |
| 5771 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5772 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5773 | /* 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] | 5774 | #if SIZE_MAX > UINT32_MAX |
| 5775 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5776 | |
| 5777 | if( operation.alg == PSA_ALG_GCM ) |
| 5778 | { |
| 5779 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5780 | SIZE_MAX ), |
| 5781 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5782 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5783 | PSA_ERROR_BAD_STATE ); |
| 5784 | } |
| 5785 | else if ( operation.alg != PSA_ALG_CCM ) |
| 5786 | { |
| 5787 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5788 | SIZE_MAX ) ); |
| 5789 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5790 | } |
| 5791 | |
| 5792 | psa_aead_abort( &operation ); |
| 5793 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5794 | /* 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] | 5795 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5796 | |
| 5797 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5798 | |
| 5799 | if( operation.alg == PSA_ALG_GCM ) |
| 5800 | { |
| 5801 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5802 | SIZE_MAX ), |
| 5803 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5804 | } |
| 5805 | else if ( operation.alg != PSA_ALG_CCM ) |
| 5806 | { |
| 5807 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5808 | SIZE_MAX ) ); |
| 5809 | } |
| 5810 | |
| 5811 | psa_aead_abort( &operation ); |
| 5812 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5813 | |
| 5814 | /* ------------------------------------------------------- */ |
| 5815 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5816 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5817 | |
| 5818 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5819 | |
| 5820 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5821 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5822 | &nonce_length ), |
| 5823 | PSA_ERROR_BAD_STATE ); |
| 5824 | |
| 5825 | psa_aead_abort( &operation ); |
| 5826 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 5827 | /* Test for generating nonce in decrypt setup. */ |
| 5828 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 5829 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5830 | |
| 5831 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5832 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5833 | &nonce_length ), |
| 5834 | PSA_ERROR_BAD_STATE ); |
| 5835 | |
| 5836 | psa_aead_abort( &operation ); |
| 5837 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5838 | /* Test for setting lengths twice. */ |
| 5839 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5840 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5841 | |
| 5842 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5843 | |
| 5844 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5845 | input_data->len ) ); |
| 5846 | |
| 5847 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5848 | input_data->len ), |
| 5849 | PSA_ERROR_BAD_STATE ); |
| 5850 | |
| 5851 | psa_aead_abort( &operation ); |
| 5852 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5853 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5854 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5855 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5856 | |
| 5857 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5858 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5859 | if( operation.alg == PSA_ALG_CCM ) |
| 5860 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5861 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5862 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5863 | additional_data->len ), |
| 5864 | PSA_ERROR_BAD_STATE ); |
| 5865 | } |
| 5866 | else |
| 5867 | { |
| 5868 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5869 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5870 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5871 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5872 | input_data->len ), |
| 5873 | PSA_ERROR_BAD_STATE ); |
| 5874 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5875 | psa_aead_abort( &operation ); |
| 5876 | |
| 5877 | /* ------------------------------------------------------- */ |
| 5878 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5879 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5880 | |
| 5881 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5882 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5883 | if( operation.alg == PSA_ALG_CCM ) |
| 5884 | { |
| 5885 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5886 | input_data->len, output_data, |
| 5887 | output_size, &output_length ), |
| 5888 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5889 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5890 | } |
| 5891 | else |
| 5892 | { |
| 5893 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5894 | input_data->len, output_data, |
| 5895 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5896 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5897 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5898 | input_data->len ), |
| 5899 | PSA_ERROR_BAD_STATE ); |
| 5900 | } |
| 5901 | psa_aead_abort( &operation ); |
| 5902 | |
| 5903 | /* ------------------------------------------------------- */ |
| 5904 | |
| 5905 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5906 | |
| 5907 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5908 | |
| 5909 | if( operation.alg == PSA_ALG_CCM ) |
| 5910 | { |
| 5911 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5912 | finish_output_size, |
| 5913 | &output_part_length, |
| 5914 | tag_buffer, tag_length, |
| 5915 | &tag_size ) ); |
| 5916 | } |
| 5917 | else |
| 5918 | { |
| 5919 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5920 | finish_output_size, |
| 5921 | &output_part_length, |
| 5922 | tag_buffer, tag_length, |
| 5923 | &tag_size ) ); |
| 5924 | |
| 5925 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5926 | input_data->len ), |
| 5927 | PSA_ERROR_BAD_STATE ); |
| 5928 | } |
| 5929 | psa_aead_abort( &operation ); |
| 5930 | |
| 5931 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 5932 | |
| 5933 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5934 | |
| 5935 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5936 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5937 | &nonce_length ) ); |
| 5938 | if( operation.alg == PSA_ALG_CCM ) |
| 5939 | { |
| 5940 | |
| 5941 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5942 | additional_data->len ), |
| 5943 | PSA_ERROR_BAD_STATE ); |
| 5944 | } |
| 5945 | else |
| 5946 | { |
| 5947 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5948 | additional_data->len ) ); |
| 5949 | |
| 5950 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5951 | input_data->len ), |
| 5952 | PSA_ERROR_BAD_STATE ); |
| 5953 | } |
| 5954 | psa_aead_abort( &operation ); |
| 5955 | |
| 5956 | /* ------------------------------------------------------- */ |
| 5957 | |
| 5958 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5959 | |
| 5960 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5961 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5962 | &nonce_length ) ); |
| 5963 | if( operation.alg == PSA_ALG_CCM ) |
| 5964 | { |
| 5965 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5966 | input_data->len, output_data, |
| 5967 | output_size, &output_length ), |
| 5968 | PSA_ERROR_BAD_STATE ); |
| 5969 | |
| 5970 | } |
| 5971 | else |
| 5972 | { |
| 5973 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5974 | input_data->len, output_data, |
| 5975 | output_size, &output_length ) ); |
| 5976 | |
| 5977 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5978 | input_data->len ), |
| 5979 | PSA_ERROR_BAD_STATE ); |
| 5980 | } |
| 5981 | psa_aead_abort( &operation ); |
| 5982 | |
| 5983 | /* ------------------------------------------------------- */ |
| 5984 | |
| 5985 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5986 | |
| 5987 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5988 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5989 | &nonce_length ) ); |
| 5990 | if( operation.alg == PSA_ALG_CCM ) |
| 5991 | { |
| 5992 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5993 | finish_output_size, |
| 5994 | &output_part_length, |
| 5995 | tag_buffer, tag_length, |
| 5996 | &tag_size ) ); |
| 5997 | } |
| 5998 | else |
| 5999 | { |
| 6000 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6001 | finish_output_size, |
| 6002 | &output_part_length, |
| 6003 | tag_buffer, tag_length, |
| 6004 | &tag_size ) ); |
| 6005 | |
| 6006 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6007 | input_data->len ), |
| 6008 | PSA_ERROR_BAD_STATE ); |
| 6009 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6010 | psa_aead_abort( &operation ); |
| 6011 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6012 | /* Test for not sending any additional data or data after setting non zero |
| 6013 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6014 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6015 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6016 | |
| 6017 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6018 | |
| 6019 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6020 | input_data->len ) ); |
| 6021 | |
| 6022 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6023 | finish_output_size, |
| 6024 | &output_part_length, |
| 6025 | tag_buffer, tag_length, |
| 6026 | &tag_size ), |
| 6027 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6028 | |
| 6029 | psa_aead_abort( &operation ); |
| 6030 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6031 | /* Test for not sending any additional data or data after setting non-zero |
| 6032 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6033 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6034 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6035 | |
| 6036 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6037 | |
| 6038 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6039 | input_data->len ) ); |
| 6040 | |
| 6041 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6042 | finish_output_size, |
| 6043 | &output_part_length, |
| 6044 | tag_buffer, |
| 6045 | tag_length ), |
| 6046 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6047 | |
| 6048 | psa_aead_abort( &operation ); |
| 6049 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6050 | /* Test for not sending any additional data after setting a non-zero length |
| 6051 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6052 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6053 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6054 | |
| 6055 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6056 | |
| 6057 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6058 | input_data->len ) ); |
| 6059 | |
| 6060 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6061 | input_data->len, output_data, |
| 6062 | output_size, &output_length ), |
| 6063 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6064 | |
| 6065 | psa_aead_abort( &operation ); |
| 6066 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6067 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6068 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6069 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6070 | |
| 6071 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6072 | |
| 6073 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6074 | input_data->len ) ); |
| 6075 | |
| 6076 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6077 | additional_data->len ) ); |
| 6078 | |
| 6079 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6080 | finish_output_size, |
| 6081 | &output_part_length, |
| 6082 | tag_buffer, tag_length, |
| 6083 | &tag_size ), |
| 6084 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6085 | |
| 6086 | psa_aead_abort( &operation ); |
| 6087 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6088 | /* Test for sending too much additional data after setting lengths. */ |
| 6089 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6090 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6091 | |
| 6092 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6093 | |
| 6094 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6095 | |
| 6096 | |
| 6097 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6098 | additional_data->len ), |
| 6099 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6100 | |
| 6101 | psa_aead_abort( &operation ); |
| 6102 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6103 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6104 | |
| 6105 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6106 | |
| 6107 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6108 | |
| 6109 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6110 | input_data->len ) ); |
| 6111 | |
| 6112 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6113 | additional_data->len ) ); |
| 6114 | |
| 6115 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6116 | 1 ), |
| 6117 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6118 | |
| 6119 | psa_aead_abort( &operation ); |
| 6120 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6121 | /* Test for sending too much data after setting lengths. */ |
| 6122 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +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 | |
| 6127 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6128 | |
| 6129 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6130 | input_data->len, output_data, |
| 6131 | output_size, &output_length ), |
| 6132 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6133 | |
| 6134 | psa_aead_abort( &operation ); |
| 6135 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6136 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6137 | |
| 6138 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6139 | |
| 6140 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6141 | |
| 6142 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6143 | input_data->len ) ); |
| 6144 | |
| 6145 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6146 | additional_data->len ) ); |
| 6147 | |
| 6148 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6149 | input_data->len, output_data, |
| 6150 | output_size, &output_length ) ); |
| 6151 | |
| 6152 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6153 | 1, output_data, |
| 6154 | output_size, &output_length ), |
| 6155 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6156 | |
| 6157 | psa_aead_abort( &operation ); |
| 6158 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6159 | /* Test sending additional data after data. */ |
| 6160 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6161 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6162 | |
| 6163 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6164 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6165 | if( operation.alg != PSA_ALG_CCM ) |
| 6166 | { |
| 6167 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6168 | input_data->len, output_data, |
| 6169 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6170 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6171 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6172 | additional_data->len ), |
| 6173 | PSA_ERROR_BAD_STATE ); |
| 6174 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6175 | psa_aead_abort( &operation ); |
| 6176 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6177 | /* Test calling finish on decryption. */ |
| 6178 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6179 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6180 | |
| 6181 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6182 | |
| 6183 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6184 | finish_output_size, |
| 6185 | &output_part_length, |
| 6186 | tag_buffer, tag_length, |
| 6187 | &tag_size ), |
| 6188 | PSA_ERROR_BAD_STATE ); |
| 6189 | |
| 6190 | psa_aead_abort( &operation ); |
| 6191 | |
| 6192 | /* Test calling verify on encryption. */ |
| 6193 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6194 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6195 | |
| 6196 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6197 | |
| 6198 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6199 | finish_output_size, |
| 6200 | &output_part_length, |
| 6201 | tag_buffer, |
| 6202 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6203 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6204 | |
| 6205 | psa_aead_abort( &operation ); |
| 6206 | |
| 6207 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6208 | exit: |
| 6209 | psa_destroy_key( key ); |
| 6210 | psa_aead_abort( &operation ); |
| 6211 | mbedtls_free( output_data ); |
| 6212 | mbedtls_free( final_data ); |
| 6213 | PSA_DONE( ); |
| 6214 | } |
| 6215 | /* END_CASE */ |
| 6216 | |
| 6217 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6218 | void signature_size( int type_arg, |
| 6219 | int bits, |
| 6220 | int alg_arg, |
| 6221 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6222 | { |
| 6223 | psa_key_type_t type = type_arg; |
| 6224 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6225 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6226 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6227 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6228 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6229 | exit: |
| 6230 | ; |
| 6231 | } |
| 6232 | /* END_CASE */ |
| 6233 | |
| 6234 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6235 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6236 | int alg_arg, data_t *input_data, |
| 6237 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6238 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6239 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6240 | psa_key_type_t key_type = key_type_arg; |
| 6241 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6242 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6243 | unsigned char *signature = NULL; |
| 6244 | size_t signature_size; |
| 6245 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6246 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6247 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6248 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6249 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6250 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6251 | psa_set_key_algorithm( &attributes, alg ); |
| 6252 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6253 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6254 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6255 | &key ) ); |
| 6256 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6257 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6258 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6259 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6260 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6261 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6262 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6263 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6264 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6265 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6266 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6267 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6268 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6269 | input_data->x, input_data->len, |
| 6270 | signature, signature_size, |
| 6271 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6272 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6273 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6274 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6275 | |
| 6276 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6277 | /* |
| 6278 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6279 | * thus reset them as required. |
| 6280 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6281 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6282 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6283 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6284 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6285 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6286 | } |
| 6287 | /* END_CASE */ |
| 6288 | |
| 6289 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6290 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6291 | int alg_arg, data_t *input_data, |
| 6292 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6293 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6294 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6295 | psa_key_type_t key_type = key_type_arg; |
| 6296 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6297 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6298 | psa_status_t actual_status; |
| 6299 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6300 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6301 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6302 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6303 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6304 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6305 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6306 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6307 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6308 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6309 | psa_set_key_algorithm( &attributes, alg ); |
| 6310 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6311 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6312 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6313 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6314 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6315 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6316 | input_data->x, input_data->len, |
| 6317 | signature, signature_size, |
| 6318 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6319 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6320 | /* The value of *signature_length is unspecified on error, but |
| 6321 | * whatever it is, it should be less than signature_size, so that |
| 6322 | * if the caller tries to read *signature_length bytes without |
| 6323 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6324 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6325 | |
| 6326 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6327 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6328 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6329 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6330 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6331 | } |
| 6332 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6333 | |
| 6334 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6335 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6336 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6337 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6338 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6339 | psa_key_type_t key_type = key_type_arg; |
| 6340 | psa_algorithm_t alg = alg_arg; |
| 6341 | size_t key_bits; |
| 6342 | unsigned char *signature = NULL; |
| 6343 | size_t signature_size; |
| 6344 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6345 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6346 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6347 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6348 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6349 | 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] | 6350 | psa_set_key_algorithm( &attributes, alg ); |
| 6351 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6352 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6353 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6354 | &key ) ); |
| 6355 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6356 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6357 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6358 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6359 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6360 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6361 | key_bits, alg ); |
| 6362 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6363 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6364 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6365 | |
| 6366 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6367 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6368 | input_data->x, input_data->len, |
| 6369 | signature, signature_size, |
| 6370 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6371 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6372 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6373 | TEST_ASSERT( signature_length > 0 ); |
| 6374 | |
| 6375 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6376 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6377 | input_data->x, input_data->len, |
| 6378 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6379 | |
| 6380 | if( input_data->len != 0 ) |
| 6381 | { |
| 6382 | /* Flip a bit in the input and verify that the signature is now |
| 6383 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6384 | * because ECDSA may ignore the last few bits of the input. */ |
| 6385 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6386 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6387 | input_data->x, input_data->len, |
| 6388 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6389 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6390 | } |
| 6391 | |
| 6392 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6393 | /* |
| 6394 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6395 | * thus reset them as required. |
| 6396 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6397 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6398 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6399 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6400 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6401 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6402 | } |
| 6403 | /* END_CASE */ |
| 6404 | |
| 6405 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6406 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6407 | int alg_arg, data_t *hash_data, |
| 6408 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6409 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6410 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6411 | psa_key_type_t key_type = key_type_arg; |
| 6412 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6413 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6414 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6415 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6416 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6417 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6418 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6419 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6420 | psa_set_key_algorithm( &attributes, alg ); |
| 6421 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6422 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6423 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6424 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6425 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6426 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6427 | hash_data->x, hash_data->len, |
| 6428 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6429 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6430 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6431 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6432 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6433 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6434 | } |
| 6435 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6436 | |
| 6437 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6438 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6439 | int alg_arg, data_t *hash_data, |
| 6440 | data_t *signature_data, |
| 6441 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6442 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6443 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6444 | psa_key_type_t key_type = key_type_arg; |
| 6445 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6446 | psa_status_t actual_status; |
| 6447 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6448 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6449 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6450 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6451 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6452 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6453 | psa_set_key_algorithm( &attributes, alg ); |
| 6454 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6455 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6456 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6457 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6458 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6459 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6460 | hash_data->x, hash_data->len, |
| 6461 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6462 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6463 | |
| 6464 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6465 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6466 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6467 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6468 | } |
| 6469 | /* END_CASE */ |
| 6470 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6471 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6472 | void sign_message_deterministic( int key_type_arg, |
| 6473 | data_t *key_data, |
| 6474 | int alg_arg, |
| 6475 | data_t *input_data, |
| 6476 | data_t *output_data ) |
| 6477 | { |
| 6478 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6479 | psa_key_type_t key_type = key_type_arg; |
| 6480 | psa_algorithm_t alg = alg_arg; |
| 6481 | size_t key_bits; |
| 6482 | unsigned char *signature = NULL; |
| 6483 | size_t signature_size; |
| 6484 | size_t signature_length = 0xdeadbeef; |
| 6485 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6486 | |
| 6487 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6488 | |
| 6489 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6490 | psa_set_key_algorithm( &attributes, alg ); |
| 6491 | psa_set_key_type( &attributes, key_type ); |
| 6492 | |
| 6493 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6494 | &key ) ); |
| 6495 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6496 | key_bits = psa_get_key_bits( &attributes ); |
| 6497 | |
| 6498 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6499 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6500 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6501 | ASSERT_ALLOC( signature, signature_size ); |
| 6502 | |
| 6503 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6504 | input_data->x, input_data->len, |
| 6505 | signature, signature_size, |
| 6506 | &signature_length ) ); |
| 6507 | |
| 6508 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6509 | signature, signature_length ); |
| 6510 | |
| 6511 | exit: |
| 6512 | psa_reset_key_attributes( &attributes ); |
| 6513 | |
| 6514 | psa_destroy_key( key ); |
| 6515 | mbedtls_free( signature ); |
| 6516 | PSA_DONE( ); |
| 6517 | |
| 6518 | } |
| 6519 | /* END_CASE */ |
| 6520 | |
| 6521 | /* BEGIN_CASE */ |
| 6522 | void sign_message_fail( int key_type_arg, |
| 6523 | data_t *key_data, |
| 6524 | int alg_arg, |
| 6525 | data_t *input_data, |
| 6526 | int signature_size_arg, |
| 6527 | int expected_status_arg ) |
| 6528 | { |
| 6529 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6530 | psa_key_type_t key_type = key_type_arg; |
| 6531 | psa_algorithm_t alg = alg_arg; |
| 6532 | size_t signature_size = signature_size_arg; |
| 6533 | psa_status_t actual_status; |
| 6534 | psa_status_t expected_status = expected_status_arg; |
| 6535 | unsigned char *signature = NULL; |
| 6536 | size_t signature_length = 0xdeadbeef; |
| 6537 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6538 | |
| 6539 | ASSERT_ALLOC( signature, signature_size ); |
| 6540 | |
| 6541 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6542 | |
| 6543 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6544 | psa_set_key_algorithm( &attributes, alg ); |
| 6545 | psa_set_key_type( &attributes, key_type ); |
| 6546 | |
| 6547 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6548 | &key ) ); |
| 6549 | |
| 6550 | actual_status = psa_sign_message( key, alg, |
| 6551 | input_data->x, input_data->len, |
| 6552 | signature, signature_size, |
| 6553 | &signature_length ); |
| 6554 | TEST_EQUAL( actual_status, expected_status ); |
| 6555 | /* The value of *signature_length is unspecified on error, but |
| 6556 | * whatever it is, it should be less than signature_size, so that |
| 6557 | * if the caller tries to read *signature_length bytes without |
| 6558 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6559 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6560 | |
| 6561 | exit: |
| 6562 | psa_reset_key_attributes( &attributes ); |
| 6563 | psa_destroy_key( key ); |
| 6564 | mbedtls_free( signature ); |
| 6565 | PSA_DONE( ); |
| 6566 | } |
| 6567 | /* END_CASE */ |
| 6568 | |
| 6569 | /* BEGIN_CASE */ |
| 6570 | void sign_verify_message( int key_type_arg, |
| 6571 | data_t *key_data, |
| 6572 | int alg_arg, |
| 6573 | data_t *input_data ) |
| 6574 | { |
| 6575 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6576 | psa_key_type_t key_type = key_type_arg; |
| 6577 | psa_algorithm_t alg = alg_arg; |
| 6578 | size_t key_bits; |
| 6579 | unsigned char *signature = NULL; |
| 6580 | size_t signature_size; |
| 6581 | size_t signature_length = 0xdeadbeef; |
| 6582 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6583 | |
| 6584 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6585 | |
| 6586 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6587 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6588 | psa_set_key_algorithm( &attributes, alg ); |
| 6589 | psa_set_key_type( &attributes, key_type ); |
| 6590 | |
| 6591 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6592 | &key ) ); |
| 6593 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6594 | key_bits = psa_get_key_bits( &attributes ); |
| 6595 | |
| 6596 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6597 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6598 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6599 | ASSERT_ALLOC( signature, signature_size ); |
| 6600 | |
| 6601 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6602 | input_data->x, input_data->len, |
| 6603 | signature, signature_size, |
| 6604 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6605 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6606 | TEST_ASSERT( signature_length > 0 ); |
| 6607 | |
| 6608 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6609 | input_data->x, input_data->len, |
| 6610 | signature, signature_length ) ); |
| 6611 | |
| 6612 | if( input_data->len != 0 ) |
| 6613 | { |
| 6614 | /* Flip a bit in the input and verify that the signature is now |
| 6615 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6616 | * because ECDSA may ignore the last few bits of the input. */ |
| 6617 | input_data->x[0] ^= 1; |
| 6618 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6619 | input_data->x, input_data->len, |
| 6620 | signature, signature_length ), |
| 6621 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6622 | } |
| 6623 | |
| 6624 | exit: |
| 6625 | psa_reset_key_attributes( &attributes ); |
| 6626 | |
| 6627 | psa_destroy_key( key ); |
| 6628 | mbedtls_free( signature ); |
| 6629 | PSA_DONE( ); |
| 6630 | } |
| 6631 | /* END_CASE */ |
| 6632 | |
| 6633 | /* BEGIN_CASE */ |
| 6634 | void verify_message( int key_type_arg, |
| 6635 | data_t *key_data, |
| 6636 | int alg_arg, |
| 6637 | data_t *input_data, |
| 6638 | data_t *signature_data ) |
| 6639 | { |
| 6640 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6641 | psa_key_type_t key_type = key_type_arg; |
| 6642 | psa_algorithm_t alg = alg_arg; |
| 6643 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6644 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6645 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6646 | |
| 6647 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6648 | |
| 6649 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6650 | psa_set_key_algorithm( &attributes, alg ); |
| 6651 | psa_set_key_type( &attributes, key_type ); |
| 6652 | |
| 6653 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6654 | &key ) ); |
| 6655 | |
| 6656 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6657 | input_data->x, input_data->len, |
| 6658 | signature_data->x, signature_data->len ) ); |
| 6659 | |
| 6660 | exit: |
| 6661 | psa_reset_key_attributes( &attributes ); |
| 6662 | psa_destroy_key( key ); |
| 6663 | PSA_DONE( ); |
| 6664 | } |
| 6665 | /* END_CASE */ |
| 6666 | |
| 6667 | /* BEGIN_CASE */ |
| 6668 | void verify_message_fail( int key_type_arg, |
| 6669 | data_t *key_data, |
| 6670 | int alg_arg, |
| 6671 | data_t *hash_data, |
| 6672 | data_t *signature_data, |
| 6673 | int expected_status_arg ) |
| 6674 | { |
| 6675 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6676 | psa_key_type_t key_type = key_type_arg; |
| 6677 | psa_algorithm_t alg = alg_arg; |
| 6678 | psa_status_t actual_status; |
| 6679 | psa_status_t expected_status = expected_status_arg; |
| 6680 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6681 | |
| 6682 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6683 | |
| 6684 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6685 | psa_set_key_algorithm( &attributes, alg ); |
| 6686 | psa_set_key_type( &attributes, key_type ); |
| 6687 | |
| 6688 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6689 | &key ) ); |
| 6690 | |
| 6691 | actual_status = psa_verify_message( key, alg, |
| 6692 | hash_data->x, hash_data->len, |
| 6693 | signature_data->x, |
| 6694 | signature_data->len ); |
| 6695 | TEST_EQUAL( actual_status, expected_status ); |
| 6696 | |
| 6697 | exit: |
| 6698 | psa_reset_key_attributes( &attributes ); |
| 6699 | psa_destroy_key( key ); |
| 6700 | PSA_DONE( ); |
| 6701 | } |
| 6702 | /* END_CASE */ |
| 6703 | |
| 6704 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6705 | void asymmetric_encrypt( int key_type_arg, |
| 6706 | data_t *key_data, |
| 6707 | int alg_arg, |
| 6708 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6709 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6710 | int expected_output_length_arg, |
| 6711 | int expected_status_arg ) |
| 6712 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6713 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6714 | psa_key_type_t key_type = key_type_arg; |
| 6715 | psa_algorithm_t alg = alg_arg; |
| 6716 | size_t expected_output_length = expected_output_length_arg; |
| 6717 | size_t key_bits; |
| 6718 | unsigned char *output = NULL; |
| 6719 | size_t output_size; |
| 6720 | size_t output_length = ~0; |
| 6721 | psa_status_t actual_status; |
| 6722 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6723 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6724 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6725 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 6726 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6727 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6728 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 6729 | psa_set_key_algorithm( &attributes, alg ); |
| 6730 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6731 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6732 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6733 | |
| 6734 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6735 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6736 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6737 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6738 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6739 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6740 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6741 | |
| 6742 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6743 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6744 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6745 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6746 | output, output_size, |
| 6747 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6748 | TEST_EQUAL( actual_status, expected_status ); |
| 6749 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6750 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6751 | /* If the label is empty, the test framework puts a non-null pointer |
| 6752 | * in label->x. Test that a null pointer works as well. */ |
| 6753 | if( label->len == 0 ) |
| 6754 | { |
| 6755 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6756 | if( output_size != 0 ) |
| 6757 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6758 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6759 | input_data->x, input_data->len, |
| 6760 | NULL, label->len, |
| 6761 | output, output_size, |
| 6762 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6763 | TEST_EQUAL( actual_status, expected_status ); |
| 6764 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6765 | } |
| 6766 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6767 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6768 | /* |
| 6769 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6770 | * thus reset them as required. |
| 6771 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6772 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6773 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6774 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6775 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6776 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6777 | } |
| 6778 | /* END_CASE */ |
| 6779 | |
| 6780 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6781 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 6782 | data_t *key_data, |
| 6783 | int alg_arg, |
| 6784 | data_t *input_data, |
| 6785 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6786 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6787 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6788 | psa_key_type_t key_type = key_type_arg; |
| 6789 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6790 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6791 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6792 | size_t output_size; |
| 6793 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 6794 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6795 | size_t output2_size; |
| 6796 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6797 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6798 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6799 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6800 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6801 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 6802 | psa_set_key_algorithm( &attributes, alg ); |
| 6803 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6804 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6805 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6806 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6807 | |
| 6808 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6809 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6810 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6811 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6812 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6813 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6814 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6815 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6816 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6817 | TEST_LE_U( output2_size, |
| 6818 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 6819 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6820 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6821 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 6822 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 6823 | * the original plaintext because of the non-optional random |
| 6824 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6825 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6826 | input_data->x, input_data->len, |
| 6827 | label->x, label->len, |
| 6828 | output, output_size, |
| 6829 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6830 | /* We don't know what ciphertext length to expect, but check that |
| 6831 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6832 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 6833 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6834 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6835 | output, output_length, |
| 6836 | label->x, label->len, |
| 6837 | output2, output2_size, |
| 6838 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6839 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 6840 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6841 | |
| 6842 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6843 | /* |
| 6844 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6845 | * thus reset them as required. |
| 6846 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6847 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6848 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6849 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 6850 | mbedtls_free( output ); |
| 6851 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6852 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6853 | } |
| 6854 | /* END_CASE */ |
| 6855 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6856 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6857 | void asymmetric_decrypt( int key_type_arg, |
| 6858 | data_t *key_data, |
| 6859 | int alg_arg, |
| 6860 | data_t *input_data, |
| 6861 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 6862 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6863 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6864 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6865 | psa_key_type_t key_type = key_type_arg; |
| 6866 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6867 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6868 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 6869 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6870 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6871 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6872 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6873 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6874 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6875 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 6876 | psa_set_key_algorithm( &attributes, alg ); |
| 6877 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6878 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6879 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6880 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6881 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6882 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6883 | key_bits = psa_get_key_bits( &attributes ); |
| 6884 | |
| 6885 | /* Determine the maximum ciphertext length */ |
| 6886 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6887 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6888 | ASSERT_ALLOC( output, output_size ); |
| 6889 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6890 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6891 | input_data->x, input_data->len, |
| 6892 | label->x, label->len, |
| 6893 | output, |
| 6894 | output_size, |
| 6895 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6896 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 6897 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6898 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6899 | /* If the label is empty, the test framework puts a non-null pointer |
| 6900 | * in label->x. Test that a null pointer works as well. */ |
| 6901 | if( label->len == 0 ) |
| 6902 | { |
| 6903 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6904 | if( output_size != 0 ) |
| 6905 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6906 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6907 | input_data->x, input_data->len, |
| 6908 | NULL, label->len, |
| 6909 | output, |
| 6910 | output_size, |
| 6911 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6912 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 6913 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6914 | } |
| 6915 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6916 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6917 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6918 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 6919 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6920 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6921 | } |
| 6922 | /* END_CASE */ |
| 6923 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6924 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6925 | void asymmetric_decrypt_fail( int key_type_arg, |
| 6926 | data_t *key_data, |
| 6927 | int alg_arg, |
| 6928 | data_t *input_data, |
| 6929 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 6930 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6931 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6932 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6933 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6934 | psa_key_type_t key_type = key_type_arg; |
| 6935 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6936 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 6937 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6938 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6939 | psa_status_t actual_status; |
| 6940 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6941 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6942 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6943 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 6944 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6945 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6946 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6947 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 6948 | psa_set_key_algorithm( &attributes, alg ); |
| 6949 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6950 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6951 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6952 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6953 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6954 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 6955 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6956 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 6957 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6958 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6959 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6960 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6961 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6962 | /* If the label is empty, the test framework puts a non-null pointer |
| 6963 | * in label->x. Test that a null pointer works as well. */ |
| 6964 | if( label->len == 0 ) |
| 6965 | { |
| 6966 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6967 | if( output_size != 0 ) |
| 6968 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6969 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6970 | input_data->x, input_data->len, |
| 6971 | NULL, label->len, |
| 6972 | output, output_size, |
| 6973 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6974 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6975 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6976 | } |
| 6977 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6978 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6979 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6980 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6981 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6982 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6983 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 6984 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6985 | |
| 6986 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 6987 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 6988 | { |
| 6989 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 6990 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 6991 | * 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] | 6992 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 6993 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6994 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 6995 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6996 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 6997 | |
| 6998 | memset( &zero, 0, sizeof( zero ) ); |
| 6999 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7000 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7001 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7002 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7003 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7004 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7005 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7006 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7007 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7008 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7009 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7010 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7011 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7012 | } |
| 7013 | /* END_CASE */ |
| 7014 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7015 | /* BEGIN_CASE */ |
| 7016 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7017 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7018 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7019 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7020 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7021 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7022 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7023 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7024 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7025 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7026 | |
| 7027 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7028 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7029 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7030 | } |
| 7031 | /* END_CASE */ |
| 7032 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7033 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7034 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7035 | int expected_status_arg ) |
| 7036 | { |
| 7037 | psa_algorithm_t alg = alg_arg; |
| 7038 | size_t capacity = capacity_arg; |
| 7039 | psa_status_t expected_status = expected_status_arg; |
| 7040 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7041 | |
| 7042 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7043 | |
| 7044 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7045 | |
| 7046 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7047 | expected_status ); |
| 7048 | |
| 7049 | exit: |
| 7050 | psa_key_derivation_abort( &operation ); |
| 7051 | PSA_DONE( ); |
| 7052 | } |
| 7053 | /* END_CASE */ |
| 7054 | |
| 7055 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7056 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7057 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7058 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7059 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7060 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7061 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7062 | int expected_status_arg3, |
| 7063 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7064 | { |
| 7065 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7066 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7067 | 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] | 7068 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7069 | expected_status_arg2, |
| 7070 | expected_status_arg3}; |
| 7071 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7072 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7073 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7074 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7075 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7076 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7077 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7078 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7079 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7080 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7081 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7082 | |
| 7083 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7084 | |
| 7085 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7086 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7087 | |
| 7088 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7089 | |
| 7090 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7091 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7092 | mbedtls_test_set_step( i ); |
| 7093 | if( steps[i] == 0 ) |
| 7094 | { |
| 7095 | /* Skip this step */ |
| 7096 | } |
| 7097 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7098 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7099 | psa_set_key_type( &attributes, key_types[i] ); |
| 7100 | PSA_ASSERT( psa_import_key( &attributes, |
| 7101 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7102 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7103 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7104 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7105 | { |
| 7106 | // When taking a private key as secret input, use key agreement |
| 7107 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7108 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7109 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7110 | expected_statuses[i] ); |
| 7111 | } |
| 7112 | else |
| 7113 | { |
| 7114 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7115 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7116 | expected_statuses[i] ); |
| 7117 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7118 | } |
| 7119 | else |
| 7120 | { |
| 7121 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7122 | &operation, steps[i], |
| 7123 | inputs[i]->x, inputs[i]->len ), |
| 7124 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7125 | } |
| 7126 | } |
| 7127 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7128 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7129 | { |
| 7130 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7131 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7132 | psa_set_key_bits( &attributes, 8 ); |
| 7133 | actual_output_status = |
| 7134 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7135 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7136 | } |
| 7137 | else |
| 7138 | { |
| 7139 | uint8_t buffer[1]; |
| 7140 | actual_output_status = |
| 7141 | psa_key_derivation_output_bytes( &operation, |
| 7142 | buffer, sizeof( buffer ) ); |
| 7143 | } |
| 7144 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7145 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7146 | exit: |
| 7147 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7148 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7149 | psa_destroy_key( keys[i] ); |
| 7150 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7151 | PSA_DONE( ); |
| 7152 | } |
| 7153 | /* END_CASE */ |
| 7154 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7155 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7156 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7157 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7158 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7159 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7160 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7161 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7162 | unsigned char input1[] = "Input 1"; |
| 7163 | size_t input1_length = sizeof( input1 ); |
| 7164 | unsigned char input2[] = "Input 2"; |
| 7165 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7166 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7167 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7168 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7169 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7170 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7171 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7172 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7173 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7174 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7175 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7176 | psa_set_key_algorithm( &attributes, alg ); |
| 7177 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7178 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7179 | PSA_ASSERT( psa_import_key( &attributes, |
| 7180 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7181 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7182 | |
| 7183 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7184 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7185 | input1, input1_length, |
| 7186 | input2, input2_length, |
| 7187 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7188 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7189 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7190 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7191 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7192 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7193 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7194 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7195 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7196 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7197 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7198 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7199 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7200 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7201 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7202 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7203 | } |
| 7204 | /* END_CASE */ |
| 7205 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7206 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7207 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7208 | { |
| 7209 | uint8_t output_buffer[16]; |
| 7210 | size_t buffer_size = 16; |
| 7211 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7212 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7213 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7214 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7215 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7216 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7217 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7218 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7219 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7220 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7221 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7222 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7223 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7224 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7225 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7226 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7227 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7228 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7229 | |
| 7230 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7231 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7232 | } |
| 7233 | /* END_CASE */ |
| 7234 | |
| 7235 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7236 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7237 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7238 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7239 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7240 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7241 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7242 | int requested_capacity_arg, |
| 7243 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7244 | data_t *expected_output2, |
| 7245 | int other_key_input_type, |
| 7246 | int key_input_type, |
| 7247 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7248 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7249 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7250 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7251 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7252 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7253 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7254 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7255 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7256 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7257 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7258 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7259 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7260 | uint8_t *expected_outputs[2] = |
| 7261 | {expected_output1->x, expected_output2->x}; |
| 7262 | size_t output_sizes[2] = |
| 7263 | {expected_output1->len, expected_output2->len}; |
| 7264 | size_t output_buffer_size = 0; |
| 7265 | uint8_t *output_buffer = NULL; |
| 7266 | size_t expected_capacity; |
| 7267 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7268 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7269 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7270 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7271 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7272 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7273 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7274 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7275 | |
| 7276 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7277 | { |
| 7278 | if( output_sizes[i] > output_buffer_size ) |
| 7279 | output_buffer_size = output_sizes[i]; |
| 7280 | if( output_sizes[i] == 0 ) |
| 7281 | expected_outputs[i] = NULL; |
| 7282 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7283 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7284 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7285 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7286 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7287 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7288 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7289 | requested_capacity ) ); |
| 7290 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7291 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7292 | switch( steps[i] ) |
| 7293 | { |
| 7294 | case 0: |
| 7295 | break; |
| 7296 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7297 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7298 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7299 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7300 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7301 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7302 | inputs[i]->x, inputs[i]->len ), |
| 7303 | statuses[i] ); |
| 7304 | |
| 7305 | if( statuses[i] != PSA_SUCCESS ) |
| 7306 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7307 | break; |
| 7308 | case 1: // input key |
| 7309 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7310 | psa_set_key_algorithm( &attributes1, alg ); |
| 7311 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7312 | |
| 7313 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7314 | inputs[i]->x, inputs[i]->len, |
| 7315 | &keys[i] ) ); |
| 7316 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7317 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7318 | { |
| 7319 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7320 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7321 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7322 | } |
| 7323 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7324 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7325 | steps[i], |
| 7326 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7327 | break; |
| 7328 | default: |
| 7329 | TEST_ASSERT( ! "default case not supported" ); |
| 7330 | break; |
| 7331 | } |
| 7332 | break; |
| 7333 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7334 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7335 | { |
| 7336 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7337 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7338 | steps[i], |
| 7339 | inputs[i]->x, |
| 7340 | inputs[i]->len ), |
| 7341 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7342 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7343 | case 1: // input key, type DERIVE |
| 7344 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7345 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7346 | psa_set_key_algorithm( &attributes2, alg ); |
| 7347 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7348 | |
| 7349 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7350 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7351 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7352 | |
| 7353 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7354 | inputs[i]->x, inputs[i]->len, |
| 7355 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7356 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7357 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7358 | steps[i], |
| 7359 | keys[i] ), |
| 7360 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7361 | break; |
| 7362 | case 2: // key agreement |
| 7363 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7364 | psa_set_key_algorithm( &attributes3, alg ); |
| 7365 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7366 | |
| 7367 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7368 | inputs[i]->x, inputs[i]->len, |
| 7369 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7370 | |
| 7371 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7372 | &operation, |
| 7373 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7374 | keys[i], key_agreement_peer_key->x, |
| 7375 | key_agreement_peer_key->len ), statuses[i] ); |
| 7376 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7377 | default: |
| 7378 | TEST_ASSERT( ! "default case not supported" ); |
| 7379 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7380 | } |
| 7381 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7382 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7383 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7384 | break; |
| 7385 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7386 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7387 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7388 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7389 | |
| 7390 | if( statuses[i] != PSA_SUCCESS ) |
| 7391 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7392 | break; |
| 7393 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7394 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7395 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7396 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7397 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7398 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7399 | expected_capacity = requested_capacity; |
| 7400 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7401 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7402 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7403 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7404 | |
| 7405 | /* For output key derivation secret must be provided using |
| 7406 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7407 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7408 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7409 | |
| 7410 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7411 | psa_set_key_algorithm( &attributes4, alg ); |
| 7412 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7413 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7414 | |
| 7415 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7416 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7417 | } |
| 7418 | else // output bytes |
| 7419 | { |
| 7420 | /* Expansion phase. */ |
| 7421 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7422 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7423 | /* Read some bytes. */ |
| 7424 | status = psa_key_derivation_output_bytes( &operation, |
| 7425 | output_buffer, output_sizes[i] ); |
| 7426 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7427 | { |
| 7428 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7429 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7430 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7431 | continue; |
| 7432 | } |
| 7433 | else if( expected_capacity == 0 || |
| 7434 | output_sizes[i] > expected_capacity ) |
| 7435 | { |
| 7436 | /* Capacity exceeded. */ |
| 7437 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7438 | expected_capacity = 0; |
| 7439 | continue; |
| 7440 | } |
| 7441 | /* Success. Check the read data. */ |
| 7442 | PSA_ASSERT( status ); |
| 7443 | if( output_sizes[i] != 0 ) |
| 7444 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7445 | expected_outputs[i], output_sizes[i] ); |
| 7446 | /* Check the operation status. */ |
| 7447 | expected_capacity -= output_sizes[i]; |
| 7448 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7449 | ¤t_capacity ) ); |
| 7450 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7451 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7452 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7453 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7454 | |
| 7455 | exit: |
| 7456 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7457 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7458 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7459 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7460 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7461 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7462 | } |
| 7463 | /* END_CASE */ |
| 7464 | |
| 7465 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7466 | void derive_full( int alg_arg, |
| 7467 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7468 | data_t *input1, |
| 7469 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7470 | int requested_capacity_arg ) |
| 7471 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7472 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7473 | psa_algorithm_t alg = alg_arg; |
| 7474 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7475 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7476 | unsigned char output_buffer[16]; |
| 7477 | size_t expected_capacity = requested_capacity; |
| 7478 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7479 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7480 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7481 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7482 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7483 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7484 | psa_set_key_algorithm( &attributes, alg ); |
| 7485 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7486 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7487 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7488 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7489 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7490 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7491 | input1->x, input1->len, |
| 7492 | input2->x, input2->len, |
| 7493 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7494 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7495 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7496 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7497 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7498 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7499 | |
| 7500 | /* Expansion phase. */ |
| 7501 | while( current_capacity > 0 ) |
| 7502 | { |
| 7503 | size_t read_size = sizeof( output_buffer ); |
| 7504 | if( read_size > current_capacity ) |
| 7505 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7506 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7507 | output_buffer, |
| 7508 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7509 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7510 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7511 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7512 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7513 | } |
| 7514 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7515 | /* Check that the operation refuses to go over capacity. */ |
| 7516 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7517 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7518 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7519 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7520 | |
| 7521 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7522 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7523 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7524 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7525 | } |
| 7526 | /* END_CASE */ |
| 7527 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7528 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7529 | void derive_key_exercise( int alg_arg, |
| 7530 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7531 | data_t *input1, |
| 7532 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7533 | int derived_type_arg, |
| 7534 | int derived_bits_arg, |
| 7535 | int derived_usage_arg, |
| 7536 | int derived_alg_arg ) |
| 7537 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7538 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7539 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7540 | psa_algorithm_t alg = alg_arg; |
| 7541 | psa_key_type_t derived_type = derived_type_arg; |
| 7542 | size_t derived_bits = derived_bits_arg; |
| 7543 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7544 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7545 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7546 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7547 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7548 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7549 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7550 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7551 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7552 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7553 | psa_set_key_algorithm( &attributes, alg ); |
| 7554 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7555 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7556 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7557 | |
| 7558 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7559 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7560 | input1->x, input1->len, |
| 7561 | input2->x, input2->len, |
| 7562 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7563 | goto exit; |
| 7564 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7565 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7566 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7567 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7568 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7569 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7570 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7571 | |
| 7572 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7573 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7574 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7575 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7576 | |
| 7577 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7578 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7579 | goto exit; |
| 7580 | |
| 7581 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7582 | /* |
| 7583 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7584 | * thus reset them as required. |
| 7585 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7586 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7587 | |
| 7588 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7589 | psa_destroy_key( base_key ); |
| 7590 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7591 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7592 | } |
| 7593 | /* END_CASE */ |
| 7594 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7595 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7596 | void derive_key_export( int alg_arg, |
| 7597 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7598 | data_t *input1, |
| 7599 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7600 | int bytes1_arg, |
| 7601 | int bytes2_arg ) |
| 7602 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7603 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7604 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7605 | psa_algorithm_t alg = alg_arg; |
| 7606 | size_t bytes1 = bytes1_arg; |
| 7607 | size_t bytes2 = bytes2_arg; |
| 7608 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7609 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7610 | uint8_t *output_buffer = NULL; |
| 7611 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7612 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7613 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7614 | size_t length; |
| 7615 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7616 | ASSERT_ALLOC( output_buffer, capacity ); |
| 7617 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7618 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7619 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7620 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7621 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7622 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7623 | 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] | 7624 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7625 | |
| 7626 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7627 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7628 | input1->x, input1->len, |
| 7629 | input2->x, input2->len, |
| 7630 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7631 | goto exit; |
| 7632 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7633 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7634 | output_buffer, |
| 7635 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7636 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7637 | |
| 7638 | /* 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] | 7639 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7640 | input1->x, input1->len, |
| 7641 | input2->x, input2->len, |
| 7642 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7643 | goto exit; |
| 7644 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7645 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7646 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7647 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7648 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7649 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7650 | &derived_key ) ); |
| 7651 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7652 | export_buffer, bytes1, |
| 7653 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7654 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7655 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7656 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7657 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7658 | &derived_key ) ); |
| 7659 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7660 | export_buffer + bytes1, bytes2, |
| 7661 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7662 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7663 | |
| 7664 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 7665 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 7666 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7667 | |
| 7668 | exit: |
| 7669 | mbedtls_free( output_buffer ); |
| 7670 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7671 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7672 | psa_destroy_key( base_key ); |
| 7673 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7674 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7675 | } |
| 7676 | /* END_CASE */ |
| 7677 | |
| 7678 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7679 | void derive_key_type( int alg_arg, |
| 7680 | data_t *key_data, |
| 7681 | data_t *input1, |
| 7682 | data_t *input2, |
| 7683 | int key_type_arg, int bits_arg, |
| 7684 | data_t *expected_export ) |
| 7685 | { |
| 7686 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7687 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7688 | const psa_algorithm_t alg = alg_arg; |
| 7689 | const psa_key_type_t key_type = key_type_arg; |
| 7690 | const size_t bits = bits_arg; |
| 7691 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7692 | const size_t export_buffer_size = |
| 7693 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 7694 | uint8_t *export_buffer = NULL; |
| 7695 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7696 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7697 | size_t export_length; |
| 7698 | |
| 7699 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 7700 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7701 | |
| 7702 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7703 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7704 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 7705 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 7706 | &base_key ) ); |
| 7707 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 7708 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7709 | &operation, base_key, alg, |
| 7710 | input1->x, input1->len, |
| 7711 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 7712 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7713 | goto exit; |
| 7714 | |
| 7715 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7716 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7717 | psa_set_key_type( &derived_attributes, key_type ); |
| 7718 | psa_set_key_bits( &derived_attributes, bits ); |
| 7719 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 7720 | &derived_key ) ); |
| 7721 | |
| 7722 | PSA_ASSERT( psa_export_key( derived_key, |
| 7723 | export_buffer, export_buffer_size, |
| 7724 | &export_length ) ); |
| 7725 | ASSERT_COMPARE( export_buffer, export_length, |
| 7726 | expected_export->x, expected_export->len ); |
| 7727 | |
| 7728 | exit: |
| 7729 | mbedtls_free( export_buffer ); |
| 7730 | psa_key_derivation_abort( &operation ); |
| 7731 | psa_destroy_key( base_key ); |
| 7732 | psa_destroy_key( derived_key ); |
| 7733 | PSA_DONE( ); |
| 7734 | } |
| 7735 | /* END_CASE */ |
| 7736 | |
| 7737 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 7738 | void derive_key( int alg_arg, |
| 7739 | data_t *key_data, data_t *input1, data_t *input2, |
| 7740 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 7741 | int expected_status_arg, |
| 7742 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7743 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7744 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7745 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7746 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 7747 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7748 | size_t bits = bits_arg; |
| 7749 | psa_status_t expected_status = expected_status_arg; |
| 7750 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7751 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7752 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7753 | |
| 7754 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7755 | |
| 7756 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7757 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7758 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 7759 | 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] | 7760 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7761 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7762 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7763 | input1->x, input1->len, |
| 7764 | input2->x, input2->len, |
| 7765 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7766 | goto exit; |
| 7767 | |
| 7768 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7769 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 7770 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7771 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 7772 | |
| 7773 | psa_status_t status = |
| 7774 | psa_key_derivation_output_key( &derived_attributes, |
| 7775 | &operation, |
| 7776 | &derived_key ); |
| 7777 | if( is_large_output > 0 ) |
| 7778 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 7779 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7780 | |
| 7781 | exit: |
| 7782 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7783 | psa_destroy_key( base_key ); |
| 7784 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7785 | PSA_DONE( ); |
| 7786 | } |
| 7787 | /* END_CASE */ |
| 7788 | |
| 7789 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7790 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 7791 | int our_key_type_arg, int our_key_alg_arg, |
| 7792 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7793 | int expected_status_arg ) |
| 7794 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7795 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7796 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7797 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7798 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7799 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7800 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7801 | psa_status_t expected_status = expected_status_arg; |
| 7802 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7803 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7804 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7805 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7806 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7807 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7808 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7809 | PSA_ASSERT( psa_import_key( &attributes, |
| 7810 | our_key_data->x, our_key_data->len, |
| 7811 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7812 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7813 | /* The tests currently include inputs that should fail at either step. |
| 7814 | * Test cases that fail at the setup step should be changed to call |
| 7815 | * key_derivation_setup instead, and this function should be renamed |
| 7816 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7817 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7818 | if( status == PSA_SUCCESS ) |
| 7819 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7820 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7821 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 7822 | our_key, |
| 7823 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7824 | expected_status ); |
| 7825 | } |
| 7826 | else |
| 7827 | { |
| 7828 | TEST_ASSERT( status == expected_status ); |
| 7829 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7830 | |
| 7831 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7832 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7833 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7834 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7835 | } |
| 7836 | /* END_CASE */ |
| 7837 | |
| 7838 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7839 | void raw_key_agreement( int alg_arg, |
| 7840 | int our_key_type_arg, data_t *our_key_data, |
| 7841 | data_t *peer_key_data, |
| 7842 | data_t *expected_output ) |
| 7843 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7844 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7845 | psa_algorithm_t alg = alg_arg; |
| 7846 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7847 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7848 | unsigned char *output = NULL; |
| 7849 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7850 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7851 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7852 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7853 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7854 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7855 | psa_set_key_algorithm( &attributes, alg ); |
| 7856 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7857 | PSA_ASSERT( psa_import_key( &attributes, |
| 7858 | our_key_data->x, our_key_data->len, |
| 7859 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7860 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7861 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 7862 | key_bits = psa_get_key_bits( &attributes ); |
| 7863 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 7864 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7865 | TEST_LE_U( expected_output->len, |
| 7866 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 7867 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 7868 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 7869 | |
| 7870 | /* Good case with exact output size */ |
| 7871 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 7872 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 7873 | peer_key_data->x, peer_key_data->len, |
| 7874 | output, expected_output->len, |
| 7875 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7876 | ASSERT_COMPARE( output, output_length, |
| 7877 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 7878 | mbedtls_free( output ); |
| 7879 | output = NULL; |
| 7880 | output_length = ~0; |
| 7881 | |
| 7882 | /* Larger buffer */ |
| 7883 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 7884 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 7885 | peer_key_data->x, peer_key_data->len, |
| 7886 | output, expected_output->len + 1, |
| 7887 | &output_length ) ); |
| 7888 | ASSERT_COMPARE( output, output_length, |
| 7889 | expected_output->x, expected_output->len ); |
| 7890 | mbedtls_free( output ); |
| 7891 | output = NULL; |
| 7892 | output_length = ~0; |
| 7893 | |
| 7894 | /* Buffer too small */ |
| 7895 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 7896 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 7897 | peer_key_data->x, peer_key_data->len, |
| 7898 | output, expected_output->len - 1, |
| 7899 | &output_length ), |
| 7900 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 7901 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7902 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 7903 | mbedtls_free( output ); |
| 7904 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7905 | |
| 7906 | exit: |
| 7907 | mbedtls_free( output ); |
| 7908 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7909 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7910 | } |
| 7911 | /* END_CASE */ |
| 7912 | |
| 7913 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7914 | void key_agreement_capacity( int alg_arg, |
| 7915 | int our_key_type_arg, data_t *our_key_data, |
| 7916 | data_t *peer_key_data, |
| 7917 | int expected_capacity_arg ) |
| 7918 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7919 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7920 | psa_algorithm_t alg = alg_arg; |
| 7921 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7922 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7923 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7924 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7925 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7926 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7927 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7928 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7929 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7930 | psa_set_key_algorithm( &attributes, alg ); |
| 7931 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7932 | PSA_ASSERT( psa_import_key( &attributes, |
| 7933 | our_key_data->x, our_key_data->len, |
| 7934 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7935 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7936 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7937 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 7938 | &operation, |
| 7939 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 7940 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 7941 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 7942 | { |
| 7943 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7944 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 7945 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 7946 | NULL, 0 ) ); |
| 7947 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7948 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7949 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7950 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7951 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7952 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7953 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7954 | /* Test the actual capacity by reading the output. */ |
| 7955 | while( actual_capacity > sizeof( output ) ) |
| 7956 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7957 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7958 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7959 | actual_capacity -= sizeof( output ); |
| 7960 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7961 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7962 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7963 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7964 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7965 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7966 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7967 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7968 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7969 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7970 | } |
| 7971 | /* END_CASE */ |
| 7972 | |
| 7973 | /* BEGIN_CASE */ |
| 7974 | void key_agreement_output( int alg_arg, |
| 7975 | int our_key_type_arg, data_t *our_key_data, |
| 7976 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 7977 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7978 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7979 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7980 | psa_algorithm_t alg = alg_arg; |
| 7981 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7982 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7983 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 7984 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7985 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 7986 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 7987 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7988 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7989 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7990 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7991 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7992 | psa_set_key_algorithm( &attributes, alg ); |
| 7993 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7994 | PSA_ASSERT( psa_import_key( &attributes, |
| 7995 | our_key_data->x, our_key_data->len, |
| 7996 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7997 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7998 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7999 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8000 | &operation, |
| 8001 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8002 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8003 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8004 | { |
| 8005 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8006 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8007 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8008 | NULL, 0 ) ); |
| 8009 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8010 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8011 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8012 | actual_output, |
| 8013 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8014 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8015 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8016 | if( expected_output2->len != 0 ) |
| 8017 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8018 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8019 | actual_output, |
| 8020 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8021 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8022 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8023 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8024 | |
| 8025 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8026 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8027 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8028 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8029 | mbedtls_free( actual_output ); |
| 8030 | } |
| 8031 | /* END_CASE */ |
| 8032 | |
| 8033 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8034 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8035 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8036 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8037 | unsigned char *output = NULL; |
| 8038 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8039 | size_t i; |
| 8040 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8041 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8042 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8043 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8044 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8045 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8046 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8047 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8048 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8049 | /* Run several times, to ensure that every output byte will be |
| 8050 | * nonzero at least once with overwhelming probability |
| 8051 | * (2^(-8*number_of_runs)). */ |
| 8052 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8053 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8054 | if( bytes != 0 ) |
| 8055 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8056 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8057 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8058 | for( i = 0; i < bytes; i++ ) |
| 8059 | { |
| 8060 | if( output[i] != 0 ) |
| 8061 | ++changed[i]; |
| 8062 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8063 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8064 | |
| 8065 | /* Check that every byte was changed to nonzero at least once. This |
| 8066 | * validates that psa_generate_random is overwriting every byte of |
| 8067 | * the output buffer. */ |
| 8068 | for( i = 0; i < bytes; i++ ) |
| 8069 | { |
| 8070 | TEST_ASSERT( changed[i] != 0 ); |
| 8071 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8072 | |
| 8073 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8074 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8075 | mbedtls_free( output ); |
| 8076 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8077 | } |
| 8078 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8079 | |
| 8080 | /* BEGIN_CASE */ |
| 8081 | void generate_key( int type_arg, |
| 8082 | int bits_arg, |
| 8083 | int usage_arg, |
| 8084 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8085 | int expected_status_arg, |
| 8086 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8087 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8088 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8089 | psa_key_type_t type = type_arg; |
| 8090 | psa_key_usage_t usage = usage_arg; |
| 8091 | size_t bits = bits_arg; |
| 8092 | psa_algorithm_t alg = alg_arg; |
| 8093 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8094 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8095 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8096 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8097 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8098 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8099 | psa_set_key_usage_flags( &attributes, usage ); |
| 8100 | psa_set_key_algorithm( &attributes, alg ); |
| 8101 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8102 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8103 | |
| 8104 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8105 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8106 | |
| 8107 | if( is_large_key > 0 ) |
| 8108 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8109 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8110 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8111 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8112 | |
| 8113 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8114 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8115 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8116 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8117 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8118 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8119 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8120 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8121 | |
| 8122 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8123 | /* |
| 8124 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8125 | * thus reset them as required. |
| 8126 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8127 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8128 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8129 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8130 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8131 | } |
| 8132 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8133 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8134 | /* 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] | 8135 | void generate_key_rsa( int bits_arg, |
| 8136 | data_t *e_arg, |
| 8137 | int expected_status_arg ) |
| 8138 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8139 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8140 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8141 | size_t bits = bits_arg; |
| 8142 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8143 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8144 | psa_status_t expected_status = expected_status_arg; |
| 8145 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8146 | uint8_t *exported = NULL; |
| 8147 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8148 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8149 | size_t exported_length = SIZE_MAX; |
| 8150 | uint8_t *e_read_buffer = NULL; |
| 8151 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8152 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8153 | size_t e_read_length = SIZE_MAX; |
| 8154 | |
| 8155 | if( e_arg->len == 0 || |
| 8156 | ( e_arg->len == 3 && |
| 8157 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8158 | { |
| 8159 | is_default_public_exponent = 1; |
| 8160 | e_read_size = 0; |
| 8161 | } |
| 8162 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8163 | ASSERT_ALLOC( exported, exported_size ); |
| 8164 | |
| 8165 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8166 | |
| 8167 | psa_set_key_usage_flags( &attributes, usage ); |
| 8168 | psa_set_key_algorithm( &attributes, alg ); |
| 8169 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8170 | e_arg->x, e_arg->len ) ); |
| 8171 | psa_set_key_bits( &attributes, bits ); |
| 8172 | |
| 8173 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8174 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8175 | if( expected_status != PSA_SUCCESS ) |
| 8176 | goto exit; |
| 8177 | |
| 8178 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8179 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8180 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8181 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8182 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8183 | e_read_buffer, e_read_size, |
| 8184 | &e_read_length ) ); |
| 8185 | if( is_default_public_exponent ) |
| 8186 | TEST_EQUAL( e_read_length, 0 ); |
| 8187 | else |
| 8188 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8189 | |
| 8190 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8191 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8192 | goto exit; |
| 8193 | |
| 8194 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8195 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8196 | exported, exported_size, |
| 8197 | &exported_length ) ); |
| 8198 | { |
| 8199 | uint8_t *p = exported; |
| 8200 | uint8_t *end = exported + exported_length; |
| 8201 | size_t len; |
| 8202 | /* RSAPublicKey ::= SEQUENCE { |
| 8203 | * modulus INTEGER, -- n |
| 8204 | * publicExponent INTEGER } -- e |
| 8205 | */ |
| 8206 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8207 | MBEDTLS_ASN1_SEQUENCE | |
| 8208 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8209 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8210 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8211 | MBEDTLS_ASN1_INTEGER ) ); |
| 8212 | if( len >= 1 && p[0] == 0 ) |
| 8213 | { |
| 8214 | ++p; |
| 8215 | --len; |
| 8216 | } |
| 8217 | if( e_arg->len == 0 ) |
| 8218 | { |
| 8219 | TEST_EQUAL( len, 3 ); |
| 8220 | TEST_EQUAL( p[0], 1 ); |
| 8221 | TEST_EQUAL( p[1], 0 ); |
| 8222 | TEST_EQUAL( p[2], 1 ); |
| 8223 | } |
| 8224 | else |
| 8225 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8226 | } |
| 8227 | |
| 8228 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8229 | /* |
| 8230 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8231 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8232 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8233 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8234 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8235 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8236 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8237 | mbedtls_free( e_read_buffer ); |
| 8238 | mbedtls_free( exported ); |
| 8239 | } |
| 8240 | /* END_CASE */ |
| 8241 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8242 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8243 | void persistent_key_load_key_from_storage( data_t *data, |
| 8244 | int type_arg, int bits_arg, |
| 8245 | int usage_flags_arg, int alg_arg, |
| 8246 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8247 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8248 | 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] | 8249 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8250 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8251 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8252 | psa_key_type_t type = type_arg; |
| 8253 | size_t bits = bits_arg; |
| 8254 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8255 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8256 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8257 | unsigned char *first_export = NULL; |
| 8258 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8259 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8260 | size_t first_exported_length; |
| 8261 | size_t second_exported_length; |
| 8262 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8263 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8264 | { |
| 8265 | ASSERT_ALLOC( first_export, export_size ); |
| 8266 | ASSERT_ALLOC( second_export, export_size ); |
| 8267 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8268 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8269 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8270 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8271 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8272 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8273 | psa_set_key_algorithm( &attributes, alg ); |
| 8274 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8275 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8276 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8277 | switch( generation_method ) |
| 8278 | { |
| 8279 | case IMPORT_KEY: |
| 8280 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8281 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8282 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8283 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8284 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8285 | case GENERATE_KEY: |
| 8286 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8287 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8288 | break; |
| 8289 | |
| 8290 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8291 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8292 | { |
| 8293 | /* Create base key */ |
| 8294 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8295 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8296 | psa_set_key_usage_flags( &base_attributes, |
| 8297 | PSA_KEY_USAGE_DERIVE ); |
| 8298 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8299 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8300 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8301 | data->x, data->len, |
| 8302 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8303 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8304 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8305 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8306 | &operation, |
| 8307 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8308 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8309 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8310 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8311 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8312 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8313 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8314 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8315 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8316 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8317 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8318 | #else |
| 8319 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8320 | #endif |
| 8321 | break; |
| 8322 | |
| 8323 | default: |
| 8324 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8325 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8326 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8327 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8328 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8329 | /* Export the key if permitted by the key policy. */ |
| 8330 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8331 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8332 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8333 | first_export, export_size, |
| 8334 | &first_exported_length ) ); |
| 8335 | if( generation_method == IMPORT_KEY ) |
| 8336 | ASSERT_COMPARE( data->x, data->len, |
| 8337 | first_export, first_exported_length ); |
| 8338 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8339 | |
| 8340 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8341 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8342 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8343 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8344 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8345 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8346 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8347 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8348 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8349 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8350 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8351 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8352 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8353 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8354 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8355 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8356 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8357 | /* Export the key again if permitted by the key policy. */ |
| 8358 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8359 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8360 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8361 | second_export, export_size, |
| 8362 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8363 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8364 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8365 | } |
| 8366 | |
| 8367 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8368 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8369 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8370 | |
| 8371 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8372 | /* |
| 8373 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8374 | * thus reset them as required. |
| 8375 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8376 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8377 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8378 | mbedtls_free( first_export ); |
| 8379 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8380 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8381 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8382 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8383 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8384 | } |
| 8385 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8386 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8387 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8388 | 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] | 8389 | int input_first, data_t *pw_data, |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8390 | int expected_status_arg ) |
| 8391 | { |
| 8392 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8393 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 8394 | psa_algorithm_t alg = alg_arg; |
| 8395 | psa_algorithm_t hash_alg = hash_arg; |
| 8396 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8397 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8398 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8399 | psa_status_t expected_status = expected_status_arg; |
| 8400 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8401 | unsigned char *output_buffer = NULL; |
| 8402 | size_t output_len = 0; |
| 8403 | |
| 8404 | PSA_INIT( ); |
| 8405 | |
| 8406 | ASSERT_ALLOC( output_buffer, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8407 | PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8408 | PSA_PAKE_STEP_KEY_SHARE) ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8409 | |
| 8410 | if( pw_data->len > 0 ) |
| 8411 | { |
| 8412 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8413 | psa_set_key_algorithm( &attributes, alg ); |
| 8414 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8415 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8416 | &key ) ); |
| 8417 | } |
| 8418 | |
| 8419 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8420 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8421 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8422 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8423 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8424 | |
| 8425 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8426 | PSA_ERROR_BAD_STATE ); |
| 8427 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8428 | PSA_ERROR_BAD_STATE ); |
| 8429 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8430 | PSA_ERROR_BAD_STATE ); |
| 8431 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8432 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8433 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8434 | NULL, 0, NULL ), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8435 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8436 | 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] | 8437 | PSA_ERROR_BAD_STATE ); |
| 8438 | |
| 8439 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8440 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8441 | status = psa_pake_setup( &operation, &cipher_suite ); |
| 8442 | if( status != PSA_SUCCESS ) |
| 8443 | { |
| 8444 | TEST_EQUAL( status, expected_status ); |
| 8445 | goto exit; |
| 8446 | } |
| 8447 | else |
| 8448 | PSA_ASSERT( status ); |
| 8449 | |
Neil Armstrong | 50de0ae | 2022-06-08 17:46:24 +0200 | [diff] [blame] | 8450 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8451 | PSA_ERROR_BAD_STATE ); |
| 8452 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8453 | status = psa_pake_set_role( &operation, role ); |
| 8454 | if( status != PSA_SUCCESS ) |
| 8455 | { |
| 8456 | TEST_EQUAL( status, expected_status ); |
| 8457 | goto exit; |
| 8458 | } |
| 8459 | else |
| 8460 | PSA_ASSERT( status ); |
| 8461 | |
| 8462 | if( pw_data->len > 0 ) |
| 8463 | { |
| 8464 | status = psa_pake_set_password_key( &operation, key ); |
| 8465 | if( status != PSA_SUCCESS ) |
| 8466 | { |
| 8467 | TEST_EQUAL( status, expected_status ); |
| 8468 | goto exit; |
| 8469 | } |
| 8470 | else |
| 8471 | PSA_ASSERT( status ); |
| 8472 | } |
| 8473 | |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8474 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8475 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8476 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8477 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8478 | |
| 8479 | const uint8_t unsupported_id[] = "abcd"; |
| 8480 | |
| 8481 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8482 | PSA_ERROR_NOT_SUPPORTED ); |
| 8483 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8484 | PSA_ERROR_NOT_SUPPORTED ); |
| 8485 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8486 | /* First round */ |
| 8487 | if( input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8488 | { |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8489 | /* Invalid parameters */ |
| 8490 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8491 | NULL, 0 ), |
| 8492 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8493 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8494 | output_buffer, 66 ), |
| 8495 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8496 | /* Invalid first step */ |
| 8497 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8498 | output_buffer, 66 ), |
| 8499 | PSA_ERROR_BAD_STATE ); |
| 8500 | |
| 8501 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8502 | output_buffer, 66 ), |
| 8503 | expected_status); |
| 8504 | |
| 8505 | if( expected_status == PSA_SUCCESS ) |
| 8506 | { |
| 8507 | /* Buffer too large */ |
| 8508 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8509 | output_buffer, 512 ), |
| 8510 | PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8511 | |
| 8512 | /* The operation should be aborted at this point */ |
| 8513 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8514 | output_buffer, 66 ), |
| 8515 | PSA_ERROR_BAD_STATE ); |
| 8516 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8517 | } |
| 8518 | else |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8519 | { |
| 8520 | /* Invalid parameters */ |
| 8521 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8522 | NULL, 0, NULL ), |
| 8523 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8524 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8525 | output_buffer, 512, &output_len ), |
| 8526 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8527 | /* Invalid first step */ |
| 8528 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8529 | output_buffer, 512, &output_len ), |
| 8530 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8531 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8532 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8533 | output_buffer, 512, &output_len ), |
| 8534 | expected_status ); |
Neil Armstrong | 98506ab | 2022-06-08 17:43:20 +0200 | [diff] [blame] | 8535 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8536 | if( expected_status == PSA_SUCCESS ) |
| 8537 | { |
| 8538 | TEST_ASSERT( output_len > 0 ); |
| 8539 | |
| 8540 | /* Buffer too small */ |
| 8541 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8542 | output_buffer, 5, &output_len ), |
| 8543 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8544 | |
| 8545 | /* The operation should be aborted at this point */ |
| 8546 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8547 | output_buffer, 512, &output_len ), |
| 8548 | PSA_ERROR_BAD_STATE ); |
| 8549 | } |
| 8550 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8551 | |
| 8552 | exit: |
| 8553 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 8554 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8555 | mbedtls_free( output_buffer ); |
| 8556 | PSA_DONE( ); |
| 8557 | } |
| 8558 | /* END_CASE */ |
| 8559 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8560 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8561 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 8562 | int client_input_first, int inject_error, |
| 8563 | data_t *pw_data ) |
| 8564 | { |
| 8565 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8566 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8567 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8568 | psa_algorithm_t alg = alg_arg; |
| 8569 | psa_algorithm_t hash_alg = hash_arg; |
| 8570 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8571 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8572 | |
| 8573 | PSA_INIT( ); |
| 8574 | |
| 8575 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8576 | psa_set_key_algorithm( &attributes, alg ); |
| 8577 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8578 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8579 | &key ) ); |
| 8580 | |
| 8581 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8582 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8583 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8584 | |
| 8585 | |
| 8586 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8587 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8588 | |
| 8589 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8590 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8591 | |
| 8592 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8593 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8594 | |
| 8595 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8596 | client_input_first, 1, |
| 8597 | inject_error ), 1 ); |
| 8598 | |
| 8599 | if( inject_error == 1 || inject_error == 2 ) |
| 8600 | goto exit; |
| 8601 | |
| 8602 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8603 | client_input_first, 2, |
| 8604 | inject_error ), 1 ); |
| 8605 | |
| 8606 | exit: |
| 8607 | psa_destroy_key( key ); |
| 8608 | psa_pake_abort( &server ); |
| 8609 | psa_pake_abort( &client ); |
| 8610 | PSA_DONE( ); |
| 8611 | } |
| 8612 | /* END_CASE */ |
| 8613 | |
| 8614 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8615 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8616 | int derive_alg_arg, data_t *pw_data, |
| 8617 | int client_input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8618 | { |
| 8619 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8620 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8621 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8622 | psa_algorithm_t alg = alg_arg; |
| 8623 | psa_algorithm_t hash_alg = hash_arg; |
| 8624 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 8625 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8626 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8627 | psa_key_derivation_operation_t server_derive = |
| 8628 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8629 | psa_key_derivation_operation_t client_derive = |
| 8630 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8631 | |
| 8632 | PSA_INIT( ); |
| 8633 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8634 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8635 | psa_set_key_algorithm( &attributes, alg ); |
| 8636 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8637 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8638 | &key ) ); |
| 8639 | |
| 8640 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8641 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8642 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8643 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8644 | /* Get shared key */ |
| 8645 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 8646 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 8647 | |
| 8648 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 8649 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 8650 | { |
| 8651 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 8652 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8653 | (const uint8_t*) "", 0) ); |
| 8654 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 8655 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8656 | (const uint8_t*) "", 0) ); |
| 8657 | } |
| 8658 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8659 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8660 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8661 | |
| 8662 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8663 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8664 | |
| 8665 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8666 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8667 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8668 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8669 | PSA_ERROR_BAD_STATE ); |
| 8670 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8671 | PSA_ERROR_BAD_STATE ); |
| 8672 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8673 | /* First round */ |
| 8674 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8675 | client_input_first, 1, 0 ), 1 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8676 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8677 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8678 | PSA_ERROR_BAD_STATE ); |
| 8679 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8680 | PSA_ERROR_BAD_STATE ); |
| 8681 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8682 | /* Second round */ |
| 8683 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8684 | client_input_first, 2, 0 ), 1 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8685 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8686 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 8687 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 8688 | |
| 8689 | exit: |
| 8690 | psa_key_derivation_abort( &server_derive ); |
| 8691 | psa_key_derivation_abort( &client_derive ); |
| 8692 | psa_destroy_key( key ); |
| 8693 | psa_pake_abort( &server ); |
| 8694 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8695 | PSA_DONE( ); |
| 8696 | } |
| 8697 | /* END_CASE */ |