Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
| 7 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 8 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 9 | * uses mbedtls_ctr_drbg internally. */ |
| 10 | #include "mbedtls/ctr_drbg.h" |
| 11 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 12 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 13 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 14 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 15 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 16 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 17 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 18 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 19 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 20 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 21 | #else |
| 22 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 23 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 24 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 25 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 26 | #define UNUSED 0xdeadbeef |
| 27 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 28 | /* Assert that an operation is (not) active. |
| 29 | * This serves as a proxy for checking if the operation is aborted. */ |
| 30 | #define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 ) |
| 31 | #define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 32 | |
| 33 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 34 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 35 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 36 | /** Test if a buffer contains a constant byte value. |
| 37 | * |
| 38 | * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 39 | * |
| 40 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 41 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 42 | * \param size Size of the buffer in bytes. |
| 43 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 44 | * \return 1 if the buffer is all-bits-zero. |
| 45 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 46 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 47 | static int mem_is_char( void *buffer, unsigned char c, size_t size ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 48 | { |
| 49 | size_t i; |
| 50 | for( i = 0; i < size; i++ ) |
| 51 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 52 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 53 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 54 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 55 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 56 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 57 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 58 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 59 | static int asn1_write_10x( unsigned char **p, |
| 60 | unsigned char *start, |
| 61 | size_t bits, |
| 62 | unsigned char x ) |
| 63 | { |
| 64 | int ret; |
| 65 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 66 | if( bits == 0 ) |
| 67 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 68 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 69 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 70 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 71 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 72 | *p -= len; |
| 73 | ( *p )[len-1] = x; |
| 74 | if( bits % 8 == 0 ) |
| 75 | ( *p )[1] |= 1; |
| 76 | else |
| 77 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 78 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 79 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 80 | MBEDTLS_ASN1_INTEGER ) ); |
| 81 | return( len ); |
| 82 | } |
| 83 | |
| 84 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 85 | size_t buffer_size, |
| 86 | unsigned char **p, |
| 87 | size_t bits, |
| 88 | int keypair ) |
| 89 | { |
| 90 | size_t half_bits = ( bits + 1 ) / 2; |
| 91 | int ret; |
| 92 | int len = 0; |
| 93 | /* Construct something that looks like a DER encoding of |
| 94 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 95 | * RSAPrivateKey ::= SEQUENCE { |
| 96 | * version Version, |
| 97 | * modulus INTEGER, -- n |
| 98 | * publicExponent INTEGER, -- e |
| 99 | * privateExponent INTEGER, -- d |
| 100 | * prime1 INTEGER, -- p |
| 101 | * prime2 INTEGER, -- q |
| 102 | * exponent1 INTEGER, -- d mod (p-1) |
| 103 | * exponent2 INTEGER, -- d mod (q-1) |
| 104 | * coefficient INTEGER, -- (inverse of q) mod p |
| 105 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 106 | * } |
| 107 | * Or, for a public key, the same structure with only |
| 108 | * version, modulus and publicExponent. |
| 109 | */ |
| 110 | *p = buffer + buffer_size; |
| 111 | if( keypair ) |
| 112 | { |
| 113 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 114 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 115 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 116 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 117 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 118 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 119 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 120 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 121 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 122 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 123 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 124 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 125 | } |
| 126 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 127 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 128 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 129 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 130 | if( keypair ) |
| 131 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 132 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 133 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 134 | { |
| 135 | const unsigned char tag = |
| 136 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 137 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 138 | } |
| 139 | return( len ); |
| 140 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 141 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 142 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 143 | int exercise_mac_setup( psa_key_type_t key_type, |
| 144 | const unsigned char *key_bytes, |
| 145 | size_t key_length, |
| 146 | psa_algorithm_t alg, |
| 147 | psa_mac_operation_t *operation, |
| 148 | psa_status_t *status ) |
| 149 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 150 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 151 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 152 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 153 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 154 | psa_set_key_algorithm( &attributes, alg ); |
| 155 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 156 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 157 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 158 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 159 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 160 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 161 | /* If setup failed, reproduce the failure, so that the caller can |
| 162 | * test the resulting state of the operation object. */ |
| 163 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 164 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 165 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 168 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 169 | return( 1 ); |
| 170 | |
| 171 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 172 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 173 | return( 0 ); |
| 174 | } |
| 175 | |
| 176 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 177 | const unsigned char *key_bytes, |
| 178 | size_t key_length, |
| 179 | psa_algorithm_t alg, |
| 180 | psa_cipher_operation_t *operation, |
| 181 | psa_status_t *status ) |
| 182 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 183 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 184 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 185 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 186 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 187 | psa_set_key_algorithm( &attributes, alg ); |
| 188 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 189 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 190 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 191 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 192 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 193 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 194 | /* If setup failed, reproduce the failure, so that the caller can |
| 195 | * test the resulting state of the operation object. */ |
| 196 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 197 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 198 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 199 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 202 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 203 | return( 1 ); |
| 204 | |
| 205 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 206 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 207 | return( 0 ); |
| 208 | } |
| 209 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 210 | static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 211 | { |
| 212 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 213 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 214 | uint8_t buffer[1]; |
| 215 | size_t length; |
| 216 | int ok = 0; |
| 217 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 218 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 219 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 220 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 221 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 222 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 223 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 224 | TEST_EQUAL( |
| 225 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 226 | TEST_EQUAL( |
| 227 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 228 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 229 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 230 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 231 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 232 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 233 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 234 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 235 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 236 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 237 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 238 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 239 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 240 | ok = 1; |
| 241 | |
| 242 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 243 | /* |
| 244 | * Key attributes may have been returned by psa_get_key_attributes() |
| 245 | * thus reset them as required. |
| 246 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 247 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 248 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 249 | return( ok ); |
| 250 | } |
| 251 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 252 | /* Assert that a key isn't reported as having a slot number. */ |
| 253 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 254 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 255 | do \ |
| 256 | { \ |
| 257 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 258 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 259 | attributes, \ |
| 260 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 261 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 262 | } \ |
| 263 | while( 0 ) |
| 264 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 265 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 266 | ( (void) 0 ) |
| 267 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 268 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 269 | /* An overapproximation of the amount of storage needed for a key of the |
| 270 | * given type and with the given content. The API doesn't make it easy |
| 271 | * to find a good value for the size. The current implementation doesn't |
| 272 | * care about the value anyway. */ |
| 273 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 274 | ( data )->len |
| 275 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 276 | typedef enum { |
| 277 | IMPORT_KEY = 0, |
| 278 | GENERATE_KEY = 1, |
| 279 | DERIVE_KEY = 2 |
| 280 | } generate_method; |
| 281 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 282 | typedef enum |
| 283 | { |
| 284 | DO_NOT_SET_LENGTHS = 0, |
| 285 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 286 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 287 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 288 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 289 | typedef enum |
| 290 | { |
| 291 | USE_NULL_TAG = 0, |
| 292 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 293 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 294 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 295 | /*! |
| 296 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 297 | * \param key_type_arg Type of key passed in |
| 298 | * \param key_data The encryption / decryption key data |
| 299 | * \param alg_arg The type of algorithm used |
| 300 | * \param nonce Nonce data |
| 301 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 302 | * \param ad_part_len_arg If not -1, the length of chunks to |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 303 | * feed additional data in to be encrypted / |
| 304 | * decrypted. If -1, no chunking. |
| 305 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 306 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 307 | * the data in to be encrypted / decrypted. If |
| 308 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 309 | * \param set_lengths_method A member of the set_lengths_method_t enum is |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 310 | * expected here, this controls whether or not |
| 311 | * to set lengths, and in what order with |
| 312 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 313 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 314 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 315 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 316 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 317 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 318 | */ |
| 319 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 320 | int alg_arg, |
| 321 | data_t *nonce, |
| 322 | data_t *additional_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 323 | int ad_part_len_arg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 324 | data_t *input_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 325 | int data_part_len_arg, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 326 | set_lengths_method_t set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 327 | data_t *expected_output, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 328 | int is_encrypt, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 329 | int do_zero_parts ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 330 | { |
| 331 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 332 | psa_key_type_t key_type = key_type_arg; |
| 333 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 334 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 335 | unsigned char *output_data = NULL; |
| 336 | unsigned char *part_data = NULL; |
| 337 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 338 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 339 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 340 | size_t output_size = 0; |
| 341 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 342 | size_t output_length = 0; |
| 343 | size_t key_bits = 0; |
| 344 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 345 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 346 | size_t part_length = 0; |
| 347 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 348 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 349 | size_t ad_part_len = 0; |
| 350 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 351 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 352 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 353 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 354 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 355 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 356 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 357 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 358 | PSA_ASSERT( psa_crypto_init( ) ); |
| 359 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 360 | if( is_encrypt ) |
| 361 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 362 | else |
| 363 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 364 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 365 | psa_set_key_algorithm( &attributes, alg ); |
| 366 | psa_set_key_type( &attributes, key_type ); |
| 367 | |
| 368 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 369 | &key ) ); |
| 370 | |
| 371 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 372 | key_bits = psa_get_key_bits( &attributes ); |
| 373 | |
| 374 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 375 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 376 | if( is_encrypt ) |
| 377 | { |
| 378 | /* Tag gets written at end of buffer. */ |
| 379 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 380 | ( input_data->len + |
| 381 | tag_length ) ); |
| 382 | data_true_size = input_data->len; |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 387 | ( input_data->len - |
| 388 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 389 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 390 | /* Do not want to attempt to decrypt tag. */ |
| 391 | data_true_size = input_data->len - tag_length; |
| 392 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 393 | |
| 394 | ASSERT_ALLOC( output_data, output_size ); |
| 395 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 396 | if( is_encrypt ) |
| 397 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 398 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 399 | TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 400 | } |
| 401 | else |
| 402 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 403 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 404 | TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 405 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 406 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 407 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 408 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 409 | if( is_encrypt ) |
| 410 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 411 | else |
| 412 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 413 | |
| 414 | /* If the operation is not supported, just skip and not fail in case the |
| 415 | * encryption involves a common limitation of cryptography hardwares and |
| 416 | * an alternative implementation. */ |
| 417 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 418 | { |
| 419 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 420 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 421 | } |
| 422 | |
| 423 | PSA_ASSERT( status ); |
| 424 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 425 | if( set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 426 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 427 | else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 428 | { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 429 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 430 | data_true_size ) ); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 431 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 432 | } |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 433 | else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 434 | { |
| 435 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 436 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 437 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 438 | data_true_size ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 439 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 440 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 441 | if( ad_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 442 | { |
| 443 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 444 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 445 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 446 | for( part_offset = 0, part_count = 0; |
| 447 | part_offset < additional_data->len; |
| 448 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 449 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 450 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 451 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 452 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 453 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 454 | else if( additional_data->len - part_offset < ad_part_len ) |
| 455 | { |
| 456 | part_length = additional_data->len - part_offset; |
| 457 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 458 | else |
| 459 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 460 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 464 | additional_data->x + part_offset, |
| 465 | part_length ) ); |
| 466 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | else |
| 470 | { |
| 471 | /* Pass additional data in one go. */ |
| 472 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 473 | additional_data->len ) ); |
| 474 | } |
| 475 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 476 | if( data_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 477 | { |
| 478 | /* Pass data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 479 | data_part_len = ( size_t ) data_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 480 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 481 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 482 | |
| 483 | ASSERT_ALLOC( part_data, part_data_size ); |
| 484 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 485 | for( part_offset = 0, part_count = 0; |
| 486 | part_offset < data_true_size; |
| 487 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 488 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 489 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 490 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 491 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 492 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 493 | else if( ( data_true_size - part_offset ) < data_part_len ) |
| 494 | { |
| 495 | part_length = ( data_true_size - part_offset ); |
| 496 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 497 | else |
| 498 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 499 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | PSA_ASSERT( psa_aead_update( &operation, |
| 503 | ( input_data->x + part_offset ), |
| 504 | part_length, part_data, |
| 505 | part_data_size, |
| 506 | &output_part_length ) ); |
| 507 | |
| 508 | if( output_data && output_part_length ) |
| 509 | { |
Mircea Udrea | 657ff4f | 2022-01-31 13:51:56 +0100 | [diff] [blame] | 510 | memcpy( ( output_data + output_length ), part_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 511 | output_part_length ); |
| 512 | } |
| 513 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 514 | output_length += output_part_length; |
| 515 | } |
| 516 | } |
| 517 | else |
| 518 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 519 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 520 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 521 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 522 | output_size, &output_length ) ); |
| 523 | } |
| 524 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 525 | if( is_encrypt ) |
| 526 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 527 | final_output_size, |
| 528 | &output_part_length, |
| 529 | tag_buffer, tag_length, |
| 530 | &tag_size ) ); |
| 531 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 532 | { |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 533 | PSA_ASSERT( psa_aead_verify( &operation, final_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 534 | final_output_size, |
| 535 | &output_part_length, |
| 536 | ( input_data->x + data_true_size ), |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 537 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 540 | if( output_data && output_part_length ) |
| 541 | memcpy( ( output_data + output_length ), final_data, |
| 542 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 543 | |
| 544 | output_length += output_part_length; |
| 545 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 546 | |
| 547 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 548 | * should be exact.*/ |
| 549 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 550 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 551 | TEST_EQUAL( tag_length, tag_size ); |
| 552 | |
| 553 | if( output_data && tag_length ) |
| 554 | memcpy( ( output_data + output_length ), tag_buffer, |
| 555 | tag_length ); |
| 556 | |
| 557 | output_length += tag_length; |
| 558 | |
| 559 | TEST_EQUAL( output_length, |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 560 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 561 | input_data->len ) ); |
| 562 | TEST_LE_U( output_length, |
| 563 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 564 | } |
| 565 | else |
| 566 | { |
| 567 | TEST_EQUAL( output_length, |
| 568 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 569 | input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 570 | TEST_LE_U( output_length, |
| 571 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 574 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 575 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 576 | output_data, output_length ); |
| 577 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 578 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 579 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 580 | |
| 581 | exit: |
| 582 | psa_destroy_key( key ); |
| 583 | psa_aead_abort( &operation ); |
| 584 | mbedtls_free( output_data ); |
| 585 | mbedtls_free( part_data ); |
| 586 | mbedtls_free( final_data ); |
| 587 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 588 | |
| 589 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 592 | /*! |
| 593 | * \brief Internal Function for MAC multipart tests. |
| 594 | * \param key_type_arg Type of key passed in |
| 595 | * \param key_data The encryption / decryption key data |
| 596 | * \param alg_arg The type of algorithm used |
| 597 | * \param input_data Data to encrypt / decrypt |
| 598 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 599 | * the data in to be encrypted / decrypted. If |
| 600 | * -1, no chunking |
| 601 | * \param expected_output Expected output |
| 602 | * \param is_verify If non-zero this is an verify operation. |
| 603 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 604 | * with normal length chunks. |
| 605 | * \return int Zero on failure, non-zero on success. |
| 606 | */ |
| 607 | static int mac_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 608 | int alg_arg, |
| 609 | data_t *input_data, |
| 610 | int data_part_len_arg, |
| 611 | data_t *expected_output, |
| 612 | int is_verify, |
| 613 | int do_zero_parts ) |
| 614 | { |
| 615 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 616 | psa_key_type_t key_type = key_type_arg; |
| 617 | psa_algorithm_t alg = alg_arg; |
| 618 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 619 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 620 | size_t part_offset = 0; |
| 621 | size_t part_length = 0; |
| 622 | size_t data_part_len = 0; |
| 623 | size_t mac_len = 0; |
| 624 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 625 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 626 | |
| 627 | int test_ok = 0; |
| 628 | size_t part_count = 0; |
| 629 | |
Neil Armstrong | fd4c259 | 2022-03-07 10:11:11 +0100 | [diff] [blame] | 630 | PSA_INIT( ); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 631 | |
| 632 | if( is_verify ) |
| 633 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 634 | else |
| 635 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 636 | |
| 637 | psa_set_key_algorithm( &attributes, alg ); |
| 638 | psa_set_key_type( &attributes, key_type ); |
| 639 | |
| 640 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 641 | &key ) ); |
| 642 | |
| 643 | if( is_verify ) |
| 644 | status = psa_mac_verify_setup( &operation, key, alg ); |
| 645 | else |
| 646 | status = psa_mac_sign_setup( &operation, key, alg ); |
| 647 | |
| 648 | PSA_ASSERT( status ); |
| 649 | |
| 650 | if( data_part_len_arg != -1 ) |
| 651 | { |
| 652 | /* Pass data in parts */ |
| 653 | data_part_len = ( size_t ) data_part_len_arg; |
| 654 | |
| 655 | for( part_offset = 0, part_count = 0; |
| 656 | part_offset < input_data->len; |
| 657 | part_offset += part_length, part_count++ ) |
| 658 | { |
| 659 | if( do_zero_parts && ( part_count & 0x01 ) ) |
| 660 | { |
| 661 | part_length = 0; |
| 662 | } |
| 663 | else if( ( input_data->len - part_offset ) < data_part_len ) |
| 664 | { |
| 665 | part_length = ( input_data->len - part_offset ); |
| 666 | } |
| 667 | else |
| 668 | { |
| 669 | part_length = data_part_len; |
| 670 | } |
| 671 | |
| 672 | PSA_ASSERT( psa_mac_update( &operation, |
| 673 | ( input_data->x + part_offset ), |
| 674 | part_length ) ); |
| 675 | } |
| 676 | } |
| 677 | else |
| 678 | { |
| 679 | /* Pass all data in one go. */ |
| 680 | PSA_ASSERT( psa_mac_update( &operation, input_data->x, |
| 681 | input_data->len ) ); |
| 682 | } |
| 683 | |
| 684 | if( is_verify ) |
| 685 | { |
| 686 | PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x, |
| 687 | expected_output->len ) ); |
| 688 | } |
| 689 | else |
| 690 | { |
| 691 | PSA_ASSERT( psa_mac_sign_finish( &operation, mac, |
| 692 | PSA_MAC_MAX_SIZE, &mac_len ) ); |
| 693 | |
| 694 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 695 | mac, mac_len ); |
| 696 | } |
| 697 | |
| 698 | test_ok = 1; |
| 699 | |
| 700 | exit: |
| 701 | psa_destroy_key( key ); |
| 702 | psa_mac_abort( &operation ); |
| 703 | PSA_DONE( ); |
| 704 | |
| 705 | return( test_ok ); |
| 706 | } |
| 707 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 708 | #if defined(PSA_WANT_ALG_JPAKE) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 709 | static int ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive, |
| 710 | psa_pake_operation_t *server, |
| 711 | psa_pake_operation_t *client, |
| 712 | int client_input_first, |
| 713 | int round, int inject_error ) |
| 714 | { |
| 715 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 716 | size_t buffer_length = ( |
| 717 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 718 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 719 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2; |
| 720 | size_t buffer0_off = 0; |
| 721 | size_t buffer1_off = 0; |
| 722 | size_t s_g1_len, s_g2_len, s_a_len; |
| 723 | size_t s_g1_off, s_g2_off, s_a_off; |
| 724 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 725 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 726 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 727 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 728 | size_t c_g1_len, c_g2_len, c_a_len; |
| 729 | size_t c_g1_off, c_g2_off, c_a_off; |
| 730 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 731 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 732 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 733 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 734 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 735 | psa_status_t status; |
| 736 | int ret = 0; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 737 | |
| 738 | ASSERT_ALLOC( buffer0, buffer_length ); |
| 739 | ASSERT_ALLOC( buffer1, buffer_length ); |
| 740 | |
| 741 | switch( round ) |
| 742 | { |
| 743 | case 1: |
| 744 | /* Server first round Output */ |
| 745 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 746 | buffer0 + buffer0_off, |
| 747 | 512 - buffer0_off, &s_g1_len ) ); |
| 748 | s_g1_off = buffer0_off; |
| 749 | buffer0_off += s_g1_len; |
| 750 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 751 | buffer0 + buffer0_off, |
| 752 | 512 - buffer0_off, &s_x1_pk_len ) ); |
| 753 | s_x1_pk_off = buffer0_off; |
| 754 | buffer0_off += s_x1_pk_len; |
| 755 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 756 | buffer0 + buffer0_off, |
| 757 | 512 - buffer0_off, &s_x1_pr_len ) ); |
| 758 | s_x1_pr_off = buffer0_off; |
| 759 | buffer0_off += s_x1_pr_len; |
| 760 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 761 | buffer0 + buffer0_off, |
| 762 | 512 - buffer0_off, &s_g2_len ) ); |
| 763 | s_g2_off = buffer0_off; |
| 764 | buffer0_off += s_g2_len; |
| 765 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 766 | buffer0 + buffer0_off, |
| 767 | 512 - buffer0_off, &s_x2_pk_len ) ); |
| 768 | s_x2_pk_off = buffer0_off; |
| 769 | buffer0_off += s_x2_pk_len; |
| 770 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 771 | buffer0 + buffer0_off, |
| 772 | 512 - buffer0_off, &s_x2_pr_len ) ); |
| 773 | s_x2_pr_off = buffer0_off; |
| 774 | buffer0_off += s_x2_pr_len; |
| 775 | |
| 776 | if( inject_error == 1 ) |
| 777 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 778 | buffer0[s_x1_pk_off + 8] >>= 4; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 779 | buffer0[s_x2_pk_off + 7] <<= 4; |
| 780 | expected_status = PSA_ERROR_DATA_INVALID; |
| 781 | } |
| 782 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame^] | 783 | /* |
| 784 | * When injecting errors in inputs, the implementation is |
| 785 | * free to detect it right away of with a delay. |
| 786 | * This permits delaying the error until the end of the input |
| 787 | * sequence, if no error appears then, this will be treated |
| 788 | * as an error. |
| 789 | */ |
| 790 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 791 | if( client_input_first == 1 ) |
| 792 | { |
| 793 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 794 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 795 | buffer0 + s_g1_off, s_g1_len ); |
| 796 | if( inject_error == 1 && status != PSA_SUCCESS ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 797 | { |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 798 | TEST_EQUAL( status, expected_status ); |
| 799 | break; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 800 | } |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 801 | else |
| 802 | { |
| 803 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 804 | } |
| 805 | |
| 806 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 807 | buffer0 + s_x1_pk_off, |
| 808 | s_x1_pk_len ); |
| 809 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 810 | { |
| 811 | TEST_EQUAL( status, expected_status ); |
| 812 | break; |
| 813 | } |
| 814 | else |
| 815 | { |
| 816 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 817 | } |
| 818 | |
| 819 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 820 | buffer0 + s_x1_pr_off, |
| 821 | s_x1_pr_len ); |
| 822 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 823 | { |
| 824 | TEST_EQUAL( status, expected_status ); |
| 825 | break; |
| 826 | } |
| 827 | else |
| 828 | { |
| 829 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 830 | } |
| 831 | |
| 832 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 833 | buffer0 + s_g2_off, |
| 834 | s_g2_len ); |
| 835 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 836 | { |
| 837 | TEST_EQUAL( status, expected_status ); |
| 838 | break; |
| 839 | } |
| 840 | else |
| 841 | { |
| 842 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 843 | } |
| 844 | |
| 845 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 846 | buffer0 + s_x2_pk_off, |
| 847 | s_x2_pk_len ); |
| 848 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 849 | { |
| 850 | TEST_EQUAL( status, expected_status ); |
| 851 | break; |
| 852 | } |
| 853 | else |
| 854 | { |
| 855 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 856 | } |
| 857 | |
| 858 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 859 | buffer0 + s_x2_pr_off, |
| 860 | s_x2_pr_len ); |
| 861 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 862 | { |
| 863 | TEST_EQUAL( status, expected_status ); |
| 864 | break; |
| 865 | } |
| 866 | else |
| 867 | { |
| 868 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 869 | } |
| 870 | |
| 871 | /* Error didn't trigger, exit with error */ |
| 872 | if( inject_error == 1 ) |
| 873 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /* Client first round Output */ |
| 877 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 878 | buffer1 + buffer1_off, |
| 879 | 512 - buffer1_off, &c_g1_len ) ); |
| 880 | c_g1_off = buffer1_off; |
| 881 | buffer1_off += c_g1_len; |
| 882 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 883 | buffer1 + buffer1_off, |
| 884 | 512 - buffer1_off, &c_x1_pk_len ) ); |
| 885 | c_x1_pk_off = buffer1_off; |
| 886 | buffer1_off += c_x1_pk_len; |
| 887 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 888 | buffer1 + buffer1_off, |
| 889 | 512 - buffer1_off, &c_x1_pr_len ) ); |
| 890 | c_x1_pr_off = buffer1_off; |
| 891 | buffer1_off += c_x1_pr_len; |
| 892 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 893 | buffer1 + buffer1_off, |
| 894 | 512 - buffer1_off, &c_g2_len ) ); |
| 895 | c_g2_off = buffer1_off; |
| 896 | buffer1_off += c_g2_len; |
| 897 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 898 | buffer1 + buffer1_off, |
| 899 | 512 - buffer1_off, &c_x2_pk_len ) ); |
| 900 | c_x2_pk_off = buffer1_off; |
| 901 | buffer1_off += c_x2_pk_len; |
| 902 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 903 | buffer1 + buffer1_off, |
| 904 | 512 - buffer1_off, &c_x2_pr_len ) ); |
| 905 | c_x2_pr_off = buffer1_off; |
| 906 | buffer1_off += c_x2_pr_len; |
| 907 | |
| 908 | if( client_input_first == 0 ) |
| 909 | { |
| 910 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 911 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 912 | buffer0 + s_g1_off, s_g1_len ); |
| 913 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 914 | { |
| 915 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 916 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 917 | } |
| 918 | else |
| 919 | { |
| 920 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 921 | } |
| 922 | |
| 923 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 924 | buffer0 + s_x1_pk_off, |
| 925 | s_x1_pk_len ); |
| 926 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 927 | { |
| 928 | TEST_EQUAL( status, expected_status ); |
| 929 | break; |
| 930 | } |
| 931 | else |
| 932 | { |
| 933 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 934 | } |
| 935 | |
| 936 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 937 | buffer0 + s_x1_pr_off, |
| 938 | s_x1_pr_len ); |
| 939 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 940 | { |
| 941 | TEST_EQUAL( status, expected_status ); |
| 942 | break; |
| 943 | } |
| 944 | else |
| 945 | { |
| 946 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 947 | } |
| 948 | |
| 949 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 950 | buffer0 + s_g2_off, |
| 951 | s_g2_len ); |
| 952 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 953 | { |
| 954 | TEST_EQUAL( status, expected_status ); |
| 955 | break; |
| 956 | } |
| 957 | else |
| 958 | { |
| 959 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 960 | } |
| 961 | |
| 962 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 963 | buffer0 + s_x2_pk_off, |
| 964 | s_x2_pk_len ); |
| 965 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 966 | { |
| 967 | TEST_EQUAL( status, expected_status ); |
| 968 | break; |
| 969 | } |
| 970 | else |
| 971 | { |
| 972 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 973 | } |
| 974 | |
| 975 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 976 | buffer0 + s_x2_pr_off, |
| 977 | s_x2_pr_len ); |
| 978 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 979 | { |
| 980 | TEST_EQUAL( status, expected_status ); |
| 981 | break; |
| 982 | } |
| 983 | else |
| 984 | { |
| 985 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 986 | } |
| 987 | |
| 988 | /* Error didn't trigger, exit with error */ |
| 989 | if( inject_error == 1 ) |
| 990 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | if( inject_error == 2 ) |
| 994 | { |
| 995 | buffer1[c_x1_pk_off + 12] >>= 4; |
| 996 | buffer1[c_x2_pk_off + 7] <<= 4; |
| 997 | expected_status = PSA_ERROR_DATA_INVALID; |
| 998 | } |
| 999 | |
| 1000 | /* Server first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1001 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1002 | buffer1 + c_g1_off, c_g1_len ); |
| 1003 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1004 | { |
| 1005 | TEST_EQUAL( status, expected_status ); |
| 1006 | break; |
| 1007 | } |
| 1008 | else |
| 1009 | { |
| 1010 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1011 | } |
| 1012 | |
| 1013 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1014 | buffer1 + c_x1_pk_off, c_x1_pk_len ); |
| 1015 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1016 | { |
| 1017 | TEST_EQUAL( status, expected_status ); |
| 1018 | break; |
| 1019 | } |
| 1020 | else |
| 1021 | { |
| 1022 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1023 | } |
| 1024 | |
| 1025 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1026 | buffer1 + c_x1_pr_off, c_x1_pr_len ); |
| 1027 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1028 | { |
| 1029 | TEST_EQUAL( status, expected_status ); |
| 1030 | break; |
| 1031 | } |
| 1032 | else |
| 1033 | { |
| 1034 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1035 | } |
| 1036 | |
| 1037 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1038 | buffer1 + c_g2_off, c_g2_len ); |
| 1039 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1040 | { |
| 1041 | TEST_EQUAL( status, expected_status ); |
| 1042 | break; |
| 1043 | } |
| 1044 | else |
| 1045 | { |
| 1046 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1047 | } |
| 1048 | |
| 1049 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1050 | buffer1 + c_x2_pk_off, c_x2_pk_len ); |
| 1051 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1052 | { |
| 1053 | TEST_EQUAL( status, expected_status ); |
| 1054 | break; |
| 1055 | } |
| 1056 | else |
| 1057 | { |
| 1058 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1059 | } |
| 1060 | |
| 1061 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1062 | buffer1 + c_x2_pr_off, c_x2_pr_len ); |
| 1063 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1064 | { |
| 1065 | TEST_EQUAL( status, expected_status ); |
| 1066 | break; |
| 1067 | } |
| 1068 | else |
| 1069 | { |
| 1070 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1071 | } |
| 1072 | |
| 1073 | /* Error didn't trigger, exit with error */ |
| 1074 | if( inject_error == 2 ) |
| 1075 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1076 | |
| 1077 | break; |
| 1078 | |
| 1079 | case 2: |
| 1080 | /* Server second round Output */ |
| 1081 | buffer0_off = 0; |
| 1082 | |
| 1083 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1084 | buffer0 + buffer0_off, |
| 1085 | 512 - buffer0_off, &s_a_len ) ); |
| 1086 | s_a_off = buffer0_off; |
| 1087 | buffer0_off += s_a_len; |
| 1088 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1089 | buffer0 + buffer0_off, |
| 1090 | 512 - buffer0_off, &s_x2s_pk_len ) ); |
| 1091 | s_x2s_pk_off = buffer0_off; |
| 1092 | buffer0_off += s_x2s_pk_len; |
| 1093 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1094 | buffer0 + buffer0_off, |
| 1095 | 512 - buffer0_off, &s_x2s_pr_len ) ); |
| 1096 | s_x2s_pr_off = buffer0_off; |
| 1097 | buffer0_off += s_x2s_pr_len; |
| 1098 | |
| 1099 | if( inject_error == 3 ) |
| 1100 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1101 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1102 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1103 | } |
| 1104 | |
| 1105 | if( client_input_first == 1 ) |
| 1106 | { |
| 1107 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1108 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1109 | buffer0 + s_a_off, s_a_len ); |
| 1110 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1111 | { |
| 1112 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1113 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1114 | } |
| 1115 | else |
| 1116 | { |
| 1117 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1118 | } |
| 1119 | |
| 1120 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1121 | buffer0 + s_x2s_pk_off, |
| 1122 | s_x2s_pk_len ); |
| 1123 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1124 | { |
| 1125 | TEST_EQUAL( status, expected_status ); |
| 1126 | break; |
| 1127 | } |
| 1128 | else |
| 1129 | { |
| 1130 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1131 | } |
| 1132 | |
| 1133 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1134 | buffer0 + s_x2s_pr_off, |
| 1135 | s_x2s_pr_len ); |
| 1136 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1137 | { |
| 1138 | TEST_EQUAL( status, expected_status ); |
| 1139 | break; |
| 1140 | } |
| 1141 | else |
| 1142 | { |
| 1143 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1144 | } |
| 1145 | |
| 1146 | /* Error didn't trigger, exit with error */ |
| 1147 | if( inject_error == 3 ) |
| 1148 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /* Client second round Output */ |
| 1152 | buffer1_off = 0; |
| 1153 | |
| 1154 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1155 | buffer1 + buffer1_off, |
| 1156 | 512 - buffer1_off, &c_a_len ) ); |
| 1157 | c_a_off = buffer1_off; |
| 1158 | buffer1_off += c_a_len; |
| 1159 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1160 | buffer1 + buffer1_off, |
| 1161 | 512 - buffer1_off, &c_x2s_pk_len ) ); |
| 1162 | c_x2s_pk_off = buffer1_off; |
| 1163 | buffer1_off += c_x2s_pk_len; |
| 1164 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1165 | buffer1 + buffer1_off, |
| 1166 | 512 - buffer1_off, &c_x2s_pr_len ) ); |
| 1167 | c_x2s_pr_off = buffer1_off; |
| 1168 | buffer1_off += c_x2s_pr_len; |
| 1169 | |
| 1170 | if( client_input_first == 0 ) |
| 1171 | { |
| 1172 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1173 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1174 | buffer0 + s_a_off, s_a_len ); |
| 1175 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1176 | { |
| 1177 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1178 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1179 | } |
| 1180 | else |
| 1181 | { |
| 1182 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1183 | } |
| 1184 | |
| 1185 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1186 | buffer0 + s_x2s_pk_off, |
| 1187 | s_x2s_pk_len ); |
| 1188 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1189 | { |
| 1190 | TEST_EQUAL( status, expected_status ); |
| 1191 | break; |
| 1192 | } |
| 1193 | else |
| 1194 | { |
| 1195 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1196 | } |
| 1197 | |
| 1198 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1199 | buffer0 + s_x2s_pr_off, |
| 1200 | s_x2s_pr_len ); |
| 1201 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1202 | { |
| 1203 | TEST_EQUAL( status, expected_status ); |
| 1204 | break; |
| 1205 | } |
| 1206 | else |
| 1207 | { |
| 1208 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1209 | } |
| 1210 | |
| 1211 | /* Error didn't trigger, exit with error */ |
| 1212 | if( inject_error == 3 ) |
| 1213 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | if( inject_error == 4 ) |
| 1217 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1218 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1219 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1220 | } |
| 1221 | |
| 1222 | /* Server second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1223 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1224 | buffer1 + c_a_off, c_a_len ); |
| 1225 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1226 | { |
| 1227 | TEST_EQUAL( status, expected_status ); |
| 1228 | break; |
| 1229 | } |
| 1230 | else |
| 1231 | { |
| 1232 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1233 | } |
| 1234 | |
| 1235 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1236 | buffer1 + c_x2s_pk_off, c_x2s_pk_len ); |
| 1237 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1238 | { |
| 1239 | TEST_EQUAL( status, expected_status ); |
| 1240 | break; |
| 1241 | } |
| 1242 | else |
| 1243 | { |
| 1244 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1245 | } |
| 1246 | |
| 1247 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1248 | buffer1 + c_x2s_pr_off, c_x2s_pr_len ); |
| 1249 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1250 | { |
| 1251 | TEST_EQUAL( status, expected_status ); |
| 1252 | break; |
| 1253 | } |
| 1254 | else |
| 1255 | { |
| 1256 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1257 | } |
| 1258 | |
| 1259 | /* Error didn't trigger, exit with error */ |
| 1260 | if( inject_error == 4 ) |
| 1261 | goto exit; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1262 | |
| 1263 | break; |
| 1264 | |
| 1265 | } |
| 1266 | |
| 1267 | ret = 1; |
| 1268 | |
| 1269 | exit: |
| 1270 | mbedtls_free( buffer0 ); |
| 1271 | mbedtls_free( buffer1 ); |
| 1272 | return( ret ); |
| 1273 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1274 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1275 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1276 | /* END_HEADER */ |
| 1277 | |
| 1278 | /* BEGIN_DEPENDENCIES |
| 1279 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1280 | * END_DEPENDENCIES |
| 1281 | */ |
| 1282 | |
| 1283 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1284 | void static_checks( ) |
| 1285 | { |
| 1286 | size_t max_truncated_mac_size = |
| 1287 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1288 | |
| 1289 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1290 | * encoding. The shifted mask is the maximum truncated value. The |
| 1291 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1292 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1293 | } |
| 1294 | /* END_CASE */ |
| 1295 | |
| 1296 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1297 | void import_with_policy( int type_arg, |
| 1298 | int usage_arg, int alg_arg, |
| 1299 | int expected_status_arg ) |
| 1300 | { |
| 1301 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1302 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1303 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1304 | psa_key_type_t type = type_arg; |
| 1305 | psa_key_usage_t usage = usage_arg; |
| 1306 | psa_algorithm_t alg = alg_arg; |
| 1307 | psa_status_t expected_status = expected_status_arg; |
| 1308 | const uint8_t key_material[16] = {0}; |
| 1309 | psa_status_t status; |
| 1310 | |
| 1311 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1312 | |
| 1313 | psa_set_key_type( &attributes, type ); |
| 1314 | psa_set_key_usage_flags( &attributes, usage ); |
| 1315 | psa_set_key_algorithm( &attributes, alg ); |
| 1316 | |
| 1317 | status = psa_import_key( &attributes, |
| 1318 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1319 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1320 | TEST_EQUAL( status, expected_status ); |
| 1321 | if( status != PSA_SUCCESS ) |
| 1322 | goto exit; |
| 1323 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1324 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1325 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1326 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1327 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1328 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1329 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1330 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1331 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1332 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1333 | |
| 1334 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1335 | /* |
| 1336 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1337 | * thus reset them as required. |
| 1338 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1339 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1340 | |
| 1341 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1342 | PSA_DONE( ); |
| 1343 | } |
| 1344 | /* END_CASE */ |
| 1345 | |
| 1346 | /* BEGIN_CASE */ |
| 1347 | void import_with_data( data_t *data, int type_arg, |
| 1348 | int attr_bits_arg, |
| 1349 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1350 | { |
| 1351 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1352 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1353 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1354 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1355 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1356 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1357 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1358 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1359 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1360 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1361 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1362 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1363 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1364 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1365 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1366 | if( status != PSA_SUCCESS ) |
| 1367 | goto exit; |
| 1368 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1369 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1370 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1371 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1372 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1373 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1374 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1375 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1376 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1377 | |
| 1378 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1379 | /* |
| 1380 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1381 | * thus reset them as required. |
| 1382 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1383 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1384 | |
| 1385 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1386 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1387 | } |
| 1388 | /* END_CASE */ |
| 1389 | |
| 1390 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1391 | void import_large_key( int type_arg, int byte_size_arg, |
| 1392 | int expected_status_arg ) |
| 1393 | { |
| 1394 | psa_key_type_t type = type_arg; |
| 1395 | size_t byte_size = byte_size_arg; |
| 1396 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1397 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1398 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1399 | psa_status_t status; |
| 1400 | uint8_t *buffer = NULL; |
| 1401 | size_t buffer_size = byte_size + 1; |
| 1402 | size_t n; |
| 1403 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1404 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1405 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1406 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1407 | memset( buffer, 'K', byte_size ); |
| 1408 | |
| 1409 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1410 | |
| 1411 | /* Try importing the key */ |
| 1412 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1413 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1414 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1415 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1416 | TEST_EQUAL( status, expected_status ); |
| 1417 | |
| 1418 | if( status == PSA_SUCCESS ) |
| 1419 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1420 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1421 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1422 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1423 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1424 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1425 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1426 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1427 | for( n = 0; n < byte_size; n++ ) |
| 1428 | TEST_EQUAL( buffer[n], 'K' ); |
| 1429 | for( n = byte_size; n < buffer_size; n++ ) |
| 1430 | TEST_EQUAL( buffer[n], 0 ); |
| 1431 | } |
| 1432 | |
| 1433 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1434 | /* |
| 1435 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1436 | * thus reset them as required. |
| 1437 | */ |
| 1438 | psa_reset_key_attributes( &attributes ); |
| 1439 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1440 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1441 | PSA_DONE( ); |
| 1442 | mbedtls_free( buffer ); |
| 1443 | } |
| 1444 | /* END_CASE */ |
| 1445 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1446 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1447 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1448 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1449 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1450 | size_t bits = bits_arg; |
| 1451 | psa_status_t expected_status = expected_status_arg; |
| 1452 | psa_status_t status; |
| 1453 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1454 | 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] | 1455 | size_t buffer_size = /* Slight overapproximations */ |
| 1456 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1457 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1458 | unsigned char *p; |
| 1459 | int ret; |
| 1460 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1461 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1462 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1463 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1464 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1465 | |
| 1466 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1467 | bits, keypair ) ) >= 0 ); |
| 1468 | length = ret; |
| 1469 | |
| 1470 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1471 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1472 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1473 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1474 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1475 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1476 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1477 | |
| 1478 | exit: |
| 1479 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1480 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1481 | } |
| 1482 | /* END_CASE */ |
| 1483 | |
| 1484 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1485 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1486 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1487 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1488 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1489 | int expected_bits, |
| 1490 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1491 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1492 | int canonical_input ) |
| 1493 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1494 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1495 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1496 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1497 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1498 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1499 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1500 | unsigned char *exported = NULL; |
| 1501 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1502 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1503 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1504 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1505 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1506 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1507 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1508 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1509 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1510 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1511 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1512 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1513 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1514 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1515 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1516 | psa_set_key_algorithm( &attributes, alg ); |
| 1517 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1518 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1519 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1520 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1521 | |
| 1522 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1523 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1524 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1525 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1526 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1527 | |
| 1528 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1529 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1530 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1531 | |
| 1532 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1533 | * and export_size. On errors, the exported length must be 0. */ |
| 1534 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1535 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1536 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1537 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1538 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1539 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1540 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1541 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1542 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1543 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1544 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1545 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1546 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1547 | * this validates the canonical representations. For canonical inputs, |
| 1548 | * this doesn't directly validate the implementation, but it still helps |
| 1549 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1550 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1551 | { |
| 1552 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1553 | goto exit; |
| 1554 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1555 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1556 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1557 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1558 | else |
| 1559 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1560 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1561 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1562 | &key2 ) ); |
| 1563 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1564 | reexported, |
| 1565 | export_size, |
| 1566 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1567 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1568 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1569 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1570 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1571 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1572 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1573 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1574 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1575 | |
| 1576 | destroy: |
| 1577 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1578 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1579 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1580 | |
| 1581 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1582 | /* |
| 1583 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1584 | * thus reset them as required. |
| 1585 | */ |
| 1586 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1587 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1588 | mbedtls_free( exported ); |
| 1589 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1590 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1591 | } |
| 1592 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1593 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1594 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1595 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1596 | int type_arg, |
| 1597 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1598 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1599 | int export_size_delta, |
| 1600 | int expected_export_status_arg, |
| 1601 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1602 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1603 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1604 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1605 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1606 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1607 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1608 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1609 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1610 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1611 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1612 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1613 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1614 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1615 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1616 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1617 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1618 | psa_set_key_algorithm( &attributes, alg ); |
| 1619 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1620 | |
| 1621 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1622 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1623 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1624 | /* Export the public key */ |
| 1625 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1626 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1627 | exported, export_size, |
| 1628 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1629 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1630 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1631 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1632 | 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] | 1633 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1634 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1635 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1636 | TEST_LE_U( expected_public_key->len, |
| 1637 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1638 | TEST_LE_U( expected_public_key->len, |
| 1639 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1640 | TEST_LE_U( expected_public_key->len, |
| 1641 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1642 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1643 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1644 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1645 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1646 | /* |
| 1647 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1648 | * thus reset them as required. |
| 1649 | */ |
| 1650 | psa_reset_key_attributes( &attributes ); |
| 1651 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1652 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1653 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1654 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1655 | } |
| 1656 | /* END_CASE */ |
| 1657 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1658 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1659 | void import_and_exercise_key( data_t *data, |
| 1660 | int type_arg, |
| 1661 | int bits_arg, |
| 1662 | int alg_arg ) |
| 1663 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1664 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1665 | psa_key_type_t type = type_arg; |
| 1666 | size_t bits = bits_arg; |
| 1667 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1668 | 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] | 1669 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1670 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1671 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1672 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1673 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1674 | psa_set_key_usage_flags( &attributes, usage ); |
| 1675 | psa_set_key_algorithm( &attributes, alg ); |
| 1676 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1677 | |
| 1678 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1679 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1680 | |
| 1681 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1682 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1683 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1684 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1685 | |
| 1686 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1687 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1688 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1689 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1690 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1691 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1692 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1693 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1694 | /* |
| 1695 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1696 | * thus reset them as required. |
| 1697 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1698 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1699 | |
| 1700 | psa_reset_key_attributes( &attributes ); |
| 1701 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1702 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1703 | } |
| 1704 | /* END_CASE */ |
| 1705 | |
| 1706 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1707 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1708 | int bits_arg, int expected_bits_arg, |
| 1709 | int usage_arg, int expected_usage_arg, |
| 1710 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1711 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1712 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1713 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1714 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1715 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1716 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1717 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1718 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1719 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1720 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1721 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1722 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1723 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1724 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1725 | psa_set_key_usage_flags( &attributes, usage ); |
| 1726 | psa_set_key_algorithm( &attributes, alg ); |
| 1727 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1728 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1729 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1730 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1731 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1732 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1733 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1734 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1735 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1736 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1737 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1738 | |
| 1739 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1740 | /* |
| 1741 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1742 | * thus reset them as required. |
| 1743 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1744 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1745 | |
| 1746 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1747 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1748 | } |
| 1749 | /* END_CASE */ |
| 1750 | |
| 1751 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1752 | void check_key_policy( int type_arg, int bits_arg, |
| 1753 | int usage_arg, int alg_arg ) |
| 1754 | { |
| 1755 | 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] | 1756 | usage_arg, |
| 1757 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1758 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1759 | goto exit; |
| 1760 | } |
| 1761 | /* END_CASE */ |
| 1762 | |
| 1763 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1764 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1765 | { |
| 1766 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1767 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1768 | * 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] | 1769 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1770 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1771 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1772 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1773 | |
| 1774 | memset( &zero, 0, sizeof( zero ) ); |
| 1775 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1776 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1777 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1778 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1779 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1780 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1781 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1782 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1783 | |
| 1784 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1785 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1786 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1787 | |
| 1788 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1789 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1790 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1791 | |
| 1792 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1793 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1794 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1795 | } |
| 1796 | /* END_CASE */ |
| 1797 | |
| 1798 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1799 | void mac_key_policy( int policy_usage_arg, |
| 1800 | int policy_alg_arg, |
| 1801 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1802 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1803 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1804 | int expected_status_sign_arg, |
| 1805 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1806 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1807 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1808 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1809 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1810 | psa_key_type_t key_type = key_type_arg; |
| 1811 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1812 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1813 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1814 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1815 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1816 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1817 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1818 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1819 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1820 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1821 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1822 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1823 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1824 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1825 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1826 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1827 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1828 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1829 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1830 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1831 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1832 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1833 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1834 | /* Calculate the MAC, one-shot case. */ |
| 1835 | uint8_t input[128] = {0}; |
| 1836 | size_t mac_len; |
| 1837 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1838 | input, 128, |
| 1839 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1840 | expected_status_sign ); |
| 1841 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1842 | /* Calculate the MAC, multi-part case. */ |
| 1843 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1844 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1845 | if( status == PSA_SUCCESS ) |
| 1846 | { |
| 1847 | status = psa_mac_update( &operation, input, 128 ); |
| 1848 | if( status == PSA_SUCCESS ) |
| 1849 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1850 | &mac_len ), |
| 1851 | expected_status_sign ); |
| 1852 | else |
| 1853 | TEST_EQUAL( status, expected_status_sign ); |
| 1854 | } |
| 1855 | else |
| 1856 | { |
| 1857 | TEST_EQUAL( status, expected_status_sign ); |
| 1858 | } |
| 1859 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1860 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1861 | /* Verify correct MAC, one-shot case. */ |
| 1862 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1863 | mac, mac_len ); |
| 1864 | |
| 1865 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1866 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1867 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1868 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1869 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1870 | /* Verify correct MAC, multi-part case. */ |
| 1871 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1872 | if( status == PSA_SUCCESS ) |
| 1873 | { |
| 1874 | status = psa_mac_update( &operation, input, 128 ); |
| 1875 | if( status == PSA_SUCCESS ) |
| 1876 | { |
| 1877 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1878 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1879 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1880 | else |
| 1881 | TEST_EQUAL( status, expected_status_verify ); |
| 1882 | } |
| 1883 | else |
| 1884 | { |
| 1885 | TEST_EQUAL( status, expected_status_verify ); |
| 1886 | } |
| 1887 | } |
| 1888 | else |
| 1889 | { |
| 1890 | TEST_EQUAL( status, expected_status_verify ); |
| 1891 | } |
| 1892 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1893 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1894 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1895 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1896 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1897 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1898 | |
| 1899 | exit: |
| 1900 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1901 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1902 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1903 | } |
| 1904 | /* END_CASE */ |
| 1905 | |
| 1906 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1907 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1908 | int policy_alg, |
| 1909 | int key_type, |
| 1910 | data_t *key_data, |
| 1911 | int exercise_alg ) |
| 1912 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1913 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1914 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1915 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1916 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1917 | size_t output_buffer_size = 0; |
| 1918 | size_t input_buffer_size = 0; |
| 1919 | size_t output_length = 0; |
| 1920 | uint8_t *output = NULL; |
| 1921 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1922 | psa_status_t status; |
| 1923 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1924 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1925 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1926 | input_buffer_size ); |
| 1927 | |
| 1928 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1929 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1930 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1931 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1932 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1933 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1934 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1935 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1936 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1937 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1938 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1939 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1940 | /* Check if no key usage flag implication is done */ |
| 1941 | TEST_EQUAL( policy_usage, |
| 1942 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1943 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1944 | /* Encrypt check, one-shot */ |
| 1945 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1946 | output, output_buffer_size, |
| 1947 | &output_length); |
| 1948 | if( policy_alg == exercise_alg && |
| 1949 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1950 | PSA_ASSERT( status ); |
| 1951 | else |
| 1952 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1953 | |
| 1954 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1955 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1956 | if( policy_alg == exercise_alg && |
| 1957 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1958 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1959 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1960 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1961 | psa_cipher_abort( &operation ); |
| 1962 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1963 | /* Decrypt check, one-shot */ |
| 1964 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 1965 | input, input_buffer_size, |
| 1966 | &output_length); |
| 1967 | if( policy_alg == exercise_alg && |
| 1968 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1969 | PSA_ASSERT( status ); |
| 1970 | else |
| 1971 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1972 | |
| 1973 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1974 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1975 | if( policy_alg == exercise_alg && |
| 1976 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1977 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1978 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1979 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1980 | |
| 1981 | exit: |
| 1982 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1983 | mbedtls_free( input ); |
| 1984 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1985 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1986 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1987 | } |
| 1988 | /* END_CASE */ |
| 1989 | |
| 1990 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1991 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1992 | int policy_alg, |
| 1993 | int key_type, |
| 1994 | data_t *key_data, |
| 1995 | int nonce_length_arg, |
| 1996 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1997 | int exercise_alg, |
| 1998 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1999 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2000 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2001 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2002 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2003 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2004 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2005 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2006 | unsigned char nonce[16] = {0}; |
| 2007 | size_t nonce_length = nonce_length_arg; |
| 2008 | unsigned char tag[16]; |
| 2009 | size_t tag_length = tag_length_arg; |
| 2010 | size_t output_length; |
| 2011 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2012 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 2013 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2014 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2015 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2016 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2017 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2018 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2019 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2020 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2021 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2022 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2023 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2024 | /* Check if no key usage implication is done */ |
| 2025 | TEST_EQUAL( policy_usage, |
| 2026 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2027 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2028 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2029 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2030 | nonce, nonce_length, |
| 2031 | NULL, 0, |
| 2032 | NULL, 0, |
| 2033 | tag, tag_length, |
| 2034 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2035 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2036 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2037 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2038 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2039 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2040 | /* Encrypt check, multi-part */ |
| 2041 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 2042 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2043 | TEST_EQUAL( status, expected_status ); |
| 2044 | else |
| 2045 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2046 | |
| 2047 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2048 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2049 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2050 | nonce, nonce_length, |
| 2051 | NULL, 0, |
| 2052 | tag, tag_length, |
| 2053 | NULL, 0, |
| 2054 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2055 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2056 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2057 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2058 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2059 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2060 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2061 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2062 | /* Decrypt check, multi-part */ |
| 2063 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 2064 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 2065 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2066 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2067 | else |
| 2068 | TEST_EQUAL( status, expected_status ); |
| 2069 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2070 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2071 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2072 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2073 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2074 | } |
| 2075 | /* END_CASE */ |
| 2076 | |
| 2077 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2078 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2079 | int policy_alg, |
| 2080 | int key_type, |
| 2081 | data_t *key_data, |
| 2082 | int exercise_alg ) |
| 2083 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2084 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2085 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2086 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2087 | psa_status_t status; |
| 2088 | size_t key_bits; |
| 2089 | size_t buffer_length; |
| 2090 | unsigned char *buffer = NULL; |
| 2091 | size_t output_length; |
| 2092 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2093 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2094 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2095 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2096 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2097 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2098 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2099 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2100 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2101 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2102 | /* Check if no key usage implication is done */ |
| 2103 | TEST_EQUAL( policy_usage, |
| 2104 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2105 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2106 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2107 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2108 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2109 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2110 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2111 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2112 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2113 | NULL, 0, |
| 2114 | NULL, 0, |
| 2115 | buffer, buffer_length, |
| 2116 | &output_length ); |
| 2117 | if( policy_alg == exercise_alg && |
| 2118 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2119 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2120 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2121 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2122 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2123 | if( buffer_length != 0 ) |
| 2124 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2125 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2126 | buffer, buffer_length, |
| 2127 | NULL, 0, |
| 2128 | buffer, buffer_length, |
| 2129 | &output_length ); |
| 2130 | if( policy_alg == exercise_alg && |
| 2131 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2132 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2133 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2134 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2135 | |
| 2136 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2137 | /* |
| 2138 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2139 | * thus reset them as required. |
| 2140 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2141 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2142 | |
| 2143 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2144 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2145 | mbedtls_free( buffer ); |
| 2146 | } |
| 2147 | /* END_CASE */ |
| 2148 | |
| 2149 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2150 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2151 | int policy_alg, |
| 2152 | int key_type, |
| 2153 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2154 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2155 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2156 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2157 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2158 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2159 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2160 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2161 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2162 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2163 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2164 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2165 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2166 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2167 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2168 | int compatible_alg = payload_length_arg > 0; |
| 2169 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2170 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2171 | size_t signature_length; |
| 2172 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2173 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2174 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2175 | TEST_EQUAL( expected_usage, |
| 2176 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2177 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2178 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2179 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2180 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2181 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2182 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2183 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2184 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2185 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2186 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2187 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 2188 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2189 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2190 | payload, payload_length, |
| 2191 | signature, sizeof( signature ), |
| 2192 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2193 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2194 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2195 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2196 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2197 | |
| 2198 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2199 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2200 | payload, payload_length, |
| 2201 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2202 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2203 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2204 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2205 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2206 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2207 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 2208 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2209 | { |
| 2210 | status = psa_sign_message( key, exercise_alg, |
| 2211 | payload, payload_length, |
| 2212 | signature, sizeof( signature ), |
| 2213 | &signature_length ); |
| 2214 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 2215 | PSA_ASSERT( status ); |
| 2216 | else |
| 2217 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2218 | |
| 2219 | memset( signature, 0, sizeof( signature ) ); |
| 2220 | status = psa_verify_message( key, exercise_alg, |
| 2221 | payload, payload_length, |
| 2222 | signature, sizeof( signature ) ); |
| 2223 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 2224 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 2225 | else |
| 2226 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2227 | } |
| 2228 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2229 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2230 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2231 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2232 | } |
| 2233 | /* END_CASE */ |
| 2234 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2235 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2236 | void derive_key_policy( int policy_usage, |
| 2237 | int policy_alg, |
| 2238 | int key_type, |
| 2239 | data_t *key_data, |
| 2240 | int exercise_alg ) |
| 2241 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2242 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2243 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2244 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2245 | psa_status_t status; |
| 2246 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2247 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2248 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2249 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2250 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2251 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2252 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2253 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2254 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2255 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2256 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2257 | |
| 2258 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2259 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2260 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2261 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2262 | &operation, |
| 2263 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2264 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2265 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2266 | |
| 2267 | status = psa_key_derivation_input_key( &operation, |
| 2268 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2269 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2270 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2271 | if( policy_alg == exercise_alg && |
| 2272 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2273 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2274 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2275 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2276 | |
| 2277 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2278 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2279 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2280 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2281 | } |
| 2282 | /* END_CASE */ |
| 2283 | |
| 2284 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2285 | void agreement_key_policy( int policy_usage, |
| 2286 | int policy_alg, |
| 2287 | int key_type_arg, |
| 2288 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2289 | int exercise_alg, |
| 2290 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2291 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2292 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2293 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2294 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2295 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2296 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2297 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2298 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2299 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2300 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2301 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2302 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2303 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2304 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2305 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2306 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2307 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2308 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2309 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2310 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2311 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2312 | |
| 2313 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2314 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2315 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2316 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2317 | } |
| 2318 | /* END_CASE */ |
| 2319 | |
| 2320 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2321 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2322 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2323 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2324 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2325 | psa_key_type_t key_type = key_type_arg; |
| 2326 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2327 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2328 | psa_key_usage_t usage = usage_arg; |
| 2329 | psa_algorithm_t alg = alg_arg; |
| 2330 | psa_algorithm_t alg2 = alg2_arg; |
| 2331 | |
| 2332 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2333 | |
| 2334 | psa_set_key_usage_flags( &attributes, usage ); |
| 2335 | psa_set_key_algorithm( &attributes, alg ); |
| 2336 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2337 | psa_set_key_type( &attributes, key_type ); |
| 2338 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2339 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2340 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2341 | /* Update the usage flags to obtain implicit usage flags */ |
| 2342 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2343 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2344 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2345 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2346 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2347 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2348 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2349 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2350 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2351 | goto exit; |
| 2352 | |
| 2353 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2354 | /* |
| 2355 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2356 | * thus reset them as required. |
| 2357 | */ |
| 2358 | psa_reset_key_attributes( &got_attributes ); |
| 2359 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2360 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2361 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2362 | } |
| 2363 | /* END_CASE */ |
| 2364 | |
| 2365 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2366 | void raw_agreement_key_policy( int policy_usage, |
| 2367 | int policy_alg, |
| 2368 | int key_type_arg, |
| 2369 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2370 | int exercise_alg, |
| 2371 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2372 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2373 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2374 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2375 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2376 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2377 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2378 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2379 | |
| 2380 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2381 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2382 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2383 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2384 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2385 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2386 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2387 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2388 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2389 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2390 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2391 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2392 | |
| 2393 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2394 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2395 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2396 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2397 | } |
| 2398 | /* END_CASE */ |
| 2399 | |
| 2400 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2401 | void copy_success( int source_usage_arg, |
| 2402 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2403 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2404 | int type_arg, data_t *material, |
| 2405 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2406 | int target_usage_arg, |
| 2407 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2408 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2409 | int expected_usage_arg, |
| 2410 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2411 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2412 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2413 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2414 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2415 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2416 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2417 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2418 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2419 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2420 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2421 | uint8_t *export_buffer = NULL; |
| 2422 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2423 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2424 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2425 | /* Prepare the source key. */ |
| 2426 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2427 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2428 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2429 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2430 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2431 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2432 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2433 | &source_key ) ); |
| 2434 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2435 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2436 | /* Prepare the target attributes. */ |
| 2437 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2438 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2439 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2440 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2441 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2442 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2443 | if( target_usage_arg != -1 ) |
| 2444 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2445 | if( target_alg_arg != -1 ) |
| 2446 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2447 | if( target_alg2_arg != -1 ) |
| 2448 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2449 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2450 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2451 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2452 | PSA_ASSERT( psa_copy_key( source_key, |
| 2453 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2454 | |
| 2455 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2456 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2457 | |
| 2458 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2459 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2460 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2461 | psa_get_key_type( &target_attributes ) ); |
| 2462 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2463 | psa_get_key_bits( &target_attributes ) ); |
| 2464 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2465 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2466 | TEST_EQUAL( expected_alg2, |
| 2467 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2468 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2469 | { |
| 2470 | size_t length; |
| 2471 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2472 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2473 | material->len, &length ) ); |
| 2474 | ASSERT_COMPARE( material->x, material->len, |
| 2475 | export_buffer, length ); |
| 2476 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2477 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2478 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2479 | { |
| 2480 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2481 | goto exit; |
| 2482 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2483 | goto exit; |
| 2484 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2485 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2486 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2487 | |
| 2488 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2489 | /* |
| 2490 | * Source and target key attributes may have been returned by |
| 2491 | * psa_get_key_attributes() thus reset them as required. |
| 2492 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2493 | psa_reset_key_attributes( &source_attributes ); |
| 2494 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2495 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2496 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2497 | mbedtls_free( export_buffer ); |
| 2498 | } |
| 2499 | /* END_CASE */ |
| 2500 | |
| 2501 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2502 | void copy_fail( int source_usage_arg, |
| 2503 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2504 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2505 | int type_arg, data_t *material, |
| 2506 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2507 | int target_usage_arg, |
| 2508 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2509 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2510 | int expected_status_arg ) |
| 2511 | { |
| 2512 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2513 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2514 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2515 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2516 | 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] | 2517 | |
| 2518 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2519 | |
| 2520 | /* Prepare the source key. */ |
| 2521 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2522 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2523 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2524 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2525 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2526 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2527 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2528 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2529 | |
| 2530 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2531 | psa_set_key_id( &target_attributes, key_id ); |
| 2532 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2533 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2534 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2535 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2536 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2537 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2538 | |
| 2539 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2540 | TEST_EQUAL( psa_copy_key( source_key, |
| 2541 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2542 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2543 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2544 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2545 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2546 | exit: |
| 2547 | psa_reset_key_attributes( &source_attributes ); |
| 2548 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2549 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2550 | } |
| 2551 | /* END_CASE */ |
| 2552 | |
| 2553 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2554 | void hash_operation_init( ) |
| 2555 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2556 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2557 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2558 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2559 | * 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] | 2560 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2561 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2562 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2563 | psa_hash_operation_t zero; |
| 2564 | |
| 2565 | memset( &zero, 0, sizeof( zero ) ); |
| 2566 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2567 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2568 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2569 | PSA_ERROR_BAD_STATE ); |
| 2570 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2571 | PSA_ERROR_BAD_STATE ); |
| 2572 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2573 | PSA_ERROR_BAD_STATE ); |
| 2574 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2575 | /* A default hash operation should be abortable without error. */ |
| 2576 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2577 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2578 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2579 | } |
| 2580 | /* END_CASE */ |
| 2581 | |
| 2582 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2583 | void hash_setup( int alg_arg, |
| 2584 | int expected_status_arg ) |
| 2585 | { |
| 2586 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2587 | uint8_t *output = NULL; |
| 2588 | size_t output_size = 0; |
| 2589 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2590 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2591 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2592 | psa_status_t status; |
| 2593 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2594 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2595 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2596 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2597 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2598 | ASSERT_ALLOC( output, output_size ); |
| 2599 | |
| 2600 | status = psa_hash_compute( alg, NULL, 0, |
| 2601 | output, output_size, &output_length ); |
| 2602 | TEST_EQUAL( status, expected_status ); |
| 2603 | |
| 2604 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2605 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2606 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2607 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2608 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2609 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2610 | |
| 2611 | /* If setup failed, reproduce the failure, so as to |
| 2612 | * test the resulting state of the operation object. */ |
| 2613 | if( status != PSA_SUCCESS ) |
| 2614 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2615 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2616 | /* Now the operation object should be reusable. */ |
| 2617 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2618 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2619 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2620 | #endif |
| 2621 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2622 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2623 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2624 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2625 | } |
| 2626 | /* END_CASE */ |
| 2627 | |
| 2628 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2629 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2630 | int output_size_arg, int expected_status_arg ) |
| 2631 | { |
| 2632 | psa_algorithm_t alg = alg_arg; |
| 2633 | uint8_t *output = NULL; |
| 2634 | size_t output_size = output_size_arg; |
| 2635 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2636 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2637 | psa_status_t expected_status = expected_status_arg; |
| 2638 | psa_status_t status; |
| 2639 | |
| 2640 | ASSERT_ALLOC( output, output_size ); |
| 2641 | |
| 2642 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2643 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2644 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2645 | status = psa_hash_compute( alg, input->x, input->len, |
| 2646 | output, output_size, &output_length ); |
| 2647 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2648 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2649 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2650 | /* Hash Compute, multi-part */ |
| 2651 | status = psa_hash_setup( &operation, alg ); |
| 2652 | if( status == PSA_SUCCESS ) |
| 2653 | { |
| 2654 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2655 | if( status == PSA_SUCCESS ) |
| 2656 | { |
| 2657 | status = psa_hash_finish( &operation, output, output_size, |
| 2658 | &output_length ); |
| 2659 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2660 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2661 | else |
| 2662 | TEST_EQUAL( status, expected_status ); |
| 2663 | } |
| 2664 | else |
| 2665 | { |
| 2666 | TEST_EQUAL( status, expected_status ); |
| 2667 | } |
| 2668 | } |
| 2669 | else |
| 2670 | { |
| 2671 | TEST_EQUAL( status, expected_status ); |
| 2672 | } |
| 2673 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2674 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2675 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2676 | mbedtls_free( output ); |
| 2677 | PSA_DONE( ); |
| 2678 | } |
| 2679 | /* END_CASE */ |
| 2680 | |
| 2681 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2682 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2683 | data_t *reference_hash, |
| 2684 | int expected_status_arg ) |
| 2685 | { |
| 2686 | psa_algorithm_t alg = alg_arg; |
| 2687 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2688 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2689 | psa_status_t status; |
| 2690 | |
| 2691 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2692 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2693 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2694 | status = psa_hash_compare( alg, input->x, input->len, |
| 2695 | reference_hash->x, reference_hash->len ); |
| 2696 | TEST_EQUAL( status, expected_status ); |
| 2697 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2698 | /* Hash Compare, multi-part */ |
| 2699 | status = psa_hash_setup( &operation, alg ); |
| 2700 | if( status == PSA_SUCCESS ) |
| 2701 | { |
| 2702 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2703 | if( status == PSA_SUCCESS ) |
| 2704 | { |
| 2705 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2706 | reference_hash->len ); |
| 2707 | TEST_EQUAL( status, expected_status ); |
| 2708 | } |
| 2709 | else |
| 2710 | { |
| 2711 | TEST_EQUAL( status, expected_status ); |
| 2712 | } |
| 2713 | } |
| 2714 | else |
| 2715 | { |
| 2716 | TEST_EQUAL( status, expected_status ); |
| 2717 | } |
| 2718 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2719 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2720 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2721 | PSA_DONE( ); |
| 2722 | } |
| 2723 | /* END_CASE */ |
| 2724 | |
| 2725 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2726 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2727 | data_t *expected_output ) |
| 2728 | { |
| 2729 | psa_algorithm_t alg = alg_arg; |
| 2730 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2731 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2732 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2733 | size_t i; |
| 2734 | |
| 2735 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2736 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2737 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2738 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2739 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2740 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2741 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2742 | ASSERT_COMPARE( output, output_length, |
| 2743 | expected_output->x, expected_output->len ); |
| 2744 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2745 | /* Compute with tight buffer, multi-part */ |
| 2746 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2747 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2748 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2749 | PSA_HASH_LENGTH( alg ), |
| 2750 | &output_length ) ); |
| 2751 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2752 | ASSERT_COMPARE( output, output_length, |
| 2753 | expected_output->x, expected_output->len ); |
| 2754 | |
| 2755 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2756 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2757 | output, sizeof( output ), |
| 2758 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2759 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2760 | ASSERT_COMPARE( output, output_length, |
| 2761 | expected_output->x, expected_output->len ); |
| 2762 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2763 | /* Compute with larger buffer, multi-part */ |
| 2764 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2765 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2766 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2767 | sizeof( output ), &output_length ) ); |
| 2768 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2769 | ASSERT_COMPARE( output, output_length, |
| 2770 | expected_output->x, expected_output->len ); |
| 2771 | |
| 2772 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2773 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2774 | output, output_length ) ); |
| 2775 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2776 | /* Compare with correct hash, multi-part */ |
| 2777 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2778 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2779 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2780 | output_length ) ); |
| 2781 | |
| 2782 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2783 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2784 | output, output_length + 1 ), |
| 2785 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2786 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2787 | /* Compare with trailing garbage, multi-part */ |
| 2788 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2789 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2790 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2791 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2792 | |
| 2793 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2794 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2795 | output, output_length - 1 ), |
| 2796 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2797 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2798 | /* Compare with truncated hash, multi-part */ |
| 2799 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2800 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2801 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2802 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2803 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2804 | /* Compare with corrupted value */ |
| 2805 | for( i = 0; i < output_length; i++ ) |
| 2806 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2807 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2808 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2809 | |
| 2810 | /* One-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2811 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2812 | output, output_length ), |
| 2813 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2814 | |
| 2815 | /* Multi-Part */ |
| 2816 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2817 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2818 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length ), |
| 2819 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2820 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2821 | output[i] ^= 1; |
| 2822 | } |
| 2823 | |
| 2824 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2825 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2826 | PSA_DONE( ); |
| 2827 | } |
| 2828 | /* END_CASE */ |
| 2829 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2830 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2831 | void hash_bad_order( ) |
| 2832 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2833 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2834 | unsigned char input[] = ""; |
| 2835 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2836 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2837 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2838 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2839 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2840 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2841 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2842 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2843 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2844 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2845 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2846 | /* Call setup twice in a row. */ |
| 2847 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2848 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2849 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2850 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2851 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2852 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2853 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2854 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2855 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2856 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2857 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2858 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2859 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2860 | /* Check that update calls abort on error. */ |
| 2861 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2862 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2863 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2864 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2865 | PSA_ERROR_BAD_STATE ); |
| 2866 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2867 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2868 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2869 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2870 | /* Call update after finish. */ |
| 2871 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2872 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2873 | hash, sizeof( hash ), &hash_len ) ); |
| 2874 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2875 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2876 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2877 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2878 | /* Call verify without calling setup beforehand. */ |
| 2879 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2880 | valid_hash, sizeof( valid_hash ) ), |
| 2881 | PSA_ERROR_BAD_STATE ); |
| 2882 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2883 | |
| 2884 | /* Call verify after finish. */ |
| 2885 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2886 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2887 | hash, sizeof( hash ), &hash_len ) ); |
| 2888 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2889 | valid_hash, sizeof( valid_hash ) ), |
| 2890 | PSA_ERROR_BAD_STATE ); |
| 2891 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2892 | |
| 2893 | /* Call verify twice in a row. */ |
| 2894 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2895 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2896 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2897 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2898 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2899 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2900 | valid_hash, sizeof( valid_hash ) ), |
| 2901 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2902 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2903 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2904 | |
| 2905 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2906 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2907 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2908 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2909 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2910 | |
| 2911 | /* Call finish twice in a row. */ |
| 2912 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2913 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2914 | hash, sizeof( hash ), &hash_len ) ); |
| 2915 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2916 | hash, sizeof( hash ), &hash_len ), |
| 2917 | PSA_ERROR_BAD_STATE ); |
| 2918 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2919 | |
| 2920 | /* Call finish after calling verify. */ |
| 2921 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2922 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2923 | valid_hash, sizeof( valid_hash ) ) ); |
| 2924 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2925 | hash, sizeof( hash ), &hash_len ), |
| 2926 | PSA_ERROR_BAD_STATE ); |
| 2927 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2928 | |
| 2929 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2930 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2931 | } |
| 2932 | /* END_CASE */ |
| 2933 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2934 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2935 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2936 | { |
| 2937 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2938 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2939 | * appended to it */ |
| 2940 | unsigned char hash[] = { |
| 2941 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2942 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2943 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2944 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2945 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2946 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2947 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2948 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2949 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2950 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2951 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2952 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2953 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2954 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2955 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2956 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2957 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2958 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2959 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2960 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2961 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2962 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2963 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2964 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2965 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2966 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2967 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2968 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2969 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2970 | } |
| 2971 | /* END_CASE */ |
| 2972 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2973 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2974 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2975 | { |
| 2976 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2977 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2978 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2979 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2980 | size_t hash_len; |
| 2981 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2982 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2983 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2984 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2985 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2986 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2987 | hash, expected_size - 1, &hash_len ), |
| 2988 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2989 | |
| 2990 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2991 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2992 | } |
| 2993 | /* END_CASE */ |
| 2994 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2995 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2996 | void hash_clone_source_state( ) |
| 2997 | { |
| 2998 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2999 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3000 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3001 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3002 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3003 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3004 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3005 | size_t hash_len; |
| 3006 | |
| 3007 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3008 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 3009 | |
| 3010 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3011 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3012 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3013 | hash, sizeof( hash ), &hash_len ) ); |
| 3014 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3015 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3016 | |
| 3017 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 3018 | PSA_ERROR_BAD_STATE ); |
| 3019 | |
| 3020 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 3021 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 3022 | hash, sizeof( hash ), &hash_len ) ); |
| 3023 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 3024 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3025 | hash, sizeof( hash ), &hash_len ) ); |
| 3026 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 3027 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 3028 | hash, sizeof( hash ), &hash_len ) ); |
| 3029 | |
| 3030 | exit: |
| 3031 | psa_hash_abort( &op_source ); |
| 3032 | psa_hash_abort( &op_init ); |
| 3033 | psa_hash_abort( &op_setup ); |
| 3034 | psa_hash_abort( &op_finished ); |
| 3035 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3036 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3037 | } |
| 3038 | /* END_CASE */ |
| 3039 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3040 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3041 | void hash_clone_target_state( ) |
| 3042 | { |
| 3043 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3044 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3045 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3046 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3047 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3048 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3049 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3050 | size_t hash_len; |
| 3051 | |
| 3052 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3053 | |
| 3054 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3055 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3056 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3057 | hash, sizeof( hash ), &hash_len ) ); |
| 3058 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3059 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3060 | |
| 3061 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 3062 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 3063 | hash, sizeof( hash ), &hash_len ) ); |
| 3064 | |
| 3065 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 3066 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 3067 | PSA_ERROR_BAD_STATE ); |
| 3068 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 3069 | PSA_ERROR_BAD_STATE ); |
| 3070 | |
| 3071 | exit: |
| 3072 | psa_hash_abort( &op_target ); |
| 3073 | psa_hash_abort( &op_init ); |
| 3074 | psa_hash_abort( &op_setup ); |
| 3075 | psa_hash_abort( &op_finished ); |
| 3076 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3077 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3078 | } |
| 3079 | /* END_CASE */ |
| 3080 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3081 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3082 | void mac_operation_init( ) |
| 3083 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3084 | const uint8_t input[1] = { 0 }; |
| 3085 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3086 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3087 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3088 | * 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] | 3089 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3090 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 3091 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3092 | psa_mac_operation_t zero; |
| 3093 | |
| 3094 | memset( &zero, 0, sizeof( zero ) ); |
| 3095 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3096 | /* A freshly-initialized MAC operation should not be usable. */ |
| 3097 | TEST_EQUAL( psa_mac_update( &func, |
| 3098 | input, sizeof( input ) ), |
| 3099 | PSA_ERROR_BAD_STATE ); |
| 3100 | TEST_EQUAL( psa_mac_update( &init, |
| 3101 | input, sizeof( input ) ), |
| 3102 | PSA_ERROR_BAD_STATE ); |
| 3103 | TEST_EQUAL( psa_mac_update( &zero, |
| 3104 | input, sizeof( input ) ), |
| 3105 | PSA_ERROR_BAD_STATE ); |
| 3106 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3107 | /* A default MAC operation should be abortable without error. */ |
| 3108 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 3109 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 3110 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3111 | } |
| 3112 | /* END_CASE */ |
| 3113 | |
| 3114 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3115 | void mac_setup( int key_type_arg, |
| 3116 | data_t *key, |
| 3117 | int alg_arg, |
| 3118 | int expected_status_arg ) |
| 3119 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3120 | psa_key_type_t key_type = key_type_arg; |
| 3121 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3122 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3123 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3124 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3125 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3126 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3127 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3128 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3129 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3130 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3131 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 3132 | &operation, &status ) ) |
| 3133 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3134 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3135 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3136 | /* The operation object should be reusable. */ |
| 3137 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3138 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3139 | smoke_test_key_data, |
| 3140 | sizeof( smoke_test_key_data ), |
| 3141 | KNOWN_SUPPORTED_MAC_ALG, |
| 3142 | &operation, &status ) ) |
| 3143 | goto exit; |
| 3144 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3145 | #endif |
| 3146 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3147 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3148 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3149 | } |
| 3150 | /* END_CASE */ |
| 3151 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3152 | /* 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] | 3153 | void mac_bad_order( ) |
| 3154 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3155 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3156 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3157 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3158 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3159 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3160 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3161 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3162 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3163 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3164 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3165 | size_t sign_mac_length = 0; |
| 3166 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3167 | const uint8_t verify_mac[] = { |
| 3168 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3169 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 3170 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 3171 | |
| 3172 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3173 | 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] | 3174 | psa_set_key_algorithm( &attributes, alg ); |
| 3175 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3176 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3177 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3178 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3179 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3180 | /* Call update without calling setup beforehand. */ |
| 3181 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3182 | PSA_ERROR_BAD_STATE ); |
| 3183 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3184 | |
| 3185 | /* Call sign finish without calling setup beforehand. */ |
| 3186 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 3187 | &sign_mac_length), |
| 3188 | PSA_ERROR_BAD_STATE ); |
| 3189 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3190 | |
| 3191 | /* Call verify finish without calling setup beforehand. */ |
| 3192 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3193 | verify_mac, sizeof( verify_mac ) ), |
| 3194 | PSA_ERROR_BAD_STATE ); |
| 3195 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3196 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3197 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3198 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3199 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3200 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3201 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3202 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3203 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3204 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3205 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3206 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3207 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3208 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3209 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3210 | sign_mac, sizeof( sign_mac ), |
| 3211 | &sign_mac_length ) ); |
| 3212 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3213 | PSA_ERROR_BAD_STATE ); |
| 3214 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3215 | |
| 3216 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3217 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3218 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3219 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3220 | verify_mac, sizeof( verify_mac ) ) ); |
| 3221 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3222 | PSA_ERROR_BAD_STATE ); |
| 3223 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3224 | |
| 3225 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3226 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3227 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3228 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3229 | sign_mac, sizeof( sign_mac ), |
| 3230 | &sign_mac_length ) ); |
| 3231 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3232 | sign_mac, sizeof( sign_mac ), |
| 3233 | &sign_mac_length ), |
| 3234 | PSA_ERROR_BAD_STATE ); |
| 3235 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3236 | |
| 3237 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3238 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3239 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3240 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3241 | verify_mac, sizeof( verify_mac ) ) ); |
| 3242 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3243 | verify_mac, sizeof( verify_mac ) ), |
| 3244 | PSA_ERROR_BAD_STATE ); |
| 3245 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3246 | |
| 3247 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3248 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3249 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3250 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3251 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3252 | verify_mac, sizeof( verify_mac ) ), |
| 3253 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3254 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3255 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3256 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3257 | |
| 3258 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3259 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3260 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3261 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3262 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3263 | sign_mac, sizeof( sign_mac ), |
| 3264 | &sign_mac_length ), |
| 3265 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3266 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3267 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3268 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3269 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3270 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3271 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3272 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3273 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3274 | } |
| 3275 | /* END_CASE */ |
| 3276 | |
| 3277 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3278 | void mac_sign_verify_multi( int key_type_arg, |
| 3279 | data_t *key_data, |
| 3280 | int alg_arg, |
| 3281 | data_t *input, |
| 3282 | int is_verify, |
| 3283 | data_t *expected_mac ) |
| 3284 | { |
| 3285 | size_t data_part_len = 0; |
| 3286 | |
| 3287 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3288 | { |
| 3289 | /* Split data into length(data_part_len) parts. */ |
| 3290 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3291 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3292 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3293 | alg_arg, |
| 3294 | input, data_part_len, |
| 3295 | expected_mac, |
| 3296 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3297 | break; |
| 3298 | |
| 3299 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3300 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3301 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3302 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3303 | alg_arg, |
| 3304 | input, data_part_len, |
| 3305 | expected_mac, |
| 3306 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3307 | break; |
| 3308 | } |
| 3309 | |
| 3310 | /* Goto is required to silence warnings about unused labels, as we |
| 3311 | * don't actually do any test assertions in this function. */ |
| 3312 | goto exit; |
| 3313 | } |
| 3314 | /* END_CASE */ |
| 3315 | |
| 3316 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3317 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3318 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3319 | int alg_arg, |
| 3320 | data_t *input, |
| 3321 | data_t *expected_mac ) |
| 3322 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3323 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3324 | psa_key_type_t key_type = key_type_arg; |
| 3325 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3326 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3327 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3328 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3329 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3330 | 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] | 3331 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3332 | const size_t output_sizes_to_test[] = { |
| 3333 | 0, |
| 3334 | 1, |
| 3335 | expected_mac->len - 1, |
| 3336 | expected_mac->len, |
| 3337 | expected_mac->len + 1, |
| 3338 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3339 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3340 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3341 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3342 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3343 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3344 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3345 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3346 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3347 | psa_set_key_algorithm( &attributes, alg ); |
| 3348 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3349 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3350 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3351 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3352 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3353 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3354 | { |
| 3355 | const size_t output_size = output_sizes_to_test[i]; |
| 3356 | psa_status_t expected_status = |
| 3357 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3358 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3359 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3360 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3361 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3362 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3363 | /* Calculate the MAC, one-shot case. */ |
| 3364 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3365 | input->x, input->len, |
| 3366 | actual_mac, output_size, &mac_length ), |
| 3367 | expected_status ); |
| 3368 | if( expected_status == PSA_SUCCESS ) |
| 3369 | { |
| 3370 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3371 | actual_mac, mac_length ); |
| 3372 | } |
| 3373 | |
| 3374 | if( output_size > 0 ) |
| 3375 | memset( actual_mac, 0, output_size ); |
| 3376 | |
| 3377 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3378 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3379 | PSA_ASSERT( psa_mac_update( &operation, |
| 3380 | input->x, input->len ) ); |
| 3381 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3382 | actual_mac, output_size, |
| 3383 | &mac_length ), |
| 3384 | expected_status ); |
| 3385 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3386 | |
| 3387 | if( expected_status == PSA_SUCCESS ) |
| 3388 | { |
| 3389 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3390 | actual_mac, mac_length ); |
| 3391 | } |
| 3392 | mbedtls_free( actual_mac ); |
| 3393 | actual_mac = NULL; |
| 3394 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3395 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3396 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3397 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3398 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3399 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3400 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3401 | } |
| 3402 | /* END_CASE */ |
| 3403 | |
| 3404 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3405 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3406 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3407 | int alg_arg, |
| 3408 | data_t *input, |
| 3409 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3410 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3411 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3412 | psa_key_type_t key_type = key_type_arg; |
| 3413 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3414 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3415 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3416 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3417 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3418 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3419 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3420 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3421 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3422 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3423 | psa_set_key_algorithm( &attributes, alg ); |
| 3424 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3425 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3426 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3427 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3428 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3429 | /* Verify correct MAC, one-shot case. */ |
| 3430 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3431 | expected_mac->x, expected_mac->len ) ); |
| 3432 | |
| 3433 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3434 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3435 | PSA_ASSERT( psa_mac_update( &operation, |
| 3436 | input->x, input->len ) ); |
| 3437 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3438 | expected_mac->x, |
| 3439 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3440 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3441 | /* Test a MAC that's too short, one-shot case. */ |
| 3442 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3443 | input->x, input->len, |
| 3444 | expected_mac->x, |
| 3445 | expected_mac->len - 1 ), |
| 3446 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3447 | |
| 3448 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3449 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3450 | PSA_ASSERT( psa_mac_update( &operation, |
| 3451 | input->x, input->len ) ); |
| 3452 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3453 | expected_mac->x, |
| 3454 | expected_mac->len - 1 ), |
| 3455 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3456 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3457 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3458 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3459 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3460 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3461 | input->x, input->len, |
| 3462 | perturbed_mac, expected_mac->len + 1 ), |
| 3463 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3464 | |
| 3465 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3466 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3467 | PSA_ASSERT( psa_mac_update( &operation, |
| 3468 | input->x, input->len ) ); |
| 3469 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3470 | perturbed_mac, |
| 3471 | expected_mac->len + 1 ), |
| 3472 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3473 | |
| 3474 | /* Test changing one byte. */ |
| 3475 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3476 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3477 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3478 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3479 | |
| 3480 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3481 | input->x, input->len, |
| 3482 | perturbed_mac, expected_mac->len ), |
| 3483 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3484 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3485 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3486 | PSA_ASSERT( psa_mac_update( &operation, |
| 3487 | input->x, input->len ) ); |
| 3488 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3489 | perturbed_mac, |
| 3490 | expected_mac->len ), |
| 3491 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3492 | perturbed_mac[i] ^= 1; |
| 3493 | } |
| 3494 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3495 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3496 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3497 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3498 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3499 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3500 | } |
| 3501 | /* END_CASE */ |
| 3502 | |
| 3503 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3504 | void cipher_operation_init( ) |
| 3505 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3506 | const uint8_t input[1] = { 0 }; |
| 3507 | unsigned char output[1] = { 0 }; |
| 3508 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3509 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3510 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3511 | * 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] | 3512 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3513 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3514 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3515 | psa_cipher_operation_t zero; |
| 3516 | |
| 3517 | memset( &zero, 0, sizeof( zero ) ); |
| 3518 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3519 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3520 | TEST_EQUAL( psa_cipher_update( &func, |
| 3521 | input, sizeof( input ), |
| 3522 | output, sizeof( output ), |
| 3523 | &output_length ), |
| 3524 | PSA_ERROR_BAD_STATE ); |
| 3525 | TEST_EQUAL( psa_cipher_update( &init, |
| 3526 | input, sizeof( input ), |
| 3527 | output, sizeof( output ), |
| 3528 | &output_length ), |
| 3529 | PSA_ERROR_BAD_STATE ); |
| 3530 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3531 | input, sizeof( input ), |
| 3532 | output, sizeof( output ), |
| 3533 | &output_length ), |
| 3534 | PSA_ERROR_BAD_STATE ); |
| 3535 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3536 | /* A default cipher operation should be abortable without error. */ |
| 3537 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3538 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3539 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3540 | } |
| 3541 | /* END_CASE */ |
| 3542 | |
| 3543 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3544 | void cipher_setup( int key_type_arg, |
| 3545 | data_t *key, |
| 3546 | int alg_arg, |
| 3547 | int expected_status_arg ) |
| 3548 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3549 | psa_key_type_t key_type = key_type_arg; |
| 3550 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3551 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3552 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3553 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3554 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3555 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3556 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3557 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3558 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3559 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3560 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3561 | &operation, &status ) ) |
| 3562 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3563 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3564 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3565 | /* The operation object should be reusable. */ |
| 3566 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3567 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3568 | smoke_test_key_data, |
| 3569 | sizeof( smoke_test_key_data ), |
| 3570 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3571 | &operation, &status ) ) |
| 3572 | goto exit; |
| 3573 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3574 | #endif |
| 3575 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3576 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3577 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3578 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3579 | } |
| 3580 | /* END_CASE */ |
| 3581 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3582 | /* 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] | 3583 | void cipher_bad_order( ) |
| 3584 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3585 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3586 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3587 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3588 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3589 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3590 | 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] | 3591 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3592 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3593 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3594 | const uint8_t text[] = { |
| 3595 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3596 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3597 | 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] | 3598 | size_t length = 0; |
| 3599 | |
| 3600 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3601 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3602 | psa_set_key_algorithm( &attributes, alg ); |
| 3603 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3604 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3605 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3606 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3607 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3608 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3609 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3610 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3611 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3612 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3613 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3614 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3615 | |
| 3616 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3617 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3618 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3619 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3620 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3621 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3622 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3623 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3624 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3625 | /* Generate an IV without calling setup beforehand. */ |
| 3626 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3627 | buffer, sizeof( buffer ), |
| 3628 | &length ), |
| 3629 | PSA_ERROR_BAD_STATE ); |
| 3630 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3631 | |
| 3632 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3633 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3634 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3635 | buffer, sizeof( buffer ), |
| 3636 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3637 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3638 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3639 | buffer, sizeof( buffer ), |
| 3640 | &length ), |
| 3641 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3642 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3643 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3644 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3645 | |
| 3646 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3647 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3648 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3649 | iv, sizeof( iv ) ) ); |
| 3650 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3651 | buffer, sizeof( buffer ), |
| 3652 | &length ), |
| 3653 | PSA_ERROR_BAD_STATE ); |
| 3654 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3655 | |
| 3656 | /* Set an IV without calling setup beforehand. */ |
| 3657 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3658 | iv, sizeof( iv ) ), |
| 3659 | PSA_ERROR_BAD_STATE ); |
| 3660 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3661 | |
| 3662 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3663 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3664 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3665 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3666 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3667 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3668 | iv, sizeof( iv ) ), |
| 3669 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3670 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3671 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3672 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3673 | |
| 3674 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3675 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3676 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3677 | buffer, sizeof( buffer ), |
| 3678 | &length ) ); |
| 3679 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3680 | iv, sizeof( iv ) ), |
| 3681 | PSA_ERROR_BAD_STATE ); |
| 3682 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3683 | |
| 3684 | /* Call update without calling setup beforehand. */ |
| 3685 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3686 | text, sizeof( text ), |
| 3687 | buffer, sizeof( buffer ), |
| 3688 | &length ), |
| 3689 | PSA_ERROR_BAD_STATE ); |
| 3690 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3691 | |
| 3692 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3693 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3694 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3695 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3696 | text, sizeof( text ), |
| 3697 | buffer, sizeof( buffer ), |
| 3698 | &length ), |
| 3699 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3700 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3701 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3702 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3703 | |
| 3704 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3705 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3706 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3707 | iv, sizeof( iv ) ) ); |
| 3708 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3709 | buffer, sizeof( buffer ), &length ) ); |
| 3710 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3711 | text, sizeof( text ), |
| 3712 | buffer, sizeof( buffer ), |
| 3713 | &length ), |
| 3714 | PSA_ERROR_BAD_STATE ); |
| 3715 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3716 | |
| 3717 | /* Call finish without calling setup beforehand. */ |
| 3718 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3719 | buffer, sizeof( buffer ), &length ), |
| 3720 | PSA_ERROR_BAD_STATE ); |
| 3721 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3722 | |
| 3723 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3724 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3725 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3726 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3727 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3728 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3729 | buffer, sizeof( buffer ), &length ), |
| 3730 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3731 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3732 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3733 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3734 | |
| 3735 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3736 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3737 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3738 | iv, sizeof( iv ) ) ); |
| 3739 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3740 | buffer, sizeof( buffer ), &length ) ); |
| 3741 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3742 | buffer, sizeof( buffer ), &length ), |
| 3743 | PSA_ERROR_BAD_STATE ); |
| 3744 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3745 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3746 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3747 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3748 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3749 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3750 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3751 | } |
| 3752 | /* END_CASE */ |
| 3753 | |
| 3754 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3755 | void cipher_encrypt_fail( int alg_arg, |
| 3756 | int key_type_arg, |
| 3757 | data_t *key_data, |
| 3758 | data_t *input, |
| 3759 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3760 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3761 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3762 | psa_status_t status; |
| 3763 | psa_key_type_t key_type = key_type_arg; |
| 3764 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3765 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3766 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3767 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3768 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3769 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3770 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3771 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3772 | size_t function_output_length; |
| 3773 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3774 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3775 | |
| 3776 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3777 | { |
| 3778 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3779 | |
| 3780 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3781 | psa_set_key_algorithm( &attributes, alg ); |
| 3782 | psa_set_key_type( &attributes, key_type ); |
| 3783 | |
| 3784 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3785 | input->len ); |
| 3786 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3787 | |
| 3788 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3789 | &key ) ); |
| 3790 | } |
| 3791 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3792 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3793 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3794 | output_buffer_size, &output_length ); |
| 3795 | |
| 3796 | TEST_EQUAL( status, expected_status ); |
| 3797 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3798 | /* Encrypt, multi-part */ |
| 3799 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3800 | if( status == PSA_SUCCESS ) |
| 3801 | { |
| 3802 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3803 | { |
| 3804 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3805 | iv, iv_size, |
| 3806 | &iv_length ) ); |
| 3807 | } |
| 3808 | |
| 3809 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3810 | output, output_buffer_size, |
| 3811 | &function_output_length ); |
| 3812 | if( status == PSA_SUCCESS ) |
| 3813 | { |
| 3814 | output_length += function_output_length; |
| 3815 | |
| 3816 | status = psa_cipher_finish( &operation, output + output_length, |
| 3817 | output_buffer_size - output_length, |
| 3818 | &function_output_length ); |
| 3819 | |
| 3820 | TEST_EQUAL( status, expected_status ); |
| 3821 | } |
| 3822 | else |
| 3823 | { |
| 3824 | TEST_EQUAL( status, expected_status ); |
| 3825 | } |
| 3826 | } |
| 3827 | else |
| 3828 | { |
| 3829 | TEST_EQUAL( status, expected_status ); |
| 3830 | } |
| 3831 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3832 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3833 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3834 | mbedtls_free( output ); |
| 3835 | psa_destroy_key( key ); |
| 3836 | PSA_DONE( ); |
| 3837 | } |
| 3838 | /* END_CASE */ |
| 3839 | |
| 3840 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3841 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3842 | data_t *input, int iv_length, |
| 3843 | int expected_result ) |
| 3844 | { |
| 3845 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3846 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3847 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3848 | size_t output_buffer_size = 0; |
| 3849 | unsigned char *output = NULL; |
| 3850 | |
| 3851 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3852 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3853 | |
| 3854 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3855 | |
| 3856 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3857 | psa_set_key_algorithm( &attributes, alg ); |
| 3858 | psa_set_key_type( &attributes, key_type ); |
| 3859 | |
| 3860 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3861 | &key ) ); |
| 3862 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3863 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3864 | iv_length ) ); |
| 3865 | |
| 3866 | exit: |
| 3867 | psa_cipher_abort( &operation ); |
| 3868 | mbedtls_free( output ); |
| 3869 | psa_destroy_key( key ); |
| 3870 | PSA_DONE( ); |
| 3871 | } |
| 3872 | /* END_CASE */ |
| 3873 | |
| 3874 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3875 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3876 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3877 | { |
| 3878 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3879 | psa_key_type_t key_type = key_type_arg; |
| 3880 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3881 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3882 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3883 | unsigned char *output = NULL; |
| 3884 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3885 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3886 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3887 | |
| 3888 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3889 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3890 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3891 | TEST_LE_U( ciphertext->len, |
| 3892 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3893 | 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] | 3894 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3895 | TEST_LE_U( plaintext->len, |
| 3896 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3897 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3898 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3899 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3900 | |
| 3901 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3902 | psa_set_key_usage_flags( &attributes, |
| 3903 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3904 | psa_set_key_algorithm( &attributes, alg ); |
| 3905 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3906 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3907 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3908 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3909 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3910 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3911 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3912 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3913 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3914 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3915 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3916 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3917 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3918 | PSA_ERROR_BAD_STATE ); |
| 3919 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3920 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3921 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3922 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3923 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3924 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3925 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3926 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3927 | &length ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3928 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3929 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3930 | /* Multipart encryption */ |
| 3931 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3932 | output_length = 0; |
| 3933 | length = ~0; |
| 3934 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3935 | plaintext->x, plaintext->len, |
| 3936 | output, output_buffer_size, |
| 3937 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3938 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3939 | output_length += length; |
| 3940 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3941 | output + output_length, |
| 3942 | output_buffer_size - output_length, |
| 3943 | &length ) ); |
| 3944 | output_length += length; |
| 3945 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3946 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3947 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3948 | /* Multipart encryption */ |
| 3949 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3950 | output_length = 0; |
| 3951 | length = ~0; |
| 3952 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3953 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3954 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3955 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3956 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3957 | output_length += length; |
| 3958 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3959 | output + output_length, |
| 3960 | output_buffer_size - output_length, |
| 3961 | &length ) ); |
| 3962 | output_length += length; |
| 3963 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 3964 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3965 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3966 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3967 | output_length = ~0; |
| 3968 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 3969 | output, output_buffer_size, |
| 3970 | &output_length ) ); |
| 3971 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 3972 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3973 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3974 | /* One-shot decryption */ |
| 3975 | output_length = ~0; |
| 3976 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 3977 | output, output_buffer_size, |
| 3978 | &output_length ) ); |
| 3979 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3980 | output, output_length ); |
| 3981 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3982 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3983 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3984 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3985 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3986 | psa_destroy_key( key ); |
| 3987 | PSA_DONE( ); |
| 3988 | } |
| 3989 | /* END_CASE */ |
| 3990 | |
| 3991 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3992 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 3993 | { |
| 3994 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3995 | psa_algorithm_t alg = alg_arg; |
| 3996 | psa_key_type_t key_type = key_type_arg; |
| 3997 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3998 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3999 | psa_status_t status; |
| 4000 | |
| 4001 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4002 | |
| 4003 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4004 | psa_set_key_algorithm( &attributes, alg ); |
| 4005 | psa_set_key_type( &attributes, key_type ); |
| 4006 | |
| 4007 | /* Usage of either of these two size macros would cause divide by zero |
| 4008 | * with incorrect key types previously. Input length should be irrelevant |
| 4009 | * here. */ |
| 4010 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 4011 | 0 ); |
| 4012 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 4013 | |
| 4014 | |
| 4015 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4016 | &key ) ); |
| 4017 | |
| 4018 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4019 | * Encrypt or decrypt will end up in the same place. */ |
| 4020 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 4021 | |
| 4022 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 4023 | |
| 4024 | exit: |
| 4025 | psa_cipher_abort( &operation ); |
| 4026 | psa_destroy_key( key ); |
| 4027 | PSA_DONE( ); |
| 4028 | } |
| 4029 | /* END_CASE */ |
| 4030 | |
| 4031 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4032 | void cipher_encrypt_validation( int alg_arg, |
| 4033 | int key_type_arg, |
| 4034 | data_t *key_data, |
| 4035 | data_t *input ) |
| 4036 | { |
| 4037 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4038 | psa_key_type_t key_type = key_type_arg; |
| 4039 | psa_algorithm_t alg = alg_arg; |
| 4040 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 4041 | unsigned char *output1 = NULL; |
| 4042 | size_t output1_buffer_size = 0; |
| 4043 | size_t output1_length = 0; |
| 4044 | unsigned char *output2 = NULL; |
| 4045 | size_t output2_buffer_size = 0; |
| 4046 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4047 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4048 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4049 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4050 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4051 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4052 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4053 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4054 | psa_set_key_algorithm( &attributes, alg ); |
| 4055 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4056 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4057 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 4058 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4059 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4060 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 4061 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 4062 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4063 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4064 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4065 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4066 | /* The one-shot cipher encryption uses generated iv so validating |
| 4067 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4068 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 4069 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4070 | TEST_LE_U( output1_length, |
| 4071 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4072 | TEST_LE_U( output1_length, |
| 4073 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4074 | |
| 4075 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 4076 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4077 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4078 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4079 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4080 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4081 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4082 | TEST_LE_U( function_output_length, |
| 4083 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4084 | TEST_LE_U( function_output_length, |
| 4085 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4086 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4087 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4088 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4089 | output2 + output2_length, |
| 4090 | output2_buffer_size - output2_length, |
| 4091 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4092 | TEST_LE_U( function_output_length, |
| 4093 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4094 | TEST_LE_U( function_output_length, |
| 4095 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4096 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4097 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4098 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4099 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 4100 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4101 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4102 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4103 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4104 | mbedtls_free( output1 ); |
| 4105 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4106 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4107 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4108 | } |
| 4109 | /* END_CASE */ |
| 4110 | |
| 4111 | /* BEGIN_CASE */ |
| 4112 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4113 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4114 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4115 | int first_part_size_arg, |
| 4116 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4117 | data_t *expected_output, |
| 4118 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4119 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4120 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4121 | psa_key_type_t key_type = key_type_arg; |
| 4122 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4123 | psa_status_t status; |
| 4124 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4125 | size_t first_part_size = first_part_size_arg; |
| 4126 | size_t output1_length = output1_length_arg; |
| 4127 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4128 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4129 | size_t output_buffer_size = 0; |
| 4130 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4131 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4132 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4133 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4134 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4135 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4136 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4137 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4138 | psa_set_key_algorithm( &attributes, alg ); |
| 4139 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4140 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4141 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4142 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4143 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4144 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4145 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4146 | if( iv->len > 0 ) |
| 4147 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4148 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4149 | } |
| 4150 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4151 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4152 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4153 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4154 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4155 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4156 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 4157 | output, output_buffer_size, |
| 4158 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4159 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4160 | TEST_LE_U( function_output_length, |
| 4161 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4162 | TEST_LE_U( function_output_length, |
| 4163 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4164 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4165 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4166 | if( first_part_size < input->len ) |
| 4167 | { |
| 4168 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4169 | input->x + first_part_size, |
| 4170 | input->len - first_part_size, |
| 4171 | ( output_buffer_size == 0 ? NULL : |
| 4172 | output + total_output_length ), |
| 4173 | output_buffer_size - total_output_length, |
| 4174 | &function_output_length ) ); |
| 4175 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4176 | TEST_LE_U( function_output_length, |
| 4177 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4178 | alg, |
| 4179 | input->len - first_part_size ) ); |
| 4180 | TEST_LE_U( function_output_length, |
| 4181 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4182 | total_output_length += function_output_length; |
| 4183 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4184 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4185 | status = psa_cipher_finish( &operation, |
| 4186 | ( output_buffer_size == 0 ? NULL : |
| 4187 | output + total_output_length ), |
| 4188 | output_buffer_size - total_output_length, |
| 4189 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4190 | TEST_LE_U( function_output_length, |
| 4191 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4192 | TEST_LE_U( function_output_length, |
| 4193 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4194 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4195 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4196 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4197 | if( expected_status == PSA_SUCCESS ) |
| 4198 | { |
| 4199 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4200 | |
| 4201 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4202 | output, total_output_length ); |
| 4203 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4204 | |
| 4205 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4206 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4207 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4208 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4209 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4210 | } |
| 4211 | /* END_CASE */ |
| 4212 | |
| 4213 | /* BEGIN_CASE */ |
| 4214 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4215 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4216 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4217 | int first_part_size_arg, |
| 4218 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4219 | data_t *expected_output, |
| 4220 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4221 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4222 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4223 | psa_key_type_t key_type = key_type_arg; |
| 4224 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4225 | psa_status_t status; |
| 4226 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4227 | size_t first_part_size = first_part_size_arg; |
| 4228 | size_t output1_length = output1_length_arg; |
| 4229 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4230 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4231 | size_t output_buffer_size = 0; |
| 4232 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4233 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4234 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4235 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4236 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4237 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4238 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4239 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4240 | psa_set_key_algorithm( &attributes, alg ); |
| 4241 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4242 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4243 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4244 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4245 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4246 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4247 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4248 | if( iv->len > 0 ) |
| 4249 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4250 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4251 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4252 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4253 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4254 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4255 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4256 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4257 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4258 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4259 | input->x, first_part_size, |
| 4260 | output, output_buffer_size, |
| 4261 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4262 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4263 | TEST_LE_U( function_output_length, |
| 4264 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4265 | TEST_LE_U( function_output_length, |
| 4266 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4267 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4268 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4269 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4270 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4271 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4272 | input->x + first_part_size, |
| 4273 | input->len - first_part_size, |
| 4274 | ( output_buffer_size == 0 ? NULL : |
| 4275 | output + total_output_length ), |
| 4276 | output_buffer_size - total_output_length, |
| 4277 | &function_output_length ) ); |
| 4278 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4279 | TEST_LE_U( function_output_length, |
| 4280 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4281 | alg, |
| 4282 | input->len - first_part_size ) ); |
| 4283 | TEST_LE_U( function_output_length, |
| 4284 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4285 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4286 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4287 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4288 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4289 | ( output_buffer_size == 0 ? NULL : |
| 4290 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4291 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4292 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4293 | TEST_LE_U( function_output_length, |
| 4294 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4295 | TEST_LE_U( function_output_length, |
| 4296 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4297 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4298 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4299 | |
| 4300 | if( expected_status == PSA_SUCCESS ) |
| 4301 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4302 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4303 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4304 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4305 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4308 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4309 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4310 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4311 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4312 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4313 | } |
| 4314 | /* END_CASE */ |
| 4315 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4316 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4317 | void cipher_decrypt_fail( int alg_arg, |
| 4318 | int key_type_arg, |
| 4319 | data_t *key_data, |
| 4320 | data_t *iv, |
| 4321 | data_t *input_arg, |
| 4322 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4323 | { |
| 4324 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4325 | psa_status_t status; |
| 4326 | psa_key_type_t key_type = key_type_arg; |
| 4327 | psa_algorithm_t alg = alg_arg; |
| 4328 | psa_status_t expected_status = expected_status_arg; |
| 4329 | unsigned char *input = NULL; |
| 4330 | size_t input_buffer_size = 0; |
| 4331 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4332 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4333 | size_t output_buffer_size = 0; |
| 4334 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4335 | size_t function_output_length; |
| 4336 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4337 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4338 | |
| 4339 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4340 | { |
| 4341 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4342 | |
| 4343 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4344 | psa_set_key_algorithm( &attributes, alg ); |
| 4345 | psa_set_key_type( &attributes, key_type ); |
| 4346 | |
| 4347 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4348 | &key ) ); |
| 4349 | } |
| 4350 | |
| 4351 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4352 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4353 | if ( input_buffer_size > 0 ) |
| 4354 | { |
| 4355 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4356 | memcpy( input, iv->x, iv->len ); |
| 4357 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4358 | } |
| 4359 | |
| 4360 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4361 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4362 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4363 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4364 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4365 | output_buffer_size, &output_length ); |
| 4366 | TEST_EQUAL( status, expected_status ); |
| 4367 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4368 | /* Decrypt, multi-part */ |
| 4369 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4370 | if( status == PSA_SUCCESS ) |
| 4371 | { |
| 4372 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4373 | input_arg->len ) + |
| 4374 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4375 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4376 | |
| 4377 | if( iv->len > 0 ) |
| 4378 | { |
| 4379 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4380 | |
| 4381 | if( status != PSA_SUCCESS ) |
| 4382 | TEST_EQUAL( status, expected_status ); |
| 4383 | } |
| 4384 | |
| 4385 | if( status == PSA_SUCCESS ) |
| 4386 | { |
| 4387 | status = psa_cipher_update( &operation, |
| 4388 | input_arg->x, input_arg->len, |
| 4389 | output_multi, output_buffer_size, |
| 4390 | &function_output_length ); |
| 4391 | if( status == PSA_SUCCESS ) |
| 4392 | { |
| 4393 | output_length = function_output_length; |
| 4394 | |
| 4395 | status = psa_cipher_finish( &operation, |
| 4396 | output_multi + output_length, |
| 4397 | output_buffer_size - output_length, |
| 4398 | &function_output_length ); |
| 4399 | |
| 4400 | TEST_EQUAL( status, expected_status ); |
| 4401 | } |
| 4402 | else |
| 4403 | { |
| 4404 | TEST_EQUAL( status, expected_status ); |
| 4405 | } |
| 4406 | } |
| 4407 | else |
| 4408 | { |
| 4409 | TEST_EQUAL( status, expected_status ); |
| 4410 | } |
| 4411 | } |
| 4412 | else |
| 4413 | { |
| 4414 | TEST_EQUAL( status, expected_status ); |
| 4415 | } |
| 4416 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4417 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4418 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4419 | mbedtls_free( input ); |
| 4420 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4421 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4422 | psa_destroy_key( key ); |
| 4423 | PSA_DONE( ); |
| 4424 | } |
| 4425 | /* END_CASE */ |
| 4426 | |
| 4427 | /* BEGIN_CASE */ |
| 4428 | void cipher_decrypt( int alg_arg, |
| 4429 | int key_type_arg, |
| 4430 | data_t *key_data, |
| 4431 | data_t *iv, |
| 4432 | data_t *input_arg, |
| 4433 | data_t *expected_output ) |
| 4434 | { |
| 4435 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4436 | psa_key_type_t key_type = key_type_arg; |
| 4437 | psa_algorithm_t alg = alg_arg; |
| 4438 | unsigned char *input = NULL; |
| 4439 | size_t input_buffer_size = 0; |
| 4440 | unsigned char *output = NULL; |
| 4441 | size_t output_buffer_size = 0; |
| 4442 | size_t output_length = 0; |
| 4443 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4444 | |
| 4445 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4446 | |
| 4447 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4448 | psa_set_key_algorithm( &attributes, alg ); |
| 4449 | psa_set_key_type( &attributes, key_type ); |
| 4450 | |
| 4451 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4452 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4453 | if ( input_buffer_size > 0 ) |
| 4454 | { |
| 4455 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4456 | memcpy( input, iv->x, iv->len ); |
| 4457 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4458 | } |
| 4459 | |
| 4460 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4461 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4462 | |
| 4463 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4464 | &key ) ); |
| 4465 | |
| 4466 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4467 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4468 | TEST_LE_U( output_length, |
| 4469 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4470 | TEST_LE_U( output_length, |
| 4471 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4472 | |
| 4473 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4474 | output, output_length ); |
| 4475 | exit: |
| 4476 | mbedtls_free( input ); |
| 4477 | mbedtls_free( output ); |
| 4478 | psa_destroy_key( key ); |
| 4479 | PSA_DONE( ); |
| 4480 | } |
| 4481 | /* END_CASE */ |
| 4482 | |
| 4483 | /* BEGIN_CASE */ |
| 4484 | void cipher_verify_output( int alg_arg, |
| 4485 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4486 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4487 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4488 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4489 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4490 | psa_key_type_t key_type = key_type_arg; |
| 4491 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4492 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4493 | size_t output1_size = 0; |
| 4494 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4495 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4496 | size_t output2_size = 0; |
| 4497 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4498 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4499 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4500 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4501 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4502 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4503 | psa_set_key_algorithm( &attributes, alg ); |
| 4504 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4505 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4506 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4507 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4508 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4509 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4510 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4511 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4512 | output1, output1_size, |
| 4513 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4514 | TEST_LE_U( output1_length, |
| 4515 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4516 | TEST_LE_U( output1_length, |
| 4517 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4518 | |
| 4519 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4520 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4521 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4522 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4523 | output2, output2_size, |
| 4524 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4525 | TEST_LE_U( output2_length, |
| 4526 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4527 | TEST_LE_U( output2_length, |
| 4528 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4529 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4530 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4531 | |
| 4532 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4533 | mbedtls_free( output1 ); |
| 4534 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4535 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4536 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4537 | } |
| 4538 | /* END_CASE */ |
| 4539 | |
| 4540 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4541 | void cipher_verify_output_multipart( int alg_arg, |
| 4542 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4543 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4544 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4545 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4546 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4547 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4548 | psa_key_type_t key_type = key_type_arg; |
| 4549 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4550 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4551 | unsigned char iv[16] = {0}; |
| 4552 | size_t iv_size = 16; |
| 4553 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4554 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4555 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4556 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4557 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4558 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4559 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4560 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4561 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4562 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4563 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4564 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4565 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4566 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4567 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4568 | psa_set_key_algorithm( &attributes, alg ); |
| 4569 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4570 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4571 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4572 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4573 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4574 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4575 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4576 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4577 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4578 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4579 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4580 | iv, iv_size, |
| 4581 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4582 | } |
| 4583 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4584 | 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] | 4585 | TEST_LE_U( output1_buffer_size, |
| 4586 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4587 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4588 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4589 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4590 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4591 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4592 | output1, output1_buffer_size, |
| 4593 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4594 | TEST_LE_U( function_output_length, |
| 4595 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4596 | TEST_LE_U( function_output_length, |
| 4597 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4598 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4599 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4600 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4601 | input->x + first_part_size, |
| 4602 | input->len - first_part_size, |
| 4603 | output1, output1_buffer_size, |
| 4604 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4605 | TEST_LE_U( function_output_length, |
| 4606 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4607 | alg, |
| 4608 | input->len - first_part_size ) ); |
| 4609 | TEST_LE_U( function_output_length, |
| 4610 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4611 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4612 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4613 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4614 | output1 + output1_length, |
| 4615 | output1_buffer_size - output1_length, |
| 4616 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4617 | TEST_LE_U( function_output_length, |
| 4618 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4619 | TEST_LE_U( function_output_length, |
| 4620 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4621 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4622 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4623 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4624 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4625 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4626 | TEST_LE_U( output2_buffer_size, |
| 4627 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4628 | TEST_LE_U( output2_buffer_size, |
| 4629 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4630 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4631 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4632 | if( iv_length > 0 ) |
| 4633 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4634 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4635 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4636 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4637 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4638 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4639 | output2, output2_buffer_size, |
| 4640 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4641 | TEST_LE_U( function_output_length, |
| 4642 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4643 | TEST_LE_U( function_output_length, |
| 4644 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4645 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4646 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4647 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4648 | output1 + first_part_size, |
| 4649 | output1_length - first_part_size, |
| 4650 | output2, output2_buffer_size, |
| 4651 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4652 | TEST_LE_U( function_output_length, |
| 4653 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4654 | alg, |
| 4655 | output1_length - first_part_size ) ); |
| 4656 | TEST_LE_U( function_output_length, |
| 4657 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4658 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4659 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4660 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4661 | output2 + output2_length, |
| 4662 | output2_buffer_size - output2_length, |
| 4663 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4664 | TEST_LE_U( function_output_length, |
| 4665 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4666 | TEST_LE_U( function_output_length, |
| 4667 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4668 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4669 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4670 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4671 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4672 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4673 | |
| 4674 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4675 | psa_cipher_abort( &operation1 ); |
| 4676 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4677 | mbedtls_free( output1 ); |
| 4678 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4679 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4680 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4681 | } |
| 4682 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4683 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4684 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4685 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4686 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4687 | data_t *nonce, |
| 4688 | data_t *additional_data, |
| 4689 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4690 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4691 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4692 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4693 | psa_key_type_t key_type = key_type_arg; |
| 4694 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4695 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4696 | unsigned char *output_data = NULL; |
| 4697 | size_t output_size = 0; |
| 4698 | size_t output_length = 0; |
| 4699 | unsigned char *output_data2 = NULL; |
| 4700 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4701 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4702 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4703 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4704 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4705 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4706 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4707 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4708 | psa_set_key_algorithm( &attributes, alg ); |
| 4709 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4710 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4711 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4712 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4713 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4714 | key_bits = psa_get_key_bits( &attributes ); |
| 4715 | |
| 4716 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4717 | alg ); |
| 4718 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4719 | * should be exact. */ |
| 4720 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4721 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4722 | { |
| 4723 | TEST_EQUAL( output_size, |
| 4724 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4725 | TEST_LE_U( output_size, |
| 4726 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4727 | } |
| 4728 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4729 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4730 | status = psa_aead_encrypt( key, alg, |
| 4731 | nonce->x, nonce->len, |
| 4732 | additional_data->x, |
| 4733 | additional_data->len, |
| 4734 | input_data->x, input_data->len, |
| 4735 | output_data, output_size, |
| 4736 | &output_length ); |
| 4737 | |
| 4738 | /* If the operation is not supported, just skip and not fail in case the |
| 4739 | * encryption involves a common limitation of cryptography hardwares and |
| 4740 | * an alternative implementation. */ |
| 4741 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4742 | { |
| 4743 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4744 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4745 | } |
| 4746 | |
| 4747 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4748 | |
| 4749 | if( PSA_SUCCESS == expected_result ) |
| 4750 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4751 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4752 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4753 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4754 | * should be exact. */ |
| 4755 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4756 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4757 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4758 | TEST_LE_U( input_data->len, |
| 4759 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4760 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4761 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4762 | nonce->x, nonce->len, |
| 4763 | additional_data->x, |
| 4764 | additional_data->len, |
| 4765 | output_data, output_length, |
| 4766 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4767 | &output_length2 ), |
| 4768 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4769 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4770 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4771 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4772 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4773 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4774 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4775 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4776 | mbedtls_free( output_data ); |
| 4777 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4778 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4779 | } |
| 4780 | /* END_CASE */ |
| 4781 | |
| 4782 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4783 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4784 | int alg_arg, |
| 4785 | data_t *nonce, |
| 4786 | data_t *additional_data, |
| 4787 | data_t *input_data, |
| 4788 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4789 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4790 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4791 | psa_key_type_t key_type = key_type_arg; |
| 4792 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4793 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4794 | unsigned char *output_data = NULL; |
| 4795 | size_t output_size = 0; |
| 4796 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4797 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4798 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4799 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4800 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4801 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4802 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4803 | psa_set_key_algorithm( &attributes, alg ); |
| 4804 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4805 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4806 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4807 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4808 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4809 | key_bits = psa_get_key_bits( &attributes ); |
| 4810 | |
| 4811 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4812 | alg ); |
| 4813 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4814 | * should be exact. */ |
| 4815 | TEST_EQUAL( output_size, |
| 4816 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4817 | TEST_LE_U( output_size, |
| 4818 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4819 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4820 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4821 | status = psa_aead_encrypt( key, alg, |
| 4822 | nonce->x, nonce->len, |
| 4823 | additional_data->x, additional_data->len, |
| 4824 | input_data->x, input_data->len, |
| 4825 | output_data, output_size, |
| 4826 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4827 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4828 | /* If the operation is not supported, just skip and not fail in case the |
| 4829 | * encryption involves a common limitation of cryptography hardwares and |
| 4830 | * an alternative implementation. */ |
| 4831 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4832 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4833 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4834 | 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] | 4835 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4836 | |
| 4837 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4838 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4839 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4840 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4841 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4842 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4843 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4844 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4845 | } |
| 4846 | /* END_CASE */ |
| 4847 | |
| 4848 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4849 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4850 | int alg_arg, |
| 4851 | data_t *nonce, |
| 4852 | data_t *additional_data, |
| 4853 | data_t *input_data, |
| 4854 | data_t *expected_data, |
| 4855 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4856 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4857 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4858 | psa_key_type_t key_type = key_type_arg; |
| 4859 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4860 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4861 | unsigned char *output_data = NULL; |
| 4862 | size_t output_size = 0; |
| 4863 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4864 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4865 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4866 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4867 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4868 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4869 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4870 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4871 | psa_set_key_algorithm( &attributes, alg ); |
| 4872 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4873 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4874 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4875 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4876 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4877 | key_bits = psa_get_key_bits( &attributes ); |
| 4878 | |
| 4879 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4880 | alg ); |
| 4881 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4882 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4883 | { |
| 4884 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4885 | * should be exact. */ |
| 4886 | TEST_EQUAL( output_size, |
| 4887 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4888 | TEST_LE_U( output_size, |
| 4889 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4890 | } |
| 4891 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4892 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4893 | status = psa_aead_decrypt( key, alg, |
| 4894 | nonce->x, nonce->len, |
| 4895 | additional_data->x, |
| 4896 | additional_data->len, |
| 4897 | input_data->x, input_data->len, |
| 4898 | output_data, output_size, |
| 4899 | &output_length ); |
| 4900 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4901 | /* If the operation is not supported, just skip and not fail in case the |
| 4902 | * decryption involves a common limitation of cryptography hardwares and |
| 4903 | * an alternative implementation. */ |
| 4904 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4905 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4906 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4907 | 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] | 4908 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4909 | |
| 4910 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4911 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4912 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4913 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4914 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4915 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4916 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4917 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4918 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4919 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4920 | } |
| 4921 | /* END_CASE */ |
| 4922 | |
| 4923 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4924 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4925 | int alg_arg, |
| 4926 | data_t *nonce, |
| 4927 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4928 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4929 | int do_set_lengths, |
| 4930 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4931 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4932 | size_t ad_part_len = 0; |
| 4933 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4934 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4935 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4936 | 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] | 4937 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4938 | mbedtls_test_set_step( ad_part_len ); |
| 4939 | |
| 4940 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4941 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4942 | if( ad_part_len & 0x01 ) |
| 4943 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4944 | else |
| 4945 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4946 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4947 | |
| 4948 | /* Split ad into length(ad_part_len) parts. */ |
| 4949 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4950 | alg_arg, nonce, |
| 4951 | additional_data, |
| 4952 | ad_part_len, |
| 4953 | input_data, -1, |
| 4954 | set_lengths_method, |
| 4955 | expected_output, |
| 4956 | 1, 0 ) ) |
| 4957 | break; |
| 4958 | |
| 4959 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4960 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4961 | |
| 4962 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4963 | alg_arg, nonce, |
| 4964 | additional_data, |
| 4965 | ad_part_len, |
| 4966 | input_data, -1, |
| 4967 | set_lengths_method, |
| 4968 | expected_output, |
| 4969 | 1, 1 ) ) |
| 4970 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4971 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4972 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4973 | 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] | 4974 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4975 | /* Split data into length(data_part_len) parts. */ |
| 4976 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 4977 | |
| 4978 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4979 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4980 | if( data_part_len & 0x01 ) |
| 4981 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4982 | else |
| 4983 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4984 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4985 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4986 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4987 | alg_arg, nonce, |
| 4988 | additional_data, -1, |
| 4989 | input_data, data_part_len, |
| 4990 | set_lengths_method, |
| 4991 | expected_output, |
| 4992 | 1, 0 ) ) |
| 4993 | break; |
| 4994 | |
| 4995 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 4996 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 4997 | |
| 4998 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4999 | alg_arg, nonce, |
| 5000 | additional_data, -1, |
| 5001 | input_data, data_part_len, |
| 5002 | set_lengths_method, |
| 5003 | expected_output, |
| 5004 | 1, 1 ) ) |
| 5005 | break; |
| 5006 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5007 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5008 | /* Goto is required to silence warnings about unused labels, as we |
| 5009 | * don't actually do any test assertions in this function. */ |
| 5010 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5011 | } |
| 5012 | /* END_CASE */ |
| 5013 | |
| 5014 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5015 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 5016 | int alg_arg, |
| 5017 | data_t *nonce, |
| 5018 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5019 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 5020 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5021 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5022 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5023 | size_t ad_part_len = 0; |
| 5024 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5025 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5026 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5027 | 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] | 5028 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5029 | /* Split ad into length(ad_part_len) parts. */ |
| 5030 | mbedtls_test_set_step( ad_part_len ); |
| 5031 | |
| 5032 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5033 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5034 | if( ad_part_len & 0x01 ) |
| 5035 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5036 | else |
| 5037 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5038 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5039 | |
| 5040 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5041 | alg_arg, nonce, |
| 5042 | additional_data, |
| 5043 | ad_part_len, |
| 5044 | input_data, -1, |
| 5045 | set_lengths_method, |
| 5046 | expected_output, |
| 5047 | 0, 0 ) ) |
| 5048 | break; |
| 5049 | |
| 5050 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5051 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5052 | |
| 5053 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5054 | alg_arg, nonce, |
| 5055 | additional_data, |
| 5056 | ad_part_len, |
| 5057 | input_data, -1, |
| 5058 | set_lengths_method, |
| 5059 | expected_output, |
| 5060 | 0, 1 ) ) |
| 5061 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5062 | } |
| 5063 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5064 | 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] | 5065 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5066 | /* Split data into length(data_part_len) parts. */ |
| 5067 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5068 | |
| 5069 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5070 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5071 | if( data_part_len & 0x01 ) |
| 5072 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5073 | else |
| 5074 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5075 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5076 | |
| 5077 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5078 | alg_arg, nonce, |
| 5079 | additional_data, -1, |
| 5080 | input_data, data_part_len, |
| 5081 | set_lengths_method, |
| 5082 | expected_output, |
| 5083 | 0, 0 ) ) |
| 5084 | break; |
| 5085 | |
| 5086 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5087 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5088 | |
| 5089 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5090 | alg_arg, nonce, |
| 5091 | additional_data, -1, |
| 5092 | input_data, data_part_len, |
| 5093 | set_lengths_method, |
| 5094 | expected_output, |
| 5095 | 0, 1 ) ) |
| 5096 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5097 | } |
| 5098 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5099 | /* Goto is required to silence warnings about unused labels, as we |
| 5100 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5101 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5102 | } |
| 5103 | /* END_CASE */ |
| 5104 | |
| 5105 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5106 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 5107 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5108 | int nonce_length, |
| 5109 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5110 | data_t *additional_data, |
| 5111 | data_t *input_data, |
| 5112 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5113 | { |
| 5114 | |
| 5115 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5116 | psa_key_type_t key_type = key_type_arg; |
| 5117 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5118 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5119 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5120 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5121 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5122 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5123 | size_t actual_nonce_length = 0; |
| 5124 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5125 | unsigned char *output = NULL; |
| 5126 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5127 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5128 | size_t ciphertext_size = 0; |
| 5129 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5130 | size_t tag_length = 0; |
| 5131 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5132 | |
| 5133 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5134 | |
| 5135 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5136 | psa_set_key_algorithm( & attributes, alg ); |
| 5137 | psa_set_key_type( & attributes, key_type ); |
| 5138 | |
| 5139 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5140 | &key ) ); |
| 5141 | |
| 5142 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5143 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5144 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5145 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5146 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5147 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5148 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5149 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5150 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5151 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5152 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5153 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5154 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5155 | |
| 5156 | /* If the operation is not supported, just skip and not fail in case the |
| 5157 | * encryption involves a common limitation of cryptography hardwares and |
| 5158 | * an alternative implementation. */ |
| 5159 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5160 | { |
| 5161 | 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] | 5162 | 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] | 5163 | } |
| 5164 | |
| 5165 | PSA_ASSERT( status ); |
| 5166 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5167 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5168 | nonce_length, |
| 5169 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5170 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5171 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5172 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5173 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5174 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5175 | if( expected_status == PSA_SUCCESS ) |
| 5176 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 5177 | alg ) ); |
| 5178 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5179 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5180 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5181 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5182 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5183 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 5184 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5185 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5186 | |
| 5187 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5188 | additional_data->len ) ); |
| 5189 | |
| 5190 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5191 | output, output_size, |
| 5192 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5193 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5194 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5195 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5196 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5197 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5198 | |
| 5199 | exit: |
| 5200 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5201 | mbedtls_free( output ); |
| 5202 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5203 | psa_aead_abort( &operation ); |
| 5204 | PSA_DONE( ); |
| 5205 | } |
| 5206 | /* END_CASE */ |
| 5207 | |
| 5208 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5209 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 5210 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5211 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5212 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5213 | data_t *additional_data, |
| 5214 | data_t *input_data, |
| 5215 | int expected_status_arg ) |
| 5216 | { |
| 5217 | |
| 5218 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5219 | psa_key_type_t key_type = key_type_arg; |
| 5220 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5221 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5222 | uint8_t *nonce_buffer = NULL; |
| 5223 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5224 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5225 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5226 | unsigned char *output = NULL; |
| 5227 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5228 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5229 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5230 | size_t ciphertext_size = 0; |
| 5231 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5232 | size_t tag_length = 0; |
| 5233 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5234 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5235 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5236 | |
| 5237 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5238 | |
| 5239 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5240 | psa_set_key_algorithm( &attributes, alg ); |
| 5241 | psa_set_key_type( &attributes, key_type ); |
| 5242 | |
| 5243 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5244 | &key ) ); |
| 5245 | |
| 5246 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5247 | |
| 5248 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5249 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5250 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5251 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5252 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5253 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5254 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5255 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5256 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5257 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5258 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5259 | |
| 5260 | /* If the operation is not supported, just skip and not fail in case the |
| 5261 | * encryption involves a common limitation of cryptography hardwares and |
| 5262 | * an alternative implementation. */ |
| 5263 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5264 | { |
| 5265 | 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] | 5266 | 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] | 5267 | } |
| 5268 | |
| 5269 | PSA_ASSERT( status ); |
| 5270 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5271 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 5272 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5273 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 5274 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5275 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5276 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5277 | } |
| 5278 | else |
| 5279 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5280 | /* If length is zero, then this will return NULL. */ |
| 5281 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5282 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5283 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5284 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5285 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5286 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5287 | { |
| 5288 | nonce_buffer[index] = 'a' + index; |
| 5289 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5290 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5291 | } |
| 5292 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5293 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5294 | { |
| 5295 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5296 | input_data->len ) ); |
| 5297 | } |
| 5298 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5299 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5300 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5301 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5302 | |
| 5303 | if( expected_status == PSA_SUCCESS ) |
| 5304 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5305 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5306 | { |
| 5307 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5308 | input_data->len ) ); |
| 5309 | } |
| 5310 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5311 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5312 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5313 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5314 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5315 | additional_data->len ), |
| 5316 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5317 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5318 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5319 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5320 | &ciphertext_length ), |
| 5321 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5322 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5323 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5324 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5325 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5326 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5327 | } |
| 5328 | |
| 5329 | exit: |
| 5330 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5331 | mbedtls_free( output ); |
| 5332 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5333 | mbedtls_free( nonce_buffer ); |
| 5334 | psa_aead_abort( &operation ); |
| 5335 | PSA_DONE( ); |
| 5336 | } |
| 5337 | /* END_CASE */ |
| 5338 | |
| 5339 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5340 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5341 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5342 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5343 | data_t *nonce, |
| 5344 | data_t *additional_data, |
| 5345 | data_t *input_data, |
| 5346 | int expected_status_arg ) |
| 5347 | { |
| 5348 | |
| 5349 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5350 | psa_key_type_t key_type = key_type_arg; |
| 5351 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5352 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5353 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5354 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5355 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5356 | unsigned char *output = NULL; |
| 5357 | unsigned char *ciphertext = NULL; |
| 5358 | size_t output_size = output_size_arg; |
| 5359 | size_t ciphertext_size = 0; |
| 5360 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5361 | size_t tag_length = 0; |
| 5362 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5363 | |
| 5364 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5365 | |
| 5366 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5367 | psa_set_key_algorithm( &attributes, alg ); |
| 5368 | psa_set_key_type( &attributes, key_type ); |
| 5369 | |
| 5370 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5371 | &key ) ); |
| 5372 | |
| 5373 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5374 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5375 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5376 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5377 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5378 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5379 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5380 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5381 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5382 | |
| 5383 | /* If the operation is not supported, just skip and not fail in case the |
| 5384 | * encryption involves a common limitation of cryptography hardwares and |
| 5385 | * an alternative implementation. */ |
| 5386 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5387 | { |
| 5388 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5389 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5390 | } |
| 5391 | |
| 5392 | PSA_ASSERT( status ); |
| 5393 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5394 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5395 | input_data->len ) ); |
| 5396 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5397 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5398 | |
| 5399 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5400 | additional_data->len ) ); |
| 5401 | |
| 5402 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5403 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5404 | |
| 5405 | TEST_EQUAL( status, expected_status ); |
| 5406 | |
| 5407 | if( expected_status == PSA_SUCCESS ) |
| 5408 | { |
| 5409 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5410 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5411 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5412 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5413 | } |
| 5414 | |
| 5415 | exit: |
| 5416 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5417 | mbedtls_free( output ); |
| 5418 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5419 | psa_aead_abort( &operation ); |
| 5420 | PSA_DONE( ); |
| 5421 | } |
| 5422 | /* END_CASE */ |
| 5423 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5424 | /* BEGIN_CASE */ |
| 5425 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5426 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5427 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5428 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5429 | data_t *nonce, |
| 5430 | data_t *additional_data, |
| 5431 | data_t *input_data, |
| 5432 | int expected_status_arg ) |
| 5433 | { |
| 5434 | |
| 5435 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5436 | psa_key_type_t key_type = key_type_arg; |
| 5437 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5438 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5439 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5440 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5441 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5442 | unsigned char *ciphertext = NULL; |
| 5443 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5444 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5445 | size_t ciphertext_size = 0; |
| 5446 | size_t ciphertext_length = 0; |
| 5447 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5448 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5449 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5450 | |
| 5451 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5452 | |
| 5453 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5454 | psa_set_key_algorithm( &attributes, alg ); |
| 5455 | psa_set_key_type( &attributes, key_type ); |
| 5456 | |
| 5457 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5458 | &key ) ); |
| 5459 | |
| 5460 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5461 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5462 | 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] | 5463 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5464 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5465 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5466 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5467 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5468 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5469 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5470 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5471 | |
| 5472 | /* If the operation is not supported, just skip and not fail in case the |
| 5473 | * encryption involves a common limitation of cryptography hardwares and |
| 5474 | * an alternative implementation. */ |
| 5475 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5476 | { |
| 5477 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5478 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5479 | } |
| 5480 | |
| 5481 | PSA_ASSERT( status ); |
| 5482 | |
| 5483 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5484 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5485 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5486 | input_data->len ) ); |
| 5487 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5488 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5489 | additional_data->len ) ); |
| 5490 | |
| 5491 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5492 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5493 | |
| 5494 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5495 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5496 | finish_ciphertext_size, |
| 5497 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5498 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5499 | |
| 5500 | TEST_EQUAL( status, expected_status ); |
| 5501 | |
| 5502 | exit: |
| 5503 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5504 | mbedtls_free( ciphertext ); |
| 5505 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5506 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5507 | psa_aead_abort( &operation ); |
| 5508 | PSA_DONE( ); |
| 5509 | } |
| 5510 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5511 | |
| 5512 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5513 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5514 | int alg_arg, |
| 5515 | data_t *nonce, |
| 5516 | data_t *additional_data, |
| 5517 | data_t *input_data, |
| 5518 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5519 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5520 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5521 | int expected_status_arg ) |
| 5522 | { |
| 5523 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5524 | psa_key_type_t key_type = key_type_arg; |
| 5525 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5526 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5527 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5528 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5529 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5530 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5531 | unsigned char *plaintext = NULL; |
| 5532 | unsigned char *finish_plaintext = NULL; |
| 5533 | size_t plaintext_size = 0; |
| 5534 | size_t plaintext_length = 0; |
| 5535 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5536 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5537 | unsigned char *tag_buffer = NULL; |
| 5538 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5539 | |
| 5540 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5541 | |
| 5542 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5543 | psa_set_key_algorithm( &attributes, alg ); |
| 5544 | psa_set_key_type( &attributes, key_type ); |
| 5545 | |
| 5546 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5547 | &key ) ); |
| 5548 | |
| 5549 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5550 | |
| 5551 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5552 | input_data->len ); |
| 5553 | |
| 5554 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5555 | |
| 5556 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5557 | |
| 5558 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5559 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5560 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5561 | |
| 5562 | /* If the operation is not supported, just skip and not fail in case the |
| 5563 | * encryption involves a common limitation of cryptography hardwares and |
| 5564 | * an alternative implementation. */ |
| 5565 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5566 | { |
| 5567 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5568 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5569 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5570 | TEST_EQUAL( status, expected_setup_status ); |
| 5571 | |
| 5572 | if( status != PSA_SUCCESS ) |
| 5573 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5574 | |
| 5575 | PSA_ASSERT( status ); |
| 5576 | |
| 5577 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5578 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5579 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5580 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5581 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5582 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5583 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5584 | additional_data->len ) ); |
| 5585 | |
| 5586 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5587 | input_data->len, |
| 5588 | plaintext, plaintext_size, |
| 5589 | &plaintext_length ) ); |
| 5590 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5591 | if( tag_usage == USE_GIVEN_TAG ) |
| 5592 | { |
| 5593 | tag_buffer = tag->x; |
| 5594 | tag_size = tag->len; |
| 5595 | } |
| 5596 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5597 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5598 | verify_plaintext_size, |
| 5599 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5600 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5601 | |
| 5602 | TEST_EQUAL( status, expected_status ); |
| 5603 | |
| 5604 | exit: |
| 5605 | psa_destroy_key( key ); |
| 5606 | mbedtls_free( plaintext ); |
| 5607 | mbedtls_free( finish_plaintext ); |
| 5608 | psa_aead_abort( &operation ); |
| 5609 | PSA_DONE( ); |
| 5610 | } |
| 5611 | /* END_CASE */ |
| 5612 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5613 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5614 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5615 | int alg_arg, int expected_status_arg ) |
| 5616 | { |
| 5617 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5618 | psa_key_type_t key_type = key_type_arg; |
| 5619 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5620 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5621 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5622 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5623 | psa_status_t expected_status = expected_status_arg; |
| 5624 | |
| 5625 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5626 | |
| 5627 | psa_set_key_usage_flags( &attributes, |
| 5628 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5629 | psa_set_key_algorithm( &attributes, alg ); |
| 5630 | psa_set_key_type( &attributes, key_type ); |
| 5631 | |
| 5632 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5633 | &key ) ); |
| 5634 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5635 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5636 | |
| 5637 | TEST_EQUAL( status, expected_status ); |
| 5638 | |
| 5639 | psa_aead_abort( &operation ); |
| 5640 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5641 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5642 | |
| 5643 | TEST_EQUAL(status, expected_status ); |
| 5644 | |
| 5645 | exit: |
| 5646 | psa_destroy_key( key ); |
| 5647 | psa_aead_abort( &operation ); |
| 5648 | PSA_DONE( ); |
| 5649 | } |
| 5650 | /* END_CASE */ |
| 5651 | |
| 5652 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5653 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5654 | int alg_arg, |
| 5655 | data_t *nonce, |
| 5656 | data_t *additional_data, |
| 5657 | data_t *input_data ) |
| 5658 | { |
| 5659 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5660 | psa_key_type_t key_type = key_type_arg; |
| 5661 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5662 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5663 | unsigned char *output_data = NULL; |
| 5664 | unsigned char *final_data = NULL; |
| 5665 | size_t output_size = 0; |
| 5666 | size_t finish_output_size = 0; |
| 5667 | size_t output_length = 0; |
| 5668 | size_t key_bits = 0; |
| 5669 | size_t tag_length = 0; |
| 5670 | size_t tag_size = 0; |
| 5671 | size_t nonce_length = 0; |
| 5672 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5673 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5674 | size_t output_part_length = 0; |
| 5675 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5676 | |
| 5677 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5678 | |
| 5679 | psa_set_key_usage_flags( & attributes, |
| 5680 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5681 | psa_set_key_algorithm( & attributes, alg ); |
| 5682 | psa_set_key_type( & attributes, key_type ); |
| 5683 | |
| 5684 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5685 | &key ) ); |
| 5686 | |
| 5687 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5688 | key_bits = psa_get_key_bits( &attributes ); |
| 5689 | |
| 5690 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5691 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5692 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5693 | |
| 5694 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5695 | |
| 5696 | ASSERT_ALLOC( output_data, output_size ); |
| 5697 | |
| 5698 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5699 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5700 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5701 | |
| 5702 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5703 | |
| 5704 | /* Test all operations error without calling setup first. */ |
| 5705 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5706 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5707 | PSA_ERROR_BAD_STATE ); |
| 5708 | |
| 5709 | psa_aead_abort( &operation ); |
| 5710 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5711 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5712 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5713 | &nonce_length ), |
| 5714 | PSA_ERROR_BAD_STATE ); |
| 5715 | |
| 5716 | psa_aead_abort( &operation ); |
| 5717 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5718 | /* ------------------------------------------------------- */ |
| 5719 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5720 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5721 | input_data->len ), |
| 5722 | PSA_ERROR_BAD_STATE ); |
| 5723 | |
| 5724 | psa_aead_abort( &operation ); |
| 5725 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5726 | /* ------------------------------------------------------- */ |
| 5727 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5728 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5729 | additional_data->len ), |
| 5730 | PSA_ERROR_BAD_STATE ); |
| 5731 | |
| 5732 | psa_aead_abort( &operation ); |
| 5733 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5734 | /* ------------------------------------------------------- */ |
| 5735 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5736 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5737 | input_data->len, output_data, |
| 5738 | output_size, &output_length ), |
| 5739 | PSA_ERROR_BAD_STATE ); |
| 5740 | |
| 5741 | psa_aead_abort( &operation ); |
| 5742 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5743 | /* ------------------------------------------------------- */ |
| 5744 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5745 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5746 | finish_output_size, |
| 5747 | &output_part_length, |
| 5748 | tag_buffer, tag_length, |
| 5749 | &tag_size ), |
| 5750 | PSA_ERROR_BAD_STATE ); |
| 5751 | |
| 5752 | psa_aead_abort( &operation ); |
| 5753 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5754 | /* ------------------------------------------------------- */ |
| 5755 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5756 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5757 | finish_output_size, |
| 5758 | &output_part_length, |
| 5759 | tag_buffer, |
| 5760 | tag_length ), |
| 5761 | PSA_ERROR_BAD_STATE ); |
| 5762 | |
| 5763 | psa_aead_abort( &operation ); |
| 5764 | |
| 5765 | /* Test for double setups. */ |
| 5766 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5767 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5768 | |
| 5769 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5770 | PSA_ERROR_BAD_STATE ); |
| 5771 | |
| 5772 | psa_aead_abort( &operation ); |
| 5773 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5774 | /* ------------------------------------------------------- */ |
| 5775 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5776 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5777 | |
| 5778 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5779 | PSA_ERROR_BAD_STATE ); |
| 5780 | |
| 5781 | psa_aead_abort( &operation ); |
| 5782 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5783 | /* ------------------------------------------------------- */ |
| 5784 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5785 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5786 | |
| 5787 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5788 | PSA_ERROR_BAD_STATE ); |
| 5789 | |
| 5790 | psa_aead_abort( &operation ); |
| 5791 | |
| 5792 | /* ------------------------------------------------------- */ |
| 5793 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5794 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5795 | |
| 5796 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5797 | PSA_ERROR_BAD_STATE ); |
| 5798 | |
| 5799 | psa_aead_abort( &operation ); |
| 5800 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5801 | /* Test for not setting a nonce. */ |
| 5802 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5803 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5804 | |
| 5805 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5806 | additional_data->len ), |
| 5807 | PSA_ERROR_BAD_STATE ); |
| 5808 | |
| 5809 | psa_aead_abort( &operation ); |
| 5810 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5811 | /* ------------------------------------------------------- */ |
| 5812 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5813 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5814 | |
| 5815 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5816 | input_data->len, output_data, |
| 5817 | output_size, &output_length ), |
| 5818 | PSA_ERROR_BAD_STATE ); |
| 5819 | |
| 5820 | psa_aead_abort( &operation ); |
| 5821 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5822 | /* ------------------------------------------------------- */ |
| 5823 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5824 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5825 | |
| 5826 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5827 | finish_output_size, |
| 5828 | &output_part_length, |
| 5829 | tag_buffer, tag_length, |
| 5830 | &tag_size ), |
| 5831 | PSA_ERROR_BAD_STATE ); |
| 5832 | |
| 5833 | psa_aead_abort( &operation ); |
| 5834 | |
| 5835 | /* ------------------------------------------------------- */ |
| 5836 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5837 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5838 | |
| 5839 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5840 | finish_output_size, |
| 5841 | &output_part_length, |
| 5842 | tag_buffer, |
| 5843 | tag_length ), |
| 5844 | PSA_ERROR_BAD_STATE ); |
| 5845 | |
| 5846 | psa_aead_abort( &operation ); |
| 5847 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5848 | /* Test for double setting nonce. */ |
| 5849 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5850 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5851 | |
| 5852 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5853 | |
| 5854 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5855 | PSA_ERROR_BAD_STATE ); |
| 5856 | |
| 5857 | psa_aead_abort( &operation ); |
| 5858 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5859 | /* Test for double generating nonce. */ |
| 5860 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5861 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5862 | |
| 5863 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5864 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5865 | &nonce_length ) ); |
| 5866 | |
| 5867 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5868 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5869 | &nonce_length ), |
| 5870 | PSA_ERROR_BAD_STATE ); |
| 5871 | |
| 5872 | |
| 5873 | psa_aead_abort( &operation ); |
| 5874 | |
| 5875 | /* Test for generate nonce then set and vice versa */ |
| 5876 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5877 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5878 | |
| 5879 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5880 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5881 | &nonce_length ) ); |
| 5882 | |
| 5883 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5884 | PSA_ERROR_BAD_STATE ); |
| 5885 | |
| 5886 | psa_aead_abort( &operation ); |
| 5887 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5888 | /* Test for generating nonce after calling set lengths */ |
| 5889 | |
| 5890 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5891 | |
| 5892 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5893 | input_data->len ) ); |
| 5894 | |
| 5895 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5896 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5897 | &nonce_length ) ); |
| 5898 | |
| 5899 | psa_aead_abort( &operation ); |
| 5900 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5901 | /* 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] | 5902 | |
| 5903 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5904 | |
| 5905 | if( operation.alg == PSA_ALG_CCM ) |
| 5906 | { |
| 5907 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5908 | input_data->len ), |
| 5909 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5910 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5911 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5912 | &nonce_length ), |
| 5913 | PSA_ERROR_BAD_STATE ); |
| 5914 | } |
| 5915 | else |
| 5916 | { |
| 5917 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5918 | input_data->len ) ); |
| 5919 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5920 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5921 | &nonce_length ) ); |
| 5922 | } |
| 5923 | |
| 5924 | psa_aead_abort( &operation ); |
| 5925 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5926 | /* 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] | 5927 | #if SIZE_MAX > UINT32_MAX |
| 5928 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5929 | |
| 5930 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5931 | { |
| 5932 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5933 | input_data->len ), |
| 5934 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5935 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5936 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5937 | &nonce_length ), |
| 5938 | PSA_ERROR_BAD_STATE ); |
| 5939 | } |
| 5940 | else |
| 5941 | { |
| 5942 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5943 | input_data->len ) ); |
| 5944 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5945 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5946 | &nonce_length ) ); |
| 5947 | } |
| 5948 | |
| 5949 | psa_aead_abort( &operation ); |
| 5950 | #endif |
| 5951 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5952 | /* 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] | 5953 | |
| 5954 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5955 | |
| 5956 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5957 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5958 | &nonce_length ) ); |
| 5959 | |
| 5960 | if( operation.alg == PSA_ALG_CCM ) |
| 5961 | { |
| 5962 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5963 | input_data->len ), |
| 5964 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5965 | } |
| 5966 | else |
| 5967 | { |
| 5968 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5969 | input_data->len ) ); |
| 5970 | } |
| 5971 | |
| 5972 | psa_aead_abort( &operation ); |
| 5973 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5974 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5975 | /* Test for setting nonce after calling set lengths */ |
| 5976 | |
| 5977 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5978 | |
| 5979 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5980 | input_data->len ) ); |
| 5981 | |
| 5982 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5983 | |
| 5984 | psa_aead_abort( &operation ); |
| 5985 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5986 | /* 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] | 5987 | |
| 5988 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5989 | |
| 5990 | if( operation.alg == PSA_ALG_CCM ) |
| 5991 | { |
| 5992 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5993 | input_data->len ), |
| 5994 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5995 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5996 | PSA_ERROR_BAD_STATE ); |
| 5997 | } |
| 5998 | else |
| 5999 | { |
| 6000 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6001 | input_data->len ) ); |
| 6002 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6003 | } |
| 6004 | |
| 6005 | psa_aead_abort( &operation ); |
| 6006 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6007 | /* 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] | 6008 | #if SIZE_MAX > UINT32_MAX |
| 6009 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6010 | |
| 6011 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 6012 | { |
| 6013 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6014 | input_data->len ), |
| 6015 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6016 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6017 | PSA_ERROR_BAD_STATE ); |
| 6018 | } |
| 6019 | else |
| 6020 | { |
| 6021 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6022 | input_data->len ) ); |
| 6023 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6024 | } |
| 6025 | |
| 6026 | psa_aead_abort( &operation ); |
| 6027 | #endif |
| 6028 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6029 | /* 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] | 6030 | |
| 6031 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6032 | |
| 6033 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6034 | |
| 6035 | if( operation.alg == PSA_ALG_CCM ) |
| 6036 | { |
| 6037 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6038 | input_data->len ), |
| 6039 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6040 | } |
| 6041 | else |
| 6042 | { |
| 6043 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6044 | input_data->len ) ); |
| 6045 | } |
| 6046 | |
| 6047 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6048 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6049 | /* 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] | 6050 | #if SIZE_MAX > UINT32_MAX |
| 6051 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6052 | |
| 6053 | if( operation.alg == PSA_ALG_GCM ) |
| 6054 | { |
| 6055 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6056 | SIZE_MAX ), |
| 6057 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6058 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6059 | PSA_ERROR_BAD_STATE ); |
| 6060 | } |
| 6061 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6062 | { |
| 6063 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6064 | SIZE_MAX ) ); |
| 6065 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6066 | } |
| 6067 | |
| 6068 | psa_aead_abort( &operation ); |
| 6069 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6070 | /* 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] | 6071 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6072 | |
| 6073 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6074 | |
| 6075 | if( operation.alg == PSA_ALG_GCM ) |
| 6076 | { |
| 6077 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6078 | SIZE_MAX ), |
| 6079 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6080 | } |
| 6081 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6082 | { |
| 6083 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6084 | SIZE_MAX ) ); |
| 6085 | } |
| 6086 | |
| 6087 | psa_aead_abort( &operation ); |
| 6088 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6089 | |
| 6090 | /* ------------------------------------------------------- */ |
| 6091 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6092 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6093 | |
| 6094 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6095 | |
| 6096 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6097 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6098 | &nonce_length ), |
| 6099 | PSA_ERROR_BAD_STATE ); |
| 6100 | |
| 6101 | psa_aead_abort( &operation ); |
| 6102 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6103 | /* Test for generating nonce in decrypt setup. */ |
| 6104 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6105 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6106 | |
| 6107 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6108 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6109 | &nonce_length ), |
| 6110 | PSA_ERROR_BAD_STATE ); |
| 6111 | |
| 6112 | psa_aead_abort( &operation ); |
| 6113 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6114 | /* Test for setting lengths twice. */ |
| 6115 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6116 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6117 | |
| 6118 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6119 | |
| 6120 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6121 | input_data->len ) ); |
| 6122 | |
| 6123 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6124 | input_data->len ), |
| 6125 | PSA_ERROR_BAD_STATE ); |
| 6126 | |
| 6127 | psa_aead_abort( &operation ); |
| 6128 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6129 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6130 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6131 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6132 | |
| 6133 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6134 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6135 | if( operation.alg == PSA_ALG_CCM ) |
| 6136 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6137 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6138 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6139 | additional_data->len ), |
| 6140 | PSA_ERROR_BAD_STATE ); |
| 6141 | } |
| 6142 | else |
| 6143 | { |
| 6144 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6145 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6146 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6147 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6148 | input_data->len ), |
| 6149 | PSA_ERROR_BAD_STATE ); |
| 6150 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6151 | psa_aead_abort( &operation ); |
| 6152 | |
| 6153 | /* ------------------------------------------------------- */ |
| 6154 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6155 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6156 | |
| 6157 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6158 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6159 | if( operation.alg == PSA_ALG_CCM ) |
| 6160 | { |
| 6161 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6162 | input_data->len, output_data, |
| 6163 | output_size, &output_length ), |
| 6164 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6165 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6166 | } |
| 6167 | else |
| 6168 | { |
| 6169 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6170 | input_data->len, output_data, |
| 6171 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6172 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6173 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6174 | input_data->len ), |
| 6175 | PSA_ERROR_BAD_STATE ); |
| 6176 | } |
| 6177 | psa_aead_abort( &operation ); |
| 6178 | |
| 6179 | /* ------------------------------------------------------- */ |
| 6180 | |
| 6181 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6182 | |
| 6183 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6184 | |
| 6185 | if( operation.alg == PSA_ALG_CCM ) |
| 6186 | { |
| 6187 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6188 | finish_output_size, |
| 6189 | &output_part_length, |
| 6190 | tag_buffer, tag_length, |
| 6191 | &tag_size ) ); |
| 6192 | } |
| 6193 | else |
| 6194 | { |
| 6195 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6196 | finish_output_size, |
| 6197 | &output_part_length, |
| 6198 | tag_buffer, tag_length, |
| 6199 | &tag_size ) ); |
| 6200 | |
| 6201 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6202 | input_data->len ), |
| 6203 | PSA_ERROR_BAD_STATE ); |
| 6204 | } |
| 6205 | psa_aead_abort( &operation ); |
| 6206 | |
| 6207 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6208 | |
| 6209 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6210 | |
| 6211 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6212 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6213 | &nonce_length ) ); |
| 6214 | if( operation.alg == PSA_ALG_CCM ) |
| 6215 | { |
| 6216 | |
| 6217 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6218 | additional_data->len ), |
| 6219 | PSA_ERROR_BAD_STATE ); |
| 6220 | } |
| 6221 | else |
| 6222 | { |
| 6223 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6224 | additional_data->len ) ); |
| 6225 | |
| 6226 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6227 | input_data->len ), |
| 6228 | PSA_ERROR_BAD_STATE ); |
| 6229 | } |
| 6230 | psa_aead_abort( &operation ); |
| 6231 | |
| 6232 | /* ------------------------------------------------------- */ |
| 6233 | |
| 6234 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6235 | |
| 6236 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6237 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6238 | &nonce_length ) ); |
| 6239 | if( operation.alg == PSA_ALG_CCM ) |
| 6240 | { |
| 6241 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6242 | input_data->len, output_data, |
| 6243 | output_size, &output_length ), |
| 6244 | PSA_ERROR_BAD_STATE ); |
| 6245 | |
| 6246 | } |
| 6247 | else |
| 6248 | { |
| 6249 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6250 | input_data->len, output_data, |
| 6251 | output_size, &output_length ) ); |
| 6252 | |
| 6253 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6254 | input_data->len ), |
| 6255 | PSA_ERROR_BAD_STATE ); |
| 6256 | } |
| 6257 | psa_aead_abort( &operation ); |
| 6258 | |
| 6259 | /* ------------------------------------------------------- */ |
| 6260 | |
| 6261 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6262 | |
| 6263 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6264 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6265 | &nonce_length ) ); |
| 6266 | if( operation.alg == PSA_ALG_CCM ) |
| 6267 | { |
| 6268 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6269 | finish_output_size, |
| 6270 | &output_part_length, |
| 6271 | tag_buffer, tag_length, |
| 6272 | &tag_size ) ); |
| 6273 | } |
| 6274 | else |
| 6275 | { |
| 6276 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6277 | finish_output_size, |
| 6278 | &output_part_length, |
| 6279 | tag_buffer, tag_length, |
| 6280 | &tag_size ) ); |
| 6281 | |
| 6282 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6283 | input_data->len ), |
| 6284 | PSA_ERROR_BAD_STATE ); |
| 6285 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6286 | psa_aead_abort( &operation ); |
| 6287 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6288 | /* Test for not sending any additional data or data after setting non zero |
| 6289 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6290 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6291 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6292 | |
| 6293 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6294 | |
| 6295 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6296 | input_data->len ) ); |
| 6297 | |
| 6298 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6299 | finish_output_size, |
| 6300 | &output_part_length, |
| 6301 | tag_buffer, tag_length, |
| 6302 | &tag_size ), |
| 6303 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6304 | |
| 6305 | psa_aead_abort( &operation ); |
| 6306 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6307 | /* Test for not sending any additional data or data after setting non-zero |
| 6308 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6309 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6310 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6311 | |
| 6312 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6313 | |
| 6314 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6315 | input_data->len ) ); |
| 6316 | |
| 6317 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6318 | finish_output_size, |
| 6319 | &output_part_length, |
| 6320 | tag_buffer, |
| 6321 | tag_length ), |
| 6322 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6323 | |
| 6324 | psa_aead_abort( &operation ); |
| 6325 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6326 | /* Test for not sending any additional data after setting a non-zero length |
| 6327 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6328 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6329 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6330 | |
| 6331 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6332 | |
| 6333 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6334 | input_data->len ) ); |
| 6335 | |
| 6336 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6337 | input_data->len, output_data, |
| 6338 | output_size, &output_length ), |
| 6339 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6340 | |
| 6341 | psa_aead_abort( &operation ); |
| 6342 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6343 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6344 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6345 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6346 | |
| 6347 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6348 | |
| 6349 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6350 | input_data->len ) ); |
| 6351 | |
| 6352 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6353 | additional_data->len ) ); |
| 6354 | |
| 6355 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6356 | finish_output_size, |
| 6357 | &output_part_length, |
| 6358 | tag_buffer, tag_length, |
| 6359 | &tag_size ), |
| 6360 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6361 | |
| 6362 | psa_aead_abort( &operation ); |
| 6363 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6364 | /* Test for sending too much additional data after setting lengths. */ |
| 6365 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6366 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6367 | |
| 6368 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6369 | |
| 6370 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6371 | |
| 6372 | |
| 6373 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6374 | additional_data->len ), |
| 6375 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6376 | |
| 6377 | psa_aead_abort( &operation ); |
| 6378 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6379 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6380 | |
| 6381 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6382 | |
| 6383 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6384 | |
| 6385 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6386 | input_data->len ) ); |
| 6387 | |
| 6388 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6389 | additional_data->len ) ); |
| 6390 | |
| 6391 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6392 | 1 ), |
| 6393 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6394 | |
| 6395 | psa_aead_abort( &operation ); |
| 6396 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6397 | /* Test for sending too much data after setting lengths. */ |
| 6398 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6399 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6400 | |
| 6401 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6402 | |
| 6403 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6404 | |
| 6405 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6406 | input_data->len, output_data, |
| 6407 | output_size, &output_length ), |
| 6408 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6409 | |
| 6410 | psa_aead_abort( &operation ); |
| 6411 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6412 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6413 | |
| 6414 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6415 | |
| 6416 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6417 | |
| 6418 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6419 | input_data->len ) ); |
| 6420 | |
| 6421 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6422 | additional_data->len ) ); |
| 6423 | |
| 6424 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6425 | input_data->len, output_data, |
| 6426 | output_size, &output_length ) ); |
| 6427 | |
| 6428 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6429 | 1, output_data, |
| 6430 | output_size, &output_length ), |
| 6431 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6432 | |
| 6433 | psa_aead_abort( &operation ); |
| 6434 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6435 | /* Test sending additional data after data. */ |
| 6436 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6437 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6438 | |
| 6439 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6440 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6441 | if( operation.alg != PSA_ALG_CCM ) |
| 6442 | { |
| 6443 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6444 | input_data->len, output_data, |
| 6445 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6446 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6447 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6448 | additional_data->len ), |
| 6449 | PSA_ERROR_BAD_STATE ); |
| 6450 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6451 | psa_aead_abort( &operation ); |
| 6452 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6453 | /* Test calling finish on decryption. */ |
| 6454 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6455 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6456 | |
| 6457 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6458 | |
| 6459 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6460 | finish_output_size, |
| 6461 | &output_part_length, |
| 6462 | tag_buffer, tag_length, |
| 6463 | &tag_size ), |
| 6464 | PSA_ERROR_BAD_STATE ); |
| 6465 | |
| 6466 | psa_aead_abort( &operation ); |
| 6467 | |
| 6468 | /* Test calling verify on encryption. */ |
| 6469 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6470 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6471 | |
| 6472 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6473 | |
| 6474 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6475 | finish_output_size, |
| 6476 | &output_part_length, |
| 6477 | tag_buffer, |
| 6478 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6479 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6480 | |
| 6481 | psa_aead_abort( &operation ); |
| 6482 | |
| 6483 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6484 | exit: |
| 6485 | psa_destroy_key( key ); |
| 6486 | psa_aead_abort( &operation ); |
| 6487 | mbedtls_free( output_data ); |
| 6488 | mbedtls_free( final_data ); |
| 6489 | PSA_DONE( ); |
| 6490 | } |
| 6491 | /* END_CASE */ |
| 6492 | |
| 6493 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6494 | void signature_size( int type_arg, |
| 6495 | int bits, |
| 6496 | int alg_arg, |
| 6497 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6498 | { |
| 6499 | psa_key_type_t type = type_arg; |
| 6500 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6501 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6502 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6503 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6504 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6505 | exit: |
| 6506 | ; |
| 6507 | } |
| 6508 | /* END_CASE */ |
| 6509 | |
| 6510 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6511 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6512 | int alg_arg, data_t *input_data, |
| 6513 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6514 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6515 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6516 | psa_key_type_t key_type = key_type_arg; |
| 6517 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6518 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6519 | unsigned char *signature = NULL; |
| 6520 | size_t signature_size; |
| 6521 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6522 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6523 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6524 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6525 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6526 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6527 | psa_set_key_algorithm( &attributes, alg ); |
| 6528 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6529 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6530 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6531 | &key ) ); |
| 6532 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6533 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6534 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6535 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6536 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6537 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6538 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6539 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6540 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6541 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6542 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6543 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6544 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6545 | input_data->x, input_data->len, |
| 6546 | signature, signature_size, |
| 6547 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6548 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6549 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6550 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6551 | |
| 6552 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6553 | /* |
| 6554 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6555 | * thus reset them as required. |
| 6556 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6557 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6558 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6559 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6560 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6561 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6562 | } |
| 6563 | /* END_CASE */ |
| 6564 | |
| 6565 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6566 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6567 | int alg_arg, data_t *input_data, |
| 6568 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6569 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6570 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6571 | psa_key_type_t key_type = key_type_arg; |
| 6572 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6573 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6574 | psa_status_t actual_status; |
| 6575 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6576 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6577 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6578 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6579 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6580 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6581 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6582 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6583 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6584 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6585 | psa_set_key_algorithm( &attributes, alg ); |
| 6586 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6587 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6588 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6589 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6590 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6591 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6592 | input_data->x, input_data->len, |
| 6593 | signature, signature_size, |
| 6594 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6595 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6596 | /* The value of *signature_length is unspecified on error, but |
| 6597 | * whatever it is, it should be less than signature_size, so that |
| 6598 | * if the caller tries to read *signature_length bytes without |
| 6599 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6600 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6601 | |
| 6602 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6603 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6604 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6605 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6606 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6607 | } |
| 6608 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6609 | |
| 6610 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6611 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6612 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6613 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6614 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6615 | psa_key_type_t key_type = key_type_arg; |
| 6616 | psa_algorithm_t alg = alg_arg; |
| 6617 | size_t key_bits; |
| 6618 | unsigned char *signature = NULL; |
| 6619 | size_t signature_size; |
| 6620 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6621 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6622 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6623 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6624 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6625 | 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] | 6626 | psa_set_key_algorithm( &attributes, alg ); |
| 6627 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6628 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6629 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6630 | &key ) ); |
| 6631 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6632 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6633 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6634 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6635 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6636 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6637 | key_bits, alg ); |
| 6638 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6639 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6640 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6641 | |
| 6642 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6643 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6644 | input_data->x, input_data->len, |
| 6645 | signature, signature_size, |
| 6646 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6647 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6648 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6649 | TEST_ASSERT( signature_length > 0 ); |
| 6650 | |
| 6651 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6652 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6653 | input_data->x, input_data->len, |
| 6654 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6655 | |
| 6656 | if( input_data->len != 0 ) |
| 6657 | { |
| 6658 | /* Flip a bit in the input and verify that the signature is now |
| 6659 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6660 | * because ECDSA may ignore the last few bits of the input. */ |
| 6661 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6662 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6663 | input_data->x, input_data->len, |
| 6664 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6665 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6666 | } |
| 6667 | |
| 6668 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6669 | /* |
| 6670 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6671 | * thus reset them as required. |
| 6672 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6673 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6674 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6675 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6676 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6677 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6678 | } |
| 6679 | /* END_CASE */ |
| 6680 | |
| 6681 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6682 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6683 | int alg_arg, data_t *hash_data, |
| 6684 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6685 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6686 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6687 | psa_key_type_t key_type = key_type_arg; |
| 6688 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6689 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6690 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6691 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6692 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6693 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6694 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6695 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6696 | psa_set_key_algorithm( &attributes, alg ); |
| 6697 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6698 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6699 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6700 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6701 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6702 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6703 | hash_data->x, hash_data->len, |
| 6704 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6705 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6706 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6707 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6708 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6709 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6710 | } |
| 6711 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6712 | |
| 6713 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6714 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6715 | int alg_arg, data_t *hash_data, |
| 6716 | data_t *signature_data, |
| 6717 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6718 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6719 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6720 | psa_key_type_t key_type = key_type_arg; |
| 6721 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6722 | psa_status_t actual_status; |
| 6723 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6724 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6725 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6726 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6727 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6728 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6729 | psa_set_key_algorithm( &attributes, alg ); |
| 6730 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6731 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6732 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6733 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6734 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6735 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6736 | hash_data->x, hash_data->len, |
| 6737 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6738 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6739 | |
| 6740 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6741 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6742 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6743 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6744 | } |
| 6745 | /* END_CASE */ |
| 6746 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6747 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6748 | void sign_message_deterministic( int key_type_arg, |
| 6749 | data_t *key_data, |
| 6750 | int alg_arg, |
| 6751 | data_t *input_data, |
| 6752 | data_t *output_data ) |
| 6753 | { |
| 6754 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6755 | psa_key_type_t key_type = key_type_arg; |
| 6756 | psa_algorithm_t alg = alg_arg; |
| 6757 | size_t key_bits; |
| 6758 | unsigned char *signature = NULL; |
| 6759 | size_t signature_size; |
| 6760 | size_t signature_length = 0xdeadbeef; |
| 6761 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6762 | |
| 6763 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6764 | |
| 6765 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6766 | psa_set_key_algorithm( &attributes, alg ); |
| 6767 | psa_set_key_type( &attributes, key_type ); |
| 6768 | |
| 6769 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6770 | &key ) ); |
| 6771 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6772 | key_bits = psa_get_key_bits( &attributes ); |
| 6773 | |
| 6774 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6775 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6776 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6777 | ASSERT_ALLOC( signature, signature_size ); |
| 6778 | |
| 6779 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6780 | input_data->x, input_data->len, |
| 6781 | signature, signature_size, |
| 6782 | &signature_length ) ); |
| 6783 | |
| 6784 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6785 | signature, signature_length ); |
| 6786 | |
| 6787 | exit: |
| 6788 | psa_reset_key_attributes( &attributes ); |
| 6789 | |
| 6790 | psa_destroy_key( key ); |
| 6791 | mbedtls_free( signature ); |
| 6792 | PSA_DONE( ); |
| 6793 | |
| 6794 | } |
| 6795 | /* END_CASE */ |
| 6796 | |
| 6797 | /* BEGIN_CASE */ |
| 6798 | void sign_message_fail( int key_type_arg, |
| 6799 | data_t *key_data, |
| 6800 | int alg_arg, |
| 6801 | data_t *input_data, |
| 6802 | int signature_size_arg, |
| 6803 | int expected_status_arg ) |
| 6804 | { |
| 6805 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6806 | psa_key_type_t key_type = key_type_arg; |
| 6807 | psa_algorithm_t alg = alg_arg; |
| 6808 | size_t signature_size = signature_size_arg; |
| 6809 | psa_status_t actual_status; |
| 6810 | psa_status_t expected_status = expected_status_arg; |
| 6811 | unsigned char *signature = NULL; |
| 6812 | size_t signature_length = 0xdeadbeef; |
| 6813 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6814 | |
| 6815 | ASSERT_ALLOC( signature, signature_size ); |
| 6816 | |
| 6817 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6818 | |
| 6819 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6820 | psa_set_key_algorithm( &attributes, alg ); |
| 6821 | psa_set_key_type( &attributes, key_type ); |
| 6822 | |
| 6823 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6824 | &key ) ); |
| 6825 | |
| 6826 | actual_status = psa_sign_message( key, alg, |
| 6827 | input_data->x, input_data->len, |
| 6828 | signature, signature_size, |
| 6829 | &signature_length ); |
| 6830 | TEST_EQUAL( actual_status, expected_status ); |
| 6831 | /* The value of *signature_length is unspecified on error, but |
| 6832 | * whatever it is, it should be less than signature_size, so that |
| 6833 | * if the caller tries to read *signature_length bytes without |
| 6834 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6835 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6836 | |
| 6837 | exit: |
| 6838 | psa_reset_key_attributes( &attributes ); |
| 6839 | psa_destroy_key( key ); |
| 6840 | mbedtls_free( signature ); |
| 6841 | PSA_DONE( ); |
| 6842 | } |
| 6843 | /* END_CASE */ |
| 6844 | |
| 6845 | /* BEGIN_CASE */ |
| 6846 | void sign_verify_message( int key_type_arg, |
| 6847 | data_t *key_data, |
| 6848 | int alg_arg, |
| 6849 | data_t *input_data ) |
| 6850 | { |
| 6851 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6852 | psa_key_type_t key_type = key_type_arg; |
| 6853 | psa_algorithm_t alg = alg_arg; |
| 6854 | size_t key_bits; |
| 6855 | unsigned char *signature = NULL; |
| 6856 | size_t signature_size; |
| 6857 | size_t signature_length = 0xdeadbeef; |
| 6858 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6859 | |
| 6860 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6861 | |
| 6862 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6863 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6864 | psa_set_key_algorithm( &attributes, alg ); |
| 6865 | psa_set_key_type( &attributes, key_type ); |
| 6866 | |
| 6867 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6868 | &key ) ); |
| 6869 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6870 | key_bits = psa_get_key_bits( &attributes ); |
| 6871 | |
| 6872 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6873 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6874 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6875 | ASSERT_ALLOC( signature, signature_size ); |
| 6876 | |
| 6877 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6878 | input_data->x, input_data->len, |
| 6879 | signature, signature_size, |
| 6880 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6881 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6882 | TEST_ASSERT( signature_length > 0 ); |
| 6883 | |
| 6884 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6885 | input_data->x, input_data->len, |
| 6886 | signature, signature_length ) ); |
| 6887 | |
| 6888 | if( input_data->len != 0 ) |
| 6889 | { |
| 6890 | /* Flip a bit in the input and verify that the signature is now |
| 6891 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6892 | * because ECDSA may ignore the last few bits of the input. */ |
| 6893 | input_data->x[0] ^= 1; |
| 6894 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6895 | input_data->x, input_data->len, |
| 6896 | signature, signature_length ), |
| 6897 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6898 | } |
| 6899 | |
| 6900 | exit: |
| 6901 | psa_reset_key_attributes( &attributes ); |
| 6902 | |
| 6903 | psa_destroy_key( key ); |
| 6904 | mbedtls_free( signature ); |
| 6905 | PSA_DONE( ); |
| 6906 | } |
| 6907 | /* END_CASE */ |
| 6908 | |
| 6909 | /* BEGIN_CASE */ |
| 6910 | void verify_message( int key_type_arg, |
| 6911 | data_t *key_data, |
| 6912 | int alg_arg, |
| 6913 | data_t *input_data, |
| 6914 | data_t *signature_data ) |
| 6915 | { |
| 6916 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6917 | psa_key_type_t key_type = key_type_arg; |
| 6918 | psa_algorithm_t alg = alg_arg; |
| 6919 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6920 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6921 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6922 | |
| 6923 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6924 | |
| 6925 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6926 | psa_set_key_algorithm( &attributes, alg ); |
| 6927 | psa_set_key_type( &attributes, key_type ); |
| 6928 | |
| 6929 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6930 | &key ) ); |
| 6931 | |
| 6932 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6933 | input_data->x, input_data->len, |
| 6934 | signature_data->x, signature_data->len ) ); |
| 6935 | |
| 6936 | exit: |
| 6937 | psa_reset_key_attributes( &attributes ); |
| 6938 | psa_destroy_key( key ); |
| 6939 | PSA_DONE( ); |
| 6940 | } |
| 6941 | /* END_CASE */ |
| 6942 | |
| 6943 | /* BEGIN_CASE */ |
| 6944 | void verify_message_fail( int key_type_arg, |
| 6945 | data_t *key_data, |
| 6946 | int alg_arg, |
| 6947 | data_t *hash_data, |
| 6948 | data_t *signature_data, |
| 6949 | int expected_status_arg ) |
| 6950 | { |
| 6951 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6952 | psa_key_type_t key_type = key_type_arg; |
| 6953 | psa_algorithm_t alg = alg_arg; |
| 6954 | psa_status_t actual_status; |
| 6955 | psa_status_t expected_status = expected_status_arg; |
| 6956 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6957 | |
| 6958 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6959 | |
| 6960 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6961 | psa_set_key_algorithm( &attributes, alg ); |
| 6962 | psa_set_key_type( &attributes, key_type ); |
| 6963 | |
| 6964 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6965 | &key ) ); |
| 6966 | |
| 6967 | actual_status = psa_verify_message( key, alg, |
| 6968 | hash_data->x, hash_data->len, |
| 6969 | signature_data->x, |
| 6970 | signature_data->len ); |
| 6971 | TEST_EQUAL( actual_status, expected_status ); |
| 6972 | |
| 6973 | exit: |
| 6974 | psa_reset_key_attributes( &attributes ); |
| 6975 | psa_destroy_key( key ); |
| 6976 | PSA_DONE( ); |
| 6977 | } |
| 6978 | /* END_CASE */ |
| 6979 | |
| 6980 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6981 | void asymmetric_encrypt( int key_type_arg, |
| 6982 | data_t *key_data, |
| 6983 | int alg_arg, |
| 6984 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6985 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6986 | int expected_output_length_arg, |
| 6987 | int expected_status_arg ) |
| 6988 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6989 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6990 | psa_key_type_t key_type = key_type_arg; |
| 6991 | psa_algorithm_t alg = alg_arg; |
| 6992 | size_t expected_output_length = expected_output_length_arg; |
| 6993 | size_t key_bits; |
| 6994 | unsigned char *output = NULL; |
| 6995 | size_t output_size; |
| 6996 | size_t output_length = ~0; |
| 6997 | psa_status_t actual_status; |
| 6998 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6999 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7000 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7001 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 7002 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7003 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7004 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7005 | psa_set_key_algorithm( &attributes, alg ); |
| 7006 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7007 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7008 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7009 | |
| 7010 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7011 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7012 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7013 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7014 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7015 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7016 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7017 | |
| 7018 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7019 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7020 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7021 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7022 | output, output_size, |
| 7023 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7024 | TEST_EQUAL( actual_status, expected_status ); |
| 7025 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7026 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7027 | /* If the label is empty, the test framework puts a non-null pointer |
| 7028 | * in label->x. Test that a null pointer works as well. */ |
| 7029 | if( label->len == 0 ) |
| 7030 | { |
| 7031 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7032 | if( output_size != 0 ) |
| 7033 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7034 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7035 | input_data->x, input_data->len, |
| 7036 | NULL, label->len, |
| 7037 | output, output_size, |
| 7038 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7039 | TEST_EQUAL( actual_status, expected_status ); |
| 7040 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7041 | } |
| 7042 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7043 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7044 | /* |
| 7045 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7046 | * thus reset them as required. |
| 7047 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7048 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7049 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7050 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7051 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7052 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7053 | } |
| 7054 | /* END_CASE */ |
| 7055 | |
| 7056 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7057 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 7058 | data_t *key_data, |
| 7059 | int alg_arg, |
| 7060 | data_t *input_data, |
| 7061 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7062 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7063 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7064 | psa_key_type_t key_type = key_type_arg; |
| 7065 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7066 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7067 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7068 | size_t output_size; |
| 7069 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7070 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7071 | size_t output2_size; |
| 7072 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7073 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7074 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7075 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7076 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7077 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 7078 | psa_set_key_algorithm( &attributes, alg ); |
| 7079 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7080 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7081 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7082 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7083 | |
| 7084 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7085 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7086 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7087 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7088 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7089 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7090 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7091 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7092 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7093 | TEST_LE_U( output2_size, |
| 7094 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 7095 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7096 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7097 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7098 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7099 | * the original plaintext because of the non-optional random |
| 7100 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7101 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7102 | input_data->x, input_data->len, |
| 7103 | label->x, label->len, |
| 7104 | output, output_size, |
| 7105 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7106 | /* We don't know what ciphertext length to expect, but check that |
| 7107 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7108 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7109 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7110 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7111 | output, output_length, |
| 7112 | label->x, label->len, |
| 7113 | output2, output2_size, |
| 7114 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7115 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 7116 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7117 | |
| 7118 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7119 | /* |
| 7120 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7121 | * thus reset them as required. |
| 7122 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7123 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7124 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7125 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7126 | mbedtls_free( output ); |
| 7127 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7128 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7129 | } |
| 7130 | /* END_CASE */ |
| 7131 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7132 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7133 | void asymmetric_decrypt( int key_type_arg, |
| 7134 | data_t *key_data, |
| 7135 | int alg_arg, |
| 7136 | data_t *input_data, |
| 7137 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 7138 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7139 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7140 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7141 | psa_key_type_t key_type = key_type_arg; |
| 7142 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7143 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7144 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7145 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7146 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7147 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7148 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7149 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7150 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7151 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7152 | psa_set_key_algorithm( &attributes, alg ); |
| 7153 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7154 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7155 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7156 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7157 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7158 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 7159 | key_bits = psa_get_key_bits( &attributes ); |
| 7160 | |
| 7161 | /* Determine the maximum ciphertext length */ |
| 7162 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7163 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7164 | ASSERT_ALLOC( output, output_size ); |
| 7165 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7166 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7167 | input_data->x, input_data->len, |
| 7168 | label->x, label->len, |
| 7169 | output, |
| 7170 | output_size, |
| 7171 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7172 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7173 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7174 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7175 | /* If the label is empty, the test framework puts a non-null pointer |
| 7176 | * in label->x. Test that a null pointer works as well. */ |
| 7177 | if( label->len == 0 ) |
| 7178 | { |
| 7179 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7180 | if( output_size != 0 ) |
| 7181 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7182 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7183 | input_data->x, input_data->len, |
| 7184 | NULL, label->len, |
| 7185 | output, |
| 7186 | output_size, |
| 7187 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7188 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7189 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7190 | } |
| 7191 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7192 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7193 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7194 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7195 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7196 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7197 | } |
| 7198 | /* END_CASE */ |
| 7199 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7200 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7201 | void asymmetric_decrypt_fail( int key_type_arg, |
| 7202 | data_t *key_data, |
| 7203 | int alg_arg, |
| 7204 | data_t *input_data, |
| 7205 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7206 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7207 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7208 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7209 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7210 | psa_key_type_t key_type = key_type_arg; |
| 7211 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7212 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7213 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7214 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7215 | psa_status_t actual_status; |
| 7216 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7217 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7218 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7219 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7220 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7221 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7222 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7223 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7224 | psa_set_key_algorithm( &attributes, alg ); |
| 7225 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7226 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7227 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7228 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7229 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7230 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7231 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7232 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7233 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7234 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7235 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7236 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7237 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7238 | /* If the label is empty, the test framework puts a non-null pointer |
| 7239 | * in label->x. Test that a null pointer works as well. */ |
| 7240 | if( label->len == 0 ) |
| 7241 | { |
| 7242 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7243 | if( output_size != 0 ) |
| 7244 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7245 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7246 | input_data->x, input_data->len, |
| 7247 | NULL, label->len, |
| 7248 | output, output_size, |
| 7249 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7250 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7251 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7252 | } |
| 7253 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7254 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7255 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7256 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7257 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7258 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7259 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7260 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7261 | |
| 7262 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 7263 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7264 | { |
| 7265 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7266 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7267 | * 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] | 7268 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7269 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7270 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 7271 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7272 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7273 | |
| 7274 | memset( &zero, 0, sizeof( zero ) ); |
| 7275 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7276 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7277 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7278 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7279 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7280 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7281 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7282 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7283 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7284 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7285 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7286 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7287 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7288 | } |
| 7289 | /* END_CASE */ |
| 7290 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7291 | /* BEGIN_CASE */ |
| 7292 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7293 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7294 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7295 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7296 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7297 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7298 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7299 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7300 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7301 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7302 | |
| 7303 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7304 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7305 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7306 | } |
| 7307 | /* END_CASE */ |
| 7308 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7309 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7310 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7311 | int expected_status_arg ) |
| 7312 | { |
| 7313 | psa_algorithm_t alg = alg_arg; |
| 7314 | size_t capacity = capacity_arg; |
| 7315 | psa_status_t expected_status = expected_status_arg; |
| 7316 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7317 | |
| 7318 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7319 | |
| 7320 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7321 | |
| 7322 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7323 | expected_status ); |
| 7324 | |
| 7325 | exit: |
| 7326 | psa_key_derivation_abort( &operation ); |
| 7327 | PSA_DONE( ); |
| 7328 | } |
| 7329 | /* END_CASE */ |
| 7330 | |
| 7331 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7332 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7333 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7334 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7335 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7336 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7337 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7338 | int expected_status_arg3, |
| 7339 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7340 | { |
| 7341 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7342 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7343 | 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] | 7344 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7345 | expected_status_arg2, |
| 7346 | expected_status_arg3}; |
| 7347 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7348 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7349 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7350 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7351 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7352 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7353 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7354 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7355 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7356 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7357 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7358 | |
| 7359 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7360 | |
| 7361 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7362 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7363 | |
| 7364 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7365 | |
| 7366 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7367 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7368 | mbedtls_test_set_step( i ); |
| 7369 | if( steps[i] == 0 ) |
| 7370 | { |
| 7371 | /* Skip this step */ |
| 7372 | } |
| 7373 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7374 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7375 | psa_set_key_type( &attributes, key_types[i] ); |
| 7376 | PSA_ASSERT( psa_import_key( &attributes, |
| 7377 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7378 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7379 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7380 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7381 | { |
| 7382 | // When taking a private key as secret input, use key agreement |
| 7383 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7384 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7385 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7386 | expected_statuses[i] ); |
| 7387 | } |
| 7388 | else |
| 7389 | { |
| 7390 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7391 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7392 | expected_statuses[i] ); |
| 7393 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7394 | } |
| 7395 | else |
| 7396 | { |
| 7397 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7398 | &operation, steps[i], |
| 7399 | inputs[i]->x, inputs[i]->len ), |
| 7400 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7401 | } |
| 7402 | } |
| 7403 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7404 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7405 | { |
| 7406 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7407 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7408 | psa_set_key_bits( &attributes, 8 ); |
| 7409 | actual_output_status = |
| 7410 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7411 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7412 | } |
| 7413 | else |
| 7414 | { |
| 7415 | uint8_t buffer[1]; |
| 7416 | actual_output_status = |
| 7417 | psa_key_derivation_output_bytes( &operation, |
| 7418 | buffer, sizeof( buffer ) ); |
| 7419 | } |
| 7420 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7421 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7422 | exit: |
| 7423 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7424 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7425 | psa_destroy_key( keys[i] ); |
| 7426 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7427 | PSA_DONE( ); |
| 7428 | } |
| 7429 | /* END_CASE */ |
| 7430 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7431 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7432 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7433 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7434 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7435 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7436 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7437 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7438 | unsigned char input1[] = "Input 1"; |
| 7439 | size_t input1_length = sizeof( input1 ); |
| 7440 | unsigned char input2[] = "Input 2"; |
| 7441 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7442 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7443 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7444 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7445 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7446 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7447 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7448 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7449 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7450 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7451 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7452 | psa_set_key_algorithm( &attributes, alg ); |
| 7453 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7454 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7455 | PSA_ASSERT( psa_import_key( &attributes, |
| 7456 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7457 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7458 | |
| 7459 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7460 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7461 | input1, input1_length, |
| 7462 | input2, input2_length, |
| 7463 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7464 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7465 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7466 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7467 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7468 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7469 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7470 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7471 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7472 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7473 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7474 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7475 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7476 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7477 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7478 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7479 | } |
| 7480 | /* END_CASE */ |
| 7481 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7482 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7483 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7484 | { |
| 7485 | uint8_t output_buffer[16]; |
| 7486 | size_t buffer_size = 16; |
| 7487 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7488 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7489 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7490 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7491 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7492 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7493 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7494 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7495 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7496 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7497 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7498 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7499 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7500 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7501 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7502 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7503 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7504 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7505 | |
| 7506 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7507 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7508 | } |
| 7509 | /* END_CASE */ |
| 7510 | |
| 7511 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7512 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7513 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7514 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7515 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7516 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7517 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7518 | int requested_capacity_arg, |
| 7519 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7520 | data_t *expected_output2, |
| 7521 | int other_key_input_type, |
| 7522 | int key_input_type, |
| 7523 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7524 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7525 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7526 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7527 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7528 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7529 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7530 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7531 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7532 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7533 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7534 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7535 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7536 | uint8_t *expected_outputs[2] = |
| 7537 | {expected_output1->x, expected_output2->x}; |
| 7538 | size_t output_sizes[2] = |
| 7539 | {expected_output1->len, expected_output2->len}; |
| 7540 | size_t output_buffer_size = 0; |
| 7541 | uint8_t *output_buffer = NULL; |
| 7542 | size_t expected_capacity; |
| 7543 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7544 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7545 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7546 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7547 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7548 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7549 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7550 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7551 | |
| 7552 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7553 | { |
| 7554 | if( output_sizes[i] > output_buffer_size ) |
| 7555 | output_buffer_size = output_sizes[i]; |
| 7556 | if( output_sizes[i] == 0 ) |
| 7557 | expected_outputs[i] = NULL; |
| 7558 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7559 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7560 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7561 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7562 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7563 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7564 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7565 | requested_capacity ) ); |
| 7566 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7567 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7568 | switch( steps[i] ) |
| 7569 | { |
| 7570 | case 0: |
| 7571 | break; |
| 7572 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7573 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7574 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7575 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7576 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7577 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7578 | inputs[i]->x, inputs[i]->len ), |
| 7579 | statuses[i] ); |
| 7580 | |
| 7581 | if( statuses[i] != PSA_SUCCESS ) |
| 7582 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7583 | break; |
| 7584 | case 1: // input key |
| 7585 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7586 | psa_set_key_algorithm( &attributes1, alg ); |
| 7587 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7588 | |
| 7589 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7590 | inputs[i]->x, inputs[i]->len, |
| 7591 | &keys[i] ) ); |
| 7592 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7593 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7594 | { |
| 7595 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7596 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7597 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7598 | } |
| 7599 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7600 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7601 | steps[i], |
| 7602 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7603 | break; |
| 7604 | default: |
| 7605 | TEST_ASSERT( ! "default case not supported" ); |
| 7606 | break; |
| 7607 | } |
| 7608 | break; |
| 7609 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7610 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7611 | { |
| 7612 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7613 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7614 | steps[i], |
| 7615 | inputs[i]->x, |
| 7616 | inputs[i]->len ), |
| 7617 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7618 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7619 | case 1: // input key, type DERIVE |
| 7620 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7621 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7622 | psa_set_key_algorithm( &attributes2, alg ); |
| 7623 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7624 | |
| 7625 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7626 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7627 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7628 | |
| 7629 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7630 | inputs[i]->x, inputs[i]->len, |
| 7631 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7632 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7633 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7634 | steps[i], |
| 7635 | keys[i] ), |
| 7636 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7637 | break; |
| 7638 | case 2: // key agreement |
| 7639 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7640 | psa_set_key_algorithm( &attributes3, alg ); |
| 7641 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7642 | |
| 7643 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7644 | inputs[i]->x, inputs[i]->len, |
| 7645 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7646 | |
| 7647 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7648 | &operation, |
| 7649 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7650 | keys[i], key_agreement_peer_key->x, |
| 7651 | key_agreement_peer_key->len ), statuses[i] ); |
| 7652 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7653 | default: |
| 7654 | TEST_ASSERT( ! "default case not supported" ); |
| 7655 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7656 | } |
| 7657 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7658 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7659 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7660 | break; |
| 7661 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7662 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7663 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7664 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7665 | |
| 7666 | if( statuses[i] != PSA_SUCCESS ) |
| 7667 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7668 | break; |
| 7669 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7670 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7671 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7672 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7673 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7674 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7675 | expected_capacity = requested_capacity; |
| 7676 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7677 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7678 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7679 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7680 | |
| 7681 | /* For output key derivation secret must be provided using |
| 7682 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7683 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7684 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7685 | |
| 7686 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7687 | psa_set_key_algorithm( &attributes4, alg ); |
| 7688 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7689 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7690 | |
| 7691 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7692 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7693 | } |
| 7694 | else // output bytes |
| 7695 | { |
| 7696 | /* Expansion phase. */ |
| 7697 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7698 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7699 | /* Read some bytes. */ |
| 7700 | status = psa_key_derivation_output_bytes( &operation, |
| 7701 | output_buffer, output_sizes[i] ); |
| 7702 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7703 | { |
| 7704 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7705 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7706 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7707 | continue; |
| 7708 | } |
| 7709 | else if( expected_capacity == 0 || |
| 7710 | output_sizes[i] > expected_capacity ) |
| 7711 | { |
| 7712 | /* Capacity exceeded. */ |
| 7713 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7714 | expected_capacity = 0; |
| 7715 | continue; |
| 7716 | } |
| 7717 | /* Success. Check the read data. */ |
| 7718 | PSA_ASSERT( status ); |
| 7719 | if( output_sizes[i] != 0 ) |
| 7720 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7721 | expected_outputs[i], output_sizes[i] ); |
| 7722 | /* Check the operation status. */ |
| 7723 | expected_capacity -= output_sizes[i]; |
| 7724 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7725 | ¤t_capacity ) ); |
| 7726 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7727 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7728 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7729 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7730 | |
| 7731 | exit: |
| 7732 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7733 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7734 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7735 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7736 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7737 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7738 | } |
| 7739 | /* END_CASE */ |
| 7740 | |
| 7741 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7742 | void derive_full( int alg_arg, |
| 7743 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7744 | data_t *input1, |
| 7745 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7746 | int requested_capacity_arg ) |
| 7747 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7748 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7749 | psa_algorithm_t alg = alg_arg; |
| 7750 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7751 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7752 | unsigned char output_buffer[16]; |
| 7753 | size_t expected_capacity = requested_capacity; |
| 7754 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7755 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7756 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7757 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7758 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7759 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7760 | psa_set_key_algorithm( &attributes, alg ); |
| 7761 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7762 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7763 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7764 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7765 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7766 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7767 | input1->x, input1->len, |
| 7768 | input2->x, input2->len, |
| 7769 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7770 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7771 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7772 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7773 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7774 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7775 | |
| 7776 | /* Expansion phase. */ |
| 7777 | while( current_capacity > 0 ) |
| 7778 | { |
| 7779 | size_t read_size = sizeof( output_buffer ); |
| 7780 | if( read_size > current_capacity ) |
| 7781 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7782 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7783 | output_buffer, |
| 7784 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7785 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7786 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7787 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7788 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7789 | } |
| 7790 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7791 | /* Check that the operation refuses to go over capacity. */ |
| 7792 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7793 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7794 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7795 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7796 | |
| 7797 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7798 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7799 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7800 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7801 | } |
| 7802 | /* END_CASE */ |
| 7803 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7804 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7805 | void derive_key_exercise( int alg_arg, |
| 7806 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7807 | data_t *input1, |
| 7808 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7809 | int derived_type_arg, |
| 7810 | int derived_bits_arg, |
| 7811 | int derived_usage_arg, |
| 7812 | int derived_alg_arg ) |
| 7813 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7814 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7815 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7816 | psa_algorithm_t alg = alg_arg; |
| 7817 | psa_key_type_t derived_type = derived_type_arg; |
| 7818 | size_t derived_bits = derived_bits_arg; |
| 7819 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7820 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7821 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7822 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7823 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7824 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7825 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7826 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7827 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7828 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7829 | psa_set_key_algorithm( &attributes, alg ); |
| 7830 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7831 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7832 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7833 | |
| 7834 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7835 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7836 | input1->x, input1->len, |
| 7837 | input2->x, input2->len, |
| 7838 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7839 | goto exit; |
| 7840 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7841 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7842 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7843 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7844 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7845 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7846 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7847 | |
| 7848 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7849 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7850 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7851 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7852 | |
| 7853 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7854 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7855 | goto exit; |
| 7856 | |
| 7857 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7858 | /* |
| 7859 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7860 | * thus reset them as required. |
| 7861 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7862 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7863 | |
| 7864 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7865 | psa_destroy_key( base_key ); |
| 7866 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7867 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7868 | } |
| 7869 | /* END_CASE */ |
| 7870 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7871 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7872 | void derive_key_export( int alg_arg, |
| 7873 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7874 | data_t *input1, |
| 7875 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7876 | int bytes1_arg, |
| 7877 | int bytes2_arg ) |
| 7878 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7879 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7880 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7881 | psa_algorithm_t alg = alg_arg; |
| 7882 | size_t bytes1 = bytes1_arg; |
| 7883 | size_t bytes2 = bytes2_arg; |
| 7884 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7885 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7886 | uint8_t *output_buffer = NULL; |
| 7887 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7888 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7889 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7890 | size_t length; |
| 7891 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7892 | ASSERT_ALLOC( output_buffer, capacity ); |
| 7893 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7894 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7895 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7896 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7897 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7898 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7899 | 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] | 7900 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7901 | |
| 7902 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7903 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7904 | input1->x, input1->len, |
| 7905 | input2->x, input2->len, |
| 7906 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7907 | goto exit; |
| 7908 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7909 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7910 | output_buffer, |
| 7911 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7912 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7913 | |
| 7914 | /* 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] | 7915 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7916 | input1->x, input1->len, |
| 7917 | input2->x, input2->len, |
| 7918 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7919 | goto exit; |
| 7920 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7921 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7922 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7923 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7924 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7925 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7926 | &derived_key ) ); |
| 7927 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7928 | export_buffer, bytes1, |
| 7929 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7930 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7931 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7932 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7933 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7934 | &derived_key ) ); |
| 7935 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7936 | export_buffer + bytes1, bytes2, |
| 7937 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7938 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7939 | |
| 7940 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 7941 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 7942 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7943 | |
| 7944 | exit: |
| 7945 | mbedtls_free( output_buffer ); |
| 7946 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7947 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7948 | psa_destroy_key( base_key ); |
| 7949 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7950 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7951 | } |
| 7952 | /* END_CASE */ |
| 7953 | |
| 7954 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7955 | void derive_key_type( int alg_arg, |
| 7956 | data_t *key_data, |
| 7957 | data_t *input1, |
| 7958 | data_t *input2, |
| 7959 | int key_type_arg, int bits_arg, |
| 7960 | data_t *expected_export ) |
| 7961 | { |
| 7962 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7963 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7964 | const psa_algorithm_t alg = alg_arg; |
| 7965 | const psa_key_type_t key_type = key_type_arg; |
| 7966 | const size_t bits = bits_arg; |
| 7967 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7968 | const size_t export_buffer_size = |
| 7969 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 7970 | uint8_t *export_buffer = NULL; |
| 7971 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7972 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7973 | size_t export_length; |
| 7974 | |
| 7975 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 7976 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7977 | |
| 7978 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7979 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7980 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 7981 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 7982 | &base_key ) ); |
| 7983 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 7984 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7985 | &operation, base_key, alg, |
| 7986 | input1->x, input1->len, |
| 7987 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 7988 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7989 | goto exit; |
| 7990 | |
| 7991 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7992 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7993 | psa_set_key_type( &derived_attributes, key_type ); |
| 7994 | psa_set_key_bits( &derived_attributes, bits ); |
| 7995 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 7996 | &derived_key ) ); |
| 7997 | |
| 7998 | PSA_ASSERT( psa_export_key( derived_key, |
| 7999 | export_buffer, export_buffer_size, |
| 8000 | &export_length ) ); |
| 8001 | ASSERT_COMPARE( export_buffer, export_length, |
| 8002 | expected_export->x, expected_export->len ); |
| 8003 | |
| 8004 | exit: |
| 8005 | mbedtls_free( export_buffer ); |
| 8006 | psa_key_derivation_abort( &operation ); |
| 8007 | psa_destroy_key( base_key ); |
| 8008 | psa_destroy_key( derived_key ); |
| 8009 | PSA_DONE( ); |
| 8010 | } |
| 8011 | /* END_CASE */ |
| 8012 | |
| 8013 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8014 | void derive_key( int alg_arg, |
| 8015 | data_t *key_data, data_t *input1, data_t *input2, |
| 8016 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8017 | int expected_status_arg, |
| 8018 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8019 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8020 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8021 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8022 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8023 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8024 | size_t bits = bits_arg; |
| 8025 | psa_status_t expected_status = expected_status_arg; |
| 8026 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8027 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8028 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8029 | |
| 8030 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8031 | |
| 8032 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8033 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8034 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8035 | 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] | 8036 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8037 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8038 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8039 | input1->x, input1->len, |
| 8040 | input2->x, input2->len, |
| 8041 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8042 | goto exit; |
| 8043 | |
| 8044 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8045 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8046 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8047 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8048 | |
| 8049 | psa_status_t status = |
| 8050 | psa_key_derivation_output_key( &derived_attributes, |
| 8051 | &operation, |
| 8052 | &derived_key ); |
| 8053 | if( is_large_output > 0 ) |
| 8054 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8055 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8056 | |
| 8057 | exit: |
| 8058 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8059 | psa_destroy_key( base_key ); |
| 8060 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8061 | PSA_DONE( ); |
| 8062 | } |
| 8063 | /* END_CASE */ |
| 8064 | |
| 8065 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8066 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 8067 | int our_key_type_arg, int our_key_alg_arg, |
| 8068 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8069 | int expected_status_arg ) |
| 8070 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8071 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8072 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8073 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8074 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8075 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8076 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8077 | psa_status_t expected_status = expected_status_arg; |
| 8078 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8079 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8080 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8081 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8082 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8083 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8084 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8085 | PSA_ASSERT( psa_import_key( &attributes, |
| 8086 | our_key_data->x, our_key_data->len, |
| 8087 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8088 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8089 | /* The tests currently include inputs that should fail at either step. |
| 8090 | * Test cases that fail at the setup step should be changed to call |
| 8091 | * key_derivation_setup instead, and this function should be renamed |
| 8092 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8093 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8094 | if( status == PSA_SUCCESS ) |
| 8095 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8096 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 8097 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8098 | our_key, |
| 8099 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8100 | expected_status ); |
| 8101 | } |
| 8102 | else |
| 8103 | { |
| 8104 | TEST_ASSERT( status == expected_status ); |
| 8105 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8106 | |
| 8107 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8108 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8109 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8110 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8111 | } |
| 8112 | /* END_CASE */ |
| 8113 | |
| 8114 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8115 | void raw_key_agreement( int alg_arg, |
| 8116 | int our_key_type_arg, data_t *our_key_data, |
| 8117 | data_t *peer_key_data, |
| 8118 | data_t *expected_output ) |
| 8119 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8120 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8121 | psa_algorithm_t alg = alg_arg; |
| 8122 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8123 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8124 | unsigned char *output = NULL; |
| 8125 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8126 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8127 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8128 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8129 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8130 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8131 | psa_set_key_algorithm( &attributes, alg ); |
| 8132 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8133 | PSA_ASSERT( psa_import_key( &attributes, |
| 8134 | our_key_data->x, our_key_data->len, |
| 8135 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8136 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8137 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 8138 | key_bits = psa_get_key_bits( &attributes ); |
| 8139 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8140 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8141 | TEST_LE_U( expected_output->len, |
| 8142 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 8143 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 8144 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8145 | |
| 8146 | /* Good case with exact output size */ |
| 8147 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 8148 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8149 | peer_key_data->x, peer_key_data->len, |
| 8150 | output, expected_output->len, |
| 8151 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8152 | ASSERT_COMPARE( output, output_length, |
| 8153 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8154 | mbedtls_free( output ); |
| 8155 | output = NULL; |
| 8156 | output_length = ~0; |
| 8157 | |
| 8158 | /* Larger buffer */ |
| 8159 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 8160 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8161 | peer_key_data->x, peer_key_data->len, |
| 8162 | output, expected_output->len + 1, |
| 8163 | &output_length ) ); |
| 8164 | ASSERT_COMPARE( output, output_length, |
| 8165 | expected_output->x, expected_output->len ); |
| 8166 | mbedtls_free( output ); |
| 8167 | output = NULL; |
| 8168 | output_length = ~0; |
| 8169 | |
| 8170 | /* Buffer too small */ |
| 8171 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 8172 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 8173 | peer_key_data->x, peer_key_data->len, |
| 8174 | output, expected_output->len - 1, |
| 8175 | &output_length ), |
| 8176 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8177 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8178 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8179 | mbedtls_free( output ); |
| 8180 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8181 | |
| 8182 | exit: |
| 8183 | mbedtls_free( output ); |
| 8184 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8185 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8186 | } |
| 8187 | /* END_CASE */ |
| 8188 | |
| 8189 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8190 | void key_agreement_capacity( int alg_arg, |
| 8191 | int our_key_type_arg, data_t *our_key_data, |
| 8192 | data_t *peer_key_data, |
| 8193 | int expected_capacity_arg ) |
| 8194 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8195 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8196 | psa_algorithm_t alg = alg_arg; |
| 8197 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8198 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8199 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8200 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8201 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8202 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8203 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8204 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8205 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8206 | psa_set_key_algorithm( &attributes, alg ); |
| 8207 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8208 | PSA_ASSERT( psa_import_key( &attributes, |
| 8209 | our_key_data->x, our_key_data->len, |
| 8210 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8211 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8212 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8213 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8214 | &operation, |
| 8215 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8216 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8217 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8218 | { |
| 8219 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8220 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8221 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8222 | NULL, 0 ) ); |
| 8223 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8224 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8225 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8226 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8227 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8228 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8229 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8230 | /* Test the actual capacity by reading the output. */ |
| 8231 | while( actual_capacity > sizeof( output ) ) |
| 8232 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8233 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8234 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8235 | actual_capacity -= sizeof( output ); |
| 8236 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8237 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8238 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8239 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 8240 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8241 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8242 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8243 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8244 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8245 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8246 | } |
| 8247 | /* END_CASE */ |
| 8248 | |
| 8249 | /* BEGIN_CASE */ |
| 8250 | void key_agreement_output( int alg_arg, |
| 8251 | int our_key_type_arg, data_t *our_key_data, |
| 8252 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8253 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8254 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8255 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8256 | psa_algorithm_t alg = alg_arg; |
| 8257 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8258 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8259 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8260 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8261 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8262 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 8263 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8264 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8265 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8266 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8267 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8268 | psa_set_key_algorithm( &attributes, alg ); |
| 8269 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8270 | PSA_ASSERT( psa_import_key( &attributes, |
| 8271 | our_key_data->x, our_key_data->len, |
| 8272 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8273 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8274 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8275 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8276 | &operation, |
| 8277 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8278 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8279 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8280 | { |
| 8281 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8282 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8283 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8284 | NULL, 0 ) ); |
| 8285 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8286 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8287 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8288 | actual_output, |
| 8289 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8290 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8291 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8292 | if( expected_output2->len != 0 ) |
| 8293 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8294 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8295 | actual_output, |
| 8296 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8297 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8298 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8299 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8300 | |
| 8301 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8302 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8303 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8304 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8305 | mbedtls_free( actual_output ); |
| 8306 | } |
| 8307 | /* END_CASE */ |
| 8308 | |
| 8309 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8310 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8311 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8312 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8313 | unsigned char *output = NULL; |
| 8314 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8315 | size_t i; |
| 8316 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8317 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8318 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8319 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8320 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8321 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8322 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8323 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8324 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8325 | /* Run several times, to ensure that every output byte will be |
| 8326 | * nonzero at least once with overwhelming probability |
| 8327 | * (2^(-8*number_of_runs)). */ |
| 8328 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8329 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8330 | if( bytes != 0 ) |
| 8331 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8332 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8333 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8334 | for( i = 0; i < bytes; i++ ) |
| 8335 | { |
| 8336 | if( output[i] != 0 ) |
| 8337 | ++changed[i]; |
| 8338 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8339 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8340 | |
| 8341 | /* Check that every byte was changed to nonzero at least once. This |
| 8342 | * validates that psa_generate_random is overwriting every byte of |
| 8343 | * the output buffer. */ |
| 8344 | for( i = 0; i < bytes; i++ ) |
| 8345 | { |
| 8346 | TEST_ASSERT( changed[i] != 0 ); |
| 8347 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8348 | |
| 8349 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8350 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8351 | mbedtls_free( output ); |
| 8352 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8353 | } |
| 8354 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8355 | |
| 8356 | /* BEGIN_CASE */ |
| 8357 | void generate_key( int type_arg, |
| 8358 | int bits_arg, |
| 8359 | int usage_arg, |
| 8360 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8361 | int expected_status_arg, |
| 8362 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8363 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8364 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8365 | psa_key_type_t type = type_arg; |
| 8366 | psa_key_usage_t usage = usage_arg; |
| 8367 | size_t bits = bits_arg; |
| 8368 | psa_algorithm_t alg = alg_arg; |
| 8369 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8370 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8371 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8372 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8373 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8374 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8375 | psa_set_key_usage_flags( &attributes, usage ); |
| 8376 | psa_set_key_algorithm( &attributes, alg ); |
| 8377 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8378 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8379 | |
| 8380 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8381 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8382 | |
| 8383 | if( is_large_key > 0 ) |
| 8384 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8385 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8386 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8387 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8388 | |
| 8389 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8390 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8391 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8392 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8393 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8394 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8395 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8396 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8397 | |
| 8398 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8399 | /* |
| 8400 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8401 | * thus reset them as required. |
| 8402 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8403 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8404 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8405 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8406 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8407 | } |
| 8408 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8409 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8410 | /* 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] | 8411 | void generate_key_rsa( int bits_arg, |
| 8412 | data_t *e_arg, |
| 8413 | int expected_status_arg ) |
| 8414 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8415 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8416 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8417 | size_t bits = bits_arg; |
| 8418 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8419 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8420 | psa_status_t expected_status = expected_status_arg; |
| 8421 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8422 | uint8_t *exported = NULL; |
| 8423 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8424 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8425 | size_t exported_length = SIZE_MAX; |
| 8426 | uint8_t *e_read_buffer = NULL; |
| 8427 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8428 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8429 | size_t e_read_length = SIZE_MAX; |
| 8430 | |
| 8431 | if( e_arg->len == 0 || |
| 8432 | ( e_arg->len == 3 && |
| 8433 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8434 | { |
| 8435 | is_default_public_exponent = 1; |
| 8436 | e_read_size = 0; |
| 8437 | } |
| 8438 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8439 | ASSERT_ALLOC( exported, exported_size ); |
| 8440 | |
| 8441 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8442 | |
| 8443 | psa_set_key_usage_flags( &attributes, usage ); |
| 8444 | psa_set_key_algorithm( &attributes, alg ); |
| 8445 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8446 | e_arg->x, e_arg->len ) ); |
| 8447 | psa_set_key_bits( &attributes, bits ); |
| 8448 | |
| 8449 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8450 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8451 | if( expected_status != PSA_SUCCESS ) |
| 8452 | goto exit; |
| 8453 | |
| 8454 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8455 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8456 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8457 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8458 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8459 | e_read_buffer, e_read_size, |
| 8460 | &e_read_length ) ); |
| 8461 | if( is_default_public_exponent ) |
| 8462 | TEST_EQUAL( e_read_length, 0 ); |
| 8463 | else |
| 8464 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8465 | |
| 8466 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8467 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8468 | goto exit; |
| 8469 | |
| 8470 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8471 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8472 | exported, exported_size, |
| 8473 | &exported_length ) ); |
| 8474 | { |
| 8475 | uint8_t *p = exported; |
| 8476 | uint8_t *end = exported + exported_length; |
| 8477 | size_t len; |
| 8478 | /* RSAPublicKey ::= SEQUENCE { |
| 8479 | * modulus INTEGER, -- n |
| 8480 | * publicExponent INTEGER } -- e |
| 8481 | */ |
| 8482 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8483 | MBEDTLS_ASN1_SEQUENCE | |
| 8484 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8485 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8486 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8487 | MBEDTLS_ASN1_INTEGER ) ); |
| 8488 | if( len >= 1 && p[0] == 0 ) |
| 8489 | { |
| 8490 | ++p; |
| 8491 | --len; |
| 8492 | } |
| 8493 | if( e_arg->len == 0 ) |
| 8494 | { |
| 8495 | TEST_EQUAL( len, 3 ); |
| 8496 | TEST_EQUAL( p[0], 1 ); |
| 8497 | TEST_EQUAL( p[1], 0 ); |
| 8498 | TEST_EQUAL( p[2], 1 ); |
| 8499 | } |
| 8500 | else |
| 8501 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8502 | } |
| 8503 | |
| 8504 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8505 | /* |
| 8506 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8507 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8508 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8509 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8510 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8511 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8512 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8513 | mbedtls_free( e_read_buffer ); |
| 8514 | mbedtls_free( exported ); |
| 8515 | } |
| 8516 | /* END_CASE */ |
| 8517 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8518 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8519 | void persistent_key_load_key_from_storage( data_t *data, |
| 8520 | int type_arg, int bits_arg, |
| 8521 | int usage_flags_arg, int alg_arg, |
| 8522 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8523 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8524 | 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] | 8525 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8526 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8527 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8528 | psa_key_type_t type = type_arg; |
| 8529 | size_t bits = bits_arg; |
| 8530 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8531 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8532 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8533 | unsigned char *first_export = NULL; |
| 8534 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8535 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8536 | size_t first_exported_length; |
| 8537 | size_t second_exported_length; |
| 8538 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8539 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8540 | { |
| 8541 | ASSERT_ALLOC( first_export, export_size ); |
| 8542 | ASSERT_ALLOC( second_export, export_size ); |
| 8543 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8544 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8545 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8546 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8547 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8548 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8549 | psa_set_key_algorithm( &attributes, alg ); |
| 8550 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8551 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8552 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8553 | switch( generation_method ) |
| 8554 | { |
| 8555 | case IMPORT_KEY: |
| 8556 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8557 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8558 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8559 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8560 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8561 | case GENERATE_KEY: |
| 8562 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8563 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8564 | break; |
| 8565 | |
| 8566 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8567 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8568 | { |
| 8569 | /* Create base key */ |
| 8570 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8571 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8572 | psa_set_key_usage_flags( &base_attributes, |
| 8573 | PSA_KEY_USAGE_DERIVE ); |
| 8574 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8575 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8576 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8577 | data->x, data->len, |
| 8578 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8579 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8580 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8581 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8582 | &operation, |
| 8583 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8584 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8585 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8586 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8587 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8588 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8589 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8590 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8591 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8592 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8593 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8594 | #else |
| 8595 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8596 | #endif |
| 8597 | break; |
| 8598 | |
| 8599 | default: |
| 8600 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8601 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8602 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8603 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8604 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8605 | /* Export the key if permitted by the key policy. */ |
| 8606 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8607 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8608 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8609 | first_export, export_size, |
| 8610 | &first_exported_length ) ); |
| 8611 | if( generation_method == IMPORT_KEY ) |
| 8612 | ASSERT_COMPARE( data->x, data->len, |
| 8613 | first_export, first_exported_length ); |
| 8614 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8615 | |
| 8616 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8617 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8618 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8619 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8620 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8621 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8622 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8623 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8624 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8625 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8626 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8627 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8628 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8629 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8630 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8631 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8632 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8633 | /* Export the key again if permitted by the key policy. */ |
| 8634 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8635 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8636 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8637 | second_export, export_size, |
| 8638 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8639 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8640 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8641 | } |
| 8642 | |
| 8643 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8644 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8645 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8646 | |
| 8647 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8648 | /* |
| 8649 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8650 | * thus reset them as required. |
| 8651 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8652 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8653 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8654 | mbedtls_free( first_export ); |
| 8655 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8656 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8657 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8658 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8659 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8660 | } |
| 8661 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8662 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8663 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8664 | 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] | 8665 | int input_first, data_t *pw_data, |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8666 | int expected_status_arg ) |
| 8667 | { |
| 8668 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8669 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 8670 | psa_algorithm_t alg = alg_arg; |
| 8671 | psa_algorithm_t hash_alg = hash_arg; |
| 8672 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8673 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8674 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8675 | psa_status_t expected_status = expected_status_arg; |
| 8676 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8677 | unsigned char *output_buffer = NULL; |
| 8678 | size_t output_len = 0; |
| 8679 | |
| 8680 | PSA_INIT( ); |
| 8681 | |
| 8682 | ASSERT_ALLOC( output_buffer, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8683 | PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8684 | PSA_PAKE_STEP_KEY_SHARE) ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8685 | |
| 8686 | if( pw_data->len > 0 ) |
| 8687 | { |
| 8688 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8689 | psa_set_key_algorithm( &attributes, alg ); |
| 8690 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8691 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8692 | &key ) ); |
| 8693 | } |
| 8694 | |
| 8695 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8696 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8697 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8698 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8699 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8700 | |
| 8701 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8702 | PSA_ERROR_BAD_STATE ); |
| 8703 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8704 | PSA_ERROR_BAD_STATE ); |
| 8705 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8706 | PSA_ERROR_BAD_STATE ); |
| 8707 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8708 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8709 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8710 | NULL, 0, NULL ), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8711 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8712 | 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] | 8713 | PSA_ERROR_BAD_STATE ); |
| 8714 | |
| 8715 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8716 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8717 | status = psa_pake_setup( &operation, &cipher_suite ); |
| 8718 | if( status != PSA_SUCCESS ) |
| 8719 | { |
| 8720 | TEST_EQUAL( status, expected_status ); |
| 8721 | goto exit; |
| 8722 | } |
| 8723 | else |
| 8724 | PSA_ASSERT( status ); |
| 8725 | |
Neil Armstrong | 50de0ae | 2022-06-08 17:46:24 +0200 | [diff] [blame] | 8726 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8727 | PSA_ERROR_BAD_STATE ); |
| 8728 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8729 | status = psa_pake_set_role( &operation, role ); |
| 8730 | if( status != PSA_SUCCESS ) |
| 8731 | { |
| 8732 | TEST_EQUAL( status, expected_status ); |
| 8733 | goto exit; |
| 8734 | } |
| 8735 | else |
| 8736 | PSA_ASSERT( status ); |
| 8737 | |
| 8738 | if( pw_data->len > 0 ) |
| 8739 | { |
| 8740 | status = psa_pake_set_password_key( &operation, key ); |
| 8741 | if( status != PSA_SUCCESS ) |
| 8742 | { |
| 8743 | TEST_EQUAL( status, expected_status ); |
| 8744 | goto exit; |
| 8745 | } |
| 8746 | else |
| 8747 | PSA_ASSERT( status ); |
| 8748 | } |
| 8749 | |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8750 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8751 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8752 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8753 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8754 | |
| 8755 | const uint8_t unsupported_id[] = "abcd"; |
| 8756 | |
| 8757 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8758 | PSA_ERROR_NOT_SUPPORTED ); |
| 8759 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8760 | PSA_ERROR_NOT_SUPPORTED ); |
| 8761 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8762 | /* First round */ |
| 8763 | if( input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8764 | { |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8765 | /* Invalid parameters */ |
| 8766 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8767 | NULL, 0 ), |
| 8768 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8769 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8770 | output_buffer, 66 ), |
| 8771 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8772 | /* Invalid first step */ |
| 8773 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8774 | output_buffer, 66 ), |
| 8775 | PSA_ERROR_BAD_STATE ); |
| 8776 | |
| 8777 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8778 | output_buffer, 66 ), |
| 8779 | expected_status); |
| 8780 | |
| 8781 | if( expected_status == PSA_SUCCESS ) |
| 8782 | { |
| 8783 | /* Buffer too large */ |
| 8784 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8785 | output_buffer, 512 ), |
| 8786 | PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8787 | |
| 8788 | /* The operation should be aborted at this point */ |
| 8789 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8790 | output_buffer, 66 ), |
| 8791 | PSA_ERROR_BAD_STATE ); |
| 8792 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8793 | } |
| 8794 | else |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8795 | { |
| 8796 | /* Invalid parameters */ |
| 8797 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8798 | NULL, 0, NULL ), |
| 8799 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8800 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8801 | output_buffer, 512, &output_len ), |
| 8802 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8803 | /* Invalid first step */ |
| 8804 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8805 | output_buffer, 512, &output_len ), |
| 8806 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8807 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8808 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8809 | output_buffer, 512, &output_len ), |
| 8810 | expected_status ); |
Neil Armstrong | 98506ab | 2022-06-08 17:43:20 +0200 | [diff] [blame] | 8811 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8812 | if( expected_status == PSA_SUCCESS ) |
| 8813 | { |
| 8814 | TEST_ASSERT( output_len > 0 ); |
| 8815 | |
| 8816 | /* Buffer too small */ |
| 8817 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8818 | output_buffer, 5, &output_len ), |
| 8819 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8820 | |
| 8821 | /* The operation should be aborted at this point */ |
| 8822 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8823 | output_buffer, 512, &output_len ), |
| 8824 | PSA_ERROR_BAD_STATE ); |
| 8825 | } |
| 8826 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8827 | |
| 8828 | exit: |
| 8829 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 8830 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8831 | mbedtls_free( output_buffer ); |
| 8832 | PSA_DONE( ); |
| 8833 | } |
| 8834 | /* END_CASE */ |
| 8835 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8836 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8837 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 8838 | int client_input_first, int inject_error, |
| 8839 | data_t *pw_data ) |
| 8840 | { |
| 8841 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8842 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8843 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8844 | psa_algorithm_t alg = alg_arg; |
| 8845 | psa_algorithm_t hash_alg = hash_arg; |
| 8846 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8847 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8848 | |
| 8849 | PSA_INIT( ); |
| 8850 | |
| 8851 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8852 | psa_set_key_algorithm( &attributes, alg ); |
| 8853 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8854 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8855 | &key ) ); |
| 8856 | |
| 8857 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8858 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8859 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8860 | |
| 8861 | |
| 8862 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8863 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8864 | |
| 8865 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8866 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8867 | |
| 8868 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8869 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8870 | |
| 8871 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8872 | client_input_first, 1, |
| 8873 | inject_error ), 1 ); |
| 8874 | |
| 8875 | if( inject_error == 1 || inject_error == 2 ) |
| 8876 | goto exit; |
| 8877 | |
| 8878 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8879 | client_input_first, 2, |
| 8880 | inject_error ), 1 ); |
| 8881 | |
| 8882 | exit: |
| 8883 | psa_destroy_key( key ); |
| 8884 | psa_pake_abort( &server ); |
| 8885 | psa_pake_abort( &client ); |
| 8886 | PSA_DONE( ); |
| 8887 | } |
| 8888 | /* END_CASE */ |
| 8889 | |
| 8890 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8891 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8892 | int derive_alg_arg, data_t *pw_data, |
| 8893 | int client_input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8894 | { |
| 8895 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8896 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8897 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8898 | psa_algorithm_t alg = alg_arg; |
| 8899 | psa_algorithm_t hash_alg = hash_arg; |
| 8900 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 8901 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8902 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8903 | psa_key_derivation_operation_t server_derive = |
| 8904 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8905 | psa_key_derivation_operation_t client_derive = |
| 8906 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8907 | |
| 8908 | PSA_INIT( ); |
| 8909 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8910 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8911 | psa_set_key_algorithm( &attributes, alg ); |
| 8912 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8913 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8914 | &key ) ); |
| 8915 | |
| 8916 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8917 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8918 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8919 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8920 | /* Get shared key */ |
| 8921 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 8922 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 8923 | |
| 8924 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 8925 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 8926 | { |
| 8927 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 8928 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8929 | (const uint8_t*) "", 0) ); |
| 8930 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 8931 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8932 | (const uint8_t*) "", 0) ); |
| 8933 | } |
| 8934 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8935 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8936 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8937 | |
| 8938 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8939 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8940 | |
| 8941 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8942 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8943 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8944 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8945 | PSA_ERROR_BAD_STATE ); |
| 8946 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8947 | PSA_ERROR_BAD_STATE ); |
| 8948 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8949 | /* First round */ |
| 8950 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8951 | client_input_first, 1, 0 ), 1 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8952 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8953 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8954 | PSA_ERROR_BAD_STATE ); |
| 8955 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8956 | PSA_ERROR_BAD_STATE ); |
| 8957 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8958 | /* Second round */ |
| 8959 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8960 | client_input_first, 2, 0 ), 1 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8961 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8962 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 8963 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 8964 | |
| 8965 | exit: |
| 8966 | psa_key_derivation_abort( &server_derive ); |
| 8967 | psa_key_derivation_abort( &client_derive ); |
| 8968 | psa_destroy_key( key ); |
| 8969 | psa_pake_abort( &server ); |
| 8970 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8971 | PSA_DONE( ); |
| 8972 | } |
| 8973 | /* END_CASE */ |