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 | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 708 | static int ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive, |
| 709 | psa_pake_operation_t *server, |
| 710 | psa_pake_operation_t *client, |
| 711 | int client_input_first, |
| 712 | int round, int inject_error ) |
| 713 | { |
| 714 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 715 | size_t buffer_length = ( |
| 716 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 717 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 718 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2; |
| 719 | size_t buffer0_off = 0; |
| 720 | size_t buffer1_off = 0; |
| 721 | size_t s_g1_len, s_g2_len, s_a_len; |
| 722 | size_t s_g1_off, s_g2_off, s_a_off; |
| 723 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 724 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 725 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 726 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 727 | size_t c_g1_len, c_g2_len, c_a_len; |
| 728 | size_t c_g1_off, c_g2_off, c_a_off; |
| 729 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 730 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 731 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 732 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 733 | psa_status_t expected_status = PSA_SUCCESS; |
| 734 | int ret; |
| 735 | |
| 736 | ASSERT_ALLOC( buffer0, buffer_length ); |
| 737 | ASSERT_ALLOC( buffer1, buffer_length ); |
| 738 | |
| 739 | switch( round ) |
| 740 | { |
| 741 | case 1: |
| 742 | /* Server first round Output */ |
| 743 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 744 | buffer0 + buffer0_off, |
| 745 | 512 - buffer0_off, &s_g1_len ) ); |
| 746 | s_g1_off = buffer0_off; |
| 747 | buffer0_off += s_g1_len; |
| 748 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 749 | buffer0 + buffer0_off, |
| 750 | 512 - buffer0_off, &s_x1_pk_len ) ); |
| 751 | s_x1_pk_off = buffer0_off; |
| 752 | buffer0_off += s_x1_pk_len; |
| 753 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 754 | buffer0 + buffer0_off, |
| 755 | 512 - buffer0_off, &s_x1_pr_len ) ); |
| 756 | s_x1_pr_off = buffer0_off; |
| 757 | buffer0_off += s_x1_pr_len; |
| 758 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 759 | buffer0 + buffer0_off, |
| 760 | 512 - buffer0_off, &s_g2_len ) ); |
| 761 | s_g2_off = buffer0_off; |
| 762 | buffer0_off += s_g2_len; |
| 763 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 764 | buffer0 + buffer0_off, |
| 765 | 512 - buffer0_off, &s_x2_pk_len ) ); |
| 766 | s_x2_pk_off = buffer0_off; |
| 767 | buffer0_off += s_x2_pk_len; |
| 768 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 769 | buffer0 + buffer0_off, |
| 770 | 512 - buffer0_off, &s_x2_pr_len ) ); |
| 771 | s_x2_pr_off = buffer0_off; |
| 772 | buffer0_off += s_x2_pr_len; |
| 773 | |
| 774 | if( inject_error == 1 ) |
| 775 | { |
| 776 | buffer0[s_x1_pk_off + 12] >>= 4; |
| 777 | buffer0[s_x2_pk_off + 7] <<= 4; |
| 778 | expected_status = PSA_ERROR_DATA_INVALID; |
| 779 | } |
| 780 | |
| 781 | if( client_input_first == 1 ) |
| 782 | { |
| 783 | /* Client first round Input */ |
| 784 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 785 | buffer0 + s_g1_off, s_g1_len ) ); |
| 786 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 787 | buffer0 + s_x1_pk_off, |
| 788 | s_x1_pk_len ) ); |
| 789 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 790 | buffer0 + s_x1_pr_off, |
| 791 | s_x1_pr_len ) ); |
| 792 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 793 | buffer0 + s_g2_off, |
| 794 | s_g2_len ) ); |
| 795 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 796 | buffer0 + s_x2_pk_off, |
| 797 | s_x2_pk_len ) ); |
| 798 | TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 799 | buffer0 + s_x2_pr_off, |
| 800 | s_x2_pr_len ), |
| 801 | expected_status ); |
| 802 | |
| 803 | if( inject_error == 1 ) |
| 804 | { |
| 805 | ret = 1; |
| 806 | goto exit; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | /* Client first round Output */ |
| 811 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 812 | buffer1 + buffer1_off, |
| 813 | 512 - buffer1_off, &c_g1_len ) ); |
| 814 | c_g1_off = buffer1_off; |
| 815 | buffer1_off += c_g1_len; |
| 816 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 817 | buffer1 + buffer1_off, |
| 818 | 512 - buffer1_off, &c_x1_pk_len ) ); |
| 819 | c_x1_pk_off = buffer1_off; |
| 820 | buffer1_off += c_x1_pk_len; |
| 821 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 822 | buffer1 + buffer1_off, |
| 823 | 512 - buffer1_off, &c_x1_pr_len ) ); |
| 824 | c_x1_pr_off = buffer1_off; |
| 825 | buffer1_off += c_x1_pr_len; |
| 826 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 827 | buffer1 + buffer1_off, |
| 828 | 512 - buffer1_off, &c_g2_len ) ); |
| 829 | c_g2_off = buffer1_off; |
| 830 | buffer1_off += c_g2_len; |
| 831 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 832 | buffer1 + buffer1_off, |
| 833 | 512 - buffer1_off, &c_x2_pk_len ) ); |
| 834 | c_x2_pk_off = buffer1_off; |
| 835 | buffer1_off += c_x2_pk_len; |
| 836 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 837 | buffer1 + buffer1_off, |
| 838 | 512 - buffer1_off, &c_x2_pr_len ) ); |
| 839 | c_x2_pr_off = buffer1_off; |
| 840 | buffer1_off += c_x2_pr_len; |
| 841 | |
| 842 | if( client_input_first == 0 ) |
| 843 | { |
| 844 | /* Client first round Input */ |
| 845 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 846 | buffer0 + s_g1_off, s_g1_len ) ); |
| 847 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 848 | buffer0 + s_x1_pk_off, |
| 849 | s_x1_pk_len ) ); |
| 850 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 851 | buffer0 + s_x1_pr_off, |
| 852 | s_x1_pr_len ) ); |
| 853 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 854 | buffer0 + s_g2_off, |
| 855 | s_g2_len ) ); |
| 856 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 857 | buffer0 + s_x2_pk_off, |
| 858 | s_x2_pk_len ) ); |
| 859 | TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 860 | buffer0 + s_x2_pr_off, |
| 861 | s_x2_pr_len ), |
| 862 | expected_status ); |
| 863 | |
| 864 | if( inject_error == 1 ) |
| 865 | break; |
| 866 | } |
| 867 | |
| 868 | if( inject_error == 2 ) |
| 869 | { |
| 870 | buffer1[c_x1_pk_off + 12] >>= 4; |
| 871 | buffer1[c_x2_pk_off + 7] <<= 4; |
| 872 | expected_status = PSA_ERROR_DATA_INVALID; |
| 873 | } |
| 874 | |
| 875 | /* Server first round Input */ |
| 876 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 877 | buffer1 + c_g1_off, c_g1_len ) ); |
| 878 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 879 | buffer1 + c_x1_pk_off, c_x1_pk_len ) ); |
| 880 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 881 | buffer1 + c_x1_pr_off, c_x1_pr_len ) ); |
| 882 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 883 | buffer1 + c_g2_off, c_g2_len ) ); |
| 884 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 885 | buffer1 + c_x2_pk_off, c_x2_pk_len ) ); |
| 886 | TEST_EQUAL( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 887 | buffer1 + c_x2_pr_off, c_x2_pr_len ), |
| 888 | expected_status ); |
| 889 | |
| 890 | break; |
| 891 | |
| 892 | case 2: |
| 893 | /* Server second round Output */ |
| 894 | buffer0_off = 0; |
| 895 | |
| 896 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 897 | buffer0 + buffer0_off, |
| 898 | 512 - buffer0_off, &s_a_len ) ); |
| 899 | s_a_off = buffer0_off; |
| 900 | buffer0_off += s_a_len; |
| 901 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 902 | buffer0 + buffer0_off, |
| 903 | 512 - buffer0_off, &s_x2s_pk_len ) ); |
| 904 | s_x2s_pk_off = buffer0_off; |
| 905 | buffer0_off += s_x2s_pk_len; |
| 906 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 907 | buffer0 + buffer0_off, |
| 908 | 512 - buffer0_off, &s_x2s_pr_len ) ); |
| 909 | s_x2s_pr_off = buffer0_off; |
| 910 | buffer0_off += s_x2s_pr_len; |
| 911 | |
| 912 | if( inject_error == 3 ) |
| 913 | { |
| 914 | buffer0[s_x2s_pk_off + 12] >>= 4; |
| 915 | expected_status = PSA_ERROR_DATA_INVALID; |
| 916 | } |
| 917 | |
| 918 | if( client_input_first == 1 ) |
| 919 | { |
| 920 | /* Client second round Input */ |
| 921 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 922 | buffer0 + s_a_off, s_a_len ) ); |
| 923 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 924 | buffer0 + s_x2s_pk_off, |
| 925 | s_x2s_pk_len ) ); |
| 926 | TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 927 | buffer0 + s_x2s_pr_off, |
| 928 | s_x2s_pr_len ), |
| 929 | expected_status ); |
| 930 | |
| 931 | if( inject_error == 3 ) |
| 932 | break; |
| 933 | } |
| 934 | |
| 935 | /* Client second round Output */ |
| 936 | buffer1_off = 0; |
| 937 | |
| 938 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 939 | buffer1 + buffer1_off, |
| 940 | 512 - buffer1_off, &c_a_len ) ); |
| 941 | c_a_off = buffer1_off; |
| 942 | buffer1_off += c_a_len; |
| 943 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 944 | buffer1 + buffer1_off, |
| 945 | 512 - buffer1_off, &c_x2s_pk_len ) ); |
| 946 | c_x2s_pk_off = buffer1_off; |
| 947 | buffer1_off += c_x2s_pk_len; |
| 948 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 949 | buffer1 + buffer1_off, |
| 950 | 512 - buffer1_off, &c_x2s_pr_len ) ); |
| 951 | c_x2s_pr_off = buffer1_off; |
| 952 | buffer1_off += c_x2s_pr_len; |
| 953 | |
| 954 | if( client_input_first == 0 ) |
| 955 | { |
| 956 | /* Client second round Input */ |
| 957 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 958 | buffer0 + s_a_off, s_a_len ) ); |
| 959 | PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 960 | buffer0 + s_x2s_pk_off, |
| 961 | s_x2s_pk_len ) ); |
| 962 | TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 963 | buffer0 + s_x2s_pr_off, |
| 964 | s_x2s_pr_len ), |
| 965 | expected_status ); |
| 966 | |
| 967 | if( inject_error == 3 ) |
| 968 | break; |
| 969 | } |
| 970 | |
| 971 | if( inject_error == 4 ) |
| 972 | { |
| 973 | buffer1[c_x2s_pk_off + 12] >>= 4; |
| 974 | expected_status = PSA_ERROR_DATA_INVALID; |
| 975 | } |
| 976 | |
| 977 | /* Server second round Input */ |
| 978 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 979 | buffer1 + c_a_off, c_a_len ) ); |
| 980 | PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 981 | buffer1 + c_x2s_pk_off, c_x2s_pk_len ) ); |
| 982 | TEST_EQUAL( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 983 | buffer1 + c_x2s_pr_off, c_x2s_pr_len ), |
| 984 | expected_status ); |
| 985 | |
| 986 | break; |
| 987 | |
| 988 | } |
| 989 | |
| 990 | ret = 1; |
| 991 | |
| 992 | exit: |
| 993 | mbedtls_free( buffer0 ); |
| 994 | mbedtls_free( buffer1 ); |
| 995 | return( ret ); |
| 996 | } |
| 997 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 998 | /* END_HEADER */ |
| 999 | |
| 1000 | /* BEGIN_DEPENDENCIES |
| 1001 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1002 | * END_DEPENDENCIES |
| 1003 | */ |
| 1004 | |
| 1005 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1006 | void static_checks( ) |
| 1007 | { |
| 1008 | size_t max_truncated_mac_size = |
| 1009 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1010 | |
| 1011 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1012 | * encoding. The shifted mask is the maximum truncated value. The |
| 1013 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1014 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1015 | } |
| 1016 | /* END_CASE */ |
| 1017 | |
| 1018 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1019 | void import_with_policy( int type_arg, |
| 1020 | int usage_arg, int alg_arg, |
| 1021 | int expected_status_arg ) |
| 1022 | { |
| 1023 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1024 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1025 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1026 | psa_key_type_t type = type_arg; |
| 1027 | psa_key_usage_t usage = usage_arg; |
| 1028 | psa_algorithm_t alg = alg_arg; |
| 1029 | psa_status_t expected_status = expected_status_arg; |
| 1030 | const uint8_t key_material[16] = {0}; |
| 1031 | psa_status_t status; |
| 1032 | |
| 1033 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1034 | |
| 1035 | psa_set_key_type( &attributes, type ); |
| 1036 | psa_set_key_usage_flags( &attributes, usage ); |
| 1037 | psa_set_key_algorithm( &attributes, alg ); |
| 1038 | |
| 1039 | status = psa_import_key( &attributes, |
| 1040 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1041 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1042 | TEST_EQUAL( status, expected_status ); |
| 1043 | if( status != PSA_SUCCESS ) |
| 1044 | goto exit; |
| 1045 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1046 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1047 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1048 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1049 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1050 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1051 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1052 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1053 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1054 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1055 | |
| 1056 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1057 | /* |
| 1058 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1059 | * thus reset them as required. |
| 1060 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1061 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1062 | |
| 1063 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1064 | PSA_DONE( ); |
| 1065 | } |
| 1066 | /* END_CASE */ |
| 1067 | |
| 1068 | /* BEGIN_CASE */ |
| 1069 | void import_with_data( data_t *data, int type_arg, |
| 1070 | int attr_bits_arg, |
| 1071 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1072 | { |
| 1073 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1074 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1075 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1076 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1077 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1078 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1079 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1080 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1081 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1082 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1083 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1084 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1085 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1086 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1087 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1088 | if( status != PSA_SUCCESS ) |
| 1089 | goto exit; |
| 1090 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1091 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1092 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1093 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1094 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1095 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1096 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1097 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1098 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1099 | |
| 1100 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1101 | /* |
| 1102 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1103 | * thus reset them as required. |
| 1104 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1105 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1106 | |
| 1107 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1108 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1109 | } |
| 1110 | /* END_CASE */ |
| 1111 | |
| 1112 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1113 | void import_large_key( int type_arg, int byte_size_arg, |
| 1114 | int expected_status_arg ) |
| 1115 | { |
| 1116 | psa_key_type_t type = type_arg; |
| 1117 | size_t byte_size = byte_size_arg; |
| 1118 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1119 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1120 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1121 | psa_status_t status; |
| 1122 | uint8_t *buffer = NULL; |
| 1123 | size_t buffer_size = byte_size + 1; |
| 1124 | size_t n; |
| 1125 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1126 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1127 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1128 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1129 | memset( buffer, 'K', byte_size ); |
| 1130 | |
| 1131 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1132 | |
| 1133 | /* Try importing the key */ |
| 1134 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1135 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1136 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1137 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1138 | TEST_EQUAL( status, expected_status ); |
| 1139 | |
| 1140 | if( status == PSA_SUCCESS ) |
| 1141 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1142 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1143 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1144 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1145 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1146 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1147 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1148 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1149 | for( n = 0; n < byte_size; n++ ) |
| 1150 | TEST_EQUAL( buffer[n], 'K' ); |
| 1151 | for( n = byte_size; n < buffer_size; n++ ) |
| 1152 | TEST_EQUAL( buffer[n], 0 ); |
| 1153 | } |
| 1154 | |
| 1155 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1156 | /* |
| 1157 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1158 | * thus reset them as required. |
| 1159 | */ |
| 1160 | psa_reset_key_attributes( &attributes ); |
| 1161 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1162 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1163 | PSA_DONE( ); |
| 1164 | mbedtls_free( buffer ); |
| 1165 | } |
| 1166 | /* END_CASE */ |
| 1167 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1168 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1169 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1170 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1171 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1172 | size_t bits = bits_arg; |
| 1173 | psa_status_t expected_status = expected_status_arg; |
| 1174 | psa_status_t status; |
| 1175 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1176 | 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] | 1177 | size_t buffer_size = /* Slight overapproximations */ |
| 1178 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1179 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1180 | unsigned char *p; |
| 1181 | int ret; |
| 1182 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1183 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1184 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1185 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1186 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1187 | |
| 1188 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1189 | bits, keypair ) ) >= 0 ); |
| 1190 | length = ret; |
| 1191 | |
| 1192 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1193 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1194 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1195 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1196 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1197 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1198 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1199 | |
| 1200 | exit: |
| 1201 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1202 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1203 | } |
| 1204 | /* END_CASE */ |
| 1205 | |
| 1206 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1207 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1208 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1209 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1210 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1211 | int expected_bits, |
| 1212 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1213 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1214 | int canonical_input ) |
| 1215 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1216 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1217 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1218 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1219 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1220 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1221 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1222 | unsigned char *exported = NULL; |
| 1223 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1224 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1225 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1226 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1227 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1228 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1229 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1230 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1231 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1232 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1233 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1234 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1235 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1236 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1237 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1238 | psa_set_key_algorithm( &attributes, alg ); |
| 1239 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1240 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1241 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1242 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1243 | |
| 1244 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1245 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1246 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1247 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1248 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1249 | |
| 1250 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1251 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1252 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1253 | |
| 1254 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1255 | * and export_size. On errors, the exported length must be 0. */ |
| 1256 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1257 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1258 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1259 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1260 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1261 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1262 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1263 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1264 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1265 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1266 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1267 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1268 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1269 | * this validates the canonical representations. For canonical inputs, |
| 1270 | * this doesn't directly validate the implementation, but it still helps |
| 1271 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1272 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1273 | { |
| 1274 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1275 | goto exit; |
| 1276 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1277 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1278 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1279 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1280 | else |
| 1281 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1282 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1283 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1284 | &key2 ) ); |
| 1285 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1286 | reexported, |
| 1287 | export_size, |
| 1288 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1289 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1290 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1291 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1292 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1293 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1294 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1295 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1296 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1297 | |
| 1298 | destroy: |
| 1299 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1300 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1301 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1302 | |
| 1303 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1304 | /* |
| 1305 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1306 | * thus reset them as required. |
| 1307 | */ |
| 1308 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1309 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1310 | mbedtls_free( exported ); |
| 1311 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1312 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1313 | } |
| 1314 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1315 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1316 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1317 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1318 | int type_arg, |
| 1319 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1320 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1321 | int export_size_delta, |
| 1322 | int expected_export_status_arg, |
| 1323 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1324 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1325 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1326 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1327 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1328 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1329 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1330 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1331 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1332 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1333 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1334 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1335 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1336 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1337 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1338 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1339 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1340 | psa_set_key_algorithm( &attributes, alg ); |
| 1341 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1342 | |
| 1343 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1344 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1345 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1346 | /* Export the public key */ |
| 1347 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1348 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1349 | exported, export_size, |
| 1350 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1351 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1352 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1353 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1354 | 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] | 1355 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1356 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1357 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1358 | TEST_LE_U( expected_public_key->len, |
| 1359 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1360 | TEST_LE_U( expected_public_key->len, |
| 1361 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1362 | TEST_LE_U( expected_public_key->len, |
| 1363 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1364 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1365 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1366 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1367 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1368 | /* |
| 1369 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1370 | * thus reset them as required. |
| 1371 | */ |
| 1372 | psa_reset_key_attributes( &attributes ); |
| 1373 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1374 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1375 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1376 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1377 | } |
| 1378 | /* END_CASE */ |
| 1379 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1380 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1381 | void import_and_exercise_key( data_t *data, |
| 1382 | int type_arg, |
| 1383 | int bits_arg, |
| 1384 | int alg_arg ) |
| 1385 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1386 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1387 | psa_key_type_t type = type_arg; |
| 1388 | size_t bits = bits_arg; |
| 1389 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1390 | 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] | 1391 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1392 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1393 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1394 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1395 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1396 | psa_set_key_usage_flags( &attributes, usage ); |
| 1397 | psa_set_key_algorithm( &attributes, alg ); |
| 1398 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1399 | |
| 1400 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1401 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1402 | |
| 1403 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1404 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1405 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1406 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1407 | |
| 1408 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1409 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1410 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1411 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1412 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1413 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1414 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1415 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1416 | /* |
| 1417 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1418 | * thus reset them as required. |
| 1419 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1420 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1421 | |
| 1422 | psa_reset_key_attributes( &attributes ); |
| 1423 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1424 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1425 | } |
| 1426 | /* END_CASE */ |
| 1427 | |
| 1428 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1429 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1430 | int bits_arg, int expected_bits_arg, |
| 1431 | int usage_arg, int expected_usage_arg, |
| 1432 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1433 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1434 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1435 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1436 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1437 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1438 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1439 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1440 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1441 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1442 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1443 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1444 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1445 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1446 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1447 | psa_set_key_usage_flags( &attributes, usage ); |
| 1448 | psa_set_key_algorithm( &attributes, alg ); |
| 1449 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1450 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1451 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1452 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1453 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1454 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1455 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1456 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1457 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1458 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1459 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1460 | |
| 1461 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1462 | /* |
| 1463 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1464 | * thus reset them as required. |
| 1465 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1466 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1467 | |
| 1468 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1469 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1470 | } |
| 1471 | /* END_CASE */ |
| 1472 | |
| 1473 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1474 | void check_key_policy( int type_arg, int bits_arg, |
| 1475 | int usage_arg, int alg_arg ) |
| 1476 | { |
| 1477 | 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] | 1478 | usage_arg, |
| 1479 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1480 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1481 | goto exit; |
| 1482 | } |
| 1483 | /* END_CASE */ |
| 1484 | |
| 1485 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1486 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1487 | { |
| 1488 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1489 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1490 | * 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] | 1491 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1492 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1493 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1494 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1495 | |
| 1496 | memset( &zero, 0, sizeof( zero ) ); |
| 1497 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1498 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1499 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1500 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1501 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1502 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1503 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1504 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1505 | |
| 1506 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1507 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1508 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1509 | |
| 1510 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1511 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1512 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1513 | |
| 1514 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1515 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1516 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1517 | } |
| 1518 | /* END_CASE */ |
| 1519 | |
| 1520 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1521 | void mac_key_policy( int policy_usage_arg, |
| 1522 | int policy_alg_arg, |
| 1523 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1524 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1525 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1526 | int expected_status_sign_arg, |
| 1527 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1528 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1529 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1530 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1531 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1532 | psa_key_type_t key_type = key_type_arg; |
| 1533 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1534 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1535 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1536 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1537 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1538 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1539 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1540 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1541 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1542 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1543 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1544 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1545 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1546 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1547 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1548 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1549 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1550 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1551 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1552 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1553 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1554 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1555 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1556 | /* Calculate the MAC, one-shot case. */ |
| 1557 | uint8_t input[128] = {0}; |
| 1558 | size_t mac_len; |
| 1559 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1560 | input, 128, |
| 1561 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1562 | expected_status_sign ); |
| 1563 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1564 | /* Calculate the MAC, multi-part case. */ |
| 1565 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1566 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1567 | if( status == PSA_SUCCESS ) |
| 1568 | { |
| 1569 | status = psa_mac_update( &operation, input, 128 ); |
| 1570 | if( status == PSA_SUCCESS ) |
| 1571 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1572 | &mac_len ), |
| 1573 | expected_status_sign ); |
| 1574 | else |
| 1575 | TEST_EQUAL( status, expected_status_sign ); |
| 1576 | } |
| 1577 | else |
| 1578 | { |
| 1579 | TEST_EQUAL( status, expected_status_sign ); |
| 1580 | } |
| 1581 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1582 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1583 | /* Verify correct MAC, one-shot case. */ |
| 1584 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1585 | mac, mac_len ); |
| 1586 | |
| 1587 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1588 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1589 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1590 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1591 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1592 | /* Verify correct MAC, multi-part case. */ |
| 1593 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1594 | if( status == PSA_SUCCESS ) |
| 1595 | { |
| 1596 | status = psa_mac_update( &operation, input, 128 ); |
| 1597 | if( status == PSA_SUCCESS ) |
| 1598 | { |
| 1599 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1600 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1601 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1602 | else |
| 1603 | TEST_EQUAL( status, expected_status_verify ); |
| 1604 | } |
| 1605 | else |
| 1606 | { |
| 1607 | TEST_EQUAL( status, expected_status_verify ); |
| 1608 | } |
| 1609 | } |
| 1610 | else |
| 1611 | { |
| 1612 | TEST_EQUAL( status, expected_status_verify ); |
| 1613 | } |
| 1614 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1615 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1616 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1617 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1618 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1619 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1620 | |
| 1621 | exit: |
| 1622 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1623 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1624 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1625 | } |
| 1626 | /* END_CASE */ |
| 1627 | |
| 1628 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1629 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1630 | int policy_alg, |
| 1631 | int key_type, |
| 1632 | data_t *key_data, |
| 1633 | int exercise_alg ) |
| 1634 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1635 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1636 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1637 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1638 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1639 | size_t output_buffer_size = 0; |
| 1640 | size_t input_buffer_size = 0; |
| 1641 | size_t output_length = 0; |
| 1642 | uint8_t *output = NULL; |
| 1643 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1644 | psa_status_t status; |
| 1645 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1646 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1647 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1648 | input_buffer_size ); |
| 1649 | |
| 1650 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1651 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1652 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1653 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1654 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1655 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1656 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1657 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1658 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1659 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1660 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1661 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1662 | /* Check if no key usage flag implication is done */ |
| 1663 | TEST_EQUAL( policy_usage, |
| 1664 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1665 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1666 | /* Encrypt check, one-shot */ |
| 1667 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1668 | output, output_buffer_size, |
| 1669 | &output_length); |
| 1670 | if( policy_alg == exercise_alg && |
| 1671 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1672 | PSA_ASSERT( status ); |
| 1673 | else |
| 1674 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1675 | |
| 1676 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1677 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1678 | if( policy_alg == exercise_alg && |
| 1679 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1680 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1681 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1682 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1683 | psa_cipher_abort( &operation ); |
| 1684 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1685 | /* Decrypt check, one-shot */ |
| 1686 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 1687 | input, input_buffer_size, |
| 1688 | &output_length); |
| 1689 | if( policy_alg == exercise_alg && |
| 1690 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1691 | PSA_ASSERT( status ); |
| 1692 | else |
| 1693 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1694 | |
| 1695 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1696 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1697 | if( policy_alg == exercise_alg && |
| 1698 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1699 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1700 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1701 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1702 | |
| 1703 | exit: |
| 1704 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1705 | mbedtls_free( input ); |
| 1706 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1707 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1708 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1709 | } |
| 1710 | /* END_CASE */ |
| 1711 | |
| 1712 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1713 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1714 | int policy_alg, |
| 1715 | int key_type, |
| 1716 | data_t *key_data, |
| 1717 | int nonce_length_arg, |
| 1718 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1719 | int exercise_alg, |
| 1720 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1721 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1722 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1723 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1724 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1725 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1726 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1727 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1728 | unsigned char nonce[16] = {0}; |
| 1729 | size_t nonce_length = nonce_length_arg; |
| 1730 | unsigned char tag[16]; |
| 1731 | size_t tag_length = tag_length_arg; |
| 1732 | size_t output_length; |
| 1733 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1734 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 1735 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1736 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1737 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1738 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1739 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1740 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1741 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1742 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1743 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1744 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1745 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1746 | /* Check if no key usage implication is done */ |
| 1747 | TEST_EQUAL( policy_usage, |
| 1748 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1749 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1750 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1751 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1752 | nonce, nonce_length, |
| 1753 | NULL, 0, |
| 1754 | NULL, 0, |
| 1755 | tag, tag_length, |
| 1756 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1757 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1758 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1759 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1760 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1761 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1762 | /* Encrypt check, multi-part */ |
| 1763 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 1764 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1765 | TEST_EQUAL( status, expected_status ); |
| 1766 | else |
| 1767 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1768 | |
| 1769 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1770 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1771 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1772 | nonce, nonce_length, |
| 1773 | NULL, 0, |
| 1774 | tag, tag_length, |
| 1775 | NULL, 0, |
| 1776 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1777 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1778 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1779 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1780 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1781 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1782 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1783 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1784 | /* Decrypt check, multi-part */ |
| 1785 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 1786 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 1787 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1788 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1789 | else |
| 1790 | TEST_EQUAL( status, expected_status ); |
| 1791 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1792 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 1793 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1794 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1795 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1796 | } |
| 1797 | /* END_CASE */ |
| 1798 | |
| 1799 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1800 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1801 | int policy_alg, |
| 1802 | int key_type, |
| 1803 | data_t *key_data, |
| 1804 | int exercise_alg ) |
| 1805 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1806 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1807 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1808 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1809 | psa_status_t status; |
| 1810 | size_t key_bits; |
| 1811 | size_t buffer_length; |
| 1812 | unsigned char *buffer = NULL; |
| 1813 | size_t output_length; |
| 1814 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1815 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1816 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1817 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1818 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1819 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1820 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1821 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1822 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1823 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1824 | /* Check if no key usage implication is done */ |
| 1825 | TEST_EQUAL( policy_usage, |
| 1826 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1827 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1828 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1829 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1830 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1831 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1832 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1833 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1834 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1835 | NULL, 0, |
| 1836 | NULL, 0, |
| 1837 | buffer, buffer_length, |
| 1838 | &output_length ); |
| 1839 | if( policy_alg == exercise_alg && |
| 1840 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1841 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1842 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1843 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1844 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1845 | if( buffer_length != 0 ) |
| 1846 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1847 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1848 | buffer, buffer_length, |
| 1849 | NULL, 0, |
| 1850 | buffer, buffer_length, |
| 1851 | &output_length ); |
| 1852 | if( policy_alg == exercise_alg && |
| 1853 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1854 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1855 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1856 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1857 | |
| 1858 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1859 | /* |
| 1860 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1861 | * thus reset them as required. |
| 1862 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1863 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1864 | |
| 1865 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1866 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1867 | mbedtls_free( buffer ); |
| 1868 | } |
| 1869 | /* END_CASE */ |
| 1870 | |
| 1871 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1872 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1873 | int policy_alg, |
| 1874 | int key_type, |
| 1875 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1876 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1877 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1878 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1879 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1880 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1881 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1882 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 1883 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1884 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1885 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1886 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1887 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1888 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1889 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1890 | int compatible_alg = payload_length_arg > 0; |
| 1891 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1892 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1893 | size_t signature_length; |
| 1894 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1895 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1896 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1897 | TEST_EQUAL( expected_usage, |
| 1898 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1899 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1900 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1901 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1902 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1903 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1904 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1905 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1906 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1907 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1908 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1909 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1910 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1911 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1912 | payload, payload_length, |
| 1913 | signature, sizeof( signature ), |
| 1914 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1915 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1916 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1917 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1918 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1919 | |
| 1920 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1921 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1922 | payload, payload_length, |
| 1923 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1924 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1925 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1926 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1927 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1928 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 1929 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 1930 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1931 | { |
| 1932 | status = psa_sign_message( key, exercise_alg, |
| 1933 | payload, payload_length, |
| 1934 | signature, sizeof( signature ), |
| 1935 | &signature_length ); |
| 1936 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 1937 | PSA_ASSERT( status ); |
| 1938 | else |
| 1939 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1940 | |
| 1941 | memset( signature, 0, sizeof( signature ) ); |
| 1942 | status = psa_verify_message( key, exercise_alg, |
| 1943 | payload, payload_length, |
| 1944 | signature, sizeof( signature ) ); |
| 1945 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 1946 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1947 | else |
| 1948 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1949 | } |
| 1950 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1951 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1952 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1953 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1954 | } |
| 1955 | /* END_CASE */ |
| 1956 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1957 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1958 | void derive_key_policy( int policy_usage, |
| 1959 | int policy_alg, |
| 1960 | int key_type, |
| 1961 | data_t *key_data, |
| 1962 | int exercise_alg ) |
| 1963 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1964 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1965 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1966 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1967 | psa_status_t status; |
| 1968 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1969 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1970 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1971 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1972 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1973 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1974 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1975 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1976 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1977 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1978 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1979 | |
| 1980 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1981 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1982 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1983 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1984 | &operation, |
| 1985 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1986 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1987 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1988 | |
| 1989 | status = psa_key_derivation_input_key( &operation, |
| 1990 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1991 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1992 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1993 | if( policy_alg == exercise_alg && |
| 1994 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1995 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1996 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1997 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1998 | |
| 1999 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2000 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2001 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2002 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2003 | } |
| 2004 | /* END_CASE */ |
| 2005 | |
| 2006 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2007 | void agreement_key_policy( int policy_usage, |
| 2008 | int policy_alg, |
| 2009 | int key_type_arg, |
| 2010 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2011 | int exercise_alg, |
| 2012 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2013 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2014 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2015 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2016 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2017 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2018 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2019 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2020 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2021 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2022 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2023 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2024 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2025 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2026 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2027 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2028 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2029 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2030 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2031 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2032 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2033 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2034 | |
| 2035 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2036 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2037 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2038 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2039 | } |
| 2040 | /* END_CASE */ |
| 2041 | |
| 2042 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2043 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2044 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2045 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2046 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2047 | psa_key_type_t key_type = key_type_arg; |
| 2048 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2049 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2050 | psa_key_usage_t usage = usage_arg; |
| 2051 | psa_algorithm_t alg = alg_arg; |
| 2052 | psa_algorithm_t alg2 = alg2_arg; |
| 2053 | |
| 2054 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2055 | |
| 2056 | psa_set_key_usage_flags( &attributes, usage ); |
| 2057 | psa_set_key_algorithm( &attributes, alg ); |
| 2058 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2059 | psa_set_key_type( &attributes, key_type ); |
| 2060 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2061 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2062 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2063 | /* Update the usage flags to obtain implicit usage flags */ |
| 2064 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2065 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2066 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2067 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2068 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2069 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2070 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2071 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2072 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2073 | goto exit; |
| 2074 | |
| 2075 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2076 | /* |
| 2077 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2078 | * thus reset them as required. |
| 2079 | */ |
| 2080 | psa_reset_key_attributes( &got_attributes ); |
| 2081 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2082 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2083 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2084 | } |
| 2085 | /* END_CASE */ |
| 2086 | |
| 2087 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2088 | void raw_agreement_key_policy( int policy_usage, |
| 2089 | int policy_alg, |
| 2090 | int key_type_arg, |
| 2091 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2092 | int exercise_alg, |
| 2093 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2094 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2095 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2096 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2097 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2098 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2099 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2100 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2101 | |
| 2102 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2103 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2104 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2105 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2106 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2107 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2108 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2109 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2110 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2111 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2112 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2113 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2114 | |
| 2115 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2116 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2117 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2118 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2119 | } |
| 2120 | /* END_CASE */ |
| 2121 | |
| 2122 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2123 | void copy_success( int source_usage_arg, |
| 2124 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2125 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2126 | int type_arg, data_t *material, |
| 2127 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2128 | int target_usage_arg, |
| 2129 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2130 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2131 | int expected_usage_arg, |
| 2132 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2133 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2134 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2135 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2136 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2137 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2138 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2139 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2140 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2141 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2142 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2143 | uint8_t *export_buffer = NULL; |
| 2144 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2145 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2146 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2147 | /* Prepare the source key. */ |
| 2148 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2149 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2150 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2151 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2152 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2153 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2154 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2155 | &source_key ) ); |
| 2156 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2157 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2158 | /* Prepare the target attributes. */ |
| 2159 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2160 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2161 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2162 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2163 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2164 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2165 | if( target_usage_arg != -1 ) |
| 2166 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2167 | if( target_alg_arg != -1 ) |
| 2168 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2169 | if( target_alg2_arg != -1 ) |
| 2170 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2171 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2172 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2173 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2174 | PSA_ASSERT( psa_copy_key( source_key, |
| 2175 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2176 | |
| 2177 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2178 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2179 | |
| 2180 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2181 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2182 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2183 | psa_get_key_type( &target_attributes ) ); |
| 2184 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2185 | psa_get_key_bits( &target_attributes ) ); |
| 2186 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2187 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2188 | TEST_EQUAL( expected_alg2, |
| 2189 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2190 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2191 | { |
| 2192 | size_t length; |
| 2193 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2194 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2195 | material->len, &length ) ); |
| 2196 | ASSERT_COMPARE( material->x, material->len, |
| 2197 | export_buffer, length ); |
| 2198 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2199 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2200 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2201 | { |
| 2202 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2203 | goto exit; |
| 2204 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2205 | goto exit; |
| 2206 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2207 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2208 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2209 | |
| 2210 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2211 | /* |
| 2212 | * Source and target key attributes may have been returned by |
| 2213 | * psa_get_key_attributes() thus reset them as required. |
| 2214 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2215 | psa_reset_key_attributes( &source_attributes ); |
| 2216 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2217 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2218 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2219 | mbedtls_free( export_buffer ); |
| 2220 | } |
| 2221 | /* END_CASE */ |
| 2222 | |
| 2223 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2224 | void copy_fail( int source_usage_arg, |
| 2225 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2226 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2227 | int type_arg, data_t *material, |
| 2228 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2229 | int target_usage_arg, |
| 2230 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2231 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2232 | int expected_status_arg ) |
| 2233 | { |
| 2234 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2235 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2236 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2237 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2238 | 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] | 2239 | |
| 2240 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2241 | |
| 2242 | /* Prepare the source key. */ |
| 2243 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2244 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2245 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2246 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2247 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2248 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2249 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2250 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2251 | |
| 2252 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2253 | psa_set_key_id( &target_attributes, key_id ); |
| 2254 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2255 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2256 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2257 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2258 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2259 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2260 | |
| 2261 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2262 | TEST_EQUAL( psa_copy_key( source_key, |
| 2263 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2264 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2265 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2266 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2267 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2268 | exit: |
| 2269 | psa_reset_key_attributes( &source_attributes ); |
| 2270 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2271 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2272 | } |
| 2273 | /* END_CASE */ |
| 2274 | |
| 2275 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2276 | void hash_operation_init( ) |
| 2277 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2278 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2279 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2280 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2281 | * 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] | 2282 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2283 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2284 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2285 | psa_hash_operation_t zero; |
| 2286 | |
| 2287 | memset( &zero, 0, sizeof( zero ) ); |
| 2288 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2289 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2290 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2291 | PSA_ERROR_BAD_STATE ); |
| 2292 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2293 | PSA_ERROR_BAD_STATE ); |
| 2294 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2295 | PSA_ERROR_BAD_STATE ); |
| 2296 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2297 | /* A default hash operation should be abortable without error. */ |
| 2298 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2299 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2300 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2301 | } |
| 2302 | /* END_CASE */ |
| 2303 | |
| 2304 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2305 | void hash_setup( int alg_arg, |
| 2306 | int expected_status_arg ) |
| 2307 | { |
| 2308 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2309 | uint8_t *output = NULL; |
| 2310 | size_t output_size = 0; |
| 2311 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2312 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2313 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2314 | psa_status_t status; |
| 2315 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2316 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2317 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2318 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2319 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2320 | ASSERT_ALLOC( output, output_size ); |
| 2321 | |
| 2322 | status = psa_hash_compute( alg, NULL, 0, |
| 2323 | output, output_size, &output_length ); |
| 2324 | TEST_EQUAL( status, expected_status ); |
| 2325 | |
| 2326 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2327 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2328 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2329 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2330 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2331 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2332 | |
| 2333 | /* If setup failed, reproduce the failure, so as to |
| 2334 | * test the resulting state of the operation object. */ |
| 2335 | if( status != PSA_SUCCESS ) |
| 2336 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2337 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2338 | /* Now the operation object should be reusable. */ |
| 2339 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2340 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2341 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2342 | #endif |
| 2343 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2344 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2345 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2346 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2347 | } |
| 2348 | /* END_CASE */ |
| 2349 | |
| 2350 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2351 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2352 | int output_size_arg, int expected_status_arg ) |
| 2353 | { |
| 2354 | psa_algorithm_t alg = alg_arg; |
| 2355 | uint8_t *output = NULL; |
| 2356 | size_t output_size = output_size_arg; |
| 2357 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2358 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2359 | psa_status_t expected_status = expected_status_arg; |
| 2360 | psa_status_t status; |
| 2361 | |
| 2362 | ASSERT_ALLOC( output, output_size ); |
| 2363 | |
| 2364 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2365 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2366 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2367 | status = psa_hash_compute( alg, input->x, input->len, |
| 2368 | output, output_size, &output_length ); |
| 2369 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2370 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2371 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2372 | /* Hash Compute, multi-part */ |
| 2373 | status = psa_hash_setup( &operation, alg ); |
| 2374 | if( status == PSA_SUCCESS ) |
| 2375 | { |
| 2376 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2377 | if( status == PSA_SUCCESS ) |
| 2378 | { |
| 2379 | status = psa_hash_finish( &operation, output, output_size, |
| 2380 | &output_length ); |
| 2381 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2382 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2383 | else |
| 2384 | TEST_EQUAL( status, expected_status ); |
| 2385 | } |
| 2386 | else |
| 2387 | { |
| 2388 | TEST_EQUAL( status, expected_status ); |
| 2389 | } |
| 2390 | } |
| 2391 | else |
| 2392 | { |
| 2393 | TEST_EQUAL( status, expected_status ); |
| 2394 | } |
| 2395 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2396 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2397 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2398 | mbedtls_free( output ); |
| 2399 | PSA_DONE( ); |
| 2400 | } |
| 2401 | /* END_CASE */ |
| 2402 | |
| 2403 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2404 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2405 | data_t *reference_hash, |
| 2406 | int expected_status_arg ) |
| 2407 | { |
| 2408 | psa_algorithm_t alg = alg_arg; |
| 2409 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2410 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2411 | psa_status_t status; |
| 2412 | |
| 2413 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2414 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2415 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2416 | status = psa_hash_compare( alg, input->x, input->len, |
| 2417 | reference_hash->x, reference_hash->len ); |
| 2418 | TEST_EQUAL( status, expected_status ); |
| 2419 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2420 | /* Hash Compare, multi-part */ |
| 2421 | status = psa_hash_setup( &operation, alg ); |
| 2422 | if( status == PSA_SUCCESS ) |
| 2423 | { |
| 2424 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2425 | if( status == PSA_SUCCESS ) |
| 2426 | { |
| 2427 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2428 | reference_hash->len ); |
| 2429 | TEST_EQUAL( status, expected_status ); |
| 2430 | } |
| 2431 | else |
| 2432 | { |
| 2433 | TEST_EQUAL( status, expected_status ); |
| 2434 | } |
| 2435 | } |
| 2436 | else |
| 2437 | { |
| 2438 | TEST_EQUAL( status, expected_status ); |
| 2439 | } |
| 2440 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2441 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2442 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2443 | PSA_DONE( ); |
| 2444 | } |
| 2445 | /* END_CASE */ |
| 2446 | |
| 2447 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2448 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2449 | data_t *expected_output ) |
| 2450 | { |
| 2451 | psa_algorithm_t alg = alg_arg; |
| 2452 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2453 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2454 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2455 | size_t i; |
| 2456 | |
| 2457 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2458 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2459 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2460 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2461 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2462 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2463 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2464 | ASSERT_COMPARE( output, output_length, |
| 2465 | expected_output->x, expected_output->len ); |
| 2466 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2467 | /* Compute with tight buffer, multi-part */ |
| 2468 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2469 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2470 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2471 | PSA_HASH_LENGTH( alg ), |
| 2472 | &output_length ) ); |
| 2473 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2474 | ASSERT_COMPARE( output, output_length, |
| 2475 | expected_output->x, expected_output->len ); |
| 2476 | |
| 2477 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2478 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2479 | output, sizeof( output ), |
| 2480 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2481 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2482 | ASSERT_COMPARE( output, output_length, |
| 2483 | expected_output->x, expected_output->len ); |
| 2484 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2485 | /* Compute with larger buffer, multi-part */ |
| 2486 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2487 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2488 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2489 | sizeof( output ), &output_length ) ); |
| 2490 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2491 | ASSERT_COMPARE( output, output_length, |
| 2492 | expected_output->x, expected_output->len ); |
| 2493 | |
| 2494 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2495 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2496 | output, output_length ) ); |
| 2497 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2498 | /* Compare with correct hash, multi-part */ |
| 2499 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2500 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2501 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2502 | output_length ) ); |
| 2503 | |
| 2504 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2505 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2506 | output, output_length + 1 ), |
| 2507 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2508 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2509 | /* Compare with trailing garbage, multi-part */ |
| 2510 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2511 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2512 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2513 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2514 | |
| 2515 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2516 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2517 | output, output_length - 1 ), |
| 2518 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2519 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2520 | /* Compare with truncated hash, multi-part */ |
| 2521 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2522 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2523 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2524 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2525 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2526 | /* Compare with corrupted value */ |
| 2527 | for( i = 0; i < output_length; i++ ) |
| 2528 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2529 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2530 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2531 | |
| 2532 | /* One-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2533 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2534 | output, output_length ), |
| 2535 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2536 | |
| 2537 | /* Multi-Part */ |
| 2538 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2539 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2540 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length ), |
| 2541 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2542 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2543 | output[i] ^= 1; |
| 2544 | } |
| 2545 | |
| 2546 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2547 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2548 | PSA_DONE( ); |
| 2549 | } |
| 2550 | /* END_CASE */ |
| 2551 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2552 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2553 | void hash_bad_order( ) |
| 2554 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2555 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2556 | unsigned char input[] = ""; |
| 2557 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2558 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2559 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2560 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2561 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2562 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2563 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2564 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2565 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2566 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2567 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2568 | /* Call setup twice in a row. */ |
| 2569 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2570 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2571 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2572 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2573 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2574 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2575 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2576 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2577 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2578 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2579 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2580 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2581 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2582 | /* Check that update calls abort on error. */ |
| 2583 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2584 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2585 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2586 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2587 | PSA_ERROR_BAD_STATE ); |
| 2588 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2589 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2590 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2591 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2592 | /* Call update after finish. */ |
| 2593 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2594 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2595 | hash, sizeof( hash ), &hash_len ) ); |
| 2596 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2597 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2598 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2599 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2600 | /* Call verify without calling setup beforehand. */ |
| 2601 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2602 | valid_hash, sizeof( valid_hash ) ), |
| 2603 | PSA_ERROR_BAD_STATE ); |
| 2604 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2605 | |
| 2606 | /* Call verify after finish. */ |
| 2607 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2608 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2609 | hash, sizeof( hash ), &hash_len ) ); |
| 2610 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2611 | valid_hash, sizeof( valid_hash ) ), |
| 2612 | PSA_ERROR_BAD_STATE ); |
| 2613 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2614 | |
| 2615 | /* Call verify twice in a row. */ |
| 2616 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2617 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2618 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2619 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2620 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2621 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2622 | valid_hash, sizeof( valid_hash ) ), |
| 2623 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2624 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2625 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2626 | |
| 2627 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2628 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2629 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2630 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2631 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2632 | |
| 2633 | /* Call finish twice in a row. */ |
| 2634 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2635 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2636 | hash, sizeof( hash ), &hash_len ) ); |
| 2637 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2638 | hash, sizeof( hash ), &hash_len ), |
| 2639 | PSA_ERROR_BAD_STATE ); |
| 2640 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2641 | |
| 2642 | /* Call finish after calling verify. */ |
| 2643 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2644 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2645 | valid_hash, sizeof( valid_hash ) ) ); |
| 2646 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2647 | hash, sizeof( hash ), &hash_len ), |
| 2648 | PSA_ERROR_BAD_STATE ); |
| 2649 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2650 | |
| 2651 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2652 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2653 | } |
| 2654 | /* END_CASE */ |
| 2655 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2656 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2657 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2658 | { |
| 2659 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2660 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2661 | * appended to it */ |
| 2662 | unsigned char hash[] = { |
| 2663 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2664 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2665 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2666 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2667 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2668 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2669 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2670 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2671 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2672 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2673 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2674 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2675 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2676 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2677 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2678 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2679 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2680 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2681 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2682 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2683 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2684 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2685 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2686 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2687 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2688 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2689 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2690 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2691 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2692 | } |
| 2693 | /* END_CASE */ |
| 2694 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2695 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2696 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2697 | { |
| 2698 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2699 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2700 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2701 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2702 | size_t hash_len; |
| 2703 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2704 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2705 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2706 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2707 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2708 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2709 | hash, expected_size - 1, &hash_len ), |
| 2710 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2711 | |
| 2712 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2713 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2714 | } |
| 2715 | /* END_CASE */ |
| 2716 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2717 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2718 | void hash_clone_source_state( ) |
| 2719 | { |
| 2720 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2721 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2722 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2723 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2724 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2725 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2726 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2727 | size_t hash_len; |
| 2728 | |
| 2729 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2730 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2731 | |
| 2732 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2733 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2734 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2735 | hash, sizeof( hash ), &hash_len ) ); |
| 2736 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2737 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2738 | |
| 2739 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2740 | PSA_ERROR_BAD_STATE ); |
| 2741 | |
| 2742 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2743 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2744 | hash, sizeof( hash ), &hash_len ) ); |
| 2745 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2746 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2747 | hash, sizeof( hash ), &hash_len ) ); |
| 2748 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2749 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2750 | hash, sizeof( hash ), &hash_len ) ); |
| 2751 | |
| 2752 | exit: |
| 2753 | psa_hash_abort( &op_source ); |
| 2754 | psa_hash_abort( &op_init ); |
| 2755 | psa_hash_abort( &op_setup ); |
| 2756 | psa_hash_abort( &op_finished ); |
| 2757 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2758 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2759 | } |
| 2760 | /* END_CASE */ |
| 2761 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2762 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2763 | void hash_clone_target_state( ) |
| 2764 | { |
| 2765 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2766 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2767 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2768 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2769 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2770 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2771 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2772 | size_t hash_len; |
| 2773 | |
| 2774 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2775 | |
| 2776 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2777 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2778 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2779 | hash, sizeof( hash ), &hash_len ) ); |
| 2780 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2781 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2782 | |
| 2783 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2784 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2785 | hash, sizeof( hash ), &hash_len ) ); |
| 2786 | |
| 2787 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2788 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2789 | PSA_ERROR_BAD_STATE ); |
| 2790 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2791 | PSA_ERROR_BAD_STATE ); |
| 2792 | |
| 2793 | exit: |
| 2794 | psa_hash_abort( &op_target ); |
| 2795 | psa_hash_abort( &op_init ); |
| 2796 | psa_hash_abort( &op_setup ); |
| 2797 | psa_hash_abort( &op_finished ); |
| 2798 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2799 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2800 | } |
| 2801 | /* END_CASE */ |
| 2802 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2803 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2804 | void mac_operation_init( ) |
| 2805 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2806 | const uint8_t input[1] = { 0 }; |
| 2807 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2808 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2809 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2810 | * 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] | 2811 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2812 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2813 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2814 | psa_mac_operation_t zero; |
| 2815 | |
| 2816 | memset( &zero, 0, sizeof( zero ) ); |
| 2817 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2818 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2819 | TEST_EQUAL( psa_mac_update( &func, |
| 2820 | input, sizeof( input ) ), |
| 2821 | PSA_ERROR_BAD_STATE ); |
| 2822 | TEST_EQUAL( psa_mac_update( &init, |
| 2823 | input, sizeof( input ) ), |
| 2824 | PSA_ERROR_BAD_STATE ); |
| 2825 | TEST_EQUAL( psa_mac_update( &zero, |
| 2826 | input, sizeof( input ) ), |
| 2827 | PSA_ERROR_BAD_STATE ); |
| 2828 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2829 | /* A default MAC operation should be abortable without error. */ |
| 2830 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2831 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2832 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2833 | } |
| 2834 | /* END_CASE */ |
| 2835 | |
| 2836 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2837 | void mac_setup( int key_type_arg, |
| 2838 | data_t *key, |
| 2839 | int alg_arg, |
| 2840 | int expected_status_arg ) |
| 2841 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2842 | psa_key_type_t key_type = key_type_arg; |
| 2843 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2844 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2845 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2846 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2847 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2848 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2849 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2850 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2851 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2852 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2853 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2854 | &operation, &status ) ) |
| 2855 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2856 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2857 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2858 | /* The operation object should be reusable. */ |
| 2859 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2860 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2861 | smoke_test_key_data, |
| 2862 | sizeof( smoke_test_key_data ), |
| 2863 | KNOWN_SUPPORTED_MAC_ALG, |
| 2864 | &operation, &status ) ) |
| 2865 | goto exit; |
| 2866 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2867 | #endif |
| 2868 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2869 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2870 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2871 | } |
| 2872 | /* END_CASE */ |
| 2873 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2874 | /* 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] | 2875 | void mac_bad_order( ) |
| 2876 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2877 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2878 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2879 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2880 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2881 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2882 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2883 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2884 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2885 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2886 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2887 | size_t sign_mac_length = 0; |
| 2888 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2889 | const uint8_t verify_mac[] = { |
| 2890 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2891 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2892 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2893 | |
| 2894 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2895 | 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] | 2896 | psa_set_key_algorithm( &attributes, alg ); |
| 2897 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2898 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2899 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2900 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2901 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2902 | /* Call update without calling setup beforehand. */ |
| 2903 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2904 | PSA_ERROR_BAD_STATE ); |
| 2905 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2906 | |
| 2907 | /* Call sign finish without calling setup beforehand. */ |
| 2908 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2909 | &sign_mac_length), |
| 2910 | PSA_ERROR_BAD_STATE ); |
| 2911 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2912 | |
| 2913 | /* Call verify finish without calling setup beforehand. */ |
| 2914 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2915 | verify_mac, sizeof( verify_mac ) ), |
| 2916 | PSA_ERROR_BAD_STATE ); |
| 2917 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2918 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2919 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2920 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2921 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2922 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2923 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2924 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2925 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2926 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2927 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2928 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2929 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2930 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2931 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2932 | sign_mac, sizeof( sign_mac ), |
| 2933 | &sign_mac_length ) ); |
| 2934 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2935 | PSA_ERROR_BAD_STATE ); |
| 2936 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2937 | |
| 2938 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2939 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2940 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2941 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2942 | verify_mac, sizeof( verify_mac ) ) ); |
| 2943 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2944 | PSA_ERROR_BAD_STATE ); |
| 2945 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2946 | |
| 2947 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2948 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2949 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2950 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2951 | sign_mac, sizeof( sign_mac ), |
| 2952 | &sign_mac_length ) ); |
| 2953 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2954 | sign_mac, sizeof( sign_mac ), |
| 2955 | &sign_mac_length ), |
| 2956 | PSA_ERROR_BAD_STATE ); |
| 2957 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2958 | |
| 2959 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2960 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2961 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2962 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2963 | verify_mac, sizeof( verify_mac ) ) ); |
| 2964 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2965 | verify_mac, sizeof( verify_mac ) ), |
| 2966 | PSA_ERROR_BAD_STATE ); |
| 2967 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2968 | |
| 2969 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2970 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2971 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2972 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2973 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2974 | verify_mac, sizeof( verify_mac ) ), |
| 2975 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2976 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2977 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2978 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2979 | |
| 2980 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2981 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2982 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2983 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2984 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2985 | sign_mac, sizeof( sign_mac ), |
| 2986 | &sign_mac_length ), |
| 2987 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2988 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2989 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2990 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2991 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2992 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2993 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2994 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2995 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2996 | } |
| 2997 | /* END_CASE */ |
| 2998 | |
| 2999 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3000 | void mac_sign_verify_multi( int key_type_arg, |
| 3001 | data_t *key_data, |
| 3002 | int alg_arg, |
| 3003 | data_t *input, |
| 3004 | int is_verify, |
| 3005 | data_t *expected_mac ) |
| 3006 | { |
| 3007 | size_t data_part_len = 0; |
| 3008 | |
| 3009 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3010 | { |
| 3011 | /* Split data into length(data_part_len) parts. */ |
| 3012 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3013 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3014 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3015 | alg_arg, |
| 3016 | input, data_part_len, |
| 3017 | expected_mac, |
| 3018 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3019 | break; |
| 3020 | |
| 3021 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3022 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3023 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3024 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3025 | alg_arg, |
| 3026 | input, data_part_len, |
| 3027 | expected_mac, |
| 3028 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3029 | break; |
| 3030 | } |
| 3031 | |
| 3032 | /* Goto is required to silence warnings about unused labels, as we |
| 3033 | * don't actually do any test assertions in this function. */ |
| 3034 | goto exit; |
| 3035 | } |
| 3036 | /* END_CASE */ |
| 3037 | |
| 3038 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3039 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3040 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3041 | int alg_arg, |
| 3042 | data_t *input, |
| 3043 | data_t *expected_mac ) |
| 3044 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3045 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3046 | psa_key_type_t key_type = key_type_arg; |
| 3047 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3048 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3049 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3050 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3051 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3052 | 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] | 3053 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3054 | const size_t output_sizes_to_test[] = { |
| 3055 | 0, |
| 3056 | 1, |
| 3057 | expected_mac->len - 1, |
| 3058 | expected_mac->len, |
| 3059 | expected_mac->len + 1, |
| 3060 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3061 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3062 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3063 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3064 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3065 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3066 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3067 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3068 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3069 | psa_set_key_algorithm( &attributes, alg ); |
| 3070 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3071 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3072 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3073 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3074 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3075 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3076 | { |
| 3077 | const size_t output_size = output_sizes_to_test[i]; |
| 3078 | psa_status_t expected_status = |
| 3079 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3080 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3081 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3082 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3083 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3084 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3085 | /* Calculate the MAC, one-shot case. */ |
| 3086 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3087 | input->x, input->len, |
| 3088 | actual_mac, output_size, &mac_length ), |
| 3089 | expected_status ); |
| 3090 | if( expected_status == PSA_SUCCESS ) |
| 3091 | { |
| 3092 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3093 | actual_mac, mac_length ); |
| 3094 | } |
| 3095 | |
| 3096 | if( output_size > 0 ) |
| 3097 | memset( actual_mac, 0, output_size ); |
| 3098 | |
| 3099 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3100 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3101 | PSA_ASSERT( psa_mac_update( &operation, |
| 3102 | input->x, input->len ) ); |
| 3103 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3104 | actual_mac, output_size, |
| 3105 | &mac_length ), |
| 3106 | expected_status ); |
| 3107 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3108 | |
| 3109 | if( expected_status == PSA_SUCCESS ) |
| 3110 | { |
| 3111 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3112 | actual_mac, mac_length ); |
| 3113 | } |
| 3114 | mbedtls_free( actual_mac ); |
| 3115 | actual_mac = NULL; |
| 3116 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3117 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3118 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3119 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3120 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3121 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3122 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3123 | } |
| 3124 | /* END_CASE */ |
| 3125 | |
| 3126 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3127 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3128 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3129 | int alg_arg, |
| 3130 | data_t *input, |
| 3131 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3132 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3133 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3134 | psa_key_type_t key_type = key_type_arg; |
| 3135 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3136 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3137 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3138 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3139 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3140 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3141 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3142 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3143 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3144 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3145 | psa_set_key_algorithm( &attributes, alg ); |
| 3146 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3147 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3148 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3149 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3150 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3151 | /* Verify correct MAC, one-shot case. */ |
| 3152 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3153 | expected_mac->x, expected_mac->len ) ); |
| 3154 | |
| 3155 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3156 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3157 | PSA_ASSERT( psa_mac_update( &operation, |
| 3158 | input->x, input->len ) ); |
| 3159 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3160 | expected_mac->x, |
| 3161 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3162 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3163 | /* Test a MAC that's too short, one-shot case. */ |
| 3164 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3165 | input->x, input->len, |
| 3166 | expected_mac->x, |
| 3167 | expected_mac->len - 1 ), |
| 3168 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3169 | |
| 3170 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3171 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3172 | PSA_ASSERT( psa_mac_update( &operation, |
| 3173 | input->x, input->len ) ); |
| 3174 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3175 | expected_mac->x, |
| 3176 | expected_mac->len - 1 ), |
| 3177 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3178 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3179 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3180 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3181 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3182 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3183 | input->x, input->len, |
| 3184 | perturbed_mac, expected_mac->len + 1 ), |
| 3185 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3186 | |
| 3187 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3188 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3189 | PSA_ASSERT( psa_mac_update( &operation, |
| 3190 | input->x, input->len ) ); |
| 3191 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3192 | perturbed_mac, |
| 3193 | expected_mac->len + 1 ), |
| 3194 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3195 | |
| 3196 | /* Test changing one byte. */ |
| 3197 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3198 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3199 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3200 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3201 | |
| 3202 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3203 | input->x, input->len, |
| 3204 | perturbed_mac, expected_mac->len ), |
| 3205 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3206 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3207 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3208 | PSA_ASSERT( psa_mac_update( &operation, |
| 3209 | input->x, input->len ) ); |
| 3210 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3211 | perturbed_mac, |
| 3212 | expected_mac->len ), |
| 3213 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3214 | perturbed_mac[i] ^= 1; |
| 3215 | } |
| 3216 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3217 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3218 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3219 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3220 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3221 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3222 | } |
| 3223 | /* END_CASE */ |
| 3224 | |
| 3225 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3226 | void cipher_operation_init( ) |
| 3227 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3228 | const uint8_t input[1] = { 0 }; |
| 3229 | unsigned char output[1] = { 0 }; |
| 3230 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3231 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3232 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3233 | * 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] | 3234 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3235 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3236 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3237 | psa_cipher_operation_t zero; |
| 3238 | |
| 3239 | memset( &zero, 0, sizeof( zero ) ); |
| 3240 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3241 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3242 | TEST_EQUAL( psa_cipher_update( &func, |
| 3243 | input, sizeof( input ), |
| 3244 | output, sizeof( output ), |
| 3245 | &output_length ), |
| 3246 | PSA_ERROR_BAD_STATE ); |
| 3247 | TEST_EQUAL( psa_cipher_update( &init, |
| 3248 | input, sizeof( input ), |
| 3249 | output, sizeof( output ), |
| 3250 | &output_length ), |
| 3251 | PSA_ERROR_BAD_STATE ); |
| 3252 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3253 | input, sizeof( input ), |
| 3254 | output, sizeof( output ), |
| 3255 | &output_length ), |
| 3256 | PSA_ERROR_BAD_STATE ); |
| 3257 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3258 | /* A default cipher operation should be abortable without error. */ |
| 3259 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3260 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3261 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3262 | } |
| 3263 | /* END_CASE */ |
| 3264 | |
| 3265 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3266 | void cipher_setup( int key_type_arg, |
| 3267 | data_t *key, |
| 3268 | int alg_arg, |
| 3269 | int expected_status_arg ) |
| 3270 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3271 | psa_key_type_t key_type = key_type_arg; |
| 3272 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3273 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3274 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3275 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3276 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3277 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3278 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3279 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3280 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3281 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3282 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3283 | &operation, &status ) ) |
| 3284 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3285 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3286 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3287 | /* The operation object should be reusable. */ |
| 3288 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3289 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3290 | smoke_test_key_data, |
| 3291 | sizeof( smoke_test_key_data ), |
| 3292 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3293 | &operation, &status ) ) |
| 3294 | goto exit; |
| 3295 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3296 | #endif |
| 3297 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3298 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3299 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3300 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3301 | } |
| 3302 | /* END_CASE */ |
| 3303 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3304 | /* 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] | 3305 | void cipher_bad_order( ) |
| 3306 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3307 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3308 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3309 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3310 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3311 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3312 | 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] | 3313 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3314 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3315 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3316 | const uint8_t text[] = { |
| 3317 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3318 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3319 | 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] | 3320 | size_t length = 0; |
| 3321 | |
| 3322 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3323 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3324 | psa_set_key_algorithm( &attributes, alg ); |
| 3325 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3326 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3327 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3328 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3329 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3330 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3331 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3332 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3333 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3334 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3335 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3336 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3337 | |
| 3338 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3339 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3340 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3341 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3342 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3343 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3344 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3345 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3346 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3347 | /* Generate an IV without calling setup beforehand. */ |
| 3348 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3349 | buffer, sizeof( buffer ), |
| 3350 | &length ), |
| 3351 | PSA_ERROR_BAD_STATE ); |
| 3352 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3353 | |
| 3354 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3355 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3356 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3357 | buffer, sizeof( buffer ), |
| 3358 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3359 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3360 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3361 | buffer, sizeof( buffer ), |
| 3362 | &length ), |
| 3363 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3364 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3365 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3366 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3367 | |
| 3368 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3369 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3370 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3371 | iv, sizeof( iv ) ) ); |
| 3372 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3373 | buffer, sizeof( buffer ), |
| 3374 | &length ), |
| 3375 | PSA_ERROR_BAD_STATE ); |
| 3376 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3377 | |
| 3378 | /* Set an IV without calling setup beforehand. */ |
| 3379 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3380 | iv, sizeof( iv ) ), |
| 3381 | PSA_ERROR_BAD_STATE ); |
| 3382 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3383 | |
| 3384 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3385 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3386 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3387 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3388 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3389 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3390 | iv, sizeof( iv ) ), |
| 3391 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3392 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3393 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3394 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3395 | |
| 3396 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3397 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3398 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3399 | buffer, sizeof( buffer ), |
| 3400 | &length ) ); |
| 3401 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3402 | iv, sizeof( iv ) ), |
| 3403 | PSA_ERROR_BAD_STATE ); |
| 3404 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3405 | |
| 3406 | /* Call update without calling setup beforehand. */ |
| 3407 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3408 | text, sizeof( text ), |
| 3409 | buffer, sizeof( buffer ), |
| 3410 | &length ), |
| 3411 | PSA_ERROR_BAD_STATE ); |
| 3412 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3413 | |
| 3414 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3415 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3416 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3417 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3418 | text, sizeof( text ), |
| 3419 | buffer, sizeof( buffer ), |
| 3420 | &length ), |
| 3421 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3422 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3423 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3424 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3425 | |
| 3426 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3427 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3428 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3429 | iv, sizeof( iv ) ) ); |
| 3430 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3431 | buffer, sizeof( buffer ), &length ) ); |
| 3432 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3433 | text, sizeof( text ), |
| 3434 | buffer, sizeof( buffer ), |
| 3435 | &length ), |
| 3436 | PSA_ERROR_BAD_STATE ); |
| 3437 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3438 | |
| 3439 | /* Call finish without calling setup beforehand. */ |
| 3440 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3441 | buffer, sizeof( buffer ), &length ), |
| 3442 | PSA_ERROR_BAD_STATE ); |
| 3443 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3444 | |
| 3445 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3446 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3447 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3448 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3449 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3450 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3451 | buffer, sizeof( buffer ), &length ), |
| 3452 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3453 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3454 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3455 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3456 | |
| 3457 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3458 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3459 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3460 | iv, sizeof( iv ) ) ); |
| 3461 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3462 | buffer, sizeof( buffer ), &length ) ); |
| 3463 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3464 | buffer, sizeof( buffer ), &length ), |
| 3465 | PSA_ERROR_BAD_STATE ); |
| 3466 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3467 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3468 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3469 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3470 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3471 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3472 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3473 | } |
| 3474 | /* END_CASE */ |
| 3475 | |
| 3476 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3477 | void cipher_encrypt_fail( int alg_arg, |
| 3478 | int key_type_arg, |
| 3479 | data_t *key_data, |
| 3480 | data_t *input, |
| 3481 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3482 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3483 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3484 | psa_status_t status; |
| 3485 | psa_key_type_t key_type = key_type_arg; |
| 3486 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3487 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3488 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3489 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3490 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3491 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3492 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3493 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3494 | size_t function_output_length; |
| 3495 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3496 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3497 | |
| 3498 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3499 | { |
| 3500 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3501 | |
| 3502 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3503 | psa_set_key_algorithm( &attributes, alg ); |
| 3504 | psa_set_key_type( &attributes, key_type ); |
| 3505 | |
| 3506 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3507 | input->len ); |
| 3508 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3509 | |
| 3510 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3511 | &key ) ); |
| 3512 | } |
| 3513 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3514 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3515 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3516 | output_buffer_size, &output_length ); |
| 3517 | |
| 3518 | TEST_EQUAL( status, expected_status ); |
| 3519 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3520 | /* Encrypt, multi-part */ |
| 3521 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3522 | if( status == PSA_SUCCESS ) |
| 3523 | { |
| 3524 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3525 | { |
| 3526 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3527 | iv, iv_size, |
| 3528 | &iv_length ) ); |
| 3529 | } |
| 3530 | |
| 3531 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3532 | output, output_buffer_size, |
| 3533 | &function_output_length ); |
| 3534 | if( status == PSA_SUCCESS ) |
| 3535 | { |
| 3536 | output_length += function_output_length; |
| 3537 | |
| 3538 | status = psa_cipher_finish( &operation, output + output_length, |
| 3539 | output_buffer_size - output_length, |
| 3540 | &function_output_length ); |
| 3541 | |
| 3542 | TEST_EQUAL( status, expected_status ); |
| 3543 | } |
| 3544 | else |
| 3545 | { |
| 3546 | TEST_EQUAL( status, expected_status ); |
| 3547 | } |
| 3548 | } |
| 3549 | else |
| 3550 | { |
| 3551 | TEST_EQUAL( status, expected_status ); |
| 3552 | } |
| 3553 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3554 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3555 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3556 | mbedtls_free( output ); |
| 3557 | psa_destroy_key( key ); |
| 3558 | PSA_DONE( ); |
| 3559 | } |
| 3560 | /* END_CASE */ |
| 3561 | |
| 3562 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3563 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3564 | data_t *input, int iv_length, |
| 3565 | int expected_result ) |
| 3566 | { |
| 3567 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3568 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3569 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3570 | size_t output_buffer_size = 0; |
| 3571 | unsigned char *output = NULL; |
| 3572 | |
| 3573 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3574 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3575 | |
| 3576 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3577 | |
| 3578 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3579 | psa_set_key_algorithm( &attributes, alg ); |
| 3580 | psa_set_key_type( &attributes, key_type ); |
| 3581 | |
| 3582 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3583 | &key ) ); |
| 3584 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3585 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3586 | iv_length ) ); |
| 3587 | |
| 3588 | exit: |
| 3589 | psa_cipher_abort( &operation ); |
| 3590 | mbedtls_free( output ); |
| 3591 | psa_destroy_key( key ); |
| 3592 | PSA_DONE( ); |
| 3593 | } |
| 3594 | /* END_CASE */ |
| 3595 | |
| 3596 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3597 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3598 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3599 | { |
| 3600 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3601 | psa_key_type_t key_type = key_type_arg; |
| 3602 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3603 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3604 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3605 | unsigned char *output = NULL; |
| 3606 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3607 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3608 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3609 | |
| 3610 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3611 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3612 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3613 | TEST_LE_U( ciphertext->len, |
| 3614 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3615 | 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] | 3616 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3617 | TEST_LE_U( plaintext->len, |
| 3618 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3619 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3620 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3621 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3622 | |
| 3623 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3624 | psa_set_key_usage_flags( &attributes, |
| 3625 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3626 | psa_set_key_algorithm( &attributes, alg ); |
| 3627 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3628 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3629 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3630 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3631 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3632 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3633 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3634 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3635 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3636 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3637 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3638 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3639 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3640 | PSA_ERROR_BAD_STATE ); |
| 3641 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3642 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3643 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3644 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3645 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3646 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3647 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3648 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3649 | &length ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3650 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3651 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3652 | /* Multipart encryption */ |
| 3653 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3654 | output_length = 0; |
| 3655 | length = ~0; |
| 3656 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3657 | plaintext->x, plaintext->len, |
| 3658 | output, output_buffer_size, |
| 3659 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3660 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3661 | output_length += length; |
| 3662 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3663 | output + output_length, |
| 3664 | output_buffer_size - output_length, |
| 3665 | &length ) ); |
| 3666 | output_length += length; |
| 3667 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3668 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3669 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3670 | /* Multipart encryption */ |
| 3671 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3672 | output_length = 0; |
| 3673 | length = ~0; |
| 3674 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3675 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3676 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3677 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3678 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3679 | output_length += length; |
| 3680 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3681 | output + output_length, |
| 3682 | output_buffer_size - output_length, |
| 3683 | &length ) ); |
| 3684 | output_length += length; |
| 3685 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 3686 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3687 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3688 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3689 | output_length = ~0; |
| 3690 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 3691 | output, output_buffer_size, |
| 3692 | &output_length ) ); |
| 3693 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 3694 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3695 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3696 | /* One-shot decryption */ |
| 3697 | output_length = ~0; |
| 3698 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 3699 | output, output_buffer_size, |
| 3700 | &output_length ) ); |
| 3701 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3702 | output, output_length ); |
| 3703 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3704 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3705 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3706 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3707 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3708 | psa_destroy_key( key ); |
| 3709 | PSA_DONE( ); |
| 3710 | } |
| 3711 | /* END_CASE */ |
| 3712 | |
| 3713 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3714 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 3715 | { |
| 3716 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3717 | psa_algorithm_t alg = alg_arg; |
| 3718 | psa_key_type_t key_type = key_type_arg; |
| 3719 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3720 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3721 | psa_status_t status; |
| 3722 | |
| 3723 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3724 | |
| 3725 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3726 | psa_set_key_algorithm( &attributes, alg ); |
| 3727 | psa_set_key_type( &attributes, key_type ); |
| 3728 | |
| 3729 | /* Usage of either of these two size macros would cause divide by zero |
| 3730 | * with incorrect key types previously. Input length should be irrelevant |
| 3731 | * here. */ |
| 3732 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 3733 | 0 ); |
| 3734 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 3735 | |
| 3736 | |
| 3737 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3738 | &key ) ); |
| 3739 | |
| 3740 | /* Should fail due to invalid alg type (to support invalid key type). |
| 3741 | * Encrypt or decrypt will end up in the same place. */ |
| 3742 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3743 | |
| 3744 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 3745 | |
| 3746 | exit: |
| 3747 | psa_cipher_abort( &operation ); |
| 3748 | psa_destroy_key( key ); |
| 3749 | PSA_DONE( ); |
| 3750 | } |
| 3751 | /* END_CASE */ |
| 3752 | |
| 3753 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3754 | void cipher_encrypt_validation( int alg_arg, |
| 3755 | int key_type_arg, |
| 3756 | data_t *key_data, |
| 3757 | data_t *input ) |
| 3758 | { |
| 3759 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3760 | psa_key_type_t key_type = key_type_arg; |
| 3761 | psa_algorithm_t alg = alg_arg; |
| 3762 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 3763 | unsigned char *output1 = NULL; |
| 3764 | size_t output1_buffer_size = 0; |
| 3765 | size_t output1_length = 0; |
| 3766 | unsigned char *output2 = NULL; |
| 3767 | size_t output2_buffer_size = 0; |
| 3768 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3769 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3770 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3771 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3772 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3773 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3774 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3775 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3776 | psa_set_key_algorithm( &attributes, alg ); |
| 3777 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3778 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3779 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3780 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3781 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 3782 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 3783 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 3784 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3785 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3786 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3787 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 3788 | /* The one-shot cipher encryption uses generated iv so validating |
| 3789 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3790 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 3791 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3792 | TEST_LE_U( output1_length, |
| 3793 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3794 | TEST_LE_U( output1_length, |
| 3795 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3796 | |
| 3797 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3798 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3799 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3800 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3801 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3802 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3803 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3804 | TEST_LE_U( function_output_length, |
| 3805 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3806 | TEST_LE_U( function_output_length, |
| 3807 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3808 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3809 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3810 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3811 | output2 + output2_length, |
| 3812 | output2_buffer_size - output2_length, |
| 3813 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3814 | TEST_LE_U( function_output_length, |
| 3815 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3816 | TEST_LE_U( function_output_length, |
| 3817 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3818 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3819 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3820 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3821 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 3822 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3823 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3824 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3825 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3826 | mbedtls_free( output1 ); |
| 3827 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3828 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3829 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3830 | } |
| 3831 | /* END_CASE */ |
| 3832 | |
| 3833 | /* BEGIN_CASE */ |
| 3834 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3835 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3836 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3837 | int first_part_size_arg, |
| 3838 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3839 | data_t *expected_output, |
| 3840 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3841 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3842 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3843 | psa_key_type_t key_type = key_type_arg; |
| 3844 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3845 | psa_status_t status; |
| 3846 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3847 | size_t first_part_size = first_part_size_arg; |
| 3848 | size_t output1_length = output1_length_arg; |
| 3849 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3850 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3851 | size_t output_buffer_size = 0; |
| 3852 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3853 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3854 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3855 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3856 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3857 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3858 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3859 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3860 | psa_set_key_algorithm( &attributes, alg ); |
| 3861 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3862 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3863 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3864 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3865 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3866 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3867 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3868 | if( iv->len > 0 ) |
| 3869 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3870 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3871 | } |
| 3872 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3873 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3874 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3875 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3876 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3877 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3878 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3879 | output, output_buffer_size, |
| 3880 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3881 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3882 | TEST_LE_U( function_output_length, |
| 3883 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3884 | TEST_LE_U( function_output_length, |
| 3885 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3886 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3887 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3888 | if( first_part_size < input->len ) |
| 3889 | { |
| 3890 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3891 | input->x + first_part_size, |
| 3892 | input->len - first_part_size, |
| 3893 | ( output_buffer_size == 0 ? NULL : |
| 3894 | output + total_output_length ), |
| 3895 | output_buffer_size - total_output_length, |
| 3896 | &function_output_length ) ); |
| 3897 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3898 | TEST_LE_U( function_output_length, |
| 3899 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3900 | alg, |
| 3901 | input->len - first_part_size ) ); |
| 3902 | TEST_LE_U( function_output_length, |
| 3903 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3904 | total_output_length += function_output_length; |
| 3905 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3906 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3907 | status = psa_cipher_finish( &operation, |
| 3908 | ( output_buffer_size == 0 ? NULL : |
| 3909 | output + total_output_length ), |
| 3910 | output_buffer_size - total_output_length, |
| 3911 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3912 | TEST_LE_U( function_output_length, |
| 3913 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3914 | TEST_LE_U( function_output_length, |
| 3915 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3916 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3917 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3918 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3919 | if( expected_status == PSA_SUCCESS ) |
| 3920 | { |
| 3921 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3922 | |
| 3923 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3924 | output, total_output_length ); |
| 3925 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3926 | |
| 3927 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3928 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3929 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3930 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3931 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3932 | } |
| 3933 | /* END_CASE */ |
| 3934 | |
| 3935 | /* BEGIN_CASE */ |
| 3936 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3937 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3938 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3939 | int first_part_size_arg, |
| 3940 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3941 | data_t *expected_output, |
| 3942 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3943 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3944 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3945 | psa_key_type_t key_type = key_type_arg; |
| 3946 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3947 | psa_status_t status; |
| 3948 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3949 | size_t first_part_size = first_part_size_arg; |
| 3950 | size_t output1_length = output1_length_arg; |
| 3951 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3952 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3953 | size_t output_buffer_size = 0; |
| 3954 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3955 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3956 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3957 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3958 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3959 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3960 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3961 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3962 | psa_set_key_algorithm( &attributes, alg ); |
| 3963 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3964 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3965 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3966 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3967 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3968 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3969 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3970 | if( iv->len > 0 ) |
| 3971 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3972 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3973 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3974 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3975 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3976 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3977 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3978 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3979 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3980 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3981 | input->x, first_part_size, |
| 3982 | output, output_buffer_size, |
| 3983 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3984 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3985 | TEST_LE_U( function_output_length, |
| 3986 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3987 | TEST_LE_U( function_output_length, |
| 3988 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3989 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3990 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3991 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3992 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3993 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3994 | input->x + first_part_size, |
| 3995 | input->len - first_part_size, |
| 3996 | ( output_buffer_size == 0 ? NULL : |
| 3997 | output + total_output_length ), |
| 3998 | output_buffer_size - total_output_length, |
| 3999 | &function_output_length ) ); |
| 4000 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4001 | TEST_LE_U( function_output_length, |
| 4002 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4003 | alg, |
| 4004 | input->len - first_part_size ) ); |
| 4005 | TEST_LE_U( function_output_length, |
| 4006 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4007 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4008 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4009 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4010 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4011 | ( output_buffer_size == 0 ? NULL : |
| 4012 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4013 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4014 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4015 | TEST_LE_U( function_output_length, |
| 4016 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4017 | TEST_LE_U( function_output_length, |
| 4018 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4019 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4020 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4021 | |
| 4022 | if( expected_status == PSA_SUCCESS ) |
| 4023 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4024 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4025 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4026 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4027 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4028 | } |
| 4029 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4030 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4031 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4032 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4033 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4034 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4035 | } |
| 4036 | /* END_CASE */ |
| 4037 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4038 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4039 | void cipher_decrypt_fail( int alg_arg, |
| 4040 | int key_type_arg, |
| 4041 | data_t *key_data, |
| 4042 | data_t *iv, |
| 4043 | data_t *input_arg, |
| 4044 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4045 | { |
| 4046 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4047 | psa_status_t status; |
| 4048 | psa_key_type_t key_type = key_type_arg; |
| 4049 | psa_algorithm_t alg = alg_arg; |
| 4050 | psa_status_t expected_status = expected_status_arg; |
| 4051 | unsigned char *input = NULL; |
| 4052 | size_t input_buffer_size = 0; |
| 4053 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4054 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4055 | size_t output_buffer_size = 0; |
| 4056 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4057 | size_t function_output_length; |
| 4058 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4059 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4060 | |
| 4061 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4062 | { |
| 4063 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4064 | |
| 4065 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4066 | psa_set_key_algorithm( &attributes, alg ); |
| 4067 | psa_set_key_type( &attributes, key_type ); |
| 4068 | |
| 4069 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4070 | &key ) ); |
| 4071 | } |
| 4072 | |
| 4073 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4074 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4075 | if ( input_buffer_size > 0 ) |
| 4076 | { |
| 4077 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4078 | memcpy( input, iv->x, iv->len ); |
| 4079 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4080 | } |
| 4081 | |
| 4082 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4083 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4084 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4085 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4086 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4087 | output_buffer_size, &output_length ); |
| 4088 | TEST_EQUAL( status, expected_status ); |
| 4089 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4090 | /* Decrypt, multi-part */ |
| 4091 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4092 | if( status == PSA_SUCCESS ) |
| 4093 | { |
| 4094 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4095 | input_arg->len ) + |
| 4096 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4097 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4098 | |
| 4099 | if( iv->len > 0 ) |
| 4100 | { |
| 4101 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4102 | |
| 4103 | if( status != PSA_SUCCESS ) |
| 4104 | TEST_EQUAL( status, expected_status ); |
| 4105 | } |
| 4106 | |
| 4107 | if( status == PSA_SUCCESS ) |
| 4108 | { |
| 4109 | status = psa_cipher_update( &operation, |
| 4110 | input_arg->x, input_arg->len, |
| 4111 | output_multi, output_buffer_size, |
| 4112 | &function_output_length ); |
| 4113 | if( status == PSA_SUCCESS ) |
| 4114 | { |
| 4115 | output_length = function_output_length; |
| 4116 | |
| 4117 | status = psa_cipher_finish( &operation, |
| 4118 | output_multi + output_length, |
| 4119 | output_buffer_size - output_length, |
| 4120 | &function_output_length ); |
| 4121 | |
| 4122 | TEST_EQUAL( status, expected_status ); |
| 4123 | } |
| 4124 | else |
| 4125 | { |
| 4126 | TEST_EQUAL( status, expected_status ); |
| 4127 | } |
| 4128 | } |
| 4129 | else |
| 4130 | { |
| 4131 | TEST_EQUAL( status, expected_status ); |
| 4132 | } |
| 4133 | } |
| 4134 | else |
| 4135 | { |
| 4136 | TEST_EQUAL( status, expected_status ); |
| 4137 | } |
| 4138 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4139 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4140 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4141 | mbedtls_free( input ); |
| 4142 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4143 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4144 | psa_destroy_key( key ); |
| 4145 | PSA_DONE( ); |
| 4146 | } |
| 4147 | /* END_CASE */ |
| 4148 | |
| 4149 | /* BEGIN_CASE */ |
| 4150 | void cipher_decrypt( int alg_arg, |
| 4151 | int key_type_arg, |
| 4152 | data_t *key_data, |
| 4153 | data_t *iv, |
| 4154 | data_t *input_arg, |
| 4155 | data_t *expected_output ) |
| 4156 | { |
| 4157 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4158 | psa_key_type_t key_type = key_type_arg; |
| 4159 | psa_algorithm_t alg = alg_arg; |
| 4160 | unsigned char *input = NULL; |
| 4161 | size_t input_buffer_size = 0; |
| 4162 | unsigned char *output = NULL; |
| 4163 | size_t output_buffer_size = 0; |
| 4164 | size_t output_length = 0; |
| 4165 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4166 | |
| 4167 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4168 | |
| 4169 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4170 | psa_set_key_algorithm( &attributes, alg ); |
| 4171 | psa_set_key_type( &attributes, key_type ); |
| 4172 | |
| 4173 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4174 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4175 | if ( input_buffer_size > 0 ) |
| 4176 | { |
| 4177 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4178 | memcpy( input, iv->x, iv->len ); |
| 4179 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4180 | } |
| 4181 | |
| 4182 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4183 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4184 | |
| 4185 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4186 | &key ) ); |
| 4187 | |
| 4188 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4189 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4190 | TEST_LE_U( output_length, |
| 4191 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4192 | TEST_LE_U( output_length, |
| 4193 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4194 | |
| 4195 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4196 | output, output_length ); |
| 4197 | exit: |
| 4198 | mbedtls_free( input ); |
| 4199 | mbedtls_free( output ); |
| 4200 | psa_destroy_key( key ); |
| 4201 | PSA_DONE( ); |
| 4202 | } |
| 4203 | /* END_CASE */ |
| 4204 | |
| 4205 | /* BEGIN_CASE */ |
| 4206 | void cipher_verify_output( int alg_arg, |
| 4207 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4208 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4209 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4210 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4211 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4212 | psa_key_type_t key_type = key_type_arg; |
| 4213 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4214 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4215 | size_t output1_size = 0; |
| 4216 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4217 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4218 | size_t output2_size = 0; |
| 4219 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4220 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4221 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4222 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4223 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4224 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4225 | psa_set_key_algorithm( &attributes, alg ); |
| 4226 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4227 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4228 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4229 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4230 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4231 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4232 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4233 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4234 | output1, output1_size, |
| 4235 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4236 | TEST_LE_U( output1_length, |
| 4237 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4238 | TEST_LE_U( output1_length, |
| 4239 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4240 | |
| 4241 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4242 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4243 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4244 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4245 | output2, output2_size, |
| 4246 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4247 | TEST_LE_U( output2_length, |
| 4248 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4249 | TEST_LE_U( output2_length, |
| 4250 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4251 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4252 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4253 | |
| 4254 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4255 | mbedtls_free( output1 ); |
| 4256 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4257 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4258 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4259 | } |
| 4260 | /* END_CASE */ |
| 4261 | |
| 4262 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4263 | void cipher_verify_output_multipart( int alg_arg, |
| 4264 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4265 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4266 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4267 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4268 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4269 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4270 | psa_key_type_t key_type = key_type_arg; |
| 4271 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4272 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4273 | unsigned char iv[16] = {0}; |
| 4274 | size_t iv_size = 16; |
| 4275 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4276 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4277 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4278 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4279 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4280 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4281 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4282 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4283 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4284 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4285 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4286 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4287 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4288 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4289 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4290 | psa_set_key_algorithm( &attributes, alg ); |
| 4291 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4292 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4293 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4294 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4295 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4296 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4297 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4298 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4299 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4300 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4301 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4302 | iv, iv_size, |
| 4303 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4304 | } |
| 4305 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4306 | 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] | 4307 | TEST_LE_U( output1_buffer_size, |
| 4308 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4309 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4310 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4311 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4312 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4313 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4314 | output1, output1_buffer_size, |
| 4315 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4316 | TEST_LE_U( function_output_length, |
| 4317 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4318 | TEST_LE_U( function_output_length, |
| 4319 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4320 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4321 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4322 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4323 | input->x + first_part_size, |
| 4324 | input->len - first_part_size, |
| 4325 | output1, output1_buffer_size, |
| 4326 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4327 | TEST_LE_U( function_output_length, |
| 4328 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4329 | alg, |
| 4330 | input->len - first_part_size ) ); |
| 4331 | TEST_LE_U( function_output_length, |
| 4332 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4333 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4334 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4335 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4336 | output1 + output1_length, |
| 4337 | output1_buffer_size - output1_length, |
| 4338 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4339 | TEST_LE_U( function_output_length, |
| 4340 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4341 | TEST_LE_U( function_output_length, |
| 4342 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4343 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4344 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4345 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4346 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4347 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4348 | TEST_LE_U( output2_buffer_size, |
| 4349 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4350 | TEST_LE_U( output2_buffer_size, |
| 4351 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4352 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4353 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4354 | if( iv_length > 0 ) |
| 4355 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4356 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4357 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4358 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4359 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4360 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4361 | output2, output2_buffer_size, |
| 4362 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4363 | TEST_LE_U( function_output_length, |
| 4364 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4365 | TEST_LE_U( function_output_length, |
| 4366 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4367 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4368 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4369 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4370 | output1 + first_part_size, |
| 4371 | output1_length - first_part_size, |
| 4372 | output2, output2_buffer_size, |
| 4373 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4374 | TEST_LE_U( function_output_length, |
| 4375 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4376 | alg, |
| 4377 | output1_length - first_part_size ) ); |
| 4378 | TEST_LE_U( function_output_length, |
| 4379 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4380 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4381 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4382 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4383 | output2 + output2_length, |
| 4384 | output2_buffer_size - output2_length, |
| 4385 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4386 | TEST_LE_U( function_output_length, |
| 4387 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4388 | TEST_LE_U( function_output_length, |
| 4389 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4390 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4391 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4392 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4393 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4394 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4395 | |
| 4396 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4397 | psa_cipher_abort( &operation1 ); |
| 4398 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4399 | mbedtls_free( output1 ); |
| 4400 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4401 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4402 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4403 | } |
| 4404 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4405 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4406 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4407 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4408 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4409 | data_t *nonce, |
| 4410 | data_t *additional_data, |
| 4411 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4412 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4413 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4414 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4415 | psa_key_type_t key_type = key_type_arg; |
| 4416 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4417 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4418 | unsigned char *output_data = NULL; |
| 4419 | size_t output_size = 0; |
| 4420 | size_t output_length = 0; |
| 4421 | unsigned char *output_data2 = NULL; |
| 4422 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4423 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4424 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4425 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4426 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4427 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4428 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4429 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4430 | psa_set_key_algorithm( &attributes, alg ); |
| 4431 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4432 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4433 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4434 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4435 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4436 | key_bits = psa_get_key_bits( &attributes ); |
| 4437 | |
| 4438 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4439 | alg ); |
| 4440 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4441 | * should be exact. */ |
| 4442 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4443 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4444 | { |
| 4445 | TEST_EQUAL( output_size, |
| 4446 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4447 | TEST_LE_U( output_size, |
| 4448 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4449 | } |
| 4450 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4451 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4452 | status = psa_aead_encrypt( key, alg, |
| 4453 | nonce->x, nonce->len, |
| 4454 | additional_data->x, |
| 4455 | additional_data->len, |
| 4456 | input_data->x, input_data->len, |
| 4457 | output_data, output_size, |
| 4458 | &output_length ); |
| 4459 | |
| 4460 | /* If the operation is not supported, just skip and not fail in case the |
| 4461 | * encryption involves a common limitation of cryptography hardwares and |
| 4462 | * an alternative implementation. */ |
| 4463 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4464 | { |
| 4465 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4466 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4467 | } |
| 4468 | |
| 4469 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4470 | |
| 4471 | if( PSA_SUCCESS == expected_result ) |
| 4472 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4473 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4474 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4475 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4476 | * should be exact. */ |
| 4477 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4478 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4479 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4480 | TEST_LE_U( input_data->len, |
| 4481 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4482 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4483 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4484 | nonce->x, nonce->len, |
| 4485 | additional_data->x, |
| 4486 | additional_data->len, |
| 4487 | output_data, output_length, |
| 4488 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4489 | &output_length2 ), |
| 4490 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4491 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4492 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4493 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4494 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4495 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4496 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4497 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4498 | mbedtls_free( output_data ); |
| 4499 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4500 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4501 | } |
| 4502 | /* END_CASE */ |
| 4503 | |
| 4504 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4505 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4506 | int alg_arg, |
| 4507 | data_t *nonce, |
| 4508 | data_t *additional_data, |
| 4509 | data_t *input_data, |
| 4510 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4511 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4512 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4513 | psa_key_type_t key_type = key_type_arg; |
| 4514 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4515 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4516 | unsigned char *output_data = NULL; |
| 4517 | size_t output_size = 0; |
| 4518 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4519 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4520 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4521 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4522 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4523 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4524 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4525 | psa_set_key_algorithm( &attributes, alg ); |
| 4526 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4527 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4528 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4529 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4530 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4531 | key_bits = psa_get_key_bits( &attributes ); |
| 4532 | |
| 4533 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4534 | alg ); |
| 4535 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4536 | * should be exact. */ |
| 4537 | TEST_EQUAL( output_size, |
| 4538 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4539 | TEST_LE_U( output_size, |
| 4540 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4541 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4542 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4543 | status = psa_aead_encrypt( key, alg, |
| 4544 | nonce->x, nonce->len, |
| 4545 | additional_data->x, additional_data->len, |
| 4546 | input_data->x, input_data->len, |
| 4547 | output_data, output_size, |
| 4548 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4549 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4550 | /* If the operation is not supported, just skip and not fail in case the |
| 4551 | * encryption involves a common limitation of cryptography hardwares and |
| 4552 | * an alternative implementation. */ |
| 4553 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4554 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4555 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4556 | 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] | 4557 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4558 | |
| 4559 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4560 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4561 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4562 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4563 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4564 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4565 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4566 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4567 | } |
| 4568 | /* END_CASE */ |
| 4569 | |
| 4570 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4571 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4572 | int alg_arg, |
| 4573 | data_t *nonce, |
| 4574 | data_t *additional_data, |
| 4575 | data_t *input_data, |
| 4576 | data_t *expected_data, |
| 4577 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4578 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4579 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4580 | psa_key_type_t key_type = key_type_arg; |
| 4581 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4582 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4583 | unsigned char *output_data = NULL; |
| 4584 | size_t output_size = 0; |
| 4585 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4586 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4587 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4588 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4589 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4590 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4591 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4592 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4593 | psa_set_key_algorithm( &attributes, alg ); |
| 4594 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4595 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4596 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4597 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4598 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4599 | key_bits = psa_get_key_bits( &attributes ); |
| 4600 | |
| 4601 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4602 | alg ); |
| 4603 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4604 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4605 | { |
| 4606 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4607 | * should be exact. */ |
| 4608 | TEST_EQUAL( output_size, |
| 4609 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4610 | TEST_LE_U( output_size, |
| 4611 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4612 | } |
| 4613 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4614 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4615 | status = psa_aead_decrypt( key, alg, |
| 4616 | nonce->x, nonce->len, |
| 4617 | additional_data->x, |
| 4618 | additional_data->len, |
| 4619 | input_data->x, input_data->len, |
| 4620 | output_data, output_size, |
| 4621 | &output_length ); |
| 4622 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4623 | /* If the operation is not supported, just skip and not fail in case the |
| 4624 | * decryption involves a common limitation of cryptography hardwares and |
| 4625 | * an alternative implementation. */ |
| 4626 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4627 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4628 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4629 | 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] | 4630 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4631 | |
| 4632 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4633 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4634 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4635 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4636 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4637 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4638 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4639 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4640 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4641 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4642 | } |
| 4643 | /* END_CASE */ |
| 4644 | |
| 4645 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4646 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4647 | int alg_arg, |
| 4648 | data_t *nonce, |
| 4649 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4650 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4651 | int do_set_lengths, |
| 4652 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4653 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4654 | size_t ad_part_len = 0; |
| 4655 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4656 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4657 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4658 | 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] | 4659 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4660 | mbedtls_test_set_step( ad_part_len ); |
| 4661 | |
| 4662 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4663 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4664 | if( ad_part_len & 0x01 ) |
| 4665 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4666 | else |
| 4667 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4668 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4669 | |
| 4670 | /* Split ad into length(ad_part_len) parts. */ |
| 4671 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4672 | alg_arg, nonce, |
| 4673 | additional_data, |
| 4674 | ad_part_len, |
| 4675 | input_data, -1, |
| 4676 | set_lengths_method, |
| 4677 | expected_output, |
| 4678 | 1, 0 ) ) |
| 4679 | break; |
| 4680 | |
| 4681 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4682 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4683 | |
| 4684 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4685 | alg_arg, nonce, |
| 4686 | additional_data, |
| 4687 | ad_part_len, |
| 4688 | input_data, -1, |
| 4689 | set_lengths_method, |
| 4690 | expected_output, |
| 4691 | 1, 1 ) ) |
| 4692 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4693 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4694 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4695 | 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] | 4696 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4697 | /* Split data into length(data_part_len) parts. */ |
| 4698 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 4699 | |
| 4700 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4701 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4702 | if( data_part_len & 0x01 ) |
| 4703 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4704 | else |
| 4705 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4706 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4707 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4708 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4709 | alg_arg, nonce, |
| 4710 | additional_data, -1, |
| 4711 | input_data, data_part_len, |
| 4712 | set_lengths_method, |
| 4713 | expected_output, |
| 4714 | 1, 0 ) ) |
| 4715 | break; |
| 4716 | |
| 4717 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 4718 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 4719 | |
| 4720 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4721 | alg_arg, nonce, |
| 4722 | additional_data, -1, |
| 4723 | input_data, data_part_len, |
| 4724 | set_lengths_method, |
| 4725 | expected_output, |
| 4726 | 1, 1 ) ) |
| 4727 | break; |
| 4728 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4729 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4730 | /* Goto is required to silence warnings about unused labels, as we |
| 4731 | * don't actually do any test assertions in this function. */ |
| 4732 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4733 | } |
| 4734 | /* END_CASE */ |
| 4735 | |
| 4736 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4737 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 4738 | int alg_arg, |
| 4739 | data_t *nonce, |
| 4740 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4741 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4742 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4743 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4744 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4745 | size_t ad_part_len = 0; |
| 4746 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4747 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4748 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4749 | 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] | 4750 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4751 | /* Split ad into length(ad_part_len) parts. */ |
| 4752 | mbedtls_test_set_step( ad_part_len ); |
| 4753 | |
| 4754 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4755 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4756 | if( ad_part_len & 0x01 ) |
| 4757 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4758 | else |
| 4759 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4760 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4761 | |
| 4762 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4763 | alg_arg, nonce, |
| 4764 | additional_data, |
| 4765 | ad_part_len, |
| 4766 | input_data, -1, |
| 4767 | set_lengths_method, |
| 4768 | expected_output, |
| 4769 | 0, 0 ) ) |
| 4770 | break; |
| 4771 | |
| 4772 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4773 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4774 | |
| 4775 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4776 | alg_arg, nonce, |
| 4777 | additional_data, |
| 4778 | ad_part_len, |
| 4779 | input_data, -1, |
| 4780 | set_lengths_method, |
| 4781 | expected_output, |
| 4782 | 0, 1 ) ) |
| 4783 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4784 | } |
| 4785 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4786 | 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] | 4787 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4788 | /* Split data into length(data_part_len) parts. */ |
| 4789 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 4790 | |
| 4791 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4792 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4793 | if( data_part_len & 0x01 ) |
| 4794 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4795 | else |
| 4796 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4797 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4798 | |
| 4799 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4800 | alg_arg, nonce, |
| 4801 | additional_data, -1, |
| 4802 | input_data, data_part_len, |
| 4803 | set_lengths_method, |
| 4804 | expected_output, |
| 4805 | 0, 0 ) ) |
| 4806 | break; |
| 4807 | |
| 4808 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 4809 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 4810 | |
| 4811 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4812 | alg_arg, nonce, |
| 4813 | additional_data, -1, |
| 4814 | input_data, data_part_len, |
| 4815 | set_lengths_method, |
| 4816 | expected_output, |
| 4817 | 0, 1 ) ) |
| 4818 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4819 | } |
| 4820 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4821 | /* Goto is required to silence warnings about unused labels, as we |
| 4822 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4823 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4824 | } |
| 4825 | /* END_CASE */ |
| 4826 | |
| 4827 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4828 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 4829 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4830 | int nonce_length, |
| 4831 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4832 | data_t *additional_data, |
| 4833 | data_t *input_data, |
| 4834 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4835 | { |
| 4836 | |
| 4837 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4838 | psa_key_type_t key_type = key_type_arg; |
| 4839 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4840 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4841 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 4842 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4843 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4844 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4845 | size_t actual_nonce_length = 0; |
| 4846 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 4847 | unsigned char *output = NULL; |
| 4848 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4849 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4850 | size_t ciphertext_size = 0; |
| 4851 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4852 | size_t tag_length = 0; |
| 4853 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4854 | |
| 4855 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4856 | |
| 4857 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4858 | psa_set_key_algorithm( & attributes, alg ); |
| 4859 | psa_set_key_type( & attributes, key_type ); |
| 4860 | |
| 4861 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4862 | &key ) ); |
| 4863 | |
| 4864 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4865 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4866 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4867 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4868 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4869 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4870 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4871 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4872 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4873 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4874 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4875 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4876 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4877 | |
| 4878 | /* If the operation is not supported, just skip and not fail in case the |
| 4879 | * encryption involves a common limitation of cryptography hardwares and |
| 4880 | * an alternative implementation. */ |
| 4881 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4882 | { |
| 4883 | 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] | 4884 | 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] | 4885 | } |
| 4886 | |
| 4887 | PSA_ASSERT( status ); |
| 4888 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4889 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4890 | nonce_length, |
| 4891 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4892 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4893 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4894 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4895 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 4896 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 4897 | if( expected_status == PSA_SUCCESS ) |
| 4898 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 4899 | alg ) ); |
| 4900 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4901 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 4902 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4903 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4904 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4905 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 4906 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4907 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4908 | |
| 4909 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4910 | additional_data->len ) ); |
| 4911 | |
| 4912 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4913 | output, output_size, |
| 4914 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4915 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4916 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 4917 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4918 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 4919 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4920 | |
| 4921 | exit: |
| 4922 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4923 | mbedtls_free( output ); |
| 4924 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4925 | psa_aead_abort( &operation ); |
| 4926 | PSA_DONE( ); |
| 4927 | } |
| 4928 | /* END_CASE */ |
| 4929 | |
| 4930 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4931 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 4932 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4933 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4934 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4935 | data_t *additional_data, |
| 4936 | data_t *input_data, |
| 4937 | int expected_status_arg ) |
| 4938 | { |
| 4939 | |
| 4940 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4941 | psa_key_type_t key_type = key_type_arg; |
| 4942 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4943 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4944 | uint8_t *nonce_buffer = NULL; |
| 4945 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4946 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4947 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4948 | unsigned char *output = NULL; |
| 4949 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4950 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4951 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4952 | size_t ciphertext_size = 0; |
| 4953 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4954 | size_t tag_length = 0; |
| 4955 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4956 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4957 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4958 | |
| 4959 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4960 | |
| 4961 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4962 | psa_set_key_algorithm( &attributes, alg ); |
| 4963 | psa_set_key_type( &attributes, key_type ); |
| 4964 | |
| 4965 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4966 | &key ) ); |
| 4967 | |
| 4968 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4969 | |
| 4970 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4971 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4972 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4973 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4974 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4975 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4976 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4977 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4978 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4979 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4980 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4981 | |
| 4982 | /* If the operation is not supported, just skip and not fail in case the |
| 4983 | * encryption involves a common limitation of cryptography hardwares and |
| 4984 | * an alternative implementation. */ |
| 4985 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4986 | { |
| 4987 | 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] | 4988 | 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] | 4989 | } |
| 4990 | |
| 4991 | PSA_ASSERT( status ); |
| 4992 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4993 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 4994 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4995 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 4996 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 4997 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4998 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 4999 | } |
| 5000 | else |
| 5001 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5002 | /* If length is zero, then this will return NULL. */ |
| 5003 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5004 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5005 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5006 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5007 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5008 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5009 | { |
| 5010 | nonce_buffer[index] = 'a' + index; |
| 5011 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5012 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5013 | } |
| 5014 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5015 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5016 | { |
| 5017 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5018 | input_data->len ) ); |
| 5019 | } |
| 5020 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5021 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5022 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5023 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5024 | |
| 5025 | if( expected_status == PSA_SUCCESS ) |
| 5026 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5027 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5028 | { |
| 5029 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5030 | input_data->len ) ); |
| 5031 | } |
| 5032 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5033 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5034 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5035 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5036 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5037 | additional_data->len ), |
| 5038 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5039 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5040 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5041 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5042 | &ciphertext_length ), |
| 5043 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5044 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5045 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5046 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5047 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5048 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | exit: |
| 5052 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5053 | mbedtls_free( output ); |
| 5054 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5055 | mbedtls_free( nonce_buffer ); |
| 5056 | psa_aead_abort( &operation ); |
| 5057 | PSA_DONE( ); |
| 5058 | } |
| 5059 | /* END_CASE */ |
| 5060 | |
| 5061 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5062 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5063 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5064 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5065 | data_t *nonce, |
| 5066 | data_t *additional_data, |
| 5067 | data_t *input_data, |
| 5068 | int expected_status_arg ) |
| 5069 | { |
| 5070 | |
| 5071 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5072 | psa_key_type_t key_type = key_type_arg; |
| 5073 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5074 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5075 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5076 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5077 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5078 | unsigned char *output = NULL; |
| 5079 | unsigned char *ciphertext = NULL; |
| 5080 | size_t output_size = output_size_arg; |
| 5081 | size_t ciphertext_size = 0; |
| 5082 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5083 | size_t tag_length = 0; |
| 5084 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5085 | |
| 5086 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5087 | |
| 5088 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5089 | psa_set_key_algorithm( &attributes, alg ); |
| 5090 | psa_set_key_type( &attributes, key_type ); |
| 5091 | |
| 5092 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5093 | &key ) ); |
| 5094 | |
| 5095 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5096 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5097 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5098 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5099 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5100 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5101 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5102 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5103 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5104 | |
| 5105 | /* If the operation is not supported, just skip and not fail in case the |
| 5106 | * encryption involves a common limitation of cryptography hardwares and |
| 5107 | * an alternative implementation. */ |
| 5108 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5109 | { |
| 5110 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5111 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5112 | } |
| 5113 | |
| 5114 | PSA_ASSERT( status ); |
| 5115 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5116 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5117 | input_data->len ) ); |
| 5118 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5119 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5120 | |
| 5121 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5122 | additional_data->len ) ); |
| 5123 | |
| 5124 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5125 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5126 | |
| 5127 | TEST_EQUAL( status, expected_status ); |
| 5128 | |
| 5129 | if( expected_status == PSA_SUCCESS ) |
| 5130 | { |
| 5131 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5132 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5133 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5134 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5135 | } |
| 5136 | |
| 5137 | exit: |
| 5138 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5139 | mbedtls_free( output ); |
| 5140 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5141 | psa_aead_abort( &operation ); |
| 5142 | PSA_DONE( ); |
| 5143 | } |
| 5144 | /* END_CASE */ |
| 5145 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5146 | /* BEGIN_CASE */ |
| 5147 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5148 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5149 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5150 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5151 | data_t *nonce, |
| 5152 | data_t *additional_data, |
| 5153 | data_t *input_data, |
| 5154 | int expected_status_arg ) |
| 5155 | { |
| 5156 | |
| 5157 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5158 | psa_key_type_t key_type = key_type_arg; |
| 5159 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5160 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5161 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5162 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5163 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5164 | unsigned char *ciphertext = NULL; |
| 5165 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5166 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5167 | size_t ciphertext_size = 0; |
| 5168 | size_t ciphertext_length = 0; |
| 5169 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5170 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5171 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5172 | |
| 5173 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5174 | |
| 5175 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5176 | psa_set_key_algorithm( &attributes, alg ); |
| 5177 | psa_set_key_type( &attributes, key_type ); |
| 5178 | |
| 5179 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5180 | &key ) ); |
| 5181 | |
| 5182 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5183 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5184 | 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] | 5185 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5186 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5187 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5188 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5189 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5190 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5191 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5192 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5193 | |
| 5194 | /* If the operation is not supported, just skip and not fail in case the |
| 5195 | * encryption involves a common limitation of cryptography hardwares and |
| 5196 | * an alternative implementation. */ |
| 5197 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5198 | { |
| 5199 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5200 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5201 | } |
| 5202 | |
| 5203 | PSA_ASSERT( status ); |
| 5204 | |
| 5205 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5206 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5207 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5208 | input_data->len ) ); |
| 5209 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5210 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5211 | additional_data->len ) ); |
| 5212 | |
| 5213 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5214 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5215 | |
| 5216 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5217 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5218 | finish_ciphertext_size, |
| 5219 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5220 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5221 | |
| 5222 | TEST_EQUAL( status, expected_status ); |
| 5223 | |
| 5224 | exit: |
| 5225 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5226 | mbedtls_free( ciphertext ); |
| 5227 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5228 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5229 | psa_aead_abort( &operation ); |
| 5230 | PSA_DONE( ); |
| 5231 | } |
| 5232 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5233 | |
| 5234 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5235 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5236 | int alg_arg, |
| 5237 | data_t *nonce, |
| 5238 | data_t *additional_data, |
| 5239 | data_t *input_data, |
| 5240 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5241 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5242 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5243 | int expected_status_arg ) |
| 5244 | { |
| 5245 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5246 | psa_key_type_t key_type = key_type_arg; |
| 5247 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5248 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5249 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5250 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5251 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5252 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5253 | unsigned char *plaintext = NULL; |
| 5254 | unsigned char *finish_plaintext = NULL; |
| 5255 | size_t plaintext_size = 0; |
| 5256 | size_t plaintext_length = 0; |
| 5257 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5258 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5259 | unsigned char *tag_buffer = NULL; |
| 5260 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5261 | |
| 5262 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5263 | |
| 5264 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5265 | psa_set_key_algorithm( &attributes, alg ); |
| 5266 | psa_set_key_type( &attributes, key_type ); |
| 5267 | |
| 5268 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5269 | &key ) ); |
| 5270 | |
| 5271 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5272 | |
| 5273 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5274 | input_data->len ); |
| 5275 | |
| 5276 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5277 | |
| 5278 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5279 | |
| 5280 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5281 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5282 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5283 | |
| 5284 | /* If the operation is not supported, just skip and not fail in case the |
| 5285 | * encryption involves a common limitation of cryptography hardwares and |
| 5286 | * an alternative implementation. */ |
| 5287 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5288 | { |
| 5289 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5290 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5291 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5292 | TEST_EQUAL( status, expected_setup_status ); |
| 5293 | |
| 5294 | if( status != PSA_SUCCESS ) |
| 5295 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5296 | |
| 5297 | PSA_ASSERT( status ); |
| 5298 | |
| 5299 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5300 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5301 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5302 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5303 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5304 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5305 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5306 | additional_data->len ) ); |
| 5307 | |
| 5308 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5309 | input_data->len, |
| 5310 | plaintext, plaintext_size, |
| 5311 | &plaintext_length ) ); |
| 5312 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5313 | if( tag_usage == USE_GIVEN_TAG ) |
| 5314 | { |
| 5315 | tag_buffer = tag->x; |
| 5316 | tag_size = tag->len; |
| 5317 | } |
| 5318 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5319 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5320 | verify_plaintext_size, |
| 5321 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5322 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5323 | |
| 5324 | TEST_EQUAL( status, expected_status ); |
| 5325 | |
| 5326 | exit: |
| 5327 | psa_destroy_key( key ); |
| 5328 | mbedtls_free( plaintext ); |
| 5329 | mbedtls_free( finish_plaintext ); |
| 5330 | psa_aead_abort( &operation ); |
| 5331 | PSA_DONE( ); |
| 5332 | } |
| 5333 | /* END_CASE */ |
| 5334 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5335 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5336 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5337 | int alg_arg, int expected_status_arg ) |
| 5338 | { |
| 5339 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5340 | psa_key_type_t key_type = key_type_arg; |
| 5341 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5342 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5343 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5344 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5345 | psa_status_t expected_status = expected_status_arg; |
| 5346 | |
| 5347 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5348 | |
| 5349 | psa_set_key_usage_flags( &attributes, |
| 5350 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5351 | psa_set_key_algorithm( &attributes, alg ); |
| 5352 | psa_set_key_type( &attributes, key_type ); |
| 5353 | |
| 5354 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5355 | &key ) ); |
| 5356 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5357 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5358 | |
| 5359 | TEST_EQUAL( status, expected_status ); |
| 5360 | |
| 5361 | psa_aead_abort( &operation ); |
| 5362 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5363 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5364 | |
| 5365 | TEST_EQUAL(status, expected_status ); |
| 5366 | |
| 5367 | exit: |
| 5368 | psa_destroy_key( key ); |
| 5369 | psa_aead_abort( &operation ); |
| 5370 | PSA_DONE( ); |
| 5371 | } |
| 5372 | /* END_CASE */ |
| 5373 | |
| 5374 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5375 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5376 | int alg_arg, |
| 5377 | data_t *nonce, |
| 5378 | data_t *additional_data, |
| 5379 | data_t *input_data ) |
| 5380 | { |
| 5381 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5382 | psa_key_type_t key_type = key_type_arg; |
| 5383 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5384 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5385 | unsigned char *output_data = NULL; |
| 5386 | unsigned char *final_data = NULL; |
| 5387 | size_t output_size = 0; |
| 5388 | size_t finish_output_size = 0; |
| 5389 | size_t output_length = 0; |
| 5390 | size_t key_bits = 0; |
| 5391 | size_t tag_length = 0; |
| 5392 | size_t tag_size = 0; |
| 5393 | size_t nonce_length = 0; |
| 5394 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5395 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5396 | size_t output_part_length = 0; |
| 5397 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5398 | |
| 5399 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5400 | |
| 5401 | psa_set_key_usage_flags( & attributes, |
| 5402 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5403 | psa_set_key_algorithm( & attributes, alg ); |
| 5404 | psa_set_key_type( & attributes, key_type ); |
| 5405 | |
| 5406 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5407 | &key ) ); |
| 5408 | |
| 5409 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5410 | key_bits = psa_get_key_bits( &attributes ); |
| 5411 | |
| 5412 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5413 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5414 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5415 | |
| 5416 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5417 | |
| 5418 | ASSERT_ALLOC( output_data, output_size ); |
| 5419 | |
| 5420 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5421 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5422 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5423 | |
| 5424 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5425 | |
| 5426 | /* Test all operations error without calling setup first. */ |
| 5427 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5428 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5429 | PSA_ERROR_BAD_STATE ); |
| 5430 | |
| 5431 | psa_aead_abort( &operation ); |
| 5432 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5433 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5434 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5435 | &nonce_length ), |
| 5436 | PSA_ERROR_BAD_STATE ); |
| 5437 | |
| 5438 | psa_aead_abort( &operation ); |
| 5439 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5440 | /* ------------------------------------------------------- */ |
| 5441 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5442 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5443 | input_data->len ), |
| 5444 | PSA_ERROR_BAD_STATE ); |
| 5445 | |
| 5446 | psa_aead_abort( &operation ); |
| 5447 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5448 | /* ------------------------------------------------------- */ |
| 5449 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5450 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5451 | additional_data->len ), |
| 5452 | PSA_ERROR_BAD_STATE ); |
| 5453 | |
| 5454 | psa_aead_abort( &operation ); |
| 5455 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5456 | /* ------------------------------------------------------- */ |
| 5457 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5458 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5459 | input_data->len, output_data, |
| 5460 | output_size, &output_length ), |
| 5461 | PSA_ERROR_BAD_STATE ); |
| 5462 | |
| 5463 | psa_aead_abort( &operation ); |
| 5464 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5465 | /* ------------------------------------------------------- */ |
| 5466 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5467 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5468 | finish_output_size, |
| 5469 | &output_part_length, |
| 5470 | tag_buffer, tag_length, |
| 5471 | &tag_size ), |
| 5472 | PSA_ERROR_BAD_STATE ); |
| 5473 | |
| 5474 | psa_aead_abort( &operation ); |
| 5475 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5476 | /* ------------------------------------------------------- */ |
| 5477 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5478 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5479 | finish_output_size, |
| 5480 | &output_part_length, |
| 5481 | tag_buffer, |
| 5482 | tag_length ), |
| 5483 | PSA_ERROR_BAD_STATE ); |
| 5484 | |
| 5485 | psa_aead_abort( &operation ); |
| 5486 | |
| 5487 | /* Test for double setups. */ |
| 5488 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5489 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5490 | |
| 5491 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5492 | PSA_ERROR_BAD_STATE ); |
| 5493 | |
| 5494 | psa_aead_abort( &operation ); |
| 5495 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5496 | /* ------------------------------------------------------- */ |
| 5497 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5498 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5499 | |
| 5500 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5501 | PSA_ERROR_BAD_STATE ); |
| 5502 | |
| 5503 | psa_aead_abort( &operation ); |
| 5504 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5505 | /* ------------------------------------------------------- */ |
| 5506 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5507 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5508 | |
| 5509 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5510 | PSA_ERROR_BAD_STATE ); |
| 5511 | |
| 5512 | psa_aead_abort( &operation ); |
| 5513 | |
| 5514 | /* ------------------------------------------------------- */ |
| 5515 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5516 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5517 | |
| 5518 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5519 | PSA_ERROR_BAD_STATE ); |
| 5520 | |
| 5521 | psa_aead_abort( &operation ); |
| 5522 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5523 | /* Test for not setting a nonce. */ |
| 5524 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5525 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5526 | |
| 5527 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5528 | additional_data->len ), |
| 5529 | PSA_ERROR_BAD_STATE ); |
| 5530 | |
| 5531 | psa_aead_abort( &operation ); |
| 5532 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5533 | /* ------------------------------------------------------- */ |
| 5534 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5535 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5536 | |
| 5537 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5538 | input_data->len, output_data, |
| 5539 | output_size, &output_length ), |
| 5540 | PSA_ERROR_BAD_STATE ); |
| 5541 | |
| 5542 | psa_aead_abort( &operation ); |
| 5543 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5544 | /* ------------------------------------------------------- */ |
| 5545 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5546 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5547 | |
| 5548 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5549 | finish_output_size, |
| 5550 | &output_part_length, |
| 5551 | tag_buffer, tag_length, |
| 5552 | &tag_size ), |
| 5553 | PSA_ERROR_BAD_STATE ); |
| 5554 | |
| 5555 | psa_aead_abort( &operation ); |
| 5556 | |
| 5557 | /* ------------------------------------------------------- */ |
| 5558 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5559 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5560 | |
| 5561 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5562 | finish_output_size, |
| 5563 | &output_part_length, |
| 5564 | tag_buffer, |
| 5565 | tag_length ), |
| 5566 | PSA_ERROR_BAD_STATE ); |
| 5567 | |
| 5568 | psa_aead_abort( &operation ); |
| 5569 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5570 | /* Test for double setting nonce. */ |
| 5571 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5572 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5573 | |
| 5574 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5575 | |
| 5576 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5577 | PSA_ERROR_BAD_STATE ); |
| 5578 | |
| 5579 | psa_aead_abort( &operation ); |
| 5580 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5581 | /* Test for double generating nonce. */ |
| 5582 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5583 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5584 | |
| 5585 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5586 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5587 | &nonce_length ) ); |
| 5588 | |
| 5589 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5590 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5591 | &nonce_length ), |
| 5592 | PSA_ERROR_BAD_STATE ); |
| 5593 | |
| 5594 | |
| 5595 | psa_aead_abort( &operation ); |
| 5596 | |
| 5597 | /* Test for generate nonce then set and vice versa */ |
| 5598 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5599 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5600 | |
| 5601 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5602 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5603 | &nonce_length ) ); |
| 5604 | |
| 5605 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5606 | PSA_ERROR_BAD_STATE ); |
| 5607 | |
| 5608 | psa_aead_abort( &operation ); |
| 5609 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5610 | /* Test for generating nonce after calling set lengths */ |
| 5611 | |
| 5612 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5613 | |
| 5614 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5615 | input_data->len ) ); |
| 5616 | |
| 5617 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5618 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5619 | &nonce_length ) ); |
| 5620 | |
| 5621 | psa_aead_abort( &operation ); |
| 5622 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5623 | /* 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] | 5624 | |
| 5625 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5626 | |
| 5627 | if( operation.alg == PSA_ALG_CCM ) |
| 5628 | { |
| 5629 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5630 | input_data->len ), |
| 5631 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5632 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5633 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5634 | &nonce_length ), |
| 5635 | PSA_ERROR_BAD_STATE ); |
| 5636 | } |
| 5637 | else |
| 5638 | { |
| 5639 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5640 | input_data->len ) ); |
| 5641 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5642 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5643 | &nonce_length ) ); |
| 5644 | } |
| 5645 | |
| 5646 | psa_aead_abort( &operation ); |
| 5647 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5648 | /* 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] | 5649 | #if SIZE_MAX > UINT32_MAX |
| 5650 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5651 | |
| 5652 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5653 | { |
| 5654 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5655 | input_data->len ), |
| 5656 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5657 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5658 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5659 | &nonce_length ), |
| 5660 | PSA_ERROR_BAD_STATE ); |
| 5661 | } |
| 5662 | else |
| 5663 | { |
| 5664 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5665 | input_data->len ) ); |
| 5666 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5667 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5668 | &nonce_length ) ); |
| 5669 | } |
| 5670 | |
| 5671 | psa_aead_abort( &operation ); |
| 5672 | #endif |
| 5673 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5674 | /* 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] | 5675 | |
| 5676 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5677 | |
| 5678 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5679 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5680 | &nonce_length ) ); |
| 5681 | |
| 5682 | if( operation.alg == PSA_ALG_CCM ) |
| 5683 | { |
| 5684 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5685 | input_data->len ), |
| 5686 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5687 | } |
| 5688 | else |
| 5689 | { |
| 5690 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5691 | input_data->len ) ); |
| 5692 | } |
| 5693 | |
| 5694 | psa_aead_abort( &operation ); |
| 5695 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5696 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5697 | /* Test for setting nonce after calling set lengths */ |
| 5698 | |
| 5699 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5700 | |
| 5701 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5702 | input_data->len ) ); |
| 5703 | |
| 5704 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5705 | |
| 5706 | psa_aead_abort( &operation ); |
| 5707 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5708 | /* 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] | 5709 | |
| 5710 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5711 | |
| 5712 | if( operation.alg == PSA_ALG_CCM ) |
| 5713 | { |
| 5714 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5715 | input_data->len ), |
| 5716 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5717 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5718 | PSA_ERROR_BAD_STATE ); |
| 5719 | } |
| 5720 | else |
| 5721 | { |
| 5722 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5723 | input_data->len ) ); |
| 5724 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5725 | } |
| 5726 | |
| 5727 | psa_aead_abort( &operation ); |
| 5728 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5729 | /* 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] | 5730 | #if SIZE_MAX > UINT32_MAX |
| 5731 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5732 | |
| 5733 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5734 | { |
| 5735 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5736 | input_data->len ), |
| 5737 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5738 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5739 | PSA_ERROR_BAD_STATE ); |
| 5740 | } |
| 5741 | else |
| 5742 | { |
| 5743 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5744 | input_data->len ) ); |
| 5745 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5746 | } |
| 5747 | |
| 5748 | psa_aead_abort( &operation ); |
| 5749 | #endif |
| 5750 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5751 | /* 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] | 5752 | |
| 5753 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5754 | |
| 5755 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5756 | |
| 5757 | if( operation.alg == PSA_ALG_CCM ) |
| 5758 | { |
| 5759 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5760 | input_data->len ), |
| 5761 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5762 | } |
| 5763 | else |
| 5764 | { |
| 5765 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5766 | input_data->len ) ); |
| 5767 | } |
| 5768 | |
| 5769 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5770 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5771 | /* 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] | 5772 | #if SIZE_MAX > UINT32_MAX |
| 5773 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5774 | |
| 5775 | if( operation.alg == PSA_ALG_GCM ) |
| 5776 | { |
| 5777 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5778 | SIZE_MAX ), |
| 5779 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5780 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5781 | PSA_ERROR_BAD_STATE ); |
| 5782 | } |
| 5783 | else if ( operation.alg != PSA_ALG_CCM ) |
| 5784 | { |
| 5785 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5786 | SIZE_MAX ) ); |
| 5787 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5788 | } |
| 5789 | |
| 5790 | psa_aead_abort( &operation ); |
| 5791 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5792 | /* 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] | 5793 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5794 | |
| 5795 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5796 | |
| 5797 | if( operation.alg == PSA_ALG_GCM ) |
| 5798 | { |
| 5799 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5800 | SIZE_MAX ), |
| 5801 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5802 | } |
| 5803 | else if ( operation.alg != PSA_ALG_CCM ) |
| 5804 | { |
| 5805 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5806 | SIZE_MAX ) ); |
| 5807 | } |
| 5808 | |
| 5809 | psa_aead_abort( &operation ); |
| 5810 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5811 | |
| 5812 | /* ------------------------------------------------------- */ |
| 5813 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5814 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5815 | |
| 5816 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5817 | |
| 5818 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5819 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5820 | &nonce_length ), |
| 5821 | PSA_ERROR_BAD_STATE ); |
| 5822 | |
| 5823 | psa_aead_abort( &operation ); |
| 5824 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 5825 | /* Test for generating nonce in decrypt setup. */ |
| 5826 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 5827 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5828 | |
| 5829 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5830 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5831 | &nonce_length ), |
| 5832 | PSA_ERROR_BAD_STATE ); |
| 5833 | |
| 5834 | psa_aead_abort( &operation ); |
| 5835 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5836 | /* Test for setting lengths twice. */ |
| 5837 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5838 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5839 | |
| 5840 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5841 | |
| 5842 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5843 | input_data->len ) ); |
| 5844 | |
| 5845 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5846 | input_data->len ), |
| 5847 | PSA_ERROR_BAD_STATE ); |
| 5848 | |
| 5849 | psa_aead_abort( &operation ); |
| 5850 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5851 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5852 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5853 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5854 | |
| 5855 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5856 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5857 | if( operation.alg == PSA_ALG_CCM ) |
| 5858 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5859 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5860 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5861 | additional_data->len ), |
| 5862 | PSA_ERROR_BAD_STATE ); |
| 5863 | } |
| 5864 | else |
| 5865 | { |
| 5866 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5867 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5868 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5869 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5870 | input_data->len ), |
| 5871 | PSA_ERROR_BAD_STATE ); |
| 5872 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5873 | psa_aead_abort( &operation ); |
| 5874 | |
| 5875 | /* ------------------------------------------------------- */ |
| 5876 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5877 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5878 | |
| 5879 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5880 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5881 | if( operation.alg == PSA_ALG_CCM ) |
| 5882 | { |
| 5883 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5884 | input_data->len, output_data, |
| 5885 | output_size, &output_length ), |
| 5886 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5887 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5888 | } |
| 5889 | else |
| 5890 | { |
| 5891 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5892 | input_data->len, output_data, |
| 5893 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5894 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5895 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5896 | input_data->len ), |
| 5897 | PSA_ERROR_BAD_STATE ); |
| 5898 | } |
| 5899 | psa_aead_abort( &operation ); |
| 5900 | |
| 5901 | /* ------------------------------------------------------- */ |
| 5902 | |
| 5903 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5904 | |
| 5905 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5906 | |
| 5907 | if( operation.alg == PSA_ALG_CCM ) |
| 5908 | { |
| 5909 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5910 | finish_output_size, |
| 5911 | &output_part_length, |
| 5912 | tag_buffer, tag_length, |
| 5913 | &tag_size ) ); |
| 5914 | } |
| 5915 | else |
| 5916 | { |
| 5917 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5918 | finish_output_size, |
| 5919 | &output_part_length, |
| 5920 | tag_buffer, tag_length, |
| 5921 | &tag_size ) ); |
| 5922 | |
| 5923 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5924 | input_data->len ), |
| 5925 | PSA_ERROR_BAD_STATE ); |
| 5926 | } |
| 5927 | psa_aead_abort( &operation ); |
| 5928 | |
| 5929 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 5930 | |
| 5931 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5932 | |
| 5933 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5934 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5935 | &nonce_length ) ); |
| 5936 | if( operation.alg == PSA_ALG_CCM ) |
| 5937 | { |
| 5938 | |
| 5939 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5940 | additional_data->len ), |
| 5941 | PSA_ERROR_BAD_STATE ); |
| 5942 | } |
| 5943 | else |
| 5944 | { |
| 5945 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5946 | additional_data->len ) ); |
| 5947 | |
| 5948 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5949 | input_data->len ), |
| 5950 | PSA_ERROR_BAD_STATE ); |
| 5951 | } |
| 5952 | psa_aead_abort( &operation ); |
| 5953 | |
| 5954 | /* ------------------------------------------------------- */ |
| 5955 | |
| 5956 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5957 | |
| 5958 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5959 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5960 | &nonce_length ) ); |
| 5961 | if( operation.alg == PSA_ALG_CCM ) |
| 5962 | { |
| 5963 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5964 | input_data->len, output_data, |
| 5965 | output_size, &output_length ), |
| 5966 | PSA_ERROR_BAD_STATE ); |
| 5967 | |
| 5968 | } |
| 5969 | else |
| 5970 | { |
| 5971 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5972 | input_data->len, output_data, |
| 5973 | output_size, &output_length ) ); |
| 5974 | |
| 5975 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5976 | input_data->len ), |
| 5977 | PSA_ERROR_BAD_STATE ); |
| 5978 | } |
| 5979 | psa_aead_abort( &operation ); |
| 5980 | |
| 5981 | /* ------------------------------------------------------- */ |
| 5982 | |
| 5983 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5984 | |
| 5985 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5986 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5987 | &nonce_length ) ); |
| 5988 | if( operation.alg == PSA_ALG_CCM ) |
| 5989 | { |
| 5990 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5991 | finish_output_size, |
| 5992 | &output_part_length, |
| 5993 | tag_buffer, tag_length, |
| 5994 | &tag_size ) ); |
| 5995 | } |
| 5996 | else |
| 5997 | { |
| 5998 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5999 | finish_output_size, |
| 6000 | &output_part_length, |
| 6001 | tag_buffer, tag_length, |
| 6002 | &tag_size ) ); |
| 6003 | |
| 6004 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6005 | input_data->len ), |
| 6006 | PSA_ERROR_BAD_STATE ); |
| 6007 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6008 | psa_aead_abort( &operation ); |
| 6009 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6010 | /* Test for not sending any additional data or data after setting non zero |
| 6011 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6012 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6013 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6014 | |
| 6015 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6016 | |
| 6017 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6018 | input_data->len ) ); |
| 6019 | |
| 6020 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6021 | finish_output_size, |
| 6022 | &output_part_length, |
| 6023 | tag_buffer, tag_length, |
| 6024 | &tag_size ), |
| 6025 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6026 | |
| 6027 | psa_aead_abort( &operation ); |
| 6028 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6029 | /* Test for not sending any additional data or data after setting non-zero |
| 6030 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6031 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6032 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6033 | |
| 6034 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6035 | |
| 6036 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6037 | input_data->len ) ); |
| 6038 | |
| 6039 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6040 | finish_output_size, |
| 6041 | &output_part_length, |
| 6042 | tag_buffer, |
| 6043 | tag_length ), |
| 6044 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6045 | |
| 6046 | psa_aead_abort( &operation ); |
| 6047 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6048 | /* Test for not sending any additional data after setting a non-zero length |
| 6049 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6050 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6051 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6052 | |
| 6053 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6054 | |
| 6055 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6056 | input_data->len ) ); |
| 6057 | |
| 6058 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6059 | input_data->len, output_data, |
| 6060 | output_size, &output_length ), |
| 6061 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6062 | |
| 6063 | psa_aead_abort( &operation ); |
| 6064 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6065 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6066 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6067 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6068 | |
| 6069 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6070 | |
| 6071 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6072 | input_data->len ) ); |
| 6073 | |
| 6074 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6075 | additional_data->len ) ); |
| 6076 | |
| 6077 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6078 | finish_output_size, |
| 6079 | &output_part_length, |
| 6080 | tag_buffer, tag_length, |
| 6081 | &tag_size ), |
| 6082 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6083 | |
| 6084 | psa_aead_abort( &operation ); |
| 6085 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6086 | /* Test for sending too much additional data after setting lengths. */ |
| 6087 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6088 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6089 | |
| 6090 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6091 | |
| 6092 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6093 | |
| 6094 | |
| 6095 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6096 | additional_data->len ), |
| 6097 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6098 | |
| 6099 | psa_aead_abort( &operation ); |
| 6100 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6101 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6102 | |
| 6103 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6104 | |
| 6105 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6106 | |
| 6107 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6108 | input_data->len ) ); |
| 6109 | |
| 6110 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6111 | additional_data->len ) ); |
| 6112 | |
| 6113 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6114 | 1 ), |
| 6115 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6116 | |
| 6117 | psa_aead_abort( &operation ); |
| 6118 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6119 | /* Test for sending too much data after setting lengths. */ |
| 6120 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6121 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6122 | |
| 6123 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6124 | |
| 6125 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6126 | |
| 6127 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6128 | input_data->len, output_data, |
| 6129 | output_size, &output_length ), |
| 6130 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6131 | |
| 6132 | psa_aead_abort( &operation ); |
| 6133 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6134 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6135 | |
| 6136 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6137 | |
| 6138 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6139 | |
| 6140 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6141 | input_data->len ) ); |
| 6142 | |
| 6143 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6144 | additional_data->len ) ); |
| 6145 | |
| 6146 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6147 | input_data->len, output_data, |
| 6148 | output_size, &output_length ) ); |
| 6149 | |
| 6150 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6151 | 1, output_data, |
| 6152 | output_size, &output_length ), |
| 6153 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6154 | |
| 6155 | psa_aead_abort( &operation ); |
| 6156 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6157 | /* Test sending additional data after data. */ |
| 6158 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6159 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6160 | |
| 6161 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6162 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6163 | if( operation.alg != PSA_ALG_CCM ) |
| 6164 | { |
| 6165 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6166 | input_data->len, output_data, |
| 6167 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6168 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6169 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6170 | additional_data->len ), |
| 6171 | PSA_ERROR_BAD_STATE ); |
| 6172 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6173 | psa_aead_abort( &operation ); |
| 6174 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6175 | /* Test calling finish on decryption. */ |
| 6176 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6177 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6178 | |
| 6179 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6180 | |
| 6181 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6182 | finish_output_size, |
| 6183 | &output_part_length, |
| 6184 | tag_buffer, tag_length, |
| 6185 | &tag_size ), |
| 6186 | PSA_ERROR_BAD_STATE ); |
| 6187 | |
| 6188 | psa_aead_abort( &operation ); |
| 6189 | |
| 6190 | /* Test calling verify on encryption. */ |
| 6191 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6192 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6193 | |
| 6194 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6195 | |
| 6196 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6197 | finish_output_size, |
| 6198 | &output_part_length, |
| 6199 | tag_buffer, |
| 6200 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6201 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6202 | |
| 6203 | psa_aead_abort( &operation ); |
| 6204 | |
| 6205 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6206 | exit: |
| 6207 | psa_destroy_key( key ); |
| 6208 | psa_aead_abort( &operation ); |
| 6209 | mbedtls_free( output_data ); |
| 6210 | mbedtls_free( final_data ); |
| 6211 | PSA_DONE( ); |
| 6212 | } |
| 6213 | /* END_CASE */ |
| 6214 | |
| 6215 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6216 | void signature_size( int type_arg, |
| 6217 | int bits, |
| 6218 | int alg_arg, |
| 6219 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6220 | { |
| 6221 | psa_key_type_t type = type_arg; |
| 6222 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6223 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6224 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6225 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6226 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6227 | exit: |
| 6228 | ; |
| 6229 | } |
| 6230 | /* END_CASE */ |
| 6231 | |
| 6232 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6233 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6234 | int alg_arg, data_t *input_data, |
| 6235 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6236 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6237 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6238 | psa_key_type_t key_type = key_type_arg; |
| 6239 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6240 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6241 | unsigned char *signature = NULL; |
| 6242 | size_t signature_size; |
| 6243 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6244 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6245 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6246 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6247 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6248 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6249 | psa_set_key_algorithm( &attributes, alg ); |
| 6250 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6251 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6252 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6253 | &key ) ); |
| 6254 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6255 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6256 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6257 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6258 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6259 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6260 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6261 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6262 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6263 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6264 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6265 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6266 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6267 | input_data->x, input_data->len, |
| 6268 | signature, signature_size, |
| 6269 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6270 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6271 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6272 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6273 | |
| 6274 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6275 | /* |
| 6276 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6277 | * thus reset them as required. |
| 6278 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6279 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6280 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6281 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6282 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6283 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6284 | } |
| 6285 | /* END_CASE */ |
| 6286 | |
| 6287 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6288 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6289 | int alg_arg, data_t *input_data, |
| 6290 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6291 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6292 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6293 | psa_key_type_t key_type = key_type_arg; |
| 6294 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6295 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6296 | psa_status_t actual_status; |
| 6297 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6298 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6299 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6300 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6301 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6302 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6303 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6304 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6305 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6306 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6307 | psa_set_key_algorithm( &attributes, alg ); |
| 6308 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6309 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6310 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6311 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6312 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6313 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6314 | input_data->x, input_data->len, |
| 6315 | signature, signature_size, |
| 6316 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6317 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6318 | /* The value of *signature_length is unspecified on error, but |
| 6319 | * whatever it is, it should be less than signature_size, so that |
| 6320 | * if the caller tries to read *signature_length bytes without |
| 6321 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6322 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6323 | |
| 6324 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6325 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6326 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6327 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6328 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6329 | } |
| 6330 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6331 | |
| 6332 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6333 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6334 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6335 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6336 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6337 | psa_key_type_t key_type = key_type_arg; |
| 6338 | psa_algorithm_t alg = alg_arg; |
| 6339 | size_t key_bits; |
| 6340 | unsigned char *signature = NULL; |
| 6341 | size_t signature_size; |
| 6342 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6343 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6344 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6345 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6346 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6347 | 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] | 6348 | psa_set_key_algorithm( &attributes, alg ); |
| 6349 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6350 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6351 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6352 | &key ) ); |
| 6353 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6354 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6355 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6356 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6357 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6358 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6359 | key_bits, alg ); |
| 6360 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6361 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6362 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6363 | |
| 6364 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6365 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6366 | input_data->x, input_data->len, |
| 6367 | signature, signature_size, |
| 6368 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6369 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6370 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6371 | TEST_ASSERT( signature_length > 0 ); |
| 6372 | |
| 6373 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6374 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6375 | input_data->x, input_data->len, |
| 6376 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6377 | |
| 6378 | if( input_data->len != 0 ) |
| 6379 | { |
| 6380 | /* Flip a bit in the input and verify that the signature is now |
| 6381 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6382 | * because ECDSA may ignore the last few bits of the input. */ |
| 6383 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6384 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6385 | input_data->x, input_data->len, |
| 6386 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6387 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6388 | } |
| 6389 | |
| 6390 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6391 | /* |
| 6392 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6393 | * thus reset them as required. |
| 6394 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6395 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6396 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6397 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6398 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6399 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6400 | } |
| 6401 | /* END_CASE */ |
| 6402 | |
| 6403 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6404 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6405 | int alg_arg, data_t *hash_data, |
| 6406 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6407 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6408 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6409 | psa_key_type_t key_type = key_type_arg; |
| 6410 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6411 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6412 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6413 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6414 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6415 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6416 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6417 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6418 | psa_set_key_algorithm( &attributes, alg ); |
| 6419 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6420 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6421 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6422 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6423 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6424 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6425 | hash_data->x, hash_data->len, |
| 6426 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6427 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6428 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6429 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6430 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6431 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6432 | } |
| 6433 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6434 | |
| 6435 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6436 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6437 | int alg_arg, data_t *hash_data, |
| 6438 | data_t *signature_data, |
| 6439 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6440 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6441 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6442 | psa_key_type_t key_type = key_type_arg; |
| 6443 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6444 | psa_status_t actual_status; |
| 6445 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6446 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6447 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6448 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6449 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6450 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6451 | psa_set_key_algorithm( &attributes, alg ); |
| 6452 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6453 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6454 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6455 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6456 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6457 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6458 | hash_data->x, hash_data->len, |
| 6459 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6460 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6461 | |
| 6462 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6463 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6464 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6465 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6466 | } |
| 6467 | /* END_CASE */ |
| 6468 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6469 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6470 | void sign_message_deterministic( int key_type_arg, |
| 6471 | data_t *key_data, |
| 6472 | int alg_arg, |
| 6473 | data_t *input_data, |
| 6474 | data_t *output_data ) |
| 6475 | { |
| 6476 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6477 | psa_key_type_t key_type = key_type_arg; |
| 6478 | psa_algorithm_t alg = alg_arg; |
| 6479 | size_t key_bits; |
| 6480 | unsigned char *signature = NULL; |
| 6481 | size_t signature_size; |
| 6482 | size_t signature_length = 0xdeadbeef; |
| 6483 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6484 | |
| 6485 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6486 | |
| 6487 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6488 | psa_set_key_algorithm( &attributes, alg ); |
| 6489 | psa_set_key_type( &attributes, key_type ); |
| 6490 | |
| 6491 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6492 | &key ) ); |
| 6493 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6494 | key_bits = psa_get_key_bits( &attributes ); |
| 6495 | |
| 6496 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6497 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6498 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6499 | ASSERT_ALLOC( signature, signature_size ); |
| 6500 | |
| 6501 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6502 | input_data->x, input_data->len, |
| 6503 | signature, signature_size, |
| 6504 | &signature_length ) ); |
| 6505 | |
| 6506 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6507 | signature, signature_length ); |
| 6508 | |
| 6509 | exit: |
| 6510 | psa_reset_key_attributes( &attributes ); |
| 6511 | |
| 6512 | psa_destroy_key( key ); |
| 6513 | mbedtls_free( signature ); |
| 6514 | PSA_DONE( ); |
| 6515 | |
| 6516 | } |
| 6517 | /* END_CASE */ |
| 6518 | |
| 6519 | /* BEGIN_CASE */ |
| 6520 | void sign_message_fail( int key_type_arg, |
| 6521 | data_t *key_data, |
| 6522 | int alg_arg, |
| 6523 | data_t *input_data, |
| 6524 | int signature_size_arg, |
| 6525 | int expected_status_arg ) |
| 6526 | { |
| 6527 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6528 | psa_key_type_t key_type = key_type_arg; |
| 6529 | psa_algorithm_t alg = alg_arg; |
| 6530 | size_t signature_size = signature_size_arg; |
| 6531 | psa_status_t actual_status; |
| 6532 | psa_status_t expected_status = expected_status_arg; |
| 6533 | unsigned char *signature = NULL; |
| 6534 | size_t signature_length = 0xdeadbeef; |
| 6535 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6536 | |
| 6537 | ASSERT_ALLOC( signature, signature_size ); |
| 6538 | |
| 6539 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6540 | |
| 6541 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6542 | psa_set_key_algorithm( &attributes, alg ); |
| 6543 | psa_set_key_type( &attributes, key_type ); |
| 6544 | |
| 6545 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6546 | &key ) ); |
| 6547 | |
| 6548 | actual_status = psa_sign_message( key, alg, |
| 6549 | input_data->x, input_data->len, |
| 6550 | signature, signature_size, |
| 6551 | &signature_length ); |
| 6552 | TEST_EQUAL( actual_status, expected_status ); |
| 6553 | /* The value of *signature_length is unspecified on error, but |
| 6554 | * whatever it is, it should be less than signature_size, so that |
| 6555 | * if the caller tries to read *signature_length bytes without |
| 6556 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6557 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6558 | |
| 6559 | exit: |
| 6560 | psa_reset_key_attributes( &attributes ); |
| 6561 | psa_destroy_key( key ); |
| 6562 | mbedtls_free( signature ); |
| 6563 | PSA_DONE( ); |
| 6564 | } |
| 6565 | /* END_CASE */ |
| 6566 | |
| 6567 | /* BEGIN_CASE */ |
| 6568 | void sign_verify_message( int key_type_arg, |
| 6569 | data_t *key_data, |
| 6570 | int alg_arg, |
| 6571 | data_t *input_data ) |
| 6572 | { |
| 6573 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6574 | psa_key_type_t key_type = key_type_arg; |
| 6575 | psa_algorithm_t alg = alg_arg; |
| 6576 | size_t key_bits; |
| 6577 | unsigned char *signature = NULL; |
| 6578 | size_t signature_size; |
| 6579 | size_t signature_length = 0xdeadbeef; |
| 6580 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6581 | |
| 6582 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6583 | |
| 6584 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6585 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6586 | psa_set_key_algorithm( &attributes, alg ); |
| 6587 | psa_set_key_type( &attributes, key_type ); |
| 6588 | |
| 6589 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6590 | &key ) ); |
| 6591 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6592 | key_bits = psa_get_key_bits( &attributes ); |
| 6593 | |
| 6594 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6595 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6596 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6597 | ASSERT_ALLOC( signature, signature_size ); |
| 6598 | |
| 6599 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6600 | input_data->x, input_data->len, |
| 6601 | signature, signature_size, |
| 6602 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6603 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6604 | TEST_ASSERT( signature_length > 0 ); |
| 6605 | |
| 6606 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6607 | input_data->x, input_data->len, |
| 6608 | signature, signature_length ) ); |
| 6609 | |
| 6610 | if( input_data->len != 0 ) |
| 6611 | { |
| 6612 | /* Flip a bit in the input and verify that the signature is now |
| 6613 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6614 | * because ECDSA may ignore the last few bits of the input. */ |
| 6615 | input_data->x[0] ^= 1; |
| 6616 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6617 | input_data->x, input_data->len, |
| 6618 | signature, signature_length ), |
| 6619 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6620 | } |
| 6621 | |
| 6622 | exit: |
| 6623 | psa_reset_key_attributes( &attributes ); |
| 6624 | |
| 6625 | psa_destroy_key( key ); |
| 6626 | mbedtls_free( signature ); |
| 6627 | PSA_DONE( ); |
| 6628 | } |
| 6629 | /* END_CASE */ |
| 6630 | |
| 6631 | /* BEGIN_CASE */ |
| 6632 | void verify_message( int key_type_arg, |
| 6633 | data_t *key_data, |
| 6634 | int alg_arg, |
| 6635 | data_t *input_data, |
| 6636 | data_t *signature_data ) |
| 6637 | { |
| 6638 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6639 | psa_key_type_t key_type = key_type_arg; |
| 6640 | psa_algorithm_t alg = alg_arg; |
| 6641 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6642 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6643 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6644 | |
| 6645 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6646 | |
| 6647 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6648 | psa_set_key_algorithm( &attributes, alg ); |
| 6649 | psa_set_key_type( &attributes, key_type ); |
| 6650 | |
| 6651 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6652 | &key ) ); |
| 6653 | |
| 6654 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6655 | input_data->x, input_data->len, |
| 6656 | signature_data->x, signature_data->len ) ); |
| 6657 | |
| 6658 | exit: |
| 6659 | psa_reset_key_attributes( &attributes ); |
| 6660 | psa_destroy_key( key ); |
| 6661 | PSA_DONE( ); |
| 6662 | } |
| 6663 | /* END_CASE */ |
| 6664 | |
| 6665 | /* BEGIN_CASE */ |
| 6666 | void verify_message_fail( int key_type_arg, |
| 6667 | data_t *key_data, |
| 6668 | int alg_arg, |
| 6669 | data_t *hash_data, |
| 6670 | data_t *signature_data, |
| 6671 | int expected_status_arg ) |
| 6672 | { |
| 6673 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6674 | psa_key_type_t key_type = key_type_arg; |
| 6675 | psa_algorithm_t alg = alg_arg; |
| 6676 | psa_status_t actual_status; |
| 6677 | psa_status_t expected_status = expected_status_arg; |
| 6678 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6679 | |
| 6680 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6681 | |
| 6682 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6683 | psa_set_key_algorithm( &attributes, alg ); |
| 6684 | psa_set_key_type( &attributes, key_type ); |
| 6685 | |
| 6686 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6687 | &key ) ); |
| 6688 | |
| 6689 | actual_status = psa_verify_message( key, alg, |
| 6690 | hash_data->x, hash_data->len, |
| 6691 | signature_data->x, |
| 6692 | signature_data->len ); |
| 6693 | TEST_EQUAL( actual_status, expected_status ); |
| 6694 | |
| 6695 | exit: |
| 6696 | psa_reset_key_attributes( &attributes ); |
| 6697 | psa_destroy_key( key ); |
| 6698 | PSA_DONE( ); |
| 6699 | } |
| 6700 | /* END_CASE */ |
| 6701 | |
| 6702 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6703 | void asymmetric_encrypt( int key_type_arg, |
| 6704 | data_t *key_data, |
| 6705 | int alg_arg, |
| 6706 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6707 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6708 | int expected_output_length_arg, |
| 6709 | int expected_status_arg ) |
| 6710 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6711 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6712 | psa_key_type_t key_type = key_type_arg; |
| 6713 | psa_algorithm_t alg = alg_arg; |
| 6714 | size_t expected_output_length = expected_output_length_arg; |
| 6715 | size_t key_bits; |
| 6716 | unsigned char *output = NULL; |
| 6717 | size_t output_size; |
| 6718 | size_t output_length = ~0; |
| 6719 | psa_status_t actual_status; |
| 6720 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6721 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6722 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6723 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 6724 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6725 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6726 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 6727 | psa_set_key_algorithm( &attributes, alg ); |
| 6728 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6729 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6730 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6731 | |
| 6732 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6733 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6734 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6735 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6736 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6737 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6738 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6739 | |
| 6740 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6741 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6742 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6743 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6744 | output, output_size, |
| 6745 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6746 | TEST_EQUAL( actual_status, expected_status ); |
| 6747 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6748 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6749 | /* If the label is empty, the test framework puts a non-null pointer |
| 6750 | * in label->x. Test that a null pointer works as well. */ |
| 6751 | if( label->len == 0 ) |
| 6752 | { |
| 6753 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6754 | if( output_size != 0 ) |
| 6755 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6756 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6757 | input_data->x, input_data->len, |
| 6758 | NULL, label->len, |
| 6759 | output, output_size, |
| 6760 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6761 | TEST_EQUAL( actual_status, expected_status ); |
| 6762 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6763 | } |
| 6764 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6765 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6766 | /* |
| 6767 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6768 | * thus reset them as required. |
| 6769 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6770 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6771 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6772 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6773 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6774 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6775 | } |
| 6776 | /* END_CASE */ |
| 6777 | |
| 6778 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6779 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 6780 | data_t *key_data, |
| 6781 | int alg_arg, |
| 6782 | data_t *input_data, |
| 6783 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6784 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6785 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6786 | psa_key_type_t key_type = key_type_arg; |
| 6787 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6788 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6789 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6790 | size_t output_size; |
| 6791 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 6792 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6793 | size_t output2_size; |
| 6794 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6795 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6796 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6797 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6798 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6799 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 6800 | psa_set_key_algorithm( &attributes, alg ); |
| 6801 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6802 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6803 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6804 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6805 | |
| 6806 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6807 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6808 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6809 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6810 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6811 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6812 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6813 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6814 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6815 | TEST_LE_U( output2_size, |
| 6816 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 6817 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6818 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6819 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 6820 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 6821 | * the original plaintext because of the non-optional random |
| 6822 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6823 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6824 | input_data->x, input_data->len, |
| 6825 | label->x, label->len, |
| 6826 | output, output_size, |
| 6827 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6828 | /* We don't know what ciphertext length to expect, but check that |
| 6829 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6830 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 6831 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6832 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6833 | output, output_length, |
| 6834 | label->x, label->len, |
| 6835 | output2, output2_size, |
| 6836 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6837 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 6838 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6839 | |
| 6840 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6841 | /* |
| 6842 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6843 | * thus reset them as required. |
| 6844 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6845 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6846 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6847 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 6848 | mbedtls_free( output ); |
| 6849 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6850 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6851 | } |
| 6852 | /* END_CASE */ |
| 6853 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6854 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6855 | void asymmetric_decrypt( int key_type_arg, |
| 6856 | data_t *key_data, |
| 6857 | int alg_arg, |
| 6858 | data_t *input_data, |
| 6859 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 6860 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6861 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6862 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6863 | psa_key_type_t key_type = key_type_arg; |
| 6864 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6865 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6866 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 6867 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6868 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6869 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6870 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6871 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6872 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6873 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 6874 | psa_set_key_algorithm( &attributes, alg ); |
| 6875 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6876 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6877 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6878 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6879 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6880 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6881 | key_bits = psa_get_key_bits( &attributes ); |
| 6882 | |
| 6883 | /* Determine the maximum ciphertext length */ |
| 6884 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6885 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6886 | ASSERT_ALLOC( output, output_size ); |
| 6887 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6888 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6889 | input_data->x, input_data->len, |
| 6890 | label->x, label->len, |
| 6891 | output, |
| 6892 | output_size, |
| 6893 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6894 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 6895 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6896 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6897 | /* If the label is empty, the test framework puts a non-null pointer |
| 6898 | * in label->x. Test that a null pointer works as well. */ |
| 6899 | if( label->len == 0 ) |
| 6900 | { |
| 6901 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6902 | if( output_size != 0 ) |
| 6903 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6904 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6905 | input_data->x, input_data->len, |
| 6906 | NULL, label->len, |
| 6907 | output, |
| 6908 | output_size, |
| 6909 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6910 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 6911 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6912 | } |
| 6913 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6914 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6915 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6916 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 6917 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6918 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6919 | } |
| 6920 | /* END_CASE */ |
| 6921 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6922 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6923 | void asymmetric_decrypt_fail( int key_type_arg, |
| 6924 | data_t *key_data, |
| 6925 | int alg_arg, |
| 6926 | data_t *input_data, |
| 6927 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 6928 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6929 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6930 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6931 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6932 | psa_key_type_t key_type = key_type_arg; |
| 6933 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6934 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 6935 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6936 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6937 | psa_status_t actual_status; |
| 6938 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6939 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6940 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6941 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 6942 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6943 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6944 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6945 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 6946 | psa_set_key_algorithm( &attributes, alg ); |
| 6947 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6948 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6949 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6950 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6951 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6952 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 6953 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6954 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 6955 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6956 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6957 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6958 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6959 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6960 | /* If the label is empty, the test framework puts a non-null pointer |
| 6961 | * in label->x. Test that a null pointer works as well. */ |
| 6962 | if( label->len == 0 ) |
| 6963 | { |
| 6964 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6965 | if( output_size != 0 ) |
| 6966 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6967 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6968 | input_data->x, input_data->len, |
| 6969 | NULL, label->len, |
| 6970 | output, output_size, |
| 6971 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6972 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6973 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6974 | } |
| 6975 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6976 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6977 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6978 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6979 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6980 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6981 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 6982 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6983 | |
| 6984 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 6985 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 6986 | { |
| 6987 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 6988 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 6989 | * 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] | 6990 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 6991 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6992 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 6993 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6994 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 6995 | |
| 6996 | memset( &zero, 0, sizeof( zero ) ); |
| 6997 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6998 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6999 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7000 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7001 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7002 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7003 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7004 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7005 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7006 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7007 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7008 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7009 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7010 | } |
| 7011 | /* END_CASE */ |
| 7012 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7013 | /* BEGIN_CASE */ |
| 7014 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7015 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7016 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7017 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7018 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7019 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7020 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7021 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7022 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7023 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7024 | |
| 7025 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7026 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7027 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7028 | } |
| 7029 | /* END_CASE */ |
| 7030 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7031 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7032 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7033 | int expected_status_arg ) |
| 7034 | { |
| 7035 | psa_algorithm_t alg = alg_arg; |
| 7036 | size_t capacity = capacity_arg; |
| 7037 | psa_status_t expected_status = expected_status_arg; |
| 7038 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7039 | |
| 7040 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7041 | |
| 7042 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7043 | |
| 7044 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7045 | expected_status ); |
| 7046 | |
| 7047 | exit: |
| 7048 | psa_key_derivation_abort( &operation ); |
| 7049 | PSA_DONE( ); |
| 7050 | } |
| 7051 | /* END_CASE */ |
| 7052 | |
| 7053 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7054 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7055 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7056 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7057 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7058 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7059 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7060 | int expected_status_arg3, |
| 7061 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7062 | { |
| 7063 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7064 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7065 | 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] | 7066 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7067 | expected_status_arg2, |
| 7068 | expected_status_arg3}; |
| 7069 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7070 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7071 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7072 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7073 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7074 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7075 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7076 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7077 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7078 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7079 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7080 | |
| 7081 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7082 | |
| 7083 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7084 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7085 | |
| 7086 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7087 | |
| 7088 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7089 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7090 | mbedtls_test_set_step( i ); |
| 7091 | if( steps[i] == 0 ) |
| 7092 | { |
| 7093 | /* Skip this step */ |
| 7094 | } |
| 7095 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7096 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7097 | psa_set_key_type( &attributes, key_types[i] ); |
| 7098 | PSA_ASSERT( psa_import_key( &attributes, |
| 7099 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7100 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7101 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7102 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7103 | { |
| 7104 | // When taking a private key as secret input, use key agreement |
| 7105 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7106 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7107 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7108 | expected_statuses[i] ); |
| 7109 | } |
| 7110 | else |
| 7111 | { |
| 7112 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7113 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7114 | expected_statuses[i] ); |
| 7115 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7116 | } |
| 7117 | else |
| 7118 | { |
| 7119 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7120 | &operation, steps[i], |
| 7121 | inputs[i]->x, inputs[i]->len ), |
| 7122 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7123 | } |
| 7124 | } |
| 7125 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7126 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7127 | { |
| 7128 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7129 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7130 | psa_set_key_bits( &attributes, 8 ); |
| 7131 | actual_output_status = |
| 7132 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7133 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7134 | } |
| 7135 | else |
| 7136 | { |
| 7137 | uint8_t buffer[1]; |
| 7138 | actual_output_status = |
| 7139 | psa_key_derivation_output_bytes( &operation, |
| 7140 | buffer, sizeof( buffer ) ); |
| 7141 | } |
| 7142 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7143 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7144 | exit: |
| 7145 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7146 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7147 | psa_destroy_key( keys[i] ); |
| 7148 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7149 | PSA_DONE( ); |
| 7150 | } |
| 7151 | /* END_CASE */ |
| 7152 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7153 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7154 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7155 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7156 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7157 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7158 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7159 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7160 | unsigned char input1[] = "Input 1"; |
| 7161 | size_t input1_length = sizeof( input1 ); |
| 7162 | unsigned char input2[] = "Input 2"; |
| 7163 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7164 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7165 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7166 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7167 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7168 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7169 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7170 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7171 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7172 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7173 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7174 | psa_set_key_algorithm( &attributes, alg ); |
| 7175 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7176 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7177 | PSA_ASSERT( psa_import_key( &attributes, |
| 7178 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7179 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7180 | |
| 7181 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7182 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7183 | input1, input1_length, |
| 7184 | input2, input2_length, |
| 7185 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7186 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7187 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7188 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7189 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7190 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7191 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7192 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7193 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7194 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7195 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7196 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7197 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7198 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7199 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7200 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7201 | } |
| 7202 | /* END_CASE */ |
| 7203 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7204 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7205 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7206 | { |
| 7207 | uint8_t output_buffer[16]; |
| 7208 | size_t buffer_size = 16; |
| 7209 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7210 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7211 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7212 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7213 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7214 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7215 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7216 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7217 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7218 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7219 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7220 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7221 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7222 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7223 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7224 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7225 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7226 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7227 | |
| 7228 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7229 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7230 | } |
| 7231 | /* END_CASE */ |
| 7232 | |
| 7233 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7234 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7235 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7236 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7237 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7238 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7239 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7240 | int requested_capacity_arg, |
| 7241 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7242 | data_t *expected_output2, |
| 7243 | int other_key_input_type, |
| 7244 | int key_input_type, |
| 7245 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7246 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7247 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7248 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7249 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7250 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7251 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7252 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7253 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7254 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7255 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7256 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7257 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7258 | uint8_t *expected_outputs[2] = |
| 7259 | {expected_output1->x, expected_output2->x}; |
| 7260 | size_t output_sizes[2] = |
| 7261 | {expected_output1->len, expected_output2->len}; |
| 7262 | size_t output_buffer_size = 0; |
| 7263 | uint8_t *output_buffer = NULL; |
| 7264 | size_t expected_capacity; |
| 7265 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7266 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7267 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7268 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7269 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7270 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7271 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7272 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7273 | |
| 7274 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7275 | { |
| 7276 | if( output_sizes[i] > output_buffer_size ) |
| 7277 | output_buffer_size = output_sizes[i]; |
| 7278 | if( output_sizes[i] == 0 ) |
| 7279 | expected_outputs[i] = NULL; |
| 7280 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7281 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7282 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7283 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7284 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7285 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7286 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7287 | requested_capacity ) ); |
| 7288 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7289 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7290 | switch( steps[i] ) |
| 7291 | { |
| 7292 | case 0: |
| 7293 | break; |
| 7294 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7295 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7296 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7297 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7298 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7299 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7300 | inputs[i]->x, inputs[i]->len ), |
| 7301 | statuses[i] ); |
| 7302 | |
| 7303 | if( statuses[i] != PSA_SUCCESS ) |
| 7304 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7305 | break; |
| 7306 | case 1: // input key |
| 7307 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7308 | psa_set_key_algorithm( &attributes1, alg ); |
| 7309 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7310 | |
| 7311 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7312 | inputs[i]->x, inputs[i]->len, |
| 7313 | &keys[i] ) ); |
| 7314 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7315 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7316 | { |
| 7317 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7318 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7319 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7320 | } |
| 7321 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7322 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7323 | steps[i], |
| 7324 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7325 | break; |
| 7326 | default: |
| 7327 | TEST_ASSERT( ! "default case not supported" ); |
| 7328 | break; |
| 7329 | } |
| 7330 | break; |
| 7331 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7332 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7333 | { |
| 7334 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7335 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7336 | steps[i], |
| 7337 | inputs[i]->x, |
| 7338 | inputs[i]->len ), |
| 7339 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7340 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7341 | case 1: // input key, type DERIVE |
| 7342 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7343 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7344 | psa_set_key_algorithm( &attributes2, alg ); |
| 7345 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7346 | |
| 7347 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7348 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7349 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7350 | |
| 7351 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7352 | inputs[i]->x, inputs[i]->len, |
| 7353 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7354 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7355 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7356 | steps[i], |
| 7357 | keys[i] ), |
| 7358 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7359 | break; |
| 7360 | case 2: // key agreement |
| 7361 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7362 | psa_set_key_algorithm( &attributes3, alg ); |
| 7363 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7364 | |
| 7365 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7366 | inputs[i]->x, inputs[i]->len, |
| 7367 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7368 | |
| 7369 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7370 | &operation, |
| 7371 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7372 | keys[i], key_agreement_peer_key->x, |
| 7373 | key_agreement_peer_key->len ), statuses[i] ); |
| 7374 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7375 | default: |
| 7376 | TEST_ASSERT( ! "default case not supported" ); |
| 7377 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7378 | } |
| 7379 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7380 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7381 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7382 | break; |
| 7383 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7384 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7385 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7386 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7387 | |
| 7388 | if( statuses[i] != PSA_SUCCESS ) |
| 7389 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7390 | break; |
| 7391 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7392 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7393 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7394 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7395 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7396 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7397 | expected_capacity = requested_capacity; |
| 7398 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7399 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7400 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7401 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7402 | |
| 7403 | /* For output key derivation secret must be provided using |
| 7404 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7405 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7406 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7407 | |
| 7408 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7409 | psa_set_key_algorithm( &attributes4, alg ); |
| 7410 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7411 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7412 | |
| 7413 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7414 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7415 | } |
| 7416 | else // output bytes |
| 7417 | { |
| 7418 | /* Expansion phase. */ |
| 7419 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7420 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7421 | /* Read some bytes. */ |
| 7422 | status = psa_key_derivation_output_bytes( &operation, |
| 7423 | output_buffer, output_sizes[i] ); |
| 7424 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7425 | { |
| 7426 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7427 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7428 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7429 | continue; |
| 7430 | } |
| 7431 | else if( expected_capacity == 0 || |
| 7432 | output_sizes[i] > expected_capacity ) |
| 7433 | { |
| 7434 | /* Capacity exceeded. */ |
| 7435 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7436 | expected_capacity = 0; |
| 7437 | continue; |
| 7438 | } |
| 7439 | /* Success. Check the read data. */ |
| 7440 | PSA_ASSERT( status ); |
| 7441 | if( output_sizes[i] != 0 ) |
| 7442 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7443 | expected_outputs[i], output_sizes[i] ); |
| 7444 | /* Check the operation status. */ |
| 7445 | expected_capacity -= output_sizes[i]; |
| 7446 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7447 | ¤t_capacity ) ); |
| 7448 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7449 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7450 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7451 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7452 | |
| 7453 | exit: |
| 7454 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7455 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7456 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7457 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7458 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7459 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7460 | } |
| 7461 | /* END_CASE */ |
| 7462 | |
| 7463 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7464 | void derive_full( int alg_arg, |
| 7465 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7466 | data_t *input1, |
| 7467 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7468 | int requested_capacity_arg ) |
| 7469 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7470 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7471 | psa_algorithm_t alg = alg_arg; |
| 7472 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7473 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7474 | unsigned char output_buffer[16]; |
| 7475 | size_t expected_capacity = requested_capacity; |
| 7476 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7477 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7478 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7479 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7480 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7481 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7482 | psa_set_key_algorithm( &attributes, alg ); |
| 7483 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7484 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7485 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7486 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7487 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7488 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7489 | input1->x, input1->len, |
| 7490 | input2->x, input2->len, |
| 7491 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7492 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7493 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7494 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7495 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7496 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7497 | |
| 7498 | /* Expansion phase. */ |
| 7499 | while( current_capacity > 0 ) |
| 7500 | { |
| 7501 | size_t read_size = sizeof( output_buffer ); |
| 7502 | if( read_size > current_capacity ) |
| 7503 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7504 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7505 | output_buffer, |
| 7506 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7507 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7508 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7509 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7510 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7511 | } |
| 7512 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7513 | /* Check that the operation refuses to go over capacity. */ |
| 7514 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7515 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7516 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7517 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7518 | |
| 7519 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7520 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7521 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7522 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7523 | } |
| 7524 | /* END_CASE */ |
| 7525 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7526 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7527 | void derive_key_exercise( int alg_arg, |
| 7528 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7529 | data_t *input1, |
| 7530 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7531 | int derived_type_arg, |
| 7532 | int derived_bits_arg, |
| 7533 | int derived_usage_arg, |
| 7534 | int derived_alg_arg ) |
| 7535 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7536 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7537 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7538 | psa_algorithm_t alg = alg_arg; |
| 7539 | psa_key_type_t derived_type = derived_type_arg; |
| 7540 | size_t derived_bits = derived_bits_arg; |
| 7541 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7542 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7543 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7544 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7545 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7546 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7547 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7548 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7549 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7550 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7551 | psa_set_key_algorithm( &attributes, alg ); |
| 7552 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7553 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7554 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7555 | |
| 7556 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7557 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7558 | input1->x, input1->len, |
| 7559 | input2->x, input2->len, |
| 7560 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7561 | goto exit; |
| 7562 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7563 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7564 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7565 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7566 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7567 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7568 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7569 | |
| 7570 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7571 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7572 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7573 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7574 | |
| 7575 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7576 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7577 | goto exit; |
| 7578 | |
| 7579 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7580 | /* |
| 7581 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7582 | * thus reset them as required. |
| 7583 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7584 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7585 | |
| 7586 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7587 | psa_destroy_key( base_key ); |
| 7588 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7589 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7590 | } |
| 7591 | /* END_CASE */ |
| 7592 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7593 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7594 | void derive_key_export( int alg_arg, |
| 7595 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7596 | data_t *input1, |
| 7597 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7598 | int bytes1_arg, |
| 7599 | int bytes2_arg ) |
| 7600 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7601 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7602 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7603 | psa_algorithm_t alg = alg_arg; |
| 7604 | size_t bytes1 = bytes1_arg; |
| 7605 | size_t bytes2 = bytes2_arg; |
| 7606 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7607 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7608 | uint8_t *output_buffer = NULL; |
| 7609 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7610 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7611 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7612 | size_t length; |
| 7613 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7614 | ASSERT_ALLOC( output_buffer, capacity ); |
| 7615 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7616 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7617 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7618 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7619 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7620 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7621 | 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] | 7622 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7623 | |
| 7624 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7625 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7626 | input1->x, input1->len, |
| 7627 | input2->x, input2->len, |
| 7628 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7629 | goto exit; |
| 7630 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7631 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7632 | output_buffer, |
| 7633 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7634 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7635 | |
| 7636 | /* 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] | 7637 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7638 | input1->x, input1->len, |
| 7639 | input2->x, input2->len, |
| 7640 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7641 | goto exit; |
| 7642 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7643 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7644 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7645 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7646 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7647 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7648 | &derived_key ) ); |
| 7649 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7650 | export_buffer, bytes1, |
| 7651 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7652 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7653 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7654 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7655 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7656 | &derived_key ) ); |
| 7657 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7658 | export_buffer + bytes1, bytes2, |
| 7659 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7660 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7661 | |
| 7662 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 7663 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 7664 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7665 | |
| 7666 | exit: |
| 7667 | mbedtls_free( output_buffer ); |
| 7668 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7669 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7670 | psa_destroy_key( base_key ); |
| 7671 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7672 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7673 | } |
| 7674 | /* END_CASE */ |
| 7675 | |
| 7676 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7677 | void derive_key_type( int alg_arg, |
| 7678 | data_t *key_data, |
| 7679 | data_t *input1, |
| 7680 | data_t *input2, |
| 7681 | int key_type_arg, int bits_arg, |
| 7682 | data_t *expected_export ) |
| 7683 | { |
| 7684 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7685 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7686 | const psa_algorithm_t alg = alg_arg; |
| 7687 | const psa_key_type_t key_type = key_type_arg; |
| 7688 | const size_t bits = bits_arg; |
| 7689 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7690 | const size_t export_buffer_size = |
| 7691 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 7692 | uint8_t *export_buffer = NULL; |
| 7693 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7694 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7695 | size_t export_length; |
| 7696 | |
| 7697 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 7698 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7699 | |
| 7700 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7701 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7702 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 7703 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 7704 | &base_key ) ); |
| 7705 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 7706 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7707 | &operation, base_key, alg, |
| 7708 | input1->x, input1->len, |
| 7709 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 7710 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 7711 | goto exit; |
| 7712 | |
| 7713 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7714 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 7715 | psa_set_key_type( &derived_attributes, key_type ); |
| 7716 | psa_set_key_bits( &derived_attributes, bits ); |
| 7717 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 7718 | &derived_key ) ); |
| 7719 | |
| 7720 | PSA_ASSERT( psa_export_key( derived_key, |
| 7721 | export_buffer, export_buffer_size, |
| 7722 | &export_length ) ); |
| 7723 | ASSERT_COMPARE( export_buffer, export_length, |
| 7724 | expected_export->x, expected_export->len ); |
| 7725 | |
| 7726 | exit: |
| 7727 | mbedtls_free( export_buffer ); |
| 7728 | psa_key_derivation_abort( &operation ); |
| 7729 | psa_destroy_key( base_key ); |
| 7730 | psa_destroy_key( derived_key ); |
| 7731 | PSA_DONE( ); |
| 7732 | } |
| 7733 | /* END_CASE */ |
| 7734 | |
| 7735 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 7736 | void derive_key( int alg_arg, |
| 7737 | data_t *key_data, data_t *input1, data_t *input2, |
| 7738 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 7739 | int expected_status_arg, |
| 7740 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7741 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7742 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7743 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7744 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 7745 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7746 | size_t bits = bits_arg; |
| 7747 | psa_status_t expected_status = expected_status_arg; |
| 7748 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7749 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7750 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7751 | |
| 7752 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7753 | |
| 7754 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7755 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7756 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 7757 | 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] | 7758 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7759 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7760 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7761 | input1->x, input1->len, |
| 7762 | input2->x, input2->len, |
| 7763 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7764 | goto exit; |
| 7765 | |
| 7766 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 7767 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 7768 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7769 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 7770 | |
| 7771 | psa_status_t status = |
| 7772 | psa_key_derivation_output_key( &derived_attributes, |
| 7773 | &operation, |
| 7774 | &derived_key ); |
| 7775 | if( is_large_output > 0 ) |
| 7776 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 7777 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7778 | |
| 7779 | exit: |
| 7780 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7781 | psa_destroy_key( base_key ); |
| 7782 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 7783 | PSA_DONE( ); |
| 7784 | } |
| 7785 | /* END_CASE */ |
| 7786 | |
| 7787 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7788 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 7789 | int our_key_type_arg, int our_key_alg_arg, |
| 7790 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7791 | int expected_status_arg ) |
| 7792 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7793 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7794 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7795 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7796 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7797 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7798 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7799 | psa_status_t expected_status = expected_status_arg; |
| 7800 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7801 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7802 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7803 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7804 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7805 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7806 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7807 | PSA_ASSERT( psa_import_key( &attributes, |
| 7808 | our_key_data->x, our_key_data->len, |
| 7809 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7810 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7811 | /* The tests currently include inputs that should fail at either step. |
| 7812 | * Test cases that fail at the setup step should be changed to call |
| 7813 | * key_derivation_setup instead, and this function should be renamed |
| 7814 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7815 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7816 | if( status == PSA_SUCCESS ) |
| 7817 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7818 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7819 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 7820 | our_key, |
| 7821 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7822 | expected_status ); |
| 7823 | } |
| 7824 | else |
| 7825 | { |
| 7826 | TEST_ASSERT( status == expected_status ); |
| 7827 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7828 | |
| 7829 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7830 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7831 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7832 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7833 | } |
| 7834 | /* END_CASE */ |
| 7835 | |
| 7836 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7837 | void raw_key_agreement( int alg_arg, |
| 7838 | int our_key_type_arg, data_t *our_key_data, |
| 7839 | data_t *peer_key_data, |
| 7840 | data_t *expected_output ) |
| 7841 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7842 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7843 | psa_algorithm_t alg = alg_arg; |
| 7844 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7845 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7846 | unsigned char *output = NULL; |
| 7847 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7848 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7849 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7850 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7851 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7852 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7853 | psa_set_key_algorithm( &attributes, alg ); |
| 7854 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7855 | PSA_ASSERT( psa_import_key( &attributes, |
| 7856 | our_key_data->x, our_key_data->len, |
| 7857 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7858 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7859 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 7860 | key_bits = psa_get_key_bits( &attributes ); |
| 7861 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 7862 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7863 | TEST_LE_U( expected_output->len, |
| 7864 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 7865 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 7866 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 7867 | |
| 7868 | /* Good case with exact output size */ |
| 7869 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 7870 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 7871 | peer_key_data->x, peer_key_data->len, |
| 7872 | output, expected_output->len, |
| 7873 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7874 | ASSERT_COMPARE( output, output_length, |
| 7875 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 7876 | mbedtls_free( output ); |
| 7877 | output = NULL; |
| 7878 | output_length = ~0; |
| 7879 | |
| 7880 | /* Larger buffer */ |
| 7881 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 7882 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 7883 | peer_key_data->x, peer_key_data->len, |
| 7884 | output, expected_output->len + 1, |
| 7885 | &output_length ) ); |
| 7886 | ASSERT_COMPARE( output, output_length, |
| 7887 | expected_output->x, expected_output->len ); |
| 7888 | mbedtls_free( output ); |
| 7889 | output = NULL; |
| 7890 | output_length = ~0; |
| 7891 | |
| 7892 | /* Buffer too small */ |
| 7893 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 7894 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 7895 | peer_key_data->x, peer_key_data->len, |
| 7896 | output, expected_output->len - 1, |
| 7897 | &output_length ), |
| 7898 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 7899 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7900 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 7901 | mbedtls_free( output ); |
| 7902 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7903 | |
| 7904 | exit: |
| 7905 | mbedtls_free( output ); |
| 7906 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7907 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7908 | } |
| 7909 | /* END_CASE */ |
| 7910 | |
| 7911 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7912 | void key_agreement_capacity( int alg_arg, |
| 7913 | int our_key_type_arg, data_t *our_key_data, |
| 7914 | data_t *peer_key_data, |
| 7915 | int expected_capacity_arg ) |
| 7916 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7917 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7918 | psa_algorithm_t alg = alg_arg; |
| 7919 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7920 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7921 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7922 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7923 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7924 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7925 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7926 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7927 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7928 | psa_set_key_algorithm( &attributes, alg ); |
| 7929 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7930 | PSA_ASSERT( psa_import_key( &attributes, |
| 7931 | our_key_data->x, our_key_data->len, |
| 7932 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7933 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7934 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7935 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 7936 | &operation, |
| 7937 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 7938 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 7939 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 7940 | { |
| 7941 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7942 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 7943 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 7944 | NULL, 0 ) ); |
| 7945 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7946 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7947 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7948 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7949 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7950 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7951 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7952 | /* Test the actual capacity by reading the output. */ |
| 7953 | while( actual_capacity > sizeof( output ) ) |
| 7954 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7955 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7956 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7957 | actual_capacity -= sizeof( output ); |
| 7958 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7959 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7960 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7961 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7962 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7963 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7964 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7965 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7966 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7967 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7968 | } |
| 7969 | /* END_CASE */ |
| 7970 | |
| 7971 | /* BEGIN_CASE */ |
| 7972 | void key_agreement_output( int alg_arg, |
| 7973 | int our_key_type_arg, data_t *our_key_data, |
| 7974 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 7975 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7976 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7977 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7978 | psa_algorithm_t alg = alg_arg; |
| 7979 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7980 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7981 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 7982 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7983 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 7984 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 7985 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7986 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7987 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7988 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7989 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7990 | psa_set_key_algorithm( &attributes, alg ); |
| 7991 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7992 | PSA_ASSERT( psa_import_key( &attributes, |
| 7993 | our_key_data->x, our_key_data->len, |
| 7994 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7995 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7996 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7997 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 7998 | &operation, |
| 7999 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8000 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8001 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8002 | { |
| 8003 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8004 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8005 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8006 | NULL, 0 ) ); |
| 8007 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8008 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8009 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8010 | actual_output, |
| 8011 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8012 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8013 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8014 | if( expected_output2->len != 0 ) |
| 8015 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8016 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8017 | actual_output, |
| 8018 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8019 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8020 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8021 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8022 | |
| 8023 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8024 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8025 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8026 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8027 | mbedtls_free( actual_output ); |
| 8028 | } |
| 8029 | /* END_CASE */ |
| 8030 | |
| 8031 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8032 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8033 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8034 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8035 | unsigned char *output = NULL; |
| 8036 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8037 | size_t i; |
| 8038 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8039 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8040 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8041 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8042 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8043 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8044 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8045 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8046 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8047 | /* Run several times, to ensure that every output byte will be |
| 8048 | * nonzero at least once with overwhelming probability |
| 8049 | * (2^(-8*number_of_runs)). */ |
| 8050 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8051 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8052 | if( bytes != 0 ) |
| 8053 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8054 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8055 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8056 | for( i = 0; i < bytes; i++ ) |
| 8057 | { |
| 8058 | if( output[i] != 0 ) |
| 8059 | ++changed[i]; |
| 8060 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8061 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8062 | |
| 8063 | /* Check that every byte was changed to nonzero at least once. This |
| 8064 | * validates that psa_generate_random is overwriting every byte of |
| 8065 | * the output buffer. */ |
| 8066 | for( i = 0; i < bytes; i++ ) |
| 8067 | { |
| 8068 | TEST_ASSERT( changed[i] != 0 ); |
| 8069 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8070 | |
| 8071 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8072 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8073 | mbedtls_free( output ); |
| 8074 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8075 | } |
| 8076 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8077 | |
| 8078 | /* BEGIN_CASE */ |
| 8079 | void generate_key( int type_arg, |
| 8080 | int bits_arg, |
| 8081 | int usage_arg, |
| 8082 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8083 | int expected_status_arg, |
| 8084 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8085 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8086 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8087 | psa_key_type_t type = type_arg; |
| 8088 | psa_key_usage_t usage = usage_arg; |
| 8089 | size_t bits = bits_arg; |
| 8090 | psa_algorithm_t alg = alg_arg; |
| 8091 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8092 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8093 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8094 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8095 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8096 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8097 | psa_set_key_usage_flags( &attributes, usage ); |
| 8098 | psa_set_key_algorithm( &attributes, alg ); |
| 8099 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8100 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8101 | |
| 8102 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8103 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8104 | |
| 8105 | if( is_large_key > 0 ) |
| 8106 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8107 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8108 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8109 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8110 | |
| 8111 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8112 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8113 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8114 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8115 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8116 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8117 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8118 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8119 | |
| 8120 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8121 | /* |
| 8122 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8123 | * thus reset them as required. |
| 8124 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8125 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8126 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8127 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8128 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8129 | } |
| 8130 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8131 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8132 | /* 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] | 8133 | void generate_key_rsa( int bits_arg, |
| 8134 | data_t *e_arg, |
| 8135 | int expected_status_arg ) |
| 8136 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8137 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8138 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8139 | size_t bits = bits_arg; |
| 8140 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8141 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8142 | psa_status_t expected_status = expected_status_arg; |
| 8143 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8144 | uint8_t *exported = NULL; |
| 8145 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8146 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8147 | size_t exported_length = SIZE_MAX; |
| 8148 | uint8_t *e_read_buffer = NULL; |
| 8149 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8150 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8151 | size_t e_read_length = SIZE_MAX; |
| 8152 | |
| 8153 | if( e_arg->len == 0 || |
| 8154 | ( e_arg->len == 3 && |
| 8155 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8156 | { |
| 8157 | is_default_public_exponent = 1; |
| 8158 | e_read_size = 0; |
| 8159 | } |
| 8160 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8161 | ASSERT_ALLOC( exported, exported_size ); |
| 8162 | |
| 8163 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8164 | |
| 8165 | psa_set_key_usage_flags( &attributes, usage ); |
| 8166 | psa_set_key_algorithm( &attributes, alg ); |
| 8167 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8168 | e_arg->x, e_arg->len ) ); |
| 8169 | psa_set_key_bits( &attributes, bits ); |
| 8170 | |
| 8171 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8172 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8173 | if( expected_status != PSA_SUCCESS ) |
| 8174 | goto exit; |
| 8175 | |
| 8176 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8177 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8178 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8179 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8180 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8181 | e_read_buffer, e_read_size, |
| 8182 | &e_read_length ) ); |
| 8183 | if( is_default_public_exponent ) |
| 8184 | TEST_EQUAL( e_read_length, 0 ); |
| 8185 | else |
| 8186 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8187 | |
| 8188 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8189 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8190 | goto exit; |
| 8191 | |
| 8192 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8193 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8194 | exported, exported_size, |
| 8195 | &exported_length ) ); |
| 8196 | { |
| 8197 | uint8_t *p = exported; |
| 8198 | uint8_t *end = exported + exported_length; |
| 8199 | size_t len; |
| 8200 | /* RSAPublicKey ::= SEQUENCE { |
| 8201 | * modulus INTEGER, -- n |
| 8202 | * publicExponent INTEGER } -- e |
| 8203 | */ |
| 8204 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8205 | MBEDTLS_ASN1_SEQUENCE | |
| 8206 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8207 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8208 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8209 | MBEDTLS_ASN1_INTEGER ) ); |
| 8210 | if( len >= 1 && p[0] == 0 ) |
| 8211 | { |
| 8212 | ++p; |
| 8213 | --len; |
| 8214 | } |
| 8215 | if( e_arg->len == 0 ) |
| 8216 | { |
| 8217 | TEST_EQUAL( len, 3 ); |
| 8218 | TEST_EQUAL( p[0], 1 ); |
| 8219 | TEST_EQUAL( p[1], 0 ); |
| 8220 | TEST_EQUAL( p[2], 1 ); |
| 8221 | } |
| 8222 | else |
| 8223 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8224 | } |
| 8225 | |
| 8226 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8227 | /* |
| 8228 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8229 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8230 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8231 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8232 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8233 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8234 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8235 | mbedtls_free( e_read_buffer ); |
| 8236 | mbedtls_free( exported ); |
| 8237 | } |
| 8238 | /* END_CASE */ |
| 8239 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8240 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8241 | void persistent_key_load_key_from_storage( data_t *data, |
| 8242 | int type_arg, int bits_arg, |
| 8243 | int usage_flags_arg, int alg_arg, |
| 8244 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8245 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8246 | 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] | 8247 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8248 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8249 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8250 | psa_key_type_t type = type_arg; |
| 8251 | size_t bits = bits_arg; |
| 8252 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8253 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8254 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8255 | unsigned char *first_export = NULL; |
| 8256 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8257 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8258 | size_t first_exported_length; |
| 8259 | size_t second_exported_length; |
| 8260 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8261 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8262 | { |
| 8263 | ASSERT_ALLOC( first_export, export_size ); |
| 8264 | ASSERT_ALLOC( second_export, export_size ); |
| 8265 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8266 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8267 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8268 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8269 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8270 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8271 | psa_set_key_algorithm( &attributes, alg ); |
| 8272 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8273 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8274 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8275 | switch( generation_method ) |
| 8276 | { |
| 8277 | case IMPORT_KEY: |
| 8278 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8279 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8280 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8281 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8282 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8283 | case GENERATE_KEY: |
| 8284 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8285 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8286 | break; |
| 8287 | |
| 8288 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8289 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8290 | { |
| 8291 | /* Create base key */ |
| 8292 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8293 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8294 | psa_set_key_usage_flags( &base_attributes, |
| 8295 | PSA_KEY_USAGE_DERIVE ); |
| 8296 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8297 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8298 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8299 | data->x, data->len, |
| 8300 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8301 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8302 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8303 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8304 | &operation, |
| 8305 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8306 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8307 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8308 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8309 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8310 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8311 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8312 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8313 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8314 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8315 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8316 | #else |
| 8317 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8318 | #endif |
| 8319 | break; |
| 8320 | |
| 8321 | default: |
| 8322 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8323 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8324 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8325 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8326 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8327 | /* Export the key if permitted by the key policy. */ |
| 8328 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8329 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8330 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8331 | first_export, export_size, |
| 8332 | &first_exported_length ) ); |
| 8333 | if( generation_method == IMPORT_KEY ) |
| 8334 | ASSERT_COMPARE( data->x, data->len, |
| 8335 | first_export, first_exported_length ); |
| 8336 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8337 | |
| 8338 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8339 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8340 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8341 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8342 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8343 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8344 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8345 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8346 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8347 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8348 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8349 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8350 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8351 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8352 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8353 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8354 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8355 | /* Export the key again if permitted by the key policy. */ |
| 8356 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8357 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8358 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8359 | second_export, export_size, |
| 8360 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8361 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8362 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8363 | } |
| 8364 | |
| 8365 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8366 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8367 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8368 | |
| 8369 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8370 | /* |
| 8371 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8372 | * thus reset them as required. |
| 8373 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8374 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8375 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8376 | mbedtls_free( first_export ); |
| 8377 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8378 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8379 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8380 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8381 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8382 | } |
| 8383 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8384 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8385 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8386 | 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] | 8387 | int input_first, data_t *pw_data, |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8388 | int expected_status_arg ) |
| 8389 | { |
| 8390 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8391 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 8392 | psa_algorithm_t alg = alg_arg; |
| 8393 | psa_algorithm_t hash_alg = hash_arg; |
| 8394 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8395 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8396 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8397 | psa_status_t expected_status = expected_status_arg; |
| 8398 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8399 | unsigned char *output_buffer = NULL; |
| 8400 | size_t output_len = 0; |
| 8401 | |
| 8402 | PSA_INIT( ); |
| 8403 | |
| 8404 | ASSERT_ALLOC( output_buffer, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8405 | PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8406 | PSA_PAKE_STEP_KEY_SHARE) ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8407 | |
| 8408 | if( pw_data->len > 0 ) |
| 8409 | { |
| 8410 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8411 | psa_set_key_algorithm( &attributes, alg ); |
| 8412 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8413 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8414 | &key ) ); |
| 8415 | } |
| 8416 | |
| 8417 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8418 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8419 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8420 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8421 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8422 | |
| 8423 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8424 | PSA_ERROR_BAD_STATE ); |
| 8425 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8426 | PSA_ERROR_BAD_STATE ); |
| 8427 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8428 | PSA_ERROR_BAD_STATE ); |
| 8429 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8430 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8431 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8432 | NULL, 0, NULL ), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8433 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8434 | 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] | 8435 | PSA_ERROR_BAD_STATE ); |
| 8436 | |
| 8437 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8438 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8439 | status = psa_pake_setup( &operation, &cipher_suite ); |
| 8440 | if( status != PSA_SUCCESS ) |
| 8441 | { |
| 8442 | TEST_EQUAL( status, expected_status ); |
| 8443 | goto exit; |
| 8444 | } |
| 8445 | else |
| 8446 | PSA_ASSERT( status ); |
| 8447 | |
Neil Armstrong | 50de0ae | 2022-06-08 17:46:24 +0200 | [diff] [blame] | 8448 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8449 | PSA_ERROR_BAD_STATE ); |
| 8450 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8451 | status = psa_pake_set_role( &operation, role ); |
| 8452 | if( status != PSA_SUCCESS ) |
| 8453 | { |
| 8454 | TEST_EQUAL( status, expected_status ); |
| 8455 | goto exit; |
| 8456 | } |
| 8457 | else |
| 8458 | PSA_ASSERT( status ); |
| 8459 | |
| 8460 | if( pw_data->len > 0 ) |
| 8461 | { |
| 8462 | status = psa_pake_set_password_key( &operation, key ); |
| 8463 | if( status != PSA_SUCCESS ) |
| 8464 | { |
| 8465 | TEST_EQUAL( status, expected_status ); |
| 8466 | goto exit; |
| 8467 | } |
| 8468 | else |
| 8469 | PSA_ASSERT( status ); |
| 8470 | } |
| 8471 | |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8472 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8473 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8474 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8475 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8476 | |
| 8477 | const uint8_t unsupported_id[] = "abcd"; |
| 8478 | |
| 8479 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8480 | PSA_ERROR_NOT_SUPPORTED ); |
| 8481 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8482 | PSA_ERROR_NOT_SUPPORTED ); |
| 8483 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8484 | /* First round */ |
| 8485 | if( input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8486 | { |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8487 | /* Invalid parameters */ |
| 8488 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8489 | NULL, 0 ), |
| 8490 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8491 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8492 | output_buffer, 66 ), |
| 8493 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8494 | /* Invalid first step */ |
| 8495 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8496 | output_buffer, 66 ), |
| 8497 | PSA_ERROR_BAD_STATE ); |
| 8498 | |
| 8499 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8500 | output_buffer, 66 ), |
| 8501 | expected_status); |
| 8502 | |
| 8503 | if( expected_status == PSA_SUCCESS ) |
| 8504 | { |
| 8505 | /* Buffer too large */ |
| 8506 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8507 | output_buffer, 512 ), |
| 8508 | PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8509 | |
| 8510 | /* The operation should be aborted at this point */ |
| 8511 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8512 | output_buffer, 66 ), |
| 8513 | PSA_ERROR_BAD_STATE ); |
| 8514 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8515 | } |
| 8516 | else |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8517 | { |
| 8518 | /* Invalid parameters */ |
| 8519 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8520 | NULL, 0, NULL ), |
| 8521 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8522 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8523 | output_buffer, 512, &output_len ), |
| 8524 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8525 | /* Invalid first step */ |
| 8526 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8527 | output_buffer, 512, &output_len ), |
| 8528 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8529 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8530 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8531 | output_buffer, 512, &output_len ), |
| 8532 | expected_status ); |
Neil Armstrong | 98506ab | 2022-06-08 17:43:20 +0200 | [diff] [blame] | 8533 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8534 | if( expected_status == PSA_SUCCESS ) |
| 8535 | { |
| 8536 | TEST_ASSERT( output_len > 0 ); |
| 8537 | |
| 8538 | /* Buffer too small */ |
| 8539 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8540 | output_buffer, 5, &output_len ), |
| 8541 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8542 | |
| 8543 | /* The operation should be aborted at this point */ |
| 8544 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8545 | output_buffer, 512, &output_len ), |
| 8546 | PSA_ERROR_BAD_STATE ); |
| 8547 | } |
| 8548 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8549 | |
| 8550 | exit: |
| 8551 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 8552 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8553 | mbedtls_free( output_buffer ); |
| 8554 | PSA_DONE( ); |
| 8555 | } |
| 8556 | /* END_CASE */ |
| 8557 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8558 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame^] | 8559 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 8560 | int client_input_first, int inject_error, |
| 8561 | data_t *pw_data ) |
| 8562 | { |
| 8563 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8564 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8565 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8566 | psa_algorithm_t alg = alg_arg; |
| 8567 | psa_algorithm_t hash_alg = hash_arg; |
| 8568 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8569 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8570 | |
| 8571 | PSA_INIT( ); |
| 8572 | |
| 8573 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8574 | psa_set_key_algorithm( &attributes, alg ); |
| 8575 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8576 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8577 | &key ) ); |
| 8578 | |
| 8579 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8580 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8581 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8582 | |
| 8583 | |
| 8584 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8585 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8586 | |
| 8587 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8588 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8589 | |
| 8590 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8591 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8592 | |
| 8593 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8594 | client_input_first, 1, |
| 8595 | inject_error ), 1 ); |
| 8596 | |
| 8597 | if( inject_error == 1 || inject_error == 2 ) |
| 8598 | goto exit; |
| 8599 | |
| 8600 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8601 | client_input_first, 2, |
| 8602 | inject_error ), 1 ); |
| 8603 | |
| 8604 | exit: |
| 8605 | psa_destroy_key( key ); |
| 8606 | psa_pake_abort( &server ); |
| 8607 | psa_pake_abort( &client ); |
| 8608 | PSA_DONE( ); |
| 8609 | } |
| 8610 | /* END_CASE */ |
| 8611 | |
| 8612 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8613 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8614 | int derive_alg_arg, data_t *pw_data, |
| 8615 | int client_input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8616 | { |
| 8617 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8618 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8619 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8620 | psa_algorithm_t alg = alg_arg; |
| 8621 | psa_algorithm_t hash_alg = hash_arg; |
| 8622 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 8623 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8624 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8625 | psa_key_derivation_operation_t server_derive = |
| 8626 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8627 | psa_key_derivation_operation_t client_derive = |
| 8628 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8629 | |
| 8630 | PSA_INIT( ); |
| 8631 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8632 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8633 | psa_set_key_algorithm( &attributes, alg ); |
| 8634 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8635 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8636 | &key ) ); |
| 8637 | |
| 8638 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8639 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8640 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8641 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8642 | /* Get shared key */ |
| 8643 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 8644 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 8645 | |
| 8646 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 8647 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 8648 | { |
| 8649 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 8650 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8651 | (const uint8_t*) "", 0) ); |
| 8652 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 8653 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 8654 | (const uint8_t*) "", 0) ); |
| 8655 | } |
| 8656 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8657 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8658 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8659 | |
| 8660 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8661 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8662 | |
| 8663 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8664 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8665 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8666 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8667 | PSA_ERROR_BAD_STATE ); |
| 8668 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8669 | PSA_ERROR_BAD_STATE ); |
| 8670 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8671 | /* First round */ |
| 8672 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8673 | client_input_first, 1, 0 ), 1 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8674 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 8675 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 8676 | PSA_ERROR_BAD_STATE ); |
| 8677 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 8678 | PSA_ERROR_BAD_STATE ); |
| 8679 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 8680 | /* Second round */ |
| 8681 | TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8682 | client_input_first, 2, 0 ), 1 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8683 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8684 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 8685 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 8686 | |
| 8687 | exit: |
| 8688 | psa_key_derivation_abort( &server_derive ); |
| 8689 | psa_key_derivation_abort( &client_derive ); |
| 8690 | psa_destroy_key( key ); |
| 8691 | psa_pake_abort( &server ); |
| 8692 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8693 | PSA_DONE( ); |
| 8694 | } |
| 8695 | /* END_CASE */ |