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" |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 7 | #include "common.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 8 | |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9 | #include "mbedtls/psa_util.h" |
| 10 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 11 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 12 | * uses mbedtls_ctr_drbg internally. */ |
| 13 | #include "mbedtls/ctr_drbg.h" |
| 14 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 15 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 16 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 18 | #include "psa_crypto_core.h" |
| 19 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 20 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 21 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 22 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 23 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 24 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 25 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 26 | #else |
| 27 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 28 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 29 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 30 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 31 | #include "mbedtls/threading.h" |
| 32 | #endif |
| 33 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 34 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 35 | #define UNUSED 0xdeadbeef |
| 36 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 37 | /* Assert that an operation is (not) active. |
| 38 | * This serves as a proxy for checking if the operation is aborted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 39 | #define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0) |
| 40 | #define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 41 | |
| 42 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 43 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 44 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 45 | /** Test if a buffer contains a constant byte value. |
| 46 | * |
| 47 | * `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] | 48 | * |
| 49 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 50 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 51 | * \param size Size of the buffer in bytes. |
| 52 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 53 | * \return 1 if the buffer is all-bits-zero. |
| 54 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 55 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | 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] | 57 | { |
| 58 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | for (i = 0; i < size; i++) { |
| 60 | if (((unsigned char *) buffer)[i] != c) { |
| 61 | return 0; |
| 62 | } |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 63 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | return 1; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 65 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 66 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 67 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | static int asn1_write_10x(unsigned char **p, |
| 69 | unsigned char *start, |
| 70 | size_t bits, |
| 71 | unsigned char x) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 72 | { |
| 73 | int ret; |
| 74 | int len = bits / 8 + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | if (bits == 0) { |
| 76 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 77 | } |
| 78 | if (bits <= 8 && x >= 1 << (bits - 1)) { |
| 79 | return MBEDTLS_ERR_ASN1_INVALID_DATA; |
| 80 | } |
| 81 | if (*p < start || *p - start < (ptrdiff_t) len) { |
| 82 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 83 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 84 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | (*p)[len-1] = x; |
| 86 | if (bits % 8 == 0) { |
| 87 | (*p)[1] |= 1; |
| 88 | } else { |
| 89 | (*p)[0] |= 1 << (bits % 8); |
| 90 | } |
| 91 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 92 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 93 | MBEDTLS_ASN1_INTEGER)); |
| 94 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | static int construct_fake_rsa_key(unsigned char *buffer, |
| 98 | size_t buffer_size, |
| 99 | unsigned char **p, |
| 100 | size_t bits, |
| 101 | int keypair) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 102 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | size_t half_bits = (bits + 1) / 2; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 104 | int ret; |
| 105 | int len = 0; |
| 106 | /* Construct something that looks like a DER encoding of |
| 107 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 108 | * RSAPrivateKey ::= SEQUENCE { |
| 109 | * version Version, |
| 110 | * modulus INTEGER, -- n |
| 111 | * publicExponent INTEGER, -- e |
| 112 | * privateExponent INTEGER, -- d |
| 113 | * prime1 INTEGER, -- p |
| 114 | * prime2 INTEGER, -- q |
| 115 | * exponent1 INTEGER, -- d mod (p-1) |
| 116 | * exponent2 INTEGER, -- d mod (q-1) |
| 117 | * coefficient INTEGER, -- (inverse of q) mod p |
| 118 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 119 | * } |
| 120 | * Or, for a public key, the same structure with only |
| 121 | * version, modulus and publicExponent. |
| 122 | */ |
| 123 | *p = buffer + buffer_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | if (keypair) { |
| 125 | MBEDTLS_ASN1_CHK_ADD(len, /* pq */ |
| 126 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 127 | MBEDTLS_ASN1_CHK_ADD(len, /* dq */ |
| 128 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 129 | MBEDTLS_ASN1_CHK_ADD(len, /* dp */ |
| 130 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 131 | MBEDTLS_ASN1_CHK_ADD(len, /* q */ |
| 132 | asn1_write_10x(p, buffer, half_bits, 1)); |
| 133 | MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */ |
| 134 | asn1_write_10x(p, buffer, half_bits, 3)); |
| 135 | MBEDTLS_ASN1_CHK_ADD(len, /* d */ |
| 136 | asn1_write_10x(p, buffer, bits, 1)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 137 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */ |
| 139 | asn1_write_10x(p, buffer, 17, 1)); |
| 140 | MBEDTLS_ASN1_CHK_ADD(len, /* n */ |
| 141 | asn1_write_10x(p, buffer, bits, 1)); |
| 142 | if (keypair) { |
| 143 | MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */ |
| 144 | mbedtls_asn1_write_int(p, buffer, 0)); |
| 145 | } |
| 146 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 147 | { |
| 148 | const unsigned char tag = |
| 149 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag)); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 151 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | return len; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 153 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 154 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 155 | |
Michael Schuster | b1e33fb | 2024-06-04 02:30:22 +0200 | [diff] [blame] | 156 | static int exercise_mac_setup(psa_key_type_t key_type, |
Michael Schuster | 31b1cb8 | 2024-06-04 02:41:10 +0200 | [diff] [blame^] | 157 | const unsigned char *key_bytes, |
| 158 | size_t key_length, |
| 159 | psa_algorithm_t alg, |
| 160 | psa_mac_operation_t *operation, |
| 161 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 162 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 163 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 164 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 166 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 167 | psa_set_key_algorithm(&attributes, alg); |
| 168 | psa_set_key_type(&attributes, key_type); |
| 169 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | *status = psa_mac_sign_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 172 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | PSA_ASSERT(psa_mac_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 174 | /* If setup failed, reproduce the failure, so that the caller can |
| 175 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 176 | if (*status != PSA_SUCCESS) { |
| 177 | TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 180 | psa_destroy_key(key); |
| 181 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 182 | |
| 183 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | psa_destroy_key(key); |
| 185 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Michael Schuster | b1e33fb | 2024-06-04 02:30:22 +0200 | [diff] [blame] | 188 | static int exercise_cipher_setup(psa_key_type_t key_type, |
Michael Schuster | 31b1cb8 | 2024-06-04 02:41:10 +0200 | [diff] [blame^] | 189 | const unsigned char *key_bytes, |
| 190 | size_t key_length, |
| 191 | psa_algorithm_t alg, |
| 192 | psa_cipher_operation_t *operation, |
| 193 | psa_status_t *status) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 194 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 195 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 196 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 199 | psa_set_key_algorithm(&attributes, alg); |
| 200 | psa_set_key_type(&attributes, key_type); |
| 201 | PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | *status = psa_cipher_encrypt_setup(operation, key, alg); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 204 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | PSA_ASSERT(psa_cipher_abort(operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 206 | /* If setup failed, reproduce the failure, so that the caller can |
| 207 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 208 | if (*status != PSA_SUCCESS) { |
| 209 | TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg), |
| 210 | *status); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 213 | psa_destroy_key(key); |
| 214 | return 1; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 215 | |
| 216 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | psa_destroy_key(key); |
| 218 | return 0; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | 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] | 222 | { |
| 223 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 224 | 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] | 225 | uint8_t buffer[1]; |
| 226 | size_t length; |
| 227 | int ok = 0; |
| 228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | psa_set_key_id(&attributes, key_id); |
| 230 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 231 | psa_set_key_algorithm(&attributes, PSA_ALG_CTR); |
| 232 | psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); |
| 233 | TEST_EQUAL(psa_get_key_attributes(key, &attributes), |
| 234 | PSA_ERROR_INVALID_HANDLE); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 235 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 237 | TEST_EQUAL( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0); |
| 239 | TEST_EQUAL(psa_get_key_lifetime(&attributes), 0); |
| 240 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0); |
| 241 | TEST_EQUAL(psa_get_key_algorithm(&attributes), 0); |
| 242 | TEST_EQUAL(psa_get_key_type(&attributes), 0); |
| 243 | TEST_EQUAL(psa_get_key_bits(&attributes), 0); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length), |
| 246 | PSA_ERROR_INVALID_HANDLE); |
| 247 | TEST_EQUAL(psa_export_public_key(key, |
| 248 | buffer, sizeof(buffer), &length), |
| 249 | PSA_ERROR_INVALID_HANDLE); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 250 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 251 | ok = 1; |
| 252 | |
| 253 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 254 | /* |
| 255 | * Key attributes may have been returned by psa_get_key_attributes() |
| 256 | * thus reset them as required. |
| 257 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | return ok; |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 263 | /* Assert that a key isn't reported as having a slot number. */ |
| 264 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 265 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
Michael Schuster | 31b1cb8 | 2024-06-04 02:41:10 +0200 | [diff] [blame^] | 266 | do \ |
| 267 | { \ |
| 268 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 269 | TEST_EQUAL(psa_get_key_slot_number( \ |
| 270 | attributes, \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | &ASSERT_NO_SLOT_NUMBER_slot_number), \ |
| 272 | PSA_ERROR_INVALID_ARGUMENT); \ |
Michael Schuster | 31b1cb8 | 2024-06-04 02:41:10 +0200 | [diff] [blame^] | 273 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 274 | while (0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 275 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | #define ASSERT_NO_SLOT_NUMBER(attributes) \ |
| 277 | ((void) 0) |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 278 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 279 | |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 280 | #define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */ |
| 281 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 282 | /* An overapproximation of the amount of storage needed for a key of the |
| 283 | * given type and with the given content. The API doesn't make it easy |
| 284 | * to find a good value for the size. The current implementation doesn't |
| 285 | * care about the value anyway. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | #define KEY_BITS_FROM_DATA(type, data) \ |
| 287 | (data)->len |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 288 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 289 | typedef enum { |
| 290 | IMPORT_KEY = 0, |
| 291 | GENERATE_KEY = 1, |
| 292 | DERIVE_KEY = 2 |
| 293 | } generate_method; |
| 294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | typedef enum { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 296 | DO_NOT_SET_LENGTHS = 0, |
| 297 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 298 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 299 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | typedef enum { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 302 | USE_NULL_TAG = 0, |
| 303 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 304 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 305 | |
Kusumit Ghoderao | a14ae5a | 2023-04-19 14:16:26 +0530 | [diff] [blame] | 306 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 307 | /*! |
| 308 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 309 | * \param key_type_arg Type of key passed in |
| 310 | * \param key_data The encryption / decryption key data |
| 311 | * \param alg_arg The type of algorithm used |
| 312 | * \param nonce Nonce data |
| 313 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 314 | * \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] | 315 | * feed additional data in to be encrypted / |
| 316 | * decrypted. If -1, no chunking. |
| 317 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 318 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 319 | * the data in to be encrypted / decrypted. If |
| 320 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 321 | * \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] | 322 | * expected here, this controls whether or not |
| 323 | * to set lengths, and in what order with |
| 324 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 325 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 326 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 327 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 328 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 329 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 330 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 331 | static int aead_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 332 | int alg_arg, |
| 333 | data_t *nonce, |
| 334 | data_t *additional_data, |
| 335 | int ad_part_len_arg, |
| 336 | data_t *input_data, |
| 337 | int data_part_len_arg, |
| 338 | set_lengths_method_t set_lengths_method, |
| 339 | data_t *expected_output, |
| 340 | int is_encrypt, |
| 341 | int do_zero_parts) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 342 | { |
| 343 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 344 | psa_key_type_t key_type = key_type_arg; |
| 345 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 346 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 347 | unsigned char *output_data = NULL; |
| 348 | unsigned char *part_data = NULL; |
| 349 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 350 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 351 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 352 | size_t output_size = 0; |
| 353 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 354 | size_t output_length = 0; |
| 355 | size_t key_bits = 0; |
| 356 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 357 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 358 | size_t part_length = 0; |
| 359 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 360 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 361 | size_t ad_part_len = 0; |
| 362 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 363 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 364 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 365 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 366 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 367 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 368 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | if (is_encrypt) { |
| 373 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 374 | } else { |
| 375 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 376 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | |
| 378 | psa_set_key_algorithm(&attributes, alg); |
| 379 | psa_set_key_type(&attributes, key_type); |
| 380 | |
| 381 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 382 | &key)); |
| 383 | |
| 384 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 385 | key_bits = psa_get_key_bits(&attributes); |
| 386 | |
| 387 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
| 388 | |
| 389 | if (is_encrypt) { |
| 390 | /* Tag gets written at end of buffer. */ |
| 391 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 392 | (input_data->len + |
| 393 | tag_length)); |
| 394 | data_true_size = input_data->len; |
| 395 | } else { |
| 396 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 397 | (input_data->len - |
| 398 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 399 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 400 | /* Do not want to attempt to decrypt tag. */ |
| 401 | data_true_size = input_data->len - tag_length; |
| 402 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 403 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 404 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | if (is_encrypt) { |
| 407 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
| 408 | TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
| 409 | } else { |
| 410 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
| 411 | TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 412 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 413 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 414 | TEST_CALLOC(final_data, final_output_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 416 | if (is_encrypt) { |
| 417 | status = psa_aead_encrypt_setup(&operation, key, alg); |
| 418 | } else { |
| 419 | status = psa_aead_decrypt_setup(&operation, key, alg); |
| 420 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 421 | |
| 422 | /* If the operation is not supported, just skip and not fail in case the |
| 423 | * encryption involves a common limitation of cryptography hardwares and |
| 424 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 425 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 426 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 427 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | PSA_ASSERT(status); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | if (set_lengths_method == DO_NOT_SET_LENGTHS) { |
| 433 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 434 | } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 435 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 436 | data_true_size)); |
| 437 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
| 438 | } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 439 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 442 | data_true_size)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 443 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | if (ad_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 446 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 447 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 450 | part_offset < additional_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | part_offset += part_length, part_count++) { |
| 452 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 453 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | } else if (additional_data->len - part_offset < ad_part_len) { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 455 | part_length = additional_data->len - part_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 457 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | PSA_ASSERT(psa_aead_update_ad(&operation, |
| 461 | additional_data->x + part_offset, |
| 462 | part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 463 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 464 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | } else { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 466 | /* Pass additional data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 468 | additional_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | if (data_part_len_arg != -1) { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 472 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | data_part_len = (size_t) data_part_len_arg; |
| 474 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 475 | (size_t) data_part_len); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 476 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 477 | TEST_CALLOC(part_data, part_data_size); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | for (part_offset = 0, part_count = 0; |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 480 | part_offset < data_true_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | part_offset += part_length, part_count++) { |
| 482 | if (do_zero_parts && (part_count & 0x01)) { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 483 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | } else if ((data_true_size - part_offset) < data_part_len) { |
| 485 | part_length = (data_true_size - part_offset); |
| 486 | } else { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 487 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 488 | } |
| 489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | PSA_ASSERT(psa_aead_update(&operation, |
| 491 | (input_data->x + part_offset), |
| 492 | part_length, part_data, |
| 493 | part_data_size, |
| 494 | &output_part_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | if (output_data && output_part_length) { |
| 497 | memcpy((output_data + output_length), part_data, |
| 498 | output_part_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 499 | } |
| 500 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 501 | output_length += output_part_length; |
| 502 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | } else { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 504 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 506 | data_true_size, output_data, |
| 507 | output_size, &output_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 508 | } |
| 509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | if (is_encrypt) { |
| 511 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 512 | final_output_size, |
| 513 | &output_part_length, |
| 514 | tag_buffer, tag_length, |
| 515 | &tag_size)); |
| 516 | } else { |
| 517 | PSA_ASSERT(psa_aead_verify(&operation, final_data, |
| 518 | final_output_size, |
| 519 | &output_part_length, |
| 520 | (input_data->x + data_true_size), |
| 521 | tag_length)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | if (output_data && output_part_length) { |
| 525 | memcpy((output_data + output_length), final_data, |
| 526 | output_part_length); |
| 527 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 528 | |
| 529 | output_length += output_part_length; |
| 530 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 531 | |
| 532 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 533 | * should be exact.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | if (is_encrypt) { |
| 535 | TEST_EQUAL(tag_length, tag_size); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | if (output_data && tag_length) { |
| 538 | memcpy((output_data + output_length), tag_buffer, |
| 539 | tag_length); |
| 540 | } |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 541 | |
| 542 | output_length += tag_length; |
| 543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 544 | TEST_EQUAL(output_length, |
| 545 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 546 | input_data->len)); |
| 547 | TEST_LE_U(output_length, |
| 548 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
| 549 | } else { |
| 550 | TEST_EQUAL(output_length, |
| 551 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, |
| 552 | input_data->len)); |
| 553 | TEST_LE_U(output_length, |
| 554 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 555 | } |
| 556 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 557 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 558 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 559 | output_data, output_length); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 560 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 561 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 562 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 563 | |
| 564 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | psa_destroy_key(key); |
| 566 | psa_aead_abort(&operation); |
| 567 | mbedtls_free(output_data); |
| 568 | mbedtls_free(part_data); |
| 569 | mbedtls_free(final_data); |
| 570 | PSA_DONE(); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 572 | return test_ok; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 575 | /*! |
| 576 | * \brief Internal Function for MAC multipart tests. |
| 577 | * \param key_type_arg Type of key passed in |
| 578 | * \param key_data The encryption / decryption key data |
| 579 | * \param alg_arg The type of algorithm used |
| 580 | * \param input_data Data to encrypt / decrypt |
| 581 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 582 | * the data in to be encrypted / decrypted. If |
| 583 | * -1, no chunking |
| 584 | * \param expected_output Expected output |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 585 | * \param is_verify If non-zero this is a verify operation. |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 586 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 587 | * with normal length chunks. |
| 588 | * \return int Zero on failure, non-zero on success. |
| 589 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 590 | static int mac_multipart_internal_func(int key_type_arg, data_t *key_data, |
| 591 | int alg_arg, |
| 592 | data_t *input_data, |
| 593 | int data_part_len_arg, |
| 594 | data_t *expected_output, |
| 595 | int is_verify, |
| 596 | int do_zero_parts) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 597 | { |
| 598 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 599 | psa_key_type_t key_type = key_type_arg; |
| 600 | psa_algorithm_t alg = alg_arg; |
| 601 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 602 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 603 | size_t part_offset = 0; |
| 604 | size_t part_length = 0; |
| 605 | size_t data_part_len = 0; |
| 606 | size_t mac_len = 0; |
| 607 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 608 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 609 | |
| 610 | int test_ok = 0; |
| 611 | size_t part_count = 0; |
| 612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | PSA_INIT(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 615 | if (is_verify) { |
| 616 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 617 | } else { |
| 618 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 619 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 621 | psa_set_key_algorithm(&attributes, alg); |
| 622 | psa_set_key_type(&attributes, key_type); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 624 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 625 | &key)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 627 | if (is_verify) { |
| 628 | status = psa_mac_verify_setup(&operation, key, alg); |
| 629 | } else { |
| 630 | status = psa_mac_sign_setup(&operation, key, alg); |
| 631 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | PSA_ASSERT(status); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 635 | if (data_part_len_arg != -1) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 636 | /* Pass data in parts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 637 | data_part_len = (size_t) data_part_len_arg; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 639 | for (part_offset = 0, part_count = 0; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 640 | part_offset < input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | part_offset += part_length, part_count++) { |
| 642 | if (do_zero_parts && (part_count & 0x01)) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 643 | part_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 644 | } else if ((input_data->len - part_offset) < data_part_len) { |
| 645 | part_length = (input_data->len - part_offset); |
| 646 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 647 | part_length = data_part_len; |
| 648 | } |
| 649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 650 | PSA_ASSERT(psa_mac_update(&operation, |
| 651 | (input_data->x + part_offset), |
| 652 | part_length)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 653 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 654 | } else { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 655 | /* Pass all data in one go. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 656 | PSA_ASSERT(psa_mac_update(&operation, input_data->x, |
| 657 | input_data->len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 658 | } |
| 659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 660 | if (is_verify) { |
| 661 | PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x, |
| 662 | expected_output->len)); |
| 663 | } else { |
| 664 | PSA_ASSERT(psa_mac_sign_finish(&operation, mac, |
| 665 | PSA_MAC_MAX_SIZE, &mac_len)); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 666 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 667 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 668 | mac, mac_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | test_ok = 1; |
| 672 | |
| 673 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 674 | psa_destroy_key(key); |
| 675 | psa_mac_abort(&operation); |
| 676 | PSA_DONE(); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 678 | return test_ok; |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 681 | #if defined(PSA_WANT_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 682 | static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive, |
| 683 | psa_pake_operation_t *server, |
| 684 | psa_pake_operation_t *client, |
| 685 | int client_input_first, |
| 686 | int round, int inject_error) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 687 | { |
| 688 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 689 | size_t buffer_length = ( |
| 690 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 691 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 692 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2; |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 693 | /* The output should be exactly this size according to the spec */ |
| 694 | const size_t expected_size_key_share = |
| 695 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 696 | /* The output should be exactly this size according to the spec */ |
| 697 | const size_t expected_size_zk_public = |
| 698 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 699 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 700 | const size_t max_expected_size_zk_proof = |
| 701 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 702 | size_t buffer0_off = 0; |
| 703 | size_t buffer1_off = 0; |
| 704 | size_t s_g1_len, s_g2_len, s_a_len; |
| 705 | size_t s_g1_off, s_g2_off, s_a_off; |
| 706 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 707 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 708 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 709 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 710 | size_t c_g1_len, c_g2_len, c_a_len; |
| 711 | size_t c_g1_off, c_g2_off, c_a_off; |
| 712 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 713 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 714 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 715 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 716 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 717 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 718 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 719 | TEST_CALLOC(buffer0, buffer_length); |
| 720 | TEST_CALLOC(buffer1, buffer_length); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 722 | switch (round) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 723 | case 1: |
| 724 | /* Server first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 725 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 726 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 727 | buffer_length - buffer0_off, &s_g1_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 728 | TEST_EQUAL(s_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 729 | s_g1_off = buffer0_off; |
| 730 | buffer0_off += s_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 731 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 732 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 733 | buffer_length - buffer0_off, &s_x1_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 734 | TEST_EQUAL(s_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 735 | s_x1_pk_off = buffer0_off; |
| 736 | buffer0_off += s_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 737 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 738 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 739 | buffer_length - buffer0_off, &s_x1_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 740 | TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 741 | s_x1_pr_off = buffer0_off; |
| 742 | buffer0_off += s_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 743 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 744 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 745 | buffer_length - buffer0_off, &s_g2_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 746 | TEST_EQUAL(s_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 747 | s_g2_off = buffer0_off; |
| 748 | buffer0_off += s_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 750 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 751 | buffer_length - buffer0_off, &s_x2_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 752 | TEST_EQUAL(s_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 753 | s_x2_pk_off = buffer0_off; |
| 754 | buffer0_off += s_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 756 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 757 | buffer_length - buffer0_off, &s_x2_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 758 | TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 759 | s_x2_pr_off = buffer0_off; |
| 760 | buffer0_off += s_x2_pr_len; |
| 761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | if (inject_error == 1) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 763 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 764 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 765 | expected_status = PSA_ERROR_DATA_INVALID; |
| 766 | } |
| 767 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 768 | /* |
| 769 | * When injecting errors in inputs, the implementation is |
| 770 | * free to detect it right away of with a delay. |
| 771 | * This permits delaying the error until the end of the input |
| 772 | * sequence, if no error appears then, this will be treated |
| 773 | * as an error. |
| 774 | */ |
| 775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 776 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 777 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 778 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 779 | buffer0 + s_g1_off, s_g1_len); |
| 780 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 781 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 782 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 783 | } else { |
| 784 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 785 | } |
| 786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 787 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 788 | buffer0 + s_x1_pk_off, |
| 789 | s_x1_pk_len); |
| 790 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 791 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 792 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 793 | } else { |
| 794 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 795 | } |
| 796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 798 | buffer0 + s_x1_pr_off, |
| 799 | s_x1_pr_len); |
| 800 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 801 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 802 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 803 | } else { |
| 804 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 805 | } |
| 806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 807 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 808 | buffer0 + s_g2_off, |
| 809 | s_g2_len); |
| 810 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 811 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 812 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 813 | } else { |
| 814 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 815 | } |
| 816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 817 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 818 | buffer0 + s_x2_pk_off, |
| 819 | s_x2_pk_len); |
| 820 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 821 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 822 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 823 | } else { |
| 824 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 825 | } |
| 826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 827 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 828 | buffer0 + s_x2_pr_off, |
| 829 | s_x2_pr_len); |
| 830 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 831 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 832 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | } else { |
| 834 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 835 | } |
| 836 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 837 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 838 | if (inject_error == 1) { |
| 839 | TEST_ASSERT( |
| 840 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 841 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | /* Client first round Output */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 845 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 846 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 847 | buffer_length - buffer1_off, &c_g1_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 848 | TEST_EQUAL(c_g1_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 849 | c_g1_off = buffer1_off; |
| 850 | buffer1_off += c_g1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 851 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 852 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 853 | buffer_length - buffer1_off, &c_x1_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 854 | TEST_EQUAL(c_x1_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 855 | c_x1_pk_off = buffer1_off; |
| 856 | buffer1_off += c_x1_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 857 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 858 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 859 | buffer_length - buffer1_off, &c_x1_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 860 | TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 861 | c_x1_pr_off = buffer1_off; |
| 862 | buffer1_off += c_x1_pr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 863 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 864 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 865 | buffer_length - buffer1_off, &c_g2_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | TEST_EQUAL(c_g2_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 867 | c_g2_off = buffer1_off; |
| 868 | buffer1_off += c_g2_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 869 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 870 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 871 | buffer_length - buffer1_off, &c_x2_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 872 | TEST_EQUAL(c_x2_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 873 | c_x2_pk_off = buffer1_off; |
| 874 | buffer1_off += c_x2_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 875 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 876 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 877 | buffer_length - buffer1_off, &c_x2_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 878 | TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 879 | c_x2_pr_off = buffer1_off; |
| 880 | buffer1_off += c_x2_pr_len; |
| 881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 882 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 883 | /* Client first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 884 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 885 | buffer0 + s_g1_off, s_g1_len); |
| 886 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 887 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 888 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 889 | } else { |
| 890 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 891 | } |
| 892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 893 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 894 | buffer0 + s_x1_pk_off, |
| 895 | s_x1_pk_len); |
| 896 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 897 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 898 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 899 | } else { |
| 900 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 901 | } |
| 902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 903 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 904 | buffer0 + s_x1_pr_off, |
| 905 | s_x1_pr_len); |
| 906 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 907 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 908 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 909 | } else { |
| 910 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 911 | } |
| 912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 913 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 914 | buffer0 + s_g2_off, |
| 915 | s_g2_len); |
| 916 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 917 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 918 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 919 | } else { |
| 920 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 921 | } |
| 922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 923 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 924 | buffer0 + s_x2_pk_off, |
| 925 | s_x2_pk_len); |
| 926 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 927 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 928 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 929 | } else { |
| 930 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 931 | } |
| 932 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 933 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 934 | buffer0 + s_x2_pr_off, |
| 935 | s_x2_pr_len); |
| 936 | if (inject_error == 1 && status != PSA_SUCCESS) { |
| 937 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 938 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 939 | } else { |
| 940 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 941 | } |
| 942 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 943 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 944 | if (inject_error == 1) { |
| 945 | TEST_ASSERT( |
| 946 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 947 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 948 | } |
| 949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 950 | if (inject_error == 2) { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 951 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 952 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 953 | expected_status = PSA_ERROR_DATA_INVALID; |
| 954 | } |
| 955 | |
| 956 | /* Server first round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 957 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 958 | buffer1 + c_g1_off, c_g1_len); |
| 959 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 960 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 961 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 962 | } else { |
| 963 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 964 | } |
| 965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 966 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 967 | buffer1 + c_x1_pk_off, c_x1_pk_len); |
| 968 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 969 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 970 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 971 | } else { |
| 972 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 973 | } |
| 974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 975 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 976 | buffer1 + c_x1_pr_off, c_x1_pr_len); |
| 977 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 978 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 979 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | } else { |
| 981 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 982 | } |
| 983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 985 | buffer1 + c_g2_off, c_g2_len); |
| 986 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 987 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 988 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 989 | } else { |
| 990 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 991 | } |
| 992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 993 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 994 | buffer1 + c_x2_pk_off, c_x2_pk_len); |
| 995 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 996 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 997 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 998 | } else { |
| 999 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1002 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1003 | buffer1 + c_x2_pr_off, c_x2_pr_len); |
| 1004 | if (inject_error == 2 && status != PSA_SUCCESS) { |
| 1005 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1006 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1007 | } else { |
| 1008 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1009 | } |
| 1010 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1011 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1012 | if (inject_error == 2) { |
| 1013 | TEST_ASSERT( |
| 1014 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1015 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1016 | |
| 1017 | break; |
| 1018 | |
| 1019 | case 2: |
| 1020 | /* Server second round Output */ |
| 1021 | buffer0_off = 0; |
| 1022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1023 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1024 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1025 | buffer_length - buffer0_off, &s_a_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1026 | TEST_EQUAL(s_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1027 | s_a_off = buffer0_off; |
| 1028 | buffer0_off += s_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1029 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1030 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1031 | buffer_length - buffer0_off, &s_x2s_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1032 | TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1033 | s_x2s_pk_off = buffer0_off; |
| 1034 | buffer0_off += s_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1035 | PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1036 | buffer0 + buffer0_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1037 | buffer_length - buffer0_off, &s_x2s_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1038 | TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1039 | s_x2s_pr_off = buffer0_off; |
| 1040 | buffer0_off += s_x2s_pr_len; |
| 1041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1042 | if (inject_error == 3) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1043 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1044 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1045 | } |
| 1046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1047 | if (client_input_first == 1) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1048 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1049 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1050 | buffer0 + s_a_off, s_a_len); |
| 1051 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1052 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1053 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | } else { |
| 1055 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1056 | } |
| 1057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1058 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1059 | buffer0 + s_x2s_pk_off, |
| 1060 | s_x2s_pk_len); |
| 1061 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1062 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1063 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1064 | } else { |
| 1065 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1068 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1069 | buffer0 + s_x2s_pr_off, |
| 1070 | s_x2s_pr_len); |
| 1071 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1072 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1073 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1074 | } else { |
| 1075 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1078 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1079 | if (inject_error == 3) { |
| 1080 | TEST_ASSERT( |
| 1081 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1082 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /* Client second round Output */ |
| 1086 | buffer1_off = 0; |
| 1087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1088 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1089 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1090 | buffer_length - buffer1_off, &c_a_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1091 | TEST_EQUAL(c_a_len, expected_size_key_share); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1092 | c_a_off = buffer1_off; |
| 1093 | buffer1_off += c_a_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1094 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1095 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1096 | buffer_length - buffer1_off, &c_x2s_pk_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1097 | TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1098 | c_x2s_pk_off = buffer1_off; |
| 1099 | buffer1_off += c_x2s_pk_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1100 | PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1101 | buffer1 + buffer1_off, |
David Horstmann | 433a58c | 2024-01-25 16:29:26 +0000 | [diff] [blame] | 1102 | buffer_length - buffer1_off, &c_x2s_pr_len)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1103 | TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1104 | c_x2s_pr_off = buffer1_off; |
| 1105 | buffer1_off += c_x2s_pr_len; |
| 1106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1107 | if (client_input_first == 0) { |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1108 | /* Client second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1109 | status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE, |
| 1110 | buffer0 + s_a_off, s_a_len); |
| 1111 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1112 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1113 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1114 | } else { |
| 1115 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1116 | } |
| 1117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1118 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1119 | buffer0 + s_x2s_pk_off, |
| 1120 | s_x2s_pk_len); |
| 1121 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1122 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1123 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1124 | } else { |
| 1125 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1126 | } |
| 1127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1128 | status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF, |
| 1129 | buffer0 + s_x2s_pr_off, |
| 1130 | s_x2s_pr_len); |
| 1131 | if (inject_error == 3 && status != PSA_SUCCESS) { |
| 1132 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1133 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1134 | } else { |
| 1135 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1138 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1139 | if (inject_error == 3) { |
| 1140 | TEST_ASSERT( |
| 1141 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1142 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1145 | if (inject_error == 4) { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1146 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1147 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1148 | } |
| 1149 | |
| 1150 | /* Server second round Input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1151 | status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE, |
| 1152 | buffer1 + c_a_off, c_a_len); |
| 1153 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1154 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1155 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1156 | } else { |
| 1157 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1158 | } |
| 1159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1160 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1161 | buffer1 + c_x2s_pk_off, c_x2s_pk_len); |
| 1162 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1163 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1164 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1165 | } else { |
| 1166 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1167 | } |
| 1168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1169 | status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF, |
| 1170 | buffer1 + c_x2s_pr_off, c_x2s_pr_len); |
| 1171 | if (inject_error == 4 && status != PSA_SUCCESS) { |
| 1172 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1173 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1174 | } else { |
| 1175 | TEST_EQUAL(status, PSA_SUCCESS); |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1178 | /* Error didn't trigger, make test fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | if (inject_error == 4) { |
| 1180 | TEST_ASSERT( |
| 1181 | !"One of the last psa_pake_input() calls should have returned the expected error."); |
| 1182 | } |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1183 | |
| 1184 | break; |
| 1185 | |
| 1186 | } |
| 1187 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1188 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1189 | mbedtls_free(buffer0); |
| 1190 | mbedtls_free(buffer1); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1191 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1192 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1194 | typedef enum { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 1195 | INJECT_ERR_NONE = 0, |
| 1196 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1197 | INJECT_ERR_DUPLICATE_SETUP, |
| 1198 | INJECT_ERR_INVALID_USER, |
| 1199 | INJECT_ERR_INVALID_PEER, |
| 1200 | INJECT_ERR_SET_USER, |
| 1201 | INJECT_ERR_SET_PEER, |
| 1202 | INJECT_EMPTY_IO_BUFFER, |
| 1203 | INJECT_UNKNOWN_STEP, |
| 1204 | INJECT_INVALID_FIRST_STEP, |
| 1205 | INJECT_WRONG_BUFFER_SIZE, |
| 1206 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1207 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1208 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1209 | } ecjpake_injected_failure_t; |
| 1210 | |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1211 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Paul Elliott | 1243f93 | 2023-02-07 11:21:10 +0000 | [diff] [blame] | 1212 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1213 | static void interruptible_signverify_get_minmax_completes(uint32_t max_ops, |
| 1214 | psa_status_t expected_status, |
| 1215 | size_t *min_completes, |
| 1216 | size_t *max_completes) |
| 1217 | { |
| 1218 | |
| 1219 | /* This is slightly contrived, but we only really know that with a minimum |
| 1220 | value of max_ops that a successful operation should take more than one op |
| 1221 | to complete, and likewise that with a max_ops of |
| 1222 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */ |
| 1223 | if (max_ops == 0 || max_ops == 1) { |
Paul Elliott | c86d45e | 2023-02-15 17:38:05 +0000 | [diff] [blame] | 1224 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1225 | if (expected_status == PSA_SUCCESS) { |
| 1226 | *min_completes = 2; |
| 1227 | } else { |
| 1228 | *min_completes = 1; |
| 1229 | } |
| 1230 | |
| 1231 | *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 1232 | } else { |
| 1233 | *min_completes = 1; |
| 1234 | *max_completes = 1; |
| 1235 | } |
| 1236 | } |
Paul Elliott | 01885fa | 2023-02-09 12:07:30 +0000 | [diff] [blame] | 1237 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 1238 | |
Gilles Peskine | 1d25a0a | 2024-02-12 16:40:04 +0100 | [diff] [blame] | 1239 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 1240 | static int rsa_test_e(mbedtls_svc_key_id_t key, |
| 1241 | size_t bits, |
| 1242 | const data_t *e_arg) |
| 1243 | { |
| 1244 | uint8_t *exported = NULL; |
| 1245 | size_t exported_size = |
| 1246 | PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits); |
| 1247 | size_t exported_length = SIZE_MAX; |
| 1248 | int ok = 0; |
| 1249 | |
| 1250 | TEST_CALLOC(exported, exported_size); |
| 1251 | PSA_ASSERT(psa_export_public_key(key, |
| 1252 | exported, exported_size, |
| 1253 | &exported_length)); |
| 1254 | uint8_t *p = exported; |
| 1255 | uint8_t *end = exported + exported_length; |
| 1256 | size_t len; |
| 1257 | /* RSAPublicKey ::= SEQUENCE { |
| 1258 | * modulus INTEGER, -- n |
| 1259 | * publicExponent INTEGER } -- e |
| 1260 | */ |
| 1261 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 1262 | MBEDTLS_ASN1_SEQUENCE | |
| 1263 | MBEDTLS_ASN1_CONSTRUCTED)); |
| 1264 | TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1)); |
| 1265 | TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len, |
| 1266 | MBEDTLS_ASN1_INTEGER)); |
| 1267 | if (len >= 1 && p[0] == 0) { |
| 1268 | ++p; |
| 1269 | --len; |
| 1270 | } |
| 1271 | if (e_arg->len == 0) { |
| 1272 | TEST_EQUAL(len, 3); |
| 1273 | TEST_EQUAL(p[0], 1); |
| 1274 | TEST_EQUAL(p[1], 0); |
| 1275 | TEST_EQUAL(p[2], 1); |
| 1276 | } else { |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 1277 | const uint8_t *expected = e_arg->x; |
| 1278 | size_t expected_len = e_arg->len; |
| 1279 | while (expected_len > 0 && *expected == 0) { |
| 1280 | ++expected; |
| 1281 | --expected_len; |
| 1282 | } |
| 1283 | TEST_MEMORY_COMPARE(p, len, expected, expected_len); |
Gilles Peskine | 1d25a0a | 2024-02-12 16:40:04 +0100 | [diff] [blame] | 1284 | } |
| 1285 | ok = 1; |
| 1286 | |
| 1287 | exit: |
| 1288 | mbedtls_free(exported); |
| 1289 | return ok; |
| 1290 | } |
| 1291 | #endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE */ |
| 1292 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1293 | static int setup_key_production_parameters( |
| 1294 | psa_key_production_parameters_t **params, size_t *params_data_length, |
| 1295 | int flags_arg, const data_t *params_data) |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 1296 | { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1297 | *params_data_length = params_data->len; |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1298 | /* If there are N bytes of padding at the end of |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1299 | * psa_key_production_parameters_t, then it's enough to allocate |
| 1300 | * MIN(sizeof(psa_key_production_parameters_t), |
| 1301 | * offsetof(psa_key_production_parameters_t, data) + params_data_length). |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1302 | * |
| 1303 | * For simplicity, here, we allocate up to N more bytes than necessary. |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1304 | * In practice, the current layout of psa_key_production_parameters_t |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 1305 | * makes padding extremely unlikely, so we don't worry about testing |
| 1306 | * that the library code doesn't try to access these extra N bytes. |
| 1307 | */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 1308 | *params = mbedtls_calloc(1, sizeof(**params) + *params_data_length); |
| 1309 | TEST_ASSERT(*params != NULL); |
| 1310 | (*params)->flags = (uint32_t) flags_arg; |
| 1311 | memcpy((*params)->data, params_data->x, params_data->len); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 1312 | return 1; |
| 1313 | exit: |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1317 | #if defined(MBEDTLS_THREADING_PTHREAD) |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1318 | |
| 1319 | typedef struct same_key_context { |
| 1320 | data_t *data; |
| 1321 | mbedtls_svc_key_id_t key; |
| 1322 | psa_key_attributes_t *attributes; |
| 1323 | int type; |
| 1324 | int bits; |
| 1325 | /* The following two parameters are used to ensure that when multiple |
| 1326 | * threads attempt to load/destroy the key, exactly one thread succeeds. */ |
| 1327 | int key_loaded; |
| 1328 | mbedtls_threading_mutex_t MBEDTLS_PRIVATE(key_loaded_mutex); |
| 1329 | } |
| 1330 | same_key_context; |
| 1331 | |
| 1332 | /* Attempt to import the key in ctx. This handles any valid error codes |
| 1333 | * and reports an error for any invalid codes. This function also insures |
| 1334 | * that once imported by some thread, all threads can use the key. */ |
Michael Schuster | b1e33fb | 2024-06-04 02:30:22 +0200 | [diff] [blame] | 1335 | static void *thread_import_key(void *ctx) |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1336 | { |
| 1337 | mbedtls_svc_key_id_t returned_key_id; |
| 1338 | same_key_context *skc = (struct same_key_context *) ctx; |
| 1339 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1340 | |
Ryan Everett | 6c48870 | 2024-03-14 17:49:44 +0000 | [diff] [blame] | 1341 | /* Import the key, exactly one thread must succeed. */ |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1342 | psa_status_t status = psa_import_key(skc->attributes, skc->data->x, |
| 1343 | skc->data->len, &returned_key_id); |
| 1344 | switch (status) { |
| 1345 | case PSA_SUCCESS: |
| 1346 | if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) { |
| 1347 | if (skc->key_loaded) { |
| 1348 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1349 | /* More than one thread has succeeded, report a failure. */ |
Ryan Everett | 3de040f | 2024-03-14 17:50:06 +0000 | [diff] [blame] | 1350 | TEST_FAIL("The same key has been loaded into the key store multiple times."); |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1351 | } |
| 1352 | skc->key_loaded = 1; |
| 1353 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1354 | } |
| 1355 | break; |
| 1356 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 1357 | /* If all of the key slots are reserved when a thread |
| 1358 | * locks the mutex to reserve a new slot, it will return |
| 1359 | * PSA_ERROR_INSUFFICIENT_MEMORY; this is correct behaviour. |
| 1360 | * There is a chance for this to occur here when the number of |
| 1361 | * threads running this function is larger than the number of |
| 1362 | * free key slots. Each thread reserves an empty key slot, |
| 1363 | * unlocks the mutex, then relocks it to finalize key creation. |
| 1364 | * It is at that point where the thread sees that the key |
| 1365 | * already exists, releases the reserved slot, |
| 1366 | * and returns PSA_ERROR_ALREADY_EXISTS. |
| 1367 | * There is no guarantee that the key is loaded upon this return |
| 1368 | * code, so we can't test the key information. Just stop this |
| 1369 | * thread from executing, note that this is not an error. */ |
| 1370 | goto exit; |
| 1371 | break; |
| 1372 | case PSA_ERROR_ALREADY_EXISTS: |
| 1373 | /* The key has been loaded by a different thread. */ |
| 1374 | break; |
| 1375 | default: |
| 1376 | PSA_ASSERT(status); |
| 1377 | } |
| 1378 | /* At this point the key must exist, test the key information. */ |
| 1379 | status = psa_get_key_attributes(skc->key, &got_attributes); |
| 1380 | if (status == PSA_ERROR_INSUFFICIENT_MEMORY) { |
| 1381 | /* This is not a test failure. The following sequence of events |
| 1382 | * causes this to occur: |
| 1383 | * 1: This thread successfuly imports a persistent key skc->key. |
| 1384 | * 2: N threads reserve an empty key slot in psa_import_key, |
| 1385 | * where N is equal to the number of free key slots. |
| 1386 | * 3: A final thread attempts to reserve an empty key slot, kicking |
| 1387 | * skc->key (which has no registered readers) out of its slot. |
| 1388 | * 4: This thread calls psa_get_key_attributes(skc->key,...): |
| 1389 | * it sees that skc->key is not in a slot, attempts to load it and |
| 1390 | * finds that there are no free slots. |
| 1391 | * This thread returns PSA_ERROR_INSUFFICIENT_MEMORY. |
| 1392 | * |
| 1393 | * The PSA spec allows this behaviour, it is an unavoidable consequence |
| 1394 | * of allowing persistent keys to be kicked out of the key store while |
| 1395 | * they are still valid. */ |
| 1396 | goto exit; |
| 1397 | } |
| 1398 | PSA_ASSERT(status); |
| 1399 | TEST_EQUAL(psa_get_key_type(&got_attributes), skc->type); |
| 1400 | TEST_EQUAL(psa_get_key_bits(&got_attributes), skc->bits); |
| 1401 | |
| 1402 | exit: |
| 1403 | /* Key attributes may have been returned by psa_get_key_attributes(), |
| 1404 | * reset them as required. */ |
| 1405 | psa_reset_key_attributes(&got_attributes); |
| 1406 | return NULL; |
| 1407 | } |
| 1408 | |
Michael Schuster | b1e33fb | 2024-06-04 02:30:22 +0200 | [diff] [blame] | 1409 | static void *thread_use_and_destroy_key(void *ctx) |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1410 | { |
| 1411 | same_key_context *skc = (struct same_key_context *) ctx; |
| 1412 | |
| 1413 | /* Do something with the key according |
| 1414 | * to its type and permitted usage. */ |
| 1415 | TEST_ASSERT(mbedtls_test_psa_exercise_key(skc->key, |
| 1416 | skc->attributes->policy.usage, |
| 1417 | skc->attributes->policy.alg, 1)); |
| 1418 | |
| 1419 | psa_status_t status = psa_destroy_key(skc->key); |
| 1420 | if (status == PSA_SUCCESS) { |
| 1421 | if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) { |
| 1422 | /* Ensure that we are the only thread to succeed. */ |
| 1423 | if (skc->key_loaded != 1) { |
| 1424 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
Ryan Everett | 3de040f | 2024-03-14 17:50:06 +0000 | [diff] [blame] | 1425 | TEST_FAIL("The same key has been destroyed multiple times."); |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1426 | } |
| 1427 | skc->key_loaded = 0; |
| 1428 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1429 | } |
| 1430 | } else { |
| 1431 | TEST_EQUAL(status, PSA_ERROR_INVALID_HANDLE); |
| 1432 | } |
| 1433 | |
| 1434 | exit: |
| 1435 | return NULL; |
| 1436 | } |
| 1437 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1438 | typedef struct generate_key_context { |
| 1439 | psa_key_type_t type; |
| 1440 | psa_key_usage_t usage; |
| 1441 | size_t bits; |
| 1442 | psa_algorithm_t alg; |
| 1443 | psa_status_t expected_status; |
| 1444 | psa_key_attributes_t *attributes; |
| 1445 | int is_large_key; |
| 1446 | int reps; |
| 1447 | } |
| 1448 | generate_key_context; |
Michael Schuster | b1e33fb | 2024-06-04 02:30:22 +0200 | [diff] [blame] | 1449 | static void *thread_generate_key(void *ctx) |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1450 | { |
| 1451 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1452 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1453 | generate_key_context *gkc = (struct generate_key_context *) ctx; |
| 1454 | |
| 1455 | /* If there are race conditions, it is likely the case that they do not |
| 1456 | * arise every time the code runs. We repeat the code to increase the |
| 1457 | * chance that any race conditions will be hit. */ |
| 1458 | for (int n = 0; n < gkc->reps; n++) { |
| 1459 | /* Generate a key */ |
| 1460 | psa_status_t status = psa_generate_key(gkc->attributes, &key); |
| 1461 | |
| 1462 | if (gkc->is_large_key > 0) { |
| 1463 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1464 | } |
| 1465 | |
| 1466 | TEST_EQUAL(status, gkc->expected_status); |
| 1467 | if (gkc->expected_status != PSA_SUCCESS) { |
| 1468 | PSA_ASSERT(psa_destroy_key(key)); |
| 1469 | goto exit; |
| 1470 | } |
| 1471 | |
| 1472 | /* Test the key information */ |
| 1473 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1474 | TEST_EQUAL(psa_get_key_type(&got_attributes), gkc->type); |
| 1475 | TEST_EQUAL(psa_get_key_bits(&got_attributes), gkc->bits); |
| 1476 | |
| 1477 | /* Do something with the key according |
| 1478 | * to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1479 | if (!mbedtls_test_psa_exercise_key(key, gkc->usage, gkc->alg, 0)) { |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1480 | psa_destroy_key(key); |
| 1481 | goto exit; |
| 1482 | } |
| 1483 | psa_reset_key_attributes(&got_attributes); |
| 1484 | |
| 1485 | PSA_ASSERT(psa_destroy_key(key)); |
| 1486 | } |
| 1487 | exit: |
| 1488 | /* |
| 1489 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1490 | * thus reset them as required. |
| 1491 | */ |
| 1492 | psa_reset_key_attributes(&got_attributes); |
| 1493 | return NULL; |
| 1494 | } |
| 1495 | #endif /* MBEDTLS_THREADING_PTHREAD */ |
| 1496 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1497 | /* END_HEADER */ |
| 1498 | |
| 1499 | /* BEGIN_DEPENDENCIES |
| 1500 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1501 | * END_DEPENDENCIES |
| 1502 | */ |
| 1503 | |
| 1504 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1505 | void psa_can_do_hash() |
| 1506 | { |
| 1507 | /* We can't test that this is specific to drivers until partial init has |
| 1508 | * been implemented, but we can at least test before/after full init. */ |
| 1509 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1510 | PSA_INIT(); |
| 1511 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1512 | PSA_DONE(); |
| 1513 | } |
| 1514 | /* END_CASE */ |
| 1515 | |
| 1516 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1517 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1518 | { |
| 1519 | size_t max_truncated_mac_size = |
| 1520 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1521 | |
| 1522 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1523 | * encoding. The shifted mask is the maximum truncated value. The |
| 1524 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1525 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1526 | } |
| 1527 | /* END_CASE */ |
| 1528 | |
| 1529 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1530 | void import_with_policy(int type_arg, |
| 1531 | int usage_arg, int alg_arg, |
| 1532 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1533 | { |
| 1534 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1535 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1536 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1537 | psa_key_type_t type = type_arg; |
| 1538 | psa_key_usage_t usage = usage_arg; |
| 1539 | psa_algorithm_t alg = alg_arg; |
| 1540 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1541 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1542 | psa_status_t status; |
| 1543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1544 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1546 | psa_set_key_type(&attributes, type); |
| 1547 | psa_set_key_usage_flags(&attributes, usage); |
| 1548 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1550 | status = psa_import_key(&attributes, |
| 1551 | key_material, sizeof(key_material), |
| 1552 | &key); |
| 1553 | TEST_EQUAL(status, expected_status); |
| 1554 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1555 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1556 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1558 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1559 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1560 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1561 | mbedtls_test_update_key_usage_flags(usage)); |
| 1562 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1563 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1565 | PSA_ASSERT(psa_destroy_key(key)); |
| 1566 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1567 | |
| 1568 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1569 | /* |
| 1570 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1571 | * thus reset them as required. |
| 1572 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1573 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1575 | psa_destroy_key(key); |
| 1576 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1577 | } |
| 1578 | /* END_CASE */ |
| 1579 | |
| 1580 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1581 | void import_with_data(data_t *data, int type_arg, |
| 1582 | int attr_bits_arg, |
| 1583 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1584 | { |
| 1585 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1586 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1587 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1588 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1589 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1590 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1591 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1593 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1595 | psa_set_key_type(&attributes, type); |
| 1596 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1598 | status = psa_import_key(&attributes, data->x, data->len, &key); |
Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1599 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. |
| 1600 | * |
| 1601 | * This can happen with a type supported only by a driver: |
| 1602 | * - the driver sees the invalid data (for example wrong size) and thinks |
| 1603 | * "well perhaps this is a key size I don't support" so it returns |
| 1604 | * NOT_SUPPORTED which is correct at this point; |
| 1605 | * - we fallback to built-ins, which don't support this type, so return |
| 1606 | * NOT_SUPPORTED which again is correct at this point. |
| 1607 | */ |
| 1608 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && |
| 1609 | status == PSA_ERROR_NOT_SUPPORTED) { |
| 1610 | ; // OK |
| 1611 | } else { |
| 1612 | TEST_EQUAL(status, expected_status); |
| 1613 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1614 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1615 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1616 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1618 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1619 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1620 | if (attr_bits != 0) { |
| 1621 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1622 | } |
| 1623 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1625 | PSA_ASSERT(psa_destroy_key(key)); |
| 1626 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1627 | |
| 1628 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1629 | /* |
| 1630 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1631 | * thus reset them as required. |
| 1632 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1633 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1635 | psa_destroy_key(key); |
| 1636 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1637 | } |
| 1638 | /* END_CASE */ |
| 1639 | |
| 1640 | /* BEGIN_CASE */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1641 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | void import_large_key(int type_arg, int byte_size_arg, |
| 1643 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1644 | { |
| 1645 | psa_key_type_t type = type_arg; |
| 1646 | size_t byte_size = byte_size_arg; |
| 1647 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1648 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1649 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1650 | psa_status_t status; |
| 1651 | uint8_t *buffer = NULL; |
| 1652 | size_t buffer_size = byte_size + 1; |
| 1653 | size_t n; |
| 1654 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1655 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1656 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1657 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1658 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1660 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1661 | |
| 1662 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1663 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1664 | psa_set_key_type(&attributes, type); |
| 1665 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1666 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1667 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1669 | if (status == PSA_SUCCESS) { |
| 1670 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1671 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1672 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1673 | PSA_BYTES_TO_BITS(byte_size)); |
| 1674 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1675 | memset(buffer, 0, byte_size + 1); |
| 1676 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1677 | for (n = 0; n < byte_size; n++) { |
| 1678 | TEST_EQUAL(buffer[n], 'K'); |
| 1679 | } |
| 1680 | for (n = byte_size; n < buffer_size; n++) { |
| 1681 | TEST_EQUAL(buffer[n], 0); |
| 1682 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1686 | /* |
| 1687 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1688 | * thus reset them as required. |
| 1689 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1690 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1692 | psa_destroy_key(key); |
| 1693 | PSA_DONE(); |
| 1694 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1695 | } |
| 1696 | /* END_CASE */ |
| 1697 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1698 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1699 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1700 | * inside, beyond having sensible size and parity). This is expected to |
| 1701 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1702 | void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1703 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1704 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1705 | size_t bits = bits_arg; |
| 1706 | psa_status_t expected_status = expected_status_arg; |
| 1707 | psa_status_t status; |
| 1708 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1709 | 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] | 1710 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1711 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1712 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1713 | unsigned char *p; |
| 1714 | int ret; |
| 1715 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1716 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1718 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1719 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1721 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1722 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1723 | length = ret; |
| 1724 | |
| 1725 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1726 | psa_set_key_type(&attributes, type); |
| 1727 | status = psa_import_key(&attributes, p, length, &key); |
| 1728 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1730 | if (status == PSA_SUCCESS) { |
| 1731 | PSA_ASSERT(psa_destroy_key(key)); |
| 1732 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1733 | |
| 1734 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1735 | mbedtls_free(buffer); |
| 1736 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1737 | } |
| 1738 | /* END_CASE */ |
| 1739 | |
| 1740 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1741 | void import_export(data_t *data, |
| 1742 | int type_arg, |
| 1743 | int usage_arg, int alg_arg, |
| 1744 | int lifetime_arg, |
| 1745 | int expected_bits, |
| 1746 | int export_size_delta, |
| 1747 | int expected_export_status_arg, |
| 1748 | /*whether reexport must give the original input exactly*/ |
| 1749 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1750 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1751 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1752 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1753 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1754 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1755 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1756 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1757 | unsigned char *exported = NULL; |
| 1758 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1759 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1760 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1761 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1762 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1763 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1764 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1765 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1766 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1767 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1768 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1769 | } |
| 1770 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1772 | psa_set_key_lifetime(&attributes, lifetime); |
| 1773 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1774 | psa_set_key_algorithm(&attributes, alg); |
| 1775 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1776 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1777 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1778 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1779 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1780 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1781 | } |
| 1782 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1783 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1784 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1785 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1786 | |
| 1787 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1788 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1789 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1790 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1791 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1792 | |
| 1793 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1794 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1795 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1796 | |
| 1797 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1798 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1799 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1800 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1801 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1803 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1804 | export_size - exported_length)); |
| 1805 | if (status != PSA_SUCCESS) { |
| 1806 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1807 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1808 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1809 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1810 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1811 | * this validates the canonical representations. For canonical inputs, |
| 1812 | * this doesn't directly validate the implementation, but it still helps |
| 1813 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1814 | if (!psa_key_lifetime_is_external(lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1815 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1816 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1817 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1818 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1820 | if (canonical_input) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1821 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1822 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1823 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1824 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1825 | &key2)); |
| 1826 | PSA_ASSERT(psa_export_key(key2, |
| 1827 | reexported, |
| 1828 | export_size, |
| 1829 | &reexported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1830 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1831 | reexported, reexported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1832 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1833 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1834 | TEST_LE_U(exported_length, |
| 1835 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1836 | psa_get_key_bits(&got_attributes))); |
Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1837 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 1838 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 1839 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 1840 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1841 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1842 | |
| 1843 | destroy: |
| 1844 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1845 | PSA_ASSERT(psa_destroy_key(key)); |
| 1846 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1847 | |
| 1848 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1849 | /* |
| 1850 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1851 | * thus reset them as required. |
| 1852 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1853 | psa_reset_key_attributes(&got_attributes); |
| 1854 | psa_destroy_key(key); |
| 1855 | mbedtls_free(exported); |
| 1856 | mbedtls_free(reexported); |
| 1857 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1858 | } |
| 1859 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1860 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1861 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1862 | void import_export_public_key(data_t *data, |
| 1863 | int type_arg, // key pair or public key |
| 1864 | int alg_arg, |
| 1865 | int lifetime_arg, |
| 1866 | int export_size_delta, |
| 1867 | int expected_export_status_arg, |
| 1868 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1869 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1870 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1871 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1872 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1873 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1874 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1875 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1876 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1877 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1878 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1879 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1881 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1883 | psa_set_key_lifetime(&attributes, lifetime); |
| 1884 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1885 | psa_set_key_algorithm(&attributes, alg); |
| 1886 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1887 | |
| 1888 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1889 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1890 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1891 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1892 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1893 | status = psa_export_public_key(key, |
| 1894 | exported, export_size, |
| 1895 | &exported_length); |
| 1896 | TEST_EQUAL(status, expected_export_status); |
| 1897 | if (status == PSA_SUCCESS) { |
| 1898 | 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] | 1899 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1900 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1901 | bits = psa_get_key_bits(&attributes); |
| 1902 | TEST_LE_U(expected_public_key->len, |
| 1903 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1904 | TEST_LE_U(expected_public_key->len, |
| 1905 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1906 | TEST_LE_U(expected_public_key->len, |
| 1907 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1908 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1909 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1910 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1911 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1912 | /* |
| 1913 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1914 | * thus reset them as required. |
| 1915 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1918 | mbedtls_free(exported); |
| 1919 | psa_destroy_key(key); |
| 1920 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1921 | } |
| 1922 | /* END_CASE */ |
| 1923 | |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1924 | |
| 1925 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 1926 | /* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 1927 | void concurrently_use_same_persistent_key(data_t *data, |
| 1928 | int type_arg, |
| 1929 | int bits_arg, |
| 1930 | int alg_arg, |
| 1931 | int thread_count_arg) |
| 1932 | { |
| 1933 | size_t thread_count = (size_t) thread_count_arg; |
| 1934 | mbedtls_test_thread_t *threads = NULL; |
| 1935 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1); |
| 1936 | same_key_context skc; |
| 1937 | skc.data = data; |
| 1938 | skc.key = key_id; |
| 1939 | skc.type = type_arg; |
| 1940 | skc.bits = bits_arg; |
| 1941 | skc.key_loaded = 0; |
| 1942 | mbedtls_mutex_init(&skc.key_loaded_mutex); |
| 1943 | psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(skc.type, alg_arg); |
| 1944 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1945 | |
| 1946 | PSA_ASSERT(psa_crypto_init()); |
| 1947 | |
| 1948 | psa_set_key_id(&attributes, key_id); |
| 1949 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT); |
| 1950 | psa_set_key_usage_flags(&attributes, usage); |
| 1951 | psa_set_key_algorithm(&attributes, alg_arg); |
| 1952 | psa_set_key_type(&attributes, type_arg); |
| 1953 | psa_set_key_bits(&attributes, bits_arg); |
| 1954 | skc.attributes = &attributes; |
| 1955 | |
| 1956 | TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count); |
| 1957 | |
| 1958 | /* Test that when multiple threads import the same key, |
| 1959 | * exactly one thread succeeds and the rest fail with valid errors. |
| 1960 | * Also test that all threads can use the key as soon as it has been |
| 1961 | * imported. */ |
| 1962 | for (size_t i = 0; i < thread_count; i++) { |
| 1963 | TEST_EQUAL( |
| 1964 | mbedtls_test_thread_create(&threads[i], thread_import_key, |
| 1965 | (void *) &skc), 0); |
| 1966 | } |
| 1967 | |
| 1968 | /* Join threads. */ |
| 1969 | for (size_t i = 0; i < thread_count; i++) { |
| 1970 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 1971 | } |
| 1972 | |
| 1973 | /* Test that when multiple threads use and destroy a key no corruption |
| 1974 | * occurs, and exactly one thread succeeds when destroying the key. */ |
| 1975 | for (size_t i = 0; i < thread_count; i++) { |
| 1976 | TEST_EQUAL( |
| 1977 | mbedtls_test_thread_create(&threads[i], thread_use_and_destroy_key, |
| 1978 | (void *) &skc), 0); |
| 1979 | } |
| 1980 | |
| 1981 | /* Join threads. */ |
| 1982 | for (size_t i = 0; i < thread_count; i++) { |
| 1983 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 1984 | } |
| 1985 | /* Ensure that one thread succeeded in destroying the key. */ |
| 1986 | TEST_ASSERT(!skc.key_loaded); |
| 1987 | exit: |
| 1988 | psa_reset_key_attributes(&attributes); |
| 1989 | mbedtls_mutex_free(&skc.key_loaded_mutex); |
| 1990 | mbedtls_free(threads); |
| 1991 | PSA_DONE(); |
| 1992 | } |
| 1993 | /* END_CASE */ |
| 1994 | #endif |
| 1995 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1996 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1997 | void import_and_exercise_key(data_t *data, |
| 1998 | int type_arg, |
| 1999 | int bits_arg, |
| 2000 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2001 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2002 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2003 | psa_key_type_t type = type_arg; |
| 2004 | size_t bits = bits_arg; |
| 2005 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2006 | 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] | 2007 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2008 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2010 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2012 | psa_set_key_usage_flags(&attributes, usage); |
| 2013 | psa_set_key_algorithm(&attributes, alg); |
| 2014 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2015 | |
| 2016 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2017 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2018 | |
| 2019 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2020 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2021 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 2022 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2023 | |
| 2024 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2025 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 2026 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2027 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2029 | PSA_ASSERT(psa_destroy_key(key)); |
| 2030 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 2031 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2032 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2033 | /* |
| 2034 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2035 | * thus reset them as required. |
| 2036 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2037 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2039 | psa_reset_key_attributes(&attributes); |
| 2040 | psa_destroy_key(key); |
| 2041 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2042 | } |
| 2043 | /* END_CASE */ |
| 2044 | |
| 2045 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2046 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 2047 | int bits_arg, int expected_bits_arg, |
| 2048 | int usage_arg, int expected_usage_arg, |
| 2049 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2050 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2051 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 2052 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2053 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 2054 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2055 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2056 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2057 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2058 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2059 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2060 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2061 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2062 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2064 | psa_set_key_usage_flags(&attributes, usage); |
| 2065 | psa_set_key_algorithm(&attributes, alg); |
| 2066 | psa_set_key_type(&attributes, key_type); |
| 2067 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2069 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 2070 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2072 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2073 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 2074 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 2075 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 2076 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2077 | |
| 2078 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2079 | /* |
| 2080 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2081 | * thus reset them as required. |
| 2082 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2083 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2085 | psa_destroy_key(key); |
| 2086 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2087 | } |
| 2088 | /* END_CASE */ |
| 2089 | |
| 2090 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2091 | void check_key_policy(int type_arg, int bits_arg, |
| 2092 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2093 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2094 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 2095 | usage_arg, |
| 2096 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 2097 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2098 | goto exit; |
| 2099 | } |
| 2100 | /* END_CASE */ |
| 2101 | |
| 2102 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2103 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2104 | { |
| 2105 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2106 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2107 | * 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] | 2108 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2109 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2110 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 2111 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2115 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 2116 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 2117 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2119 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 2120 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 2121 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2123 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 2124 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 2125 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2127 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 2128 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 2129 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2131 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 2132 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 2133 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2134 | } |
| 2135 | /* END_CASE */ |
| 2136 | |
| 2137 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2138 | void mac_key_policy(int policy_usage_arg, |
| 2139 | int policy_alg_arg, |
| 2140 | int key_type_arg, |
| 2141 | data_t *key_data, |
| 2142 | int exercise_alg_arg, |
| 2143 | int expected_status_sign_arg, |
| 2144 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2145 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2146 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2147 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2148 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2149 | psa_key_type_t key_type = key_type_arg; |
| 2150 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 2151 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 2152 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2153 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 2154 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 2155 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2156 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2158 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2160 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2161 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2162 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2164 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2165 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2167 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 2168 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2170 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2171 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2172 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2173 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2174 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2175 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2176 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 2177 | input, 128, |
| 2178 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 2179 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2180 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2181 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2182 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2183 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2184 | if (status == PSA_SUCCESS) { |
| 2185 | status = psa_mac_update(&operation, input, 128); |
| 2186 | if (status == PSA_SUCCESS) { |
| 2187 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 2188 | &mac_len), |
| 2189 | expected_status_sign); |
| 2190 | } else { |
| 2191 | TEST_EQUAL(status, expected_status_sign); |
| 2192 | } |
| 2193 | } else { |
| 2194 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2195 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2196 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2197 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2198 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2199 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 2200 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2203 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2204 | } else { |
| 2205 | TEST_EQUAL(status, expected_status_verify); |
| 2206 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2207 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2208 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2209 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2210 | if (status == PSA_SUCCESS) { |
| 2211 | status = psa_mac_update(&operation, input, 128); |
| 2212 | if (status == PSA_SUCCESS) { |
| 2213 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 2214 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2215 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2216 | } else { |
| 2217 | TEST_EQUAL(status, expected_status_verify); |
| 2218 | } |
| 2219 | } else { |
| 2220 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2221 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2222 | } else { |
| 2223 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2224 | } |
| 2225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2226 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2228 | memset(mac, 0, sizeof(mac)); |
| 2229 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2230 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2231 | |
| 2232 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2233 | psa_mac_abort(&operation); |
| 2234 | psa_destroy_key(key); |
| 2235 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2236 | } |
| 2237 | /* END_CASE */ |
| 2238 | |
| 2239 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2240 | void cipher_key_policy(int policy_usage_arg, |
| 2241 | int policy_alg, |
| 2242 | int key_type, |
| 2243 | data_t *key_data, |
| 2244 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2245 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2246 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2247 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2248 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2249 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2250 | size_t output_buffer_size = 0; |
| 2251 | size_t input_buffer_size = 0; |
| 2252 | size_t output_length = 0; |
| 2253 | uint8_t *output = NULL; |
| 2254 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2255 | psa_status_t status; |
| 2256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2257 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 2258 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 2259 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2260 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2261 | TEST_CALLOC(input, input_buffer_size); |
| 2262 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2264 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2266 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2267 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2268 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2270 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2271 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2272 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2273 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2274 | TEST_EQUAL(policy_usage, |
| 2275 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2276 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2277 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2278 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 2279 | output, output_buffer_size, |
| 2280 | &output_length); |
| 2281 | if (policy_alg == exercise_alg && |
| 2282 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2283 | PSA_ASSERT(status); |
| 2284 | } else { |
| 2285 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2286 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2287 | |
| 2288 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2289 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 2290 | if (policy_alg == exercise_alg && |
| 2291 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2292 | PSA_ASSERT(status); |
| 2293 | } else { |
| 2294 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2295 | } |
| 2296 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2297 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2298 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2299 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 2300 | input, input_buffer_size, |
| 2301 | &output_length); |
| 2302 | if (policy_alg == exercise_alg && |
| 2303 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2304 | PSA_ASSERT(status); |
| 2305 | } else { |
| 2306 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2307 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2308 | |
| 2309 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2310 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 2311 | if (policy_alg == exercise_alg && |
| 2312 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2313 | PSA_ASSERT(status); |
| 2314 | } else { |
| 2315 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2316 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2317 | |
| 2318 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2319 | psa_cipher_abort(&operation); |
| 2320 | mbedtls_free(input); |
| 2321 | mbedtls_free(output); |
| 2322 | psa_destroy_key(key); |
| 2323 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2324 | } |
| 2325 | /* END_CASE */ |
| 2326 | |
| 2327 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2328 | void aead_key_policy(int policy_usage_arg, |
| 2329 | int policy_alg, |
| 2330 | int key_type, |
| 2331 | data_t *key_data, |
| 2332 | int nonce_length_arg, |
| 2333 | int tag_length_arg, |
| 2334 | int exercise_alg, |
| 2335 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2336 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2337 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2338 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2339 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2340 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2341 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2342 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2343 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2344 | size_t nonce_length = nonce_length_arg; |
| 2345 | unsigned char tag[16]; |
| 2346 | size_t tag_length = tag_length_arg; |
| 2347 | size_t output_length; |
| 2348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2349 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2350 | TEST_LE_U(tag_length, sizeof(tag)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2352 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2354 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2355 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2356 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2358 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2359 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2360 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2361 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2362 | TEST_EQUAL(policy_usage, |
| 2363 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2364 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2365 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2366 | status = psa_aead_encrypt(key, exercise_alg, |
| 2367 | nonce, nonce_length, |
| 2368 | NULL, 0, |
| 2369 | NULL, 0, |
| 2370 | tag, tag_length, |
| 2371 | &output_length); |
| 2372 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2373 | TEST_EQUAL(status, expected_status); |
| 2374 | } else { |
| 2375 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2376 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2377 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2378 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2379 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2380 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2381 | TEST_EQUAL(status, expected_status); |
| 2382 | } else { |
| 2383 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2384 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2385 | |
| 2386 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2387 | memset(tag, 0, sizeof(tag)); |
| 2388 | status = psa_aead_decrypt(key, exercise_alg, |
| 2389 | nonce, nonce_length, |
| 2390 | NULL, 0, |
| 2391 | tag, tag_length, |
| 2392 | NULL, 0, |
| 2393 | &output_length); |
| 2394 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2395 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2396 | } else if (expected_status == PSA_SUCCESS) { |
| 2397 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2398 | } else { |
| 2399 | TEST_EQUAL(status, expected_status); |
| 2400 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2401 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2402 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2403 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2404 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2405 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2406 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2407 | } else { |
| 2408 | TEST_EQUAL(status, expected_status); |
| 2409 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2410 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2411 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2412 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2413 | psa_destroy_key(key); |
| 2414 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2415 | } |
| 2416 | /* END_CASE */ |
| 2417 | |
| 2418 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2419 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2420 | int policy_alg, |
| 2421 | int key_type, |
| 2422 | data_t *key_data, |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2423 | int exercise_alg, |
| 2424 | int use_opaque_key) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2425 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2426 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2427 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2428 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2429 | psa_status_t status; |
| 2430 | size_t key_bits; |
| 2431 | size_t buffer_length; |
| 2432 | unsigned char *buffer = NULL; |
| 2433 | size_t output_length; |
| 2434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2435 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2437 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2438 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2439 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2440 | |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2441 | if (use_opaque_key) { |
| 2442 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 2443 | PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION)); |
| 2444 | } |
| 2445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2446 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2447 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2448 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2449 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2450 | TEST_EQUAL(policy_usage, |
| 2451 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2453 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2454 | key_bits = psa_get_key_bits(&attributes); |
| 2455 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2456 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2457 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2459 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2460 | NULL, 0, |
| 2461 | NULL, 0, |
| 2462 | buffer, buffer_length, |
| 2463 | &output_length); |
| 2464 | if (policy_alg == exercise_alg && |
| 2465 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2466 | PSA_ASSERT(status); |
| 2467 | } else { |
| 2468 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2469 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2471 | if (buffer_length != 0) { |
| 2472 | memset(buffer, 0, buffer_length); |
| 2473 | } |
| 2474 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2475 | buffer, buffer_length, |
| 2476 | NULL, 0, |
| 2477 | buffer, buffer_length, |
| 2478 | &output_length); |
| 2479 | if (policy_alg == exercise_alg && |
| 2480 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2481 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2482 | } else { |
| 2483 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2484 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2485 | |
| 2486 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2487 | /* |
| 2488 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2489 | * thus reset them as required. |
| 2490 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2491 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2493 | psa_destroy_key(key); |
| 2494 | PSA_DONE(); |
| 2495 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2496 | } |
| 2497 | /* END_CASE */ |
| 2498 | |
| 2499 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2500 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2501 | int policy_alg, |
| 2502 | int key_type, |
| 2503 | data_t *key_data, |
| 2504 | int exercise_alg, |
| 2505 | int payload_length_arg, |
| 2506 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2507 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2508 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2509 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2510 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2511 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2512 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2513 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2514 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2515 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2516 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2517 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2518 | int compatible_alg = payload_length_arg > 0; |
| 2519 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2520 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2521 | size_t signature_length; |
| 2522 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2523 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2524 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2525 | TEST_EQUAL(expected_usage, |
| 2526 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2528 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2530 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2531 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2532 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2534 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2535 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2537 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2539 | status = psa_sign_hash(key, exercise_alg, |
| 2540 | payload, payload_length, |
| 2541 | signature, sizeof(signature), |
| 2542 | &signature_length); |
| 2543 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2544 | PSA_ASSERT(status); |
| 2545 | } else { |
| 2546 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2547 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2549 | memset(signature, 0, sizeof(signature)); |
| 2550 | status = psa_verify_hash(key, exercise_alg, |
| 2551 | payload, payload_length, |
| 2552 | signature, sizeof(signature)); |
| 2553 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2554 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2555 | } else { |
| 2556 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2557 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2559 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2560 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2561 | status = psa_sign_message(key, exercise_alg, |
| 2562 | payload, payload_length, |
| 2563 | signature, sizeof(signature), |
| 2564 | &signature_length); |
| 2565 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2566 | PSA_ASSERT(status); |
| 2567 | } else { |
| 2568 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2569 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2571 | memset(signature, 0, sizeof(signature)); |
| 2572 | status = psa_verify_message(key, exercise_alg, |
| 2573 | payload, payload_length, |
| 2574 | signature, sizeof(signature)); |
| 2575 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2576 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2577 | } else { |
| 2578 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2579 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2580 | } |
| 2581 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2582 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2583 | psa_destroy_key(key); |
| 2584 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2585 | } |
| 2586 | /* END_CASE */ |
| 2587 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2588 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2589 | void derive_key_policy(int policy_usage, |
| 2590 | int policy_alg, |
| 2591 | int key_type, |
| 2592 | data_t *key_data, |
| 2593 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2594 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2595 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2596 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2597 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2598 | psa_status_t status; |
| 2599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2600 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2602 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2603 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2604 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2606 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2607 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2609 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2611 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2612 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2613 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2614 | &operation, |
| 2615 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2616 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2617 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2619 | status = psa_key_derivation_input_key(&operation, |
| 2620 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2621 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2623 | if (policy_alg == exercise_alg && |
| 2624 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2625 | PSA_ASSERT(status); |
| 2626 | } else { |
| 2627 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2628 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2629 | |
| 2630 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2631 | psa_key_derivation_abort(&operation); |
| 2632 | psa_destroy_key(key); |
| 2633 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2634 | } |
| 2635 | /* END_CASE */ |
| 2636 | |
| 2637 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2638 | void agreement_key_policy(int policy_usage, |
| 2639 | int policy_alg, |
| 2640 | int key_type_arg, |
| 2641 | data_t *key_data, |
| 2642 | int exercise_alg, |
| 2643 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2644 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2645 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2646 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2647 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2648 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2649 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2650 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2652 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2654 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2655 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2656 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2658 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2659 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2661 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 2662 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key, 0); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2664 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2665 | |
| 2666 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2667 | psa_key_derivation_abort(&operation); |
| 2668 | psa_destroy_key(key); |
| 2669 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2670 | } |
| 2671 | /* END_CASE */ |
| 2672 | |
| 2673 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2674 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2675 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2676 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2677 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2678 | psa_key_type_t key_type = key_type_arg; |
| 2679 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2680 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2681 | psa_key_usage_t usage = usage_arg; |
| 2682 | psa_algorithm_t alg = alg_arg; |
| 2683 | psa_algorithm_t alg2 = alg2_arg; |
| 2684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2685 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2687 | psa_set_key_usage_flags(&attributes, usage); |
| 2688 | psa_set_key_algorithm(&attributes, alg); |
| 2689 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2690 | psa_set_key_type(&attributes, key_type); |
| 2691 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2692 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2693 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2694 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2695 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2696 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2697 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2698 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2699 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2700 | |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2701 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2702 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2703 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2704 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2705 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2706 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2707 | |
| 2708 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2709 | /* |
| 2710 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2711 | * thus reset them as required. |
| 2712 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2713 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2715 | psa_destroy_key(key); |
| 2716 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2717 | } |
| 2718 | /* END_CASE */ |
| 2719 | |
| 2720 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2721 | void raw_agreement_key_policy(int policy_usage, |
| 2722 | int policy_alg, |
| 2723 | int key_type_arg, |
| 2724 | data_t *key_data, |
| 2725 | int exercise_alg, |
| 2726 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2727 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2728 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2729 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2730 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2731 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2732 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2733 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2735 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2737 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2738 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2739 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2741 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2742 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2743 | |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 2744 | status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key, 0); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2746 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2747 | |
| 2748 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2749 | psa_key_derivation_abort(&operation); |
| 2750 | psa_destroy_key(key); |
| 2751 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2752 | } |
| 2753 | /* END_CASE */ |
| 2754 | |
| 2755 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2756 | void copy_success(int source_usage_arg, |
| 2757 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2758 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2759 | int type_arg, data_t *material, |
| 2760 | int copy_attributes, |
| 2761 | int target_usage_arg, |
| 2762 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2763 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2764 | int expected_usage_arg, |
| 2765 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2766 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2767 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2768 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2769 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2770 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2771 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2772 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2773 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2774 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2775 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2776 | uint8_t *export_buffer = NULL; |
| 2777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2778 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2779 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2780 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2781 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2782 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2783 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2784 | psa_set_key_type(&source_attributes, type_arg); |
| 2785 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2786 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2787 | material->x, material->len, |
| 2788 | &source_key)); |
| 2789 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2790 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2791 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2792 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2793 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2794 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2795 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2797 | if (target_usage_arg != -1) { |
| 2798 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2799 | } |
| 2800 | if (target_alg_arg != -1) { |
| 2801 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2802 | } |
| 2803 | if (target_alg2_arg != -1) { |
| 2804 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2805 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2806 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2807 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2808 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2809 | PSA_ASSERT(psa_copy_key(source_key, |
| 2810 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2811 | |
| 2812 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2813 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2814 | |
| 2815 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2816 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2817 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2818 | psa_get_key_type(&target_attributes)); |
| 2819 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2820 | psa_get_key_bits(&target_attributes)); |
| 2821 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2822 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2823 | TEST_EQUAL(expected_alg2, |
| 2824 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2825 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2826 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2827 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2828 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2829 | material->len, &length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2830 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2831 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2832 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2834 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2835 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2836 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2837 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2838 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2839 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2840 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2841 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2843 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2844 | |
| 2845 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2846 | /* |
| 2847 | * Source and target key attributes may have been returned by |
| 2848 | * psa_get_key_attributes() thus reset them as required. |
| 2849 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2850 | psa_reset_key_attributes(&source_attributes); |
| 2851 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2853 | PSA_DONE(); |
| 2854 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2855 | } |
| 2856 | /* END_CASE */ |
| 2857 | |
| 2858 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2859 | void copy_fail(int source_usage_arg, |
| 2860 | int source_alg_arg, int source_alg2_arg, |
| 2861 | int source_lifetime_arg, |
| 2862 | int type_arg, data_t *material, |
| 2863 | int target_type_arg, int target_bits_arg, |
| 2864 | int target_usage_arg, |
| 2865 | int target_alg_arg, int target_alg2_arg, |
| 2866 | int target_id_arg, int target_lifetime_arg, |
| 2867 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2868 | { |
| 2869 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2870 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2871 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2872 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2873 | 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] | 2874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2876 | |
| 2877 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2878 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2879 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2880 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2881 | psa_set_key_type(&source_attributes, type_arg); |
| 2882 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2883 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2884 | material->x, material->len, |
| 2885 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2886 | |
| 2887 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2888 | psa_set_key_id(&target_attributes, key_id); |
| 2889 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2890 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2891 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2892 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2893 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2894 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2895 | |
| 2896 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2897 | TEST_EQUAL(psa_copy_key(source_key, |
| 2898 | &target_attributes, &target_key), |
| 2899 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2901 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2902 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2903 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2904 | psa_reset_key_attributes(&source_attributes); |
| 2905 | psa_reset_key_attributes(&target_attributes); |
| 2906 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2907 | } |
| 2908 | /* END_CASE */ |
| 2909 | |
| 2910 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2911 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2912 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2913 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2914 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2915 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2916 | * 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] | 2917 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2918 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2919 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2920 | psa_hash_operation_t zero; |
| 2921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2922 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2923 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2924 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2925 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2926 | PSA_ERROR_BAD_STATE); |
| 2927 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2928 | PSA_ERROR_BAD_STATE); |
| 2929 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2930 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2931 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2932 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2933 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2934 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2935 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2936 | } |
| 2937 | /* END_CASE */ |
| 2938 | |
| 2939 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2940 | void hash_setup(int alg_arg, |
| 2941 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2942 | { |
| 2943 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2944 | uint8_t *output = NULL; |
| 2945 | size_t output_size = 0; |
| 2946 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2947 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2948 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2949 | psa_status_t status; |
| 2950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2951 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2952 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2953 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2954 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2955 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2957 | status = psa_hash_compute(alg, NULL, 0, |
| 2958 | output, output_size, &output_length); |
| 2959 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2960 | |
| 2961 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | status = psa_hash_setup(&operation, alg); |
| 2963 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2964 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2965 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2966 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2967 | |
| 2968 | /* If setup failed, reproduce the failure, so as to |
| 2969 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | if (status != PSA_SUCCESS) { |
| 2971 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2972 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2973 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2974 | /* Now the operation object should be reusable. */ |
| 2975 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2976 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2977 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2978 | #endif |
| 2979 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2980 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2981 | mbedtls_free(output); |
| 2982 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2983 | } |
| 2984 | /* END_CASE */ |
| 2985 | |
| 2986 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2987 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2988 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2989 | { |
| 2990 | psa_algorithm_t alg = alg_arg; |
| 2991 | uint8_t *output = NULL; |
| 2992 | size_t output_size = output_size_arg; |
| 2993 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2994 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2995 | psa_status_t expected_status = expected_status_arg; |
| 2996 | psa_status_t status; |
| 2997 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2998 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3000 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3001 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3002 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3003 | status = psa_hash_compute(alg, input->x, input->len, |
| 3004 | output, output_size, &output_length); |
| 3005 | TEST_EQUAL(status, expected_status); |
| 3006 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3007 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3008 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3009 | status = psa_hash_setup(&operation, alg); |
| 3010 | if (status == PSA_SUCCESS) { |
| 3011 | status = psa_hash_update(&operation, input->x, input->len); |
| 3012 | if (status == PSA_SUCCESS) { |
| 3013 | status = psa_hash_finish(&operation, output, output_size, |
| 3014 | &output_length); |
| 3015 | if (status == PSA_SUCCESS) { |
| 3016 | TEST_LE_U(output_length, output_size); |
| 3017 | } else { |
| 3018 | TEST_EQUAL(status, expected_status); |
| 3019 | } |
| 3020 | } else { |
| 3021 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3022 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3023 | } else { |
| 3024 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3025 | } |
| 3026 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3027 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3028 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3029 | mbedtls_free(output); |
| 3030 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3031 | } |
| 3032 | /* END_CASE */ |
| 3033 | |
| 3034 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3035 | void hash_compare_fail(int alg_arg, data_t *input, |
| 3036 | data_t *reference_hash, |
| 3037 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3038 | { |
| 3039 | psa_algorithm_t alg = alg_arg; |
| 3040 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3041 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3042 | psa_status_t status; |
| 3043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3044 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3045 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3046 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3047 | status = psa_hash_compare(alg, input->x, input->len, |
| 3048 | reference_hash->x, reference_hash->len); |
| 3049 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3050 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3051 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3052 | status = psa_hash_setup(&operation, alg); |
| 3053 | if (status == PSA_SUCCESS) { |
| 3054 | status = psa_hash_update(&operation, input->x, input->len); |
| 3055 | if (status == PSA_SUCCESS) { |
| 3056 | status = psa_hash_verify(&operation, reference_hash->x, |
| 3057 | reference_hash->len); |
| 3058 | TEST_EQUAL(status, expected_status); |
| 3059 | } else { |
| 3060 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3061 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3062 | } else { |
| 3063 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3064 | } |
| 3065 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3066 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3067 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3068 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3069 | } |
| 3070 | /* END_CASE */ |
| 3071 | |
| 3072 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3073 | void hash_compute_compare(int alg_arg, data_t *input, |
| 3074 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3075 | { |
| 3076 | psa_algorithm_t alg = alg_arg; |
| 3077 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 3078 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3079 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3080 | size_t i; |
| 3081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3082 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3083 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3084 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3085 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 3086 | output, PSA_HASH_LENGTH(alg), |
| 3087 | &output_length)); |
| 3088 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3089 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3090 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3091 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3092 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3093 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3094 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3095 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 3096 | PSA_HASH_LENGTH(alg), |
| 3097 | &output_length)); |
| 3098 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3099 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3100 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3101 | |
| 3102 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3103 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 3104 | output, sizeof(output), |
| 3105 | &output_length)); |
| 3106 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3107 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3108 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3109 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3110 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3111 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3112 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3113 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 3114 | sizeof(output), &output_length)); |
| 3115 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3116 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3117 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3118 | |
| 3119 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3120 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 3121 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3122 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3123 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3124 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3125 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3126 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 3127 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3128 | |
| 3129 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3130 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3131 | output, output_length + 1), |
| 3132 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3133 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3134 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3135 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3136 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3137 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 3138 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3139 | |
| 3140 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3141 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3142 | output, output_length - 1), |
| 3143 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3144 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3145 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3146 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3147 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3148 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 3149 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3150 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3151 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3152 | for (i = 0; i < output_length; i++) { |
| 3153 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3154 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3155 | |
| 3156 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3157 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3158 | output, output_length), |
| 3159 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3160 | |
| 3161 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3162 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3163 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3164 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 3165 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3166 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3167 | output[i] ^= 1; |
| 3168 | } |
| 3169 | |
| 3170 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3171 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3172 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3173 | } |
| 3174 | /* END_CASE */ |
| 3175 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3176 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3177 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3178 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3179 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3180 | unsigned char input[] = ""; |
| 3181 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3182 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3183 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3184 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3185 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 3186 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3187 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3188 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3189 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3191 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3192 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3193 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3194 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3195 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3196 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 3197 | PSA_ERROR_BAD_STATE); |
| 3198 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3199 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3200 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3201 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3202 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3203 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3204 | PSA_ERROR_BAD_STATE); |
| 3205 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3206 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3207 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3208 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 3209 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3210 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3211 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3212 | PSA_ERROR_BAD_STATE); |
| 3213 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3214 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3215 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3216 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3217 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3218 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3219 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3220 | hash, sizeof(hash), &hash_len)); |
| 3221 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3222 | PSA_ERROR_BAD_STATE); |
| 3223 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3224 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3225 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3226 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3227 | valid_hash, sizeof(valid_hash)), |
| 3228 | PSA_ERROR_BAD_STATE); |
| 3229 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3230 | |
| 3231 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3232 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3233 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3234 | hash, sizeof(hash), &hash_len)); |
| 3235 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3236 | valid_hash, sizeof(valid_hash)), |
| 3237 | PSA_ERROR_BAD_STATE); |
| 3238 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3239 | |
| 3240 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3241 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3242 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3243 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3244 | valid_hash, sizeof(valid_hash))); |
| 3245 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3246 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3247 | valid_hash, sizeof(valid_hash)), |
| 3248 | PSA_ERROR_BAD_STATE); |
| 3249 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3250 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3251 | |
| 3252 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3253 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3254 | hash, sizeof(hash), &hash_len), |
| 3255 | PSA_ERROR_BAD_STATE); |
| 3256 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3257 | |
| 3258 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3259 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3260 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3261 | hash, sizeof(hash), &hash_len)); |
| 3262 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3263 | hash, sizeof(hash), &hash_len), |
| 3264 | PSA_ERROR_BAD_STATE); |
| 3265 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3266 | |
| 3267 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3268 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3269 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3270 | valid_hash, sizeof(valid_hash))); |
| 3271 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3272 | hash, sizeof(hash), &hash_len), |
| 3273 | PSA_ERROR_BAD_STATE); |
| 3274 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3275 | |
| 3276 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3277 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3278 | } |
| 3279 | /* END_CASE */ |
| 3280 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3281 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3282 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3283 | { |
| 3284 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3285 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 3286 | * appended to it */ |
| 3287 | unsigned char hash[] = { |
| 3288 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3289 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3290 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 3291 | }; |
| 3292 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3293 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3295 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3296 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3297 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3298 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3299 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3300 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 3301 | PSA_ERROR_INVALID_SIGNATURE); |
| 3302 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3303 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3304 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3305 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3306 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3307 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3308 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 3309 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3310 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3311 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3312 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3313 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 3314 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3315 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3316 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3317 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3318 | } |
| 3319 | /* END_CASE */ |
| 3320 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3321 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3322 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3323 | { |
| 3324 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3325 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3326 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3327 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3328 | size_t hash_len; |
| 3329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3330 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3331 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3332 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3333 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3334 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3335 | hash, expected_size - 1, &hash_len), |
| 3336 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3337 | |
| 3338 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3339 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3340 | } |
| 3341 | /* END_CASE */ |
| 3342 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3343 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3344 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3345 | { |
| 3346 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3347 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3348 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3349 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3350 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3351 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3352 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3353 | size_t hash_len; |
| 3354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | PSA_ASSERT(psa_crypto_init()); |
| 3356 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3358 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3359 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3360 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3361 | hash, sizeof(hash), &hash_len)); |
| 3362 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3363 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3365 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3366 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3368 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3369 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3370 | hash, sizeof(hash), &hash_len)); |
| 3371 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3372 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3373 | hash, sizeof(hash), &hash_len)); |
| 3374 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3375 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3376 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3377 | |
| 3378 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3379 | psa_hash_abort(&op_source); |
| 3380 | psa_hash_abort(&op_init); |
| 3381 | psa_hash_abort(&op_setup); |
| 3382 | psa_hash_abort(&op_finished); |
| 3383 | psa_hash_abort(&op_aborted); |
| 3384 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3385 | } |
| 3386 | /* END_CASE */ |
| 3387 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3388 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3389 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3390 | { |
| 3391 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3392 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3393 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3394 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3395 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3396 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3397 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3398 | size_t hash_len; |
| 3399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3400 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3402 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3403 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3404 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3405 | hash, sizeof(hash), &hash_len)); |
| 3406 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3407 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3409 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3410 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3411 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3413 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3414 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3415 | PSA_ERROR_BAD_STATE); |
| 3416 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3417 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3418 | |
| 3419 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3420 | psa_hash_abort(&op_target); |
| 3421 | psa_hash_abort(&op_init); |
| 3422 | psa_hash_abort(&op_setup); |
| 3423 | psa_hash_abort(&op_finished); |
| 3424 | psa_hash_abort(&op_aborted); |
| 3425 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3426 | } |
| 3427 | /* END_CASE */ |
| 3428 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3429 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3430 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3431 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3432 | const uint8_t input[1] = { 0 }; |
| 3433 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3434 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3435 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3436 | * 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] | 3437 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3438 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3439 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3440 | psa_mac_operation_t zero; |
| 3441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3442 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3443 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3444 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3445 | TEST_EQUAL(psa_mac_update(&func, |
| 3446 | input, sizeof(input)), |
| 3447 | PSA_ERROR_BAD_STATE); |
| 3448 | TEST_EQUAL(psa_mac_update(&init, |
| 3449 | input, sizeof(input)), |
| 3450 | PSA_ERROR_BAD_STATE); |
| 3451 | TEST_EQUAL(psa_mac_update(&zero, |
| 3452 | input, sizeof(input)), |
| 3453 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3454 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3455 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3456 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3457 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3458 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3459 | } |
| 3460 | /* END_CASE */ |
| 3461 | |
| 3462 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3463 | void mac_setup(int key_type_arg, |
| 3464 | data_t *key, |
| 3465 | int alg_arg, |
| 3466 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3467 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3468 | psa_key_type_t key_type = key_type_arg; |
| 3469 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3470 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3471 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3472 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3473 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3474 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3475 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3477 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3479 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3480 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3481 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3482 | } |
| 3483 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3484 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3485 | /* The operation object should be reusable. */ |
| 3486 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3487 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3488 | smoke_test_key_data, |
| 3489 | sizeof(smoke_test_key_data), |
| 3490 | KNOWN_SUPPORTED_MAC_ALG, |
| 3491 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3492 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3493 | } |
| 3494 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3495 | #endif |
| 3496 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3497 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3498 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3499 | } |
| 3500 | /* END_CASE */ |
| 3501 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3502 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3503 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3504 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3505 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3506 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3507 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3508 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3509 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3510 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3511 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3512 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3513 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3514 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3515 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3516 | size_t sign_mac_length = 0; |
| 3517 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3518 | const uint8_t verify_mac[] = { |
| 3519 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3520 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3521 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3522 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3524 | PSA_ASSERT(psa_crypto_init()); |
| 3525 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3526 | psa_set_key_algorithm(&attributes, alg); |
| 3527 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3529 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3530 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3531 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3532 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3533 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3534 | PSA_ERROR_BAD_STATE); |
| 3535 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3536 | |
| 3537 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3538 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3539 | &sign_mac_length), |
| 3540 | PSA_ERROR_BAD_STATE); |
| 3541 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3542 | |
| 3543 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3544 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3545 | verify_mac, sizeof(verify_mac)), |
| 3546 | PSA_ERROR_BAD_STATE); |
| 3547 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3548 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3549 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3550 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3551 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3552 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3553 | PSA_ERROR_BAD_STATE); |
| 3554 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3555 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3556 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3557 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3558 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3559 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3560 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3561 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3562 | sign_mac, sizeof(sign_mac), |
| 3563 | &sign_mac_length)); |
| 3564 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3565 | PSA_ERROR_BAD_STATE); |
| 3566 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3567 | |
| 3568 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3569 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3570 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3571 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3572 | verify_mac, sizeof(verify_mac))); |
| 3573 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3574 | PSA_ERROR_BAD_STATE); |
| 3575 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3576 | |
| 3577 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3578 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3579 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3580 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3581 | sign_mac, sizeof(sign_mac), |
| 3582 | &sign_mac_length)); |
| 3583 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3584 | sign_mac, sizeof(sign_mac), |
| 3585 | &sign_mac_length), |
| 3586 | PSA_ERROR_BAD_STATE); |
| 3587 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3588 | |
| 3589 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3590 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3591 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3592 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3593 | verify_mac, sizeof(verify_mac))); |
| 3594 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3595 | verify_mac, sizeof(verify_mac)), |
| 3596 | PSA_ERROR_BAD_STATE); |
| 3597 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3598 | |
| 3599 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3600 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3601 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3602 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3603 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3604 | verify_mac, sizeof(verify_mac)), |
| 3605 | PSA_ERROR_BAD_STATE); |
| 3606 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3607 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3608 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3609 | |
| 3610 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3611 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3612 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3613 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3614 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3615 | sign_mac, sizeof(sign_mac), |
| 3616 | &sign_mac_length), |
| 3617 | PSA_ERROR_BAD_STATE); |
| 3618 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3619 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3620 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3622 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3623 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3624 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3625 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3626 | } |
| 3627 | /* END_CASE */ |
| 3628 | |
| 3629 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3630 | void mac_sign_verify_multi(int key_type_arg, |
| 3631 | data_t *key_data, |
| 3632 | int alg_arg, |
| 3633 | data_t *input, |
| 3634 | int is_verify, |
| 3635 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3636 | { |
| 3637 | size_t data_part_len = 0; |
| 3638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3639 | for (data_part_len = 1; data_part_len <= input->len; data_part_len++) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3640 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3641 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3643 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3644 | alg_arg, |
| 3645 | input, data_part_len, |
| 3646 | expected_mac, |
| 3647 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3648 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3649 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3650 | |
| 3651 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3652 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3654 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3655 | alg_arg, |
| 3656 | input, data_part_len, |
| 3657 | expected_mac, |
| 3658 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3659 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3660 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3661 | } |
| 3662 | |
| 3663 | /* Goto is required to silence warnings about unused labels, as we |
| 3664 | * don't actually do any test assertions in this function. */ |
| 3665 | goto exit; |
| 3666 | } |
| 3667 | /* END_CASE */ |
| 3668 | |
| 3669 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3670 | void mac_sign(int key_type_arg, |
| 3671 | data_t *key_data, |
| 3672 | int alg_arg, |
| 3673 | data_t *input, |
| 3674 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3675 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3676 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3677 | psa_key_type_t key_type = key_type_arg; |
| 3678 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3679 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3680 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3681 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3682 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3683 | 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] | 3684 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3685 | const size_t output_sizes_to_test[] = { |
| 3686 | 0, |
| 3687 | 1, |
| 3688 | expected_mac->len - 1, |
| 3689 | expected_mac->len, |
| 3690 | expected_mac->len + 1, |
| 3691 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3693 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3694 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3695 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3697 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3699 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3700 | psa_set_key_algorithm(&attributes, alg); |
| 3701 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3703 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3704 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3706 | for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) { |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3707 | const size_t output_size = output_sizes_to_test[i]; |
| 3708 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3709 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3710 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3712 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3713 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3714 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3715 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3716 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3717 | input->x, input->len, |
| 3718 | actual_mac, output_size, &mac_length), |
| 3719 | expected_status); |
| 3720 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3721 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3722 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3723 | } |
| 3724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3725 | if (output_size > 0) { |
| 3726 | memset(actual_mac, 0, output_size); |
| 3727 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3728 | |
| 3729 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3730 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3731 | PSA_ASSERT(psa_mac_update(&operation, |
| 3732 | input->x, input->len)); |
| 3733 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3734 | actual_mac, output_size, |
| 3735 | &mac_length), |
| 3736 | expected_status); |
| 3737 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3739 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3740 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3741 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3742 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3743 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3744 | actual_mac = NULL; |
| 3745 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3746 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3747 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3748 | psa_mac_abort(&operation); |
| 3749 | psa_destroy_key(key); |
| 3750 | PSA_DONE(); |
| 3751 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3752 | } |
| 3753 | /* END_CASE */ |
| 3754 | |
| 3755 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3756 | void mac_verify(int key_type_arg, |
| 3757 | data_t *key_data, |
| 3758 | int alg_arg, |
| 3759 | data_t *input, |
| 3760 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3761 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3762 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3763 | psa_key_type_t key_type = key_type_arg; |
| 3764 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3765 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3766 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3767 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3769 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3771 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3773 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3774 | psa_set_key_algorithm(&attributes, alg); |
| 3775 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3777 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3778 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3779 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3780 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3781 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3782 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3783 | |
| 3784 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3785 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3786 | PSA_ASSERT(psa_mac_update(&operation, |
| 3787 | input->x, input->len)); |
| 3788 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3789 | expected_mac->x, |
| 3790 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3791 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3792 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3793 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3794 | input->x, input->len, |
| 3795 | expected_mac->x, |
| 3796 | expected_mac->len - 1), |
| 3797 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3798 | |
| 3799 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3800 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3801 | PSA_ASSERT(psa_mac_update(&operation, |
| 3802 | input->x, input->len)); |
| 3803 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3804 | expected_mac->x, |
| 3805 | expected_mac->len - 1), |
| 3806 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3807 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3808 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3809 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3810 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3811 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3812 | input->x, input->len, |
| 3813 | perturbed_mac, expected_mac->len + 1), |
| 3814 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3815 | |
| 3816 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3817 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3818 | PSA_ASSERT(psa_mac_update(&operation, |
| 3819 | input->x, input->len)); |
| 3820 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3821 | perturbed_mac, |
| 3822 | expected_mac->len + 1), |
| 3823 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3824 | |
| 3825 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3826 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3827 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3828 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3830 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3831 | input->x, input->len, |
| 3832 | perturbed_mac, expected_mac->len), |
| 3833 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3835 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3836 | PSA_ASSERT(psa_mac_update(&operation, |
| 3837 | input->x, input->len)); |
| 3838 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3839 | perturbed_mac, |
| 3840 | expected_mac->len), |
| 3841 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3842 | perturbed_mac[i] ^= 1; |
| 3843 | } |
| 3844 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3845 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3846 | psa_mac_abort(&operation); |
| 3847 | psa_destroy_key(key); |
| 3848 | PSA_DONE(); |
| 3849 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3850 | } |
| 3851 | /* END_CASE */ |
| 3852 | |
| 3853 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3854 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3855 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3856 | const uint8_t input[1] = { 0 }; |
| 3857 | unsigned char output[1] = { 0 }; |
| 3858 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3859 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3860 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3861 | * 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] | 3862 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3863 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3864 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3865 | psa_cipher_operation_t zero; |
| 3866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3867 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3868 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3869 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3870 | TEST_EQUAL(psa_cipher_update(&func, |
| 3871 | input, sizeof(input), |
| 3872 | output, sizeof(output), |
| 3873 | &output_length), |
| 3874 | PSA_ERROR_BAD_STATE); |
| 3875 | TEST_EQUAL(psa_cipher_update(&init, |
| 3876 | input, sizeof(input), |
| 3877 | output, sizeof(output), |
| 3878 | &output_length), |
| 3879 | PSA_ERROR_BAD_STATE); |
| 3880 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3881 | input, sizeof(input), |
| 3882 | output, sizeof(output), |
| 3883 | &output_length), |
| 3884 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3885 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3886 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3887 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3888 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3889 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3890 | } |
| 3891 | /* END_CASE */ |
| 3892 | |
| 3893 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3894 | void cipher_setup(int key_type_arg, |
| 3895 | data_t *key, |
| 3896 | int alg_arg, |
| 3897 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3898 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3899 | psa_key_type_t key_type = key_type_arg; |
| 3900 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3901 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3902 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3903 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3904 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3905 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3906 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3908 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3910 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3911 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3912 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3913 | } |
| 3914 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3915 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3916 | /* The operation object should be reusable. */ |
| 3917 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3918 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3919 | smoke_test_key_data, |
| 3920 | sizeof(smoke_test_key_data), |
| 3921 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3922 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3923 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3924 | } |
| 3925 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3926 | #endif |
| 3927 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3928 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3929 | psa_cipher_abort(&operation); |
| 3930 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3931 | } |
| 3932 | /* END_CASE */ |
| 3933 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3934 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3935 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3936 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3937 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3938 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3939 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3940 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3941 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3942 | 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] | 3943 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3944 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3945 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3946 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3947 | const uint8_t text[] = { |
| 3948 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3949 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3950 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3951 | 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] | 3952 | size_t length = 0; |
| 3953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3954 | PSA_ASSERT(psa_crypto_init()); |
| 3955 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3956 | psa_set_key_algorithm(&attributes, alg); |
| 3957 | psa_set_key_type(&attributes, key_type); |
| 3958 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3959 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3960 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3961 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3962 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3963 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3964 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3965 | PSA_ERROR_BAD_STATE); |
| 3966 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3967 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3968 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3969 | |
| 3970 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3971 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3972 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3973 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3974 | PSA_ERROR_BAD_STATE); |
| 3975 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3976 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3977 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3978 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3979 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3980 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3981 | buffer, sizeof(buffer), |
| 3982 | &length), |
| 3983 | PSA_ERROR_BAD_STATE); |
| 3984 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3985 | |
| 3986 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3987 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3988 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3989 | buffer, sizeof(buffer), |
| 3990 | &length)); |
| 3991 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3992 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3993 | buffer, sizeof(buffer), |
| 3994 | &length), |
| 3995 | PSA_ERROR_BAD_STATE); |
| 3996 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3997 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3998 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3999 | |
| 4000 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4001 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4002 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4003 | iv, sizeof(iv))); |
| 4004 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 4005 | buffer, sizeof(buffer), |
| 4006 | &length), |
| 4007 | PSA_ERROR_BAD_STATE); |
| 4008 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4009 | |
| 4010 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4011 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4012 | iv, sizeof(iv)), |
| 4013 | PSA_ERROR_BAD_STATE); |
| 4014 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4015 | |
| 4016 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4017 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4018 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4019 | iv, sizeof(iv))); |
| 4020 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4021 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4022 | iv, sizeof(iv)), |
| 4023 | PSA_ERROR_BAD_STATE); |
| 4024 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4025 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4026 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4027 | |
| 4028 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4029 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4030 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4031 | buffer, sizeof(buffer), |
| 4032 | &length)); |
| 4033 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4034 | iv, sizeof(iv)), |
| 4035 | PSA_ERROR_BAD_STATE); |
| 4036 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4037 | |
| 4038 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4039 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4040 | text, sizeof(text), |
| 4041 | buffer, sizeof(buffer), |
| 4042 | &length), |
| 4043 | PSA_ERROR_BAD_STATE); |
| 4044 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4045 | |
| 4046 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4047 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4048 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4049 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4050 | text, sizeof(text), |
| 4051 | buffer, sizeof(buffer), |
| 4052 | &length), |
| 4053 | PSA_ERROR_BAD_STATE); |
| 4054 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4055 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4056 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4057 | |
| 4058 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4059 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4060 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4061 | iv, sizeof(iv))); |
| 4062 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4063 | buffer, sizeof(buffer), &length)); |
| 4064 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4065 | text, sizeof(text), |
| 4066 | buffer, sizeof(buffer), |
| 4067 | &length), |
| 4068 | PSA_ERROR_BAD_STATE); |
| 4069 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4070 | |
| 4071 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4072 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4073 | buffer, sizeof(buffer), &length), |
| 4074 | PSA_ERROR_BAD_STATE); |
| 4075 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4076 | |
| 4077 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4078 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4079 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 4080 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4081 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4082 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4083 | buffer, sizeof(buffer), &length), |
| 4084 | PSA_ERROR_BAD_STATE); |
| 4085 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4086 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4087 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4088 | |
| 4089 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4090 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4091 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4092 | iv, sizeof(iv))); |
| 4093 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4094 | buffer, sizeof(buffer), &length)); |
| 4095 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4096 | buffer, sizeof(buffer), &length), |
| 4097 | PSA_ERROR_BAD_STATE); |
| 4098 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4100 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 4101 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4102 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4103 | psa_cipher_abort(&operation); |
| 4104 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4105 | } |
| 4106 | /* END_CASE */ |
| 4107 | |
| 4108 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4109 | void cipher_encrypt_fail(int alg_arg, |
| 4110 | int key_type_arg, |
| 4111 | data_t *key_data, |
| 4112 | data_t *input, |
| 4113 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4114 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4115 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4116 | psa_status_t status; |
| 4117 | psa_key_type_t key_type = key_type_arg; |
| 4118 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 4119 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4120 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4121 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 4122 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4123 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4124 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4125 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4126 | size_t function_output_length; |
| 4127 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4128 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4130 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4131 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4133 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4134 | psa_set_key_algorithm(&attributes, alg); |
| 4135 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4137 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4138 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4139 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4141 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4142 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4143 | } |
| 4144 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4145 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4146 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 4147 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4149 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4150 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4151 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4152 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 4153 | if (status == PSA_SUCCESS) { |
| 4154 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4155 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4156 | iv, iv_size, |
| 4157 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4158 | } |
| 4159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4160 | status = psa_cipher_update(&operation, input->x, input->len, |
| 4161 | output, output_buffer_size, |
| 4162 | &function_output_length); |
| 4163 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4164 | output_length += function_output_length; |
| 4165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4166 | status = psa_cipher_finish(&operation, output + output_length, |
| 4167 | output_buffer_size - output_length, |
| 4168 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4170 | TEST_EQUAL(status, expected_status); |
| 4171 | } else { |
| 4172 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4173 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4174 | } else { |
| 4175 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4176 | } |
| 4177 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4178 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4179 | psa_cipher_abort(&operation); |
| 4180 | mbedtls_free(output); |
| 4181 | psa_destroy_key(key); |
| 4182 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4183 | } |
| 4184 | /* END_CASE */ |
| 4185 | |
| 4186 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4187 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 4188 | data_t *input, int iv_length, |
| 4189 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4190 | { |
| 4191 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4192 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4193 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4194 | size_t output_buffer_size = 0; |
| 4195 | unsigned char *output = NULL; |
| 4196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4197 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4198 | TEST_CALLOC(output, output_buffer_size); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4200 | PSA_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4202 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4203 | psa_set_key_algorithm(&attributes, alg); |
| 4204 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4206 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4207 | &key)); |
| 4208 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4209 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 4210 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4211 | |
| 4212 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4213 | psa_cipher_abort(&operation); |
| 4214 | mbedtls_free(output); |
| 4215 | psa_destroy_key(key); |
| 4216 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4217 | } |
| 4218 | /* END_CASE */ |
| 4219 | |
| 4220 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4221 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 4222 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4223 | { |
| 4224 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4225 | psa_key_type_t key_type = key_type_arg; |
| 4226 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4227 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4228 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4229 | unsigned char *output = NULL; |
| 4230 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4231 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4232 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4234 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4235 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4236 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4237 | TEST_LE_U(ciphertext->len, |
| 4238 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 4239 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 4240 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 4241 | TEST_LE_U(plaintext->len, |
| 4242 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 4243 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 4244 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4245 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4246 | |
| 4247 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4248 | psa_set_key_usage_flags(&attributes, |
| 4249 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4250 | psa_set_key_algorithm(&attributes, alg); |
| 4251 | psa_set_key_type(&attributes, key_type); |
| 4252 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4253 | &key)); |
| 4254 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4255 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4256 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4257 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4258 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4259 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4260 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4261 | PSA_ERROR_BAD_STATE); |
| 4262 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4263 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4264 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4265 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4266 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4267 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4268 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4269 | &length), |
| 4270 | PSA_ERROR_BAD_STATE); |
| 4271 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4272 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4273 | &length), |
| 4274 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4275 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4276 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4277 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4278 | output_length = 0; |
| 4279 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4280 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4281 | plaintext->x, plaintext->len, |
| 4282 | output, output_buffer_size, |
| 4283 | &length)); |
| 4284 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4285 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4286 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4287 | mbedtls_buffer_offset(output, output_length), |
| 4288 | output_buffer_size - output_length, |
| 4289 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4290 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4291 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4292 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4293 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4294 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4295 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4296 | output_length = 0; |
| 4297 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4298 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4299 | ciphertext->x, ciphertext->len, |
| 4300 | output, output_buffer_size, |
| 4301 | &length)); |
| 4302 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4303 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4304 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4305 | mbedtls_buffer_offset(output, output_length), |
| 4306 | output_buffer_size - output_length, |
| 4307 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4308 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4309 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4310 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4311 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4312 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4313 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4314 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 4315 | output, output_buffer_size, |
| 4316 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4317 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4318 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4319 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4320 | /* One-shot decryption */ |
| 4321 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4322 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 4323 | output, output_buffer_size, |
| 4324 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4325 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4326 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4327 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4328 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4329 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4330 | mbedtls_free(output); |
| 4331 | psa_cipher_abort(&operation); |
| 4332 | psa_destroy_key(key); |
| 4333 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4334 | } |
| 4335 | /* END_CASE */ |
| 4336 | |
| 4337 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4338 | void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data) |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4339 | { |
| 4340 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4341 | psa_algorithm_t alg = alg_arg; |
| 4342 | psa_key_type_t key_type = key_type_arg; |
| 4343 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4344 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4345 | psa_status_t status; |
| 4346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4347 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4349 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4350 | psa_set_key_algorithm(&attributes, alg); |
| 4351 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4352 | |
| 4353 | /* Usage of either of these two size macros would cause divide by zero |
| 4354 | * with incorrect key types previously. Input length should be irrelevant |
| 4355 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4356 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4357 | 0); |
| 4358 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4359 | |
| 4360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4361 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4362 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4363 | |
| 4364 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4365 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4366 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4368 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4369 | |
| 4370 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4371 | psa_cipher_abort(&operation); |
| 4372 | psa_destroy_key(key); |
| 4373 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4374 | } |
| 4375 | /* END_CASE */ |
| 4376 | |
| 4377 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4378 | void cipher_encrypt_validation(int alg_arg, |
| 4379 | int key_type_arg, |
| 4380 | data_t *key_data, |
| 4381 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4382 | { |
| 4383 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4384 | psa_key_type_t key_type = key_type_arg; |
| 4385 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4386 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4387 | unsigned char *output1 = NULL; |
| 4388 | size_t output1_buffer_size = 0; |
| 4389 | size_t output1_length = 0; |
| 4390 | unsigned char *output2 = NULL; |
| 4391 | size_t output2_buffer_size = 0; |
| 4392 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4393 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4394 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4395 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4397 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4399 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4400 | psa_set_key_algorithm(&attributes, alg); |
| 4401 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4403 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4404 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4405 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4406 | TEST_CALLOC(output1, output1_buffer_size); |
| 4407 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4409 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4410 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4411 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4412 | /* The one-shot cipher encryption uses generated iv so validating |
| 4413 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4414 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4415 | output1_buffer_size, &output1_length)); |
| 4416 | TEST_LE_U(output1_length, |
| 4417 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4418 | TEST_LE_U(output1_length, |
| 4419 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4421 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4422 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4424 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4425 | input->x, input->len, |
| 4426 | output2, output2_buffer_size, |
| 4427 | &function_output_length)); |
| 4428 | TEST_LE_U(function_output_length, |
| 4429 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4430 | TEST_LE_U(function_output_length, |
| 4431 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4432 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4434 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4435 | output2 + output2_length, |
| 4436 | output2_buffer_size - output2_length, |
| 4437 | &function_output_length)); |
| 4438 | TEST_LE_U(function_output_length, |
| 4439 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4440 | TEST_LE_U(function_output_length, |
| 4441 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4442 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4444 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4445 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4446 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4447 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4448 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4449 | psa_cipher_abort(&operation); |
| 4450 | mbedtls_free(output1); |
| 4451 | mbedtls_free(output2); |
| 4452 | psa_destroy_key(key); |
| 4453 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4454 | } |
| 4455 | /* END_CASE */ |
| 4456 | |
| 4457 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4458 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4459 | data_t *key_data, data_t *iv, |
| 4460 | data_t *input, |
| 4461 | int first_part_size_arg, |
| 4462 | int output1_length_arg, int output2_length_arg, |
| 4463 | data_t *expected_output, |
| 4464 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4465 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4466 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4467 | psa_key_type_t key_type = key_type_arg; |
| 4468 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4469 | psa_status_t status; |
| 4470 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4471 | size_t first_part_size = first_part_size_arg; |
| 4472 | size_t output1_length = output1_length_arg; |
| 4473 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4474 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4475 | size_t output_buffer_size = 0; |
| 4476 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4477 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4478 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4479 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4481 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4483 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4484 | psa_set_key_algorithm(&attributes, alg); |
| 4485 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4487 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4488 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4490 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4492 | if (iv->len > 0) { |
| 4493 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4494 | } |
| 4495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4496 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4497 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4498 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4500 | TEST_LE_U(first_part_size, input->len); |
| 4501 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4502 | output, output_buffer_size, |
| 4503 | &function_output_length)); |
| 4504 | TEST_ASSERT(function_output_length == output1_length); |
| 4505 | TEST_LE_U(function_output_length, |
| 4506 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4507 | TEST_LE_U(function_output_length, |
| 4508 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4509 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | if (first_part_size < input->len) { |
| 4512 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4513 | input->x + first_part_size, |
| 4514 | input->len - first_part_size, |
| 4515 | (output_buffer_size == 0 ? NULL : |
| 4516 | output + total_output_length), |
| 4517 | output_buffer_size - total_output_length, |
| 4518 | &function_output_length)); |
| 4519 | TEST_ASSERT(function_output_length == output2_length); |
| 4520 | TEST_LE_U(function_output_length, |
| 4521 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4522 | alg, |
| 4523 | input->len - first_part_size)); |
| 4524 | TEST_LE_U(function_output_length, |
| 4525 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4526 | total_output_length += function_output_length; |
| 4527 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4529 | status = psa_cipher_finish(&operation, |
| 4530 | (output_buffer_size == 0 ? NULL : |
| 4531 | output + total_output_length), |
| 4532 | output_buffer_size - total_output_length, |
| 4533 | &function_output_length); |
| 4534 | TEST_LE_U(function_output_length, |
| 4535 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4536 | TEST_LE_U(function_output_length, |
| 4537 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4538 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4539 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4541 | if (expected_status == PSA_SUCCESS) { |
| 4542 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4543 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4544 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4545 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4546 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4547 | |
| 4548 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4549 | psa_cipher_abort(&operation); |
| 4550 | mbedtls_free(output); |
| 4551 | psa_destroy_key(key); |
| 4552 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4553 | } |
| 4554 | /* END_CASE */ |
| 4555 | |
| 4556 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4557 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4558 | data_t *key_data, data_t *iv, |
| 4559 | data_t *input, |
| 4560 | int first_part_size_arg, |
| 4561 | int output1_length_arg, int output2_length_arg, |
| 4562 | data_t *expected_output, |
| 4563 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4564 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4565 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4566 | psa_key_type_t key_type = key_type_arg; |
| 4567 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4568 | psa_status_t status; |
| 4569 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4570 | size_t first_part_size = first_part_size_arg; |
| 4571 | size_t output1_length = output1_length_arg; |
| 4572 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4573 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4574 | size_t output_buffer_size = 0; |
| 4575 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4576 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4577 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4578 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4580 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4582 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4583 | psa_set_key_algorithm(&attributes, alg); |
| 4584 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4586 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4587 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4589 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4591 | if (iv->len > 0) { |
| 4592 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4593 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4595 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4596 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4597 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4599 | TEST_LE_U(first_part_size, input->len); |
| 4600 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4601 | input->x, first_part_size, |
| 4602 | output, output_buffer_size, |
| 4603 | &function_output_length)); |
| 4604 | TEST_ASSERT(function_output_length == output1_length); |
| 4605 | TEST_LE_U(function_output_length, |
| 4606 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4607 | TEST_LE_U(function_output_length, |
| 4608 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4609 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4611 | if (first_part_size < input->len) { |
| 4612 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4613 | input->x + first_part_size, |
| 4614 | input->len - first_part_size, |
| 4615 | (output_buffer_size == 0 ? NULL : |
| 4616 | output + total_output_length), |
| 4617 | output_buffer_size - total_output_length, |
| 4618 | &function_output_length)); |
| 4619 | TEST_ASSERT(function_output_length == output2_length); |
| 4620 | TEST_LE_U(function_output_length, |
| 4621 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4622 | alg, |
| 4623 | input->len - first_part_size)); |
| 4624 | TEST_LE_U(function_output_length, |
| 4625 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4626 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4627 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4629 | status = psa_cipher_finish(&operation, |
| 4630 | (output_buffer_size == 0 ? NULL : |
| 4631 | output + total_output_length), |
| 4632 | output_buffer_size - total_output_length, |
| 4633 | &function_output_length); |
| 4634 | TEST_LE_U(function_output_length, |
| 4635 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4636 | TEST_LE_U(function_output_length, |
| 4637 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4638 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4639 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4641 | if (expected_status == PSA_SUCCESS) { |
| 4642 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4643 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4644 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4645 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4646 | } |
| 4647 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4648 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4649 | psa_cipher_abort(&operation); |
| 4650 | mbedtls_free(output); |
| 4651 | psa_destroy_key(key); |
| 4652 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4653 | } |
| 4654 | /* END_CASE */ |
| 4655 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4656 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4657 | void cipher_decrypt_fail(int alg_arg, |
| 4658 | int key_type_arg, |
| 4659 | data_t *key_data, |
| 4660 | data_t *iv, |
| 4661 | data_t *input_arg, |
| 4662 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4663 | { |
| 4664 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4665 | psa_status_t status; |
| 4666 | psa_key_type_t key_type = key_type_arg; |
| 4667 | psa_algorithm_t alg = alg_arg; |
| 4668 | psa_status_t expected_status = expected_status_arg; |
| 4669 | unsigned char *input = NULL; |
| 4670 | size_t input_buffer_size = 0; |
| 4671 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4672 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4673 | size_t output_buffer_size = 0; |
| 4674 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4675 | size_t function_output_length; |
| 4676 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4677 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4679 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4680 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4682 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4683 | psa_set_key_algorithm(&attributes, alg); |
| 4684 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4686 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4687 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4688 | } |
| 4689 | |
| 4690 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4691 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4692 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4693 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4694 | memcpy(input, iv->x, iv->len); |
| 4695 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4696 | } |
| 4697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4698 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4699 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4700 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4701 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4702 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4703 | output_buffer_size, &output_length); |
| 4704 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4705 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4706 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4707 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4708 | if (status == PSA_SUCCESS) { |
| 4709 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4710 | input_arg->len) + |
| 4711 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4712 | TEST_CALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4714 | if (iv->len > 0) { |
| 4715 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4717 | if (status != PSA_SUCCESS) { |
| 4718 | TEST_EQUAL(status, expected_status); |
| 4719 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4720 | } |
| 4721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4722 | if (status == PSA_SUCCESS) { |
| 4723 | status = psa_cipher_update(&operation, |
| 4724 | input_arg->x, input_arg->len, |
| 4725 | output_multi, output_buffer_size, |
| 4726 | &function_output_length); |
| 4727 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4728 | output_length = function_output_length; |
| 4729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4730 | status = psa_cipher_finish(&operation, |
| 4731 | output_multi + output_length, |
| 4732 | output_buffer_size - output_length, |
| 4733 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4735 | TEST_EQUAL(status, expected_status); |
| 4736 | } else { |
| 4737 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4738 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4739 | } else { |
| 4740 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4741 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4742 | } else { |
| 4743 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4744 | } |
| 4745 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4746 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4747 | psa_cipher_abort(&operation); |
| 4748 | mbedtls_free(input); |
| 4749 | mbedtls_free(output); |
| 4750 | mbedtls_free(output_multi); |
| 4751 | psa_destroy_key(key); |
| 4752 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4753 | } |
| 4754 | /* END_CASE */ |
| 4755 | |
| 4756 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4757 | void cipher_decrypt(int alg_arg, |
| 4758 | int key_type_arg, |
| 4759 | data_t *key_data, |
| 4760 | data_t *iv, |
| 4761 | data_t *input_arg, |
| 4762 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4763 | { |
| 4764 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4765 | psa_key_type_t key_type = key_type_arg; |
| 4766 | psa_algorithm_t alg = alg_arg; |
| 4767 | unsigned char *input = NULL; |
| 4768 | size_t input_buffer_size = 0; |
| 4769 | unsigned char *output = NULL; |
| 4770 | size_t output_buffer_size = 0; |
| 4771 | size_t output_length = 0; |
| 4772 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4774 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4776 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4777 | psa_set_key_algorithm(&attributes, alg); |
| 4778 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4779 | |
| 4780 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4781 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4782 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4783 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4784 | memcpy(input, iv->x, iv->len); |
| 4785 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4786 | } |
| 4787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4788 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4789 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4791 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4792 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4794 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4795 | output_buffer_size, &output_length)); |
| 4796 | TEST_LE_U(output_length, |
| 4797 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4798 | TEST_LE_U(output_length, |
| 4799 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4800 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4801 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4802 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4803 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | mbedtls_free(input); |
| 4805 | mbedtls_free(output); |
| 4806 | psa_destroy_key(key); |
| 4807 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4808 | } |
| 4809 | /* END_CASE */ |
| 4810 | |
| 4811 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4812 | void cipher_verify_output(int alg_arg, |
| 4813 | int key_type_arg, |
| 4814 | data_t *key_data, |
| 4815 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4816 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4817 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4818 | psa_key_type_t key_type = key_type_arg; |
| 4819 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4820 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4821 | size_t output1_size = 0; |
| 4822 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4823 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4824 | size_t output2_size = 0; |
| 4825 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4826 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4828 | PSA_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4829 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4830 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4831 | psa_set_key_algorithm(&attributes, alg); |
| 4832 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4834 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4835 | &key)); |
| 4836 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4837 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4839 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4840 | output1, output1_size, |
| 4841 | &output1_length)); |
| 4842 | TEST_LE_U(output1_length, |
| 4843 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4844 | TEST_LE_U(output1_length, |
| 4845 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4846 | |
| 4847 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4848 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4850 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4851 | output2, output2_size, |
| 4852 | &output2_length)); |
| 4853 | TEST_LE_U(output2_length, |
| 4854 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4855 | TEST_LE_U(output2_length, |
| 4856 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4857 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4858 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4859 | |
| 4860 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4861 | mbedtls_free(output1); |
| 4862 | mbedtls_free(output2); |
| 4863 | psa_destroy_key(key); |
| 4864 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4865 | } |
| 4866 | /* END_CASE */ |
| 4867 | |
| 4868 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4869 | void cipher_verify_output_multipart(int alg_arg, |
| 4870 | int key_type_arg, |
| 4871 | data_t *key_data, |
| 4872 | data_t *input, |
| 4873 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4874 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4875 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4876 | psa_key_type_t key_type = key_type_arg; |
| 4877 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4878 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4879 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4880 | size_t iv_size = 16; |
| 4881 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4882 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4883 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4884 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4885 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4886 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4887 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4888 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4889 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4890 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4891 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4893 | PSA_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4895 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4896 | psa_set_key_algorithm(&attributes, alg); |
| 4897 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4900 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4902 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4903 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4905 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4906 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4907 | iv, iv_size, |
| 4908 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4909 | } |
| 4910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4911 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4912 | TEST_LE_U(output1_buffer_size, |
| 4913 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4914 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4916 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4918 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4919 | output1, output1_buffer_size, |
| 4920 | &function_output_length)); |
| 4921 | TEST_LE_U(function_output_length, |
| 4922 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4923 | TEST_LE_U(function_output_length, |
| 4924 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4925 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4927 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4928 | input->x + first_part_size, |
| 4929 | input->len - first_part_size, |
David Horstmann | b8dc245 | 2024-02-06 17:03:13 +0000 | [diff] [blame] | 4930 | output1 + output1_length, |
| 4931 | output1_buffer_size - output1_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4932 | &function_output_length)); |
| 4933 | TEST_LE_U(function_output_length, |
| 4934 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4935 | alg, |
| 4936 | input->len - first_part_size)); |
| 4937 | TEST_LE_U(function_output_length, |
| 4938 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4939 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4941 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4942 | output1 + output1_length, |
| 4943 | output1_buffer_size - output1_length, |
| 4944 | &function_output_length)); |
| 4945 | TEST_LE_U(function_output_length, |
| 4946 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4947 | TEST_LE_U(function_output_length, |
| 4948 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4949 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4951 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4952 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4953 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4954 | TEST_LE_U(output2_buffer_size, |
| 4955 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4956 | TEST_LE_U(output2_buffer_size, |
| 4957 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4958 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4959 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4960 | if (iv_length > 0) { |
| 4961 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4962 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4963 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4964 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4965 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4966 | output2, output2_buffer_size, |
| 4967 | &function_output_length)); |
| 4968 | TEST_LE_U(function_output_length, |
| 4969 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4970 | TEST_LE_U(function_output_length, |
| 4971 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4972 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4974 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4975 | output1 + first_part_size, |
| 4976 | output1_length - first_part_size, |
David Horstmann | b8dc245 | 2024-02-06 17:03:13 +0000 | [diff] [blame] | 4977 | output2 + output2_length, |
| 4978 | output2_buffer_size - output2_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4979 | &function_output_length)); |
| 4980 | TEST_LE_U(function_output_length, |
| 4981 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4982 | alg, |
| 4983 | output1_length - first_part_size)); |
| 4984 | TEST_LE_U(function_output_length, |
| 4985 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4986 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4988 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4989 | output2 + output2_length, |
| 4990 | output2_buffer_size - output2_length, |
| 4991 | &function_output_length)); |
| 4992 | TEST_LE_U(function_output_length, |
| 4993 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4994 | TEST_LE_U(function_output_length, |
| 4995 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4996 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4998 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4999 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5000 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5001 | |
| 5002 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5003 | psa_cipher_abort(&operation1); |
| 5004 | psa_cipher_abort(&operation2); |
| 5005 | mbedtls_free(output1); |
| 5006 | mbedtls_free(output2); |
| 5007 | psa_destroy_key(key); |
| 5008 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5009 | } |
| 5010 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 5011 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5012 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5013 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 5014 | int alg_arg, |
| 5015 | data_t *nonce, |
| 5016 | data_t *additional_data, |
| 5017 | data_t *input_data, |
| 5018 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5019 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5020 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5021 | psa_key_type_t key_type = key_type_arg; |
| 5022 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5023 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5024 | unsigned char *output_data = NULL; |
| 5025 | size_t output_size = 0; |
| 5026 | size_t output_length = 0; |
| 5027 | unsigned char *output_data2 = NULL; |
| 5028 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5029 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5030 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5031 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5033 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5035 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5036 | psa_set_key_algorithm(&attributes, alg); |
| 5037 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5038 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5039 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5040 | &key)); |
| 5041 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5042 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5044 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5045 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5046 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 5047 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5048 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 5049 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 5050 | TEST_EQUAL(output_size, |
| 5051 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5052 | TEST_LE_U(output_size, |
| 5053 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5054 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5055 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5057 | status = psa_aead_encrypt(key, alg, |
| 5058 | nonce->x, nonce->len, |
| 5059 | additional_data->x, |
| 5060 | additional_data->len, |
| 5061 | input_data->x, input_data->len, |
| 5062 | output_data, output_size, |
| 5063 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5064 | |
| 5065 | /* If the operation is not supported, just skip and not fail in case the |
| 5066 | * encryption involves a common limitation of cryptography hardwares and |
| 5067 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5068 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5069 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5070 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5071 | } |
| 5072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5073 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5075 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5076 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5077 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 5078 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 5079 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5080 | TEST_EQUAL(input_data->len, |
| 5081 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 5082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5083 | TEST_LE_U(input_data->len, |
| 5084 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5086 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 5087 | nonce->x, nonce->len, |
| 5088 | additional_data->x, |
| 5089 | additional_data->len, |
| 5090 | output_data, output_length, |
| 5091 | output_data2, output_length, |
| 5092 | &output_length2), |
| 5093 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5094 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5095 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5096 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5097 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5098 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5099 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5100 | psa_destroy_key(key); |
| 5101 | mbedtls_free(output_data); |
| 5102 | mbedtls_free(output_data2); |
| 5103 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5104 | } |
| 5105 | /* END_CASE */ |
| 5106 | |
| 5107 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5108 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 5109 | int alg_arg, |
| 5110 | data_t *nonce, |
| 5111 | data_t *additional_data, |
| 5112 | data_t *input_data, |
| 5113 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5114 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5115 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5116 | psa_key_type_t key_type = key_type_arg; |
| 5117 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5118 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5119 | unsigned char *output_data = NULL; |
| 5120 | size_t output_size = 0; |
| 5121 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5122 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5123 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5125 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5127 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5128 | psa_set_key_algorithm(&attributes, alg); |
| 5129 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5131 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5132 | &key)); |
| 5133 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5134 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5136 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5137 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5138 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 5139 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5140 | TEST_EQUAL(output_size, |
| 5141 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5142 | TEST_LE_U(output_size, |
| 5143 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5144 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5146 | status = psa_aead_encrypt(key, alg, |
| 5147 | nonce->x, nonce->len, |
| 5148 | additional_data->x, additional_data->len, |
| 5149 | input_data->x, input_data->len, |
| 5150 | output_data, output_size, |
| 5151 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5152 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 5153 | /* If the operation is not supported, just skip and not fail in case the |
| 5154 | * encryption involves a common limitation of cryptography hardwares and |
| 5155 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5156 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5157 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5158 | 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] | 5159 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5161 | PSA_ASSERT(status); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5162 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5163 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5164 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5165 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5166 | psa_destroy_key(key); |
| 5167 | mbedtls_free(output_data); |
| 5168 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5169 | } |
| 5170 | /* END_CASE */ |
| 5171 | |
| 5172 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 5174 | int alg_arg, |
| 5175 | data_t *nonce, |
| 5176 | data_t *additional_data, |
| 5177 | data_t *input_data, |
| 5178 | data_t *expected_data, |
| 5179 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5180 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5181 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5182 | psa_key_type_t key_type = key_type_arg; |
| 5183 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5184 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5185 | unsigned char *output_data = NULL; |
| 5186 | size_t output_size = 0; |
| 5187 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5188 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5189 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5190 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5192 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5194 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5195 | psa_set_key_algorithm(&attributes, alg); |
| 5196 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5198 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5199 | &key)); |
| 5200 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5201 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5203 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5204 | alg); |
| 5205 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 5206 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5207 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 5208 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5209 | TEST_EQUAL(output_size, |
| 5210 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5211 | TEST_LE_U(output_size, |
| 5212 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5213 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5214 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5216 | status = psa_aead_decrypt(key, alg, |
| 5217 | nonce->x, nonce->len, |
| 5218 | additional_data->x, |
| 5219 | additional_data->len, |
| 5220 | input_data->x, input_data->len, |
| 5221 | output_data, output_size, |
| 5222 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5223 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 5224 | /* If the operation is not supported, just skip and not fail in case the |
| 5225 | * decryption involves a common limitation of cryptography hardwares and |
| 5226 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5227 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5228 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5229 | 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] | 5230 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5232 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5234 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5235 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5236 | output_data, output_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5237 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5238 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5239 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5240 | psa_destroy_key(key); |
| 5241 | mbedtls_free(output_data); |
| 5242 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5243 | } |
| 5244 | /* END_CASE */ |
| 5245 | |
| 5246 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5247 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 5248 | int alg_arg, |
| 5249 | data_t *nonce, |
| 5250 | data_t *additional_data, |
| 5251 | data_t *input_data, |
| 5252 | int do_set_lengths, |
| 5253 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5254 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5255 | size_t ad_part_len = 0; |
| 5256 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5257 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5259 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 5260 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5262 | if (do_set_lengths) { |
| 5263 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5264 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5265 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5266 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5267 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5268 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5269 | |
| 5270 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5271 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5272 | alg_arg, nonce, |
| 5273 | additional_data, |
| 5274 | ad_part_len, |
| 5275 | input_data, -1, |
| 5276 | set_lengths_method, |
| 5277 | expected_output, |
| 5278 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5279 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5280 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5282 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5283 | mbedtls_test_set_step(1000 + ad_part_len); |
| 5284 | |
| 5285 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5286 | alg_arg, nonce, |
| 5287 | additional_data, |
| 5288 | ad_part_len, |
| 5289 | input_data, -1, |
| 5290 | set_lengths_method, |
| 5291 | expected_output, |
| 5292 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5293 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5294 | } |
| 5295 | } |
| 5296 | |
| 5297 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 5298 | /* Split data into length(data_part_len) parts. */ |
| 5299 | mbedtls_test_set_step(2000 + data_part_len); |
| 5300 | |
| 5301 | if (do_set_lengths) { |
| 5302 | if (data_part_len & 0x01) { |
| 5303 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5304 | } else { |
| 5305 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 5306 | } |
| 5307 | } |
| 5308 | |
| 5309 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5310 | alg_arg, nonce, |
| 5311 | additional_data, -1, |
| 5312 | input_data, data_part_len, |
| 5313 | set_lengths_method, |
| 5314 | expected_output, |
| 5315 | 1, 0)) { |
| 5316 | break; |
| 5317 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5318 | |
| 5319 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5320 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5322 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5323 | alg_arg, nonce, |
| 5324 | additional_data, -1, |
| 5325 | input_data, data_part_len, |
| 5326 | set_lengths_method, |
| 5327 | expected_output, |
| 5328 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5329 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5330 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5331 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5332 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5333 | /* Goto is required to silence warnings about unused labels, as we |
| 5334 | * don't actually do any test assertions in this function. */ |
| 5335 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5336 | } |
| 5337 | /* END_CASE */ |
| 5338 | |
| 5339 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5340 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5341 | int alg_arg, |
| 5342 | data_t *nonce, |
| 5343 | data_t *additional_data, |
| 5344 | data_t *input_data, |
| 5345 | int do_set_lengths, |
| 5346 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5347 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5348 | size_t ad_part_len = 0; |
| 5349 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5350 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5353 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5354 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | if (do_set_lengths) { |
| 5357 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5358 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5359 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5360 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5361 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5362 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5364 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5365 | alg_arg, nonce, |
| 5366 | additional_data, |
| 5367 | ad_part_len, |
| 5368 | input_data, -1, |
| 5369 | set_lengths_method, |
| 5370 | expected_output, |
| 5371 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5372 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5373 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5374 | |
| 5375 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5376 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5379 | alg_arg, nonce, |
| 5380 | additional_data, |
| 5381 | ad_part_len, |
| 5382 | input_data, -1, |
| 5383 | set_lengths_method, |
| 5384 | expected_output, |
| 5385 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5386 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5387 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5388 | } |
| 5389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5390 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5391 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5394 | if (do_set_lengths) { |
| 5395 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5396 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5397 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5398 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5399 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5400 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5402 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5403 | alg_arg, nonce, |
| 5404 | additional_data, -1, |
| 5405 | input_data, data_part_len, |
| 5406 | set_lengths_method, |
| 5407 | expected_output, |
| 5408 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5409 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5410 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5411 | |
| 5412 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5413 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5415 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5416 | alg_arg, nonce, |
| 5417 | additional_data, -1, |
| 5418 | input_data, data_part_len, |
| 5419 | set_lengths_method, |
| 5420 | expected_output, |
| 5421 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5422 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5423 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5424 | } |
| 5425 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5426 | /* Goto is required to silence warnings about unused labels, as we |
| 5427 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5428 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5429 | } |
| 5430 | /* END_CASE */ |
| 5431 | |
| 5432 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5433 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5434 | int alg_arg, |
| 5435 | int nonce_length, |
| 5436 | int expected_nonce_length_arg, |
| 5437 | data_t *additional_data, |
| 5438 | data_t *input_data, |
| 5439 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5440 | { |
| 5441 | |
| 5442 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5443 | psa_key_type_t key_type = key_type_arg; |
| 5444 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5445 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
David Horstmann | 52402ec | 2023-12-11 15:09:46 +0000 | [diff] [blame] | 5446 | /* Some tests try to get more than the maximum nonce length, |
| 5447 | * so allocate double. */ |
| 5448 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE * 2]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5449 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5450 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5451 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5452 | size_t actual_nonce_length = 0; |
| 5453 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5454 | unsigned char *output = NULL; |
| 5455 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5456 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5457 | size_t ciphertext_size = 0; |
| 5458 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5459 | size_t tag_length = 0; |
| 5460 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5462 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5464 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5465 | psa_set_key_algorithm(&attributes, alg); |
| 5466 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5468 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5469 | &key)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5471 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5473 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5474 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5475 | TEST_CALLOC(output, output_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5477 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5480 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5481 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5483 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5484 | |
| 5485 | /* If the operation is not supported, just skip and not fail in case the |
| 5486 | * encryption involves a common limitation of cryptography hardwares and |
| 5487 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5488 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5489 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5490 | 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] | 5491 | } |
| 5492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5495 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5496 | nonce_length, |
| 5497 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5499 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5501 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5503 | if (expected_status == PSA_SUCCESS) { |
| 5504 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5505 | alg)); |
| 5506 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5508 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5510 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5511 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5512 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5513 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5515 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5516 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5518 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5519 | output, output_size, |
| 5520 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5522 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5523 | &ciphertext_length, tag_buffer, |
| 5524 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5525 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5526 | |
| 5527 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5528 | psa_destroy_key(key); |
| 5529 | mbedtls_free(output); |
| 5530 | mbedtls_free(ciphertext); |
| 5531 | psa_aead_abort(&operation); |
| 5532 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5533 | } |
| 5534 | /* END_CASE */ |
| 5535 | |
| 5536 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5537 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5538 | int alg_arg, |
| 5539 | int nonce_length_arg, |
| 5540 | int set_lengths_method_arg, |
| 5541 | data_t *additional_data, |
| 5542 | data_t *input_data, |
| 5543 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5544 | { |
| 5545 | |
| 5546 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5547 | psa_key_type_t key_type = key_type_arg; |
| 5548 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5549 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5550 | uint8_t *nonce_buffer = NULL; |
| 5551 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5552 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5553 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5554 | unsigned char *output = NULL; |
| 5555 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5556 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5557 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5558 | size_t ciphertext_size = 0; |
| 5559 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5560 | size_t tag_length = 0; |
| 5561 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5562 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5563 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5565 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5567 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5568 | psa_set_key_algorithm(&attributes, alg); |
| 5569 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5571 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5572 | &key)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5574 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5577 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5578 | TEST_CALLOC(output, output_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5582 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5583 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5584 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5586 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5587 | |
| 5588 | /* If the operation is not supported, just skip and not fail in case the |
| 5589 | * encryption involves a common limitation of cryptography hardwares and |
| 5590 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5592 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5593 | 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] | 5594 | } |
| 5595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5596 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5597 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5598 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5599 | if (nonce_length_arg == -1) { |
| 5600 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5601 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5602 | nonce_length = 0; |
| 5603 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5604 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5605 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5606 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5608 | if (nonce_buffer) { |
| 5609 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5610 | nonce_buffer[index] = 'a' + index; |
| 5611 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5612 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5613 | } |
| 5614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5615 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5616 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5617 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5618 | } |
| 5619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5620 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5622 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5624 | if (expected_status == PSA_SUCCESS) { |
| 5625 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5626 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5627 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5628 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5629 | if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5630 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5631 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5632 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5633 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5634 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5635 | additional_data->len), |
| 5636 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5638 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5639 | output, output_size, |
| 5640 | &ciphertext_length), |
| 5641 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5643 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5644 | &ciphertext_length, tag_buffer, |
| 5645 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5646 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5650 | psa_destroy_key(key); |
| 5651 | mbedtls_free(output); |
| 5652 | mbedtls_free(ciphertext); |
| 5653 | mbedtls_free(nonce_buffer); |
| 5654 | psa_aead_abort(&operation); |
| 5655 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5656 | } |
| 5657 | /* END_CASE */ |
| 5658 | |
| 5659 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5660 | void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5661 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5662 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5663 | data_t *nonce, |
| 5664 | data_t *additional_data, |
| 5665 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5666 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5667 | { |
| 5668 | |
| 5669 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5670 | psa_key_type_t key_type = key_type_arg; |
| 5671 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5672 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5673 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5674 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5675 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5676 | unsigned char *output = NULL; |
| 5677 | unsigned char *ciphertext = NULL; |
| 5678 | size_t output_size = output_size_arg; |
| 5679 | size_t ciphertext_size = 0; |
| 5680 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5681 | size_t tag_length = 0; |
| 5682 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5684 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5686 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5687 | psa_set_key_algorithm(&attributes, alg); |
| 5688 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5690 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5691 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5694 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5695 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5697 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5698 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5699 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5701 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5702 | |
| 5703 | /* If the operation is not supported, just skip and not fail in case the |
| 5704 | * encryption involves a common limitation of cryptography hardwares and |
| 5705 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5706 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5707 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5708 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5709 | } |
| 5710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5711 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5714 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5716 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5718 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5719 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5721 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5722 | output, output_size, &ciphertext_length); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5724 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5726 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5727 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5728 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5729 | &ciphertext_length, tag_buffer, |
| 5730 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5731 | } |
| 5732 | |
| 5733 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5734 | psa_destroy_key(key); |
| 5735 | mbedtls_free(output); |
| 5736 | mbedtls_free(ciphertext); |
| 5737 | psa_aead_abort(&operation); |
| 5738 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5739 | } |
| 5740 | /* END_CASE */ |
| 5741 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5742 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5743 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5744 | int alg_arg, |
| 5745 | int finish_ciphertext_size_arg, |
| 5746 | int tag_size_arg, |
| 5747 | data_t *nonce, |
| 5748 | data_t *additional_data, |
| 5749 | data_t *input_data, |
| 5750 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5751 | { |
| 5752 | |
| 5753 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5754 | psa_key_type_t key_type = key_type_arg; |
| 5755 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5756 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5757 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5758 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5759 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5760 | unsigned char *ciphertext = NULL; |
| 5761 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5762 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5763 | size_t ciphertext_size = 0; |
| 5764 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5765 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5766 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5767 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5772 | psa_set_key_algorithm(&attributes, alg); |
| 5773 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5775 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5776 | &key)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5780 | 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] | 5781 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5782 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5783 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5784 | TEST_CALLOC(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5785 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5786 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5788 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5789 | |
| 5790 | /* If the operation is not supported, just skip and not fail in case the |
| 5791 | * encryption involves a common limitation of cryptography hardwares and |
| 5792 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5793 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5794 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5795 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5796 | } |
| 5797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5798 | PSA_ASSERT(status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5800 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5802 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5803 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5805 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5806 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5807 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5808 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5809 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5810 | |
| 5811 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5812 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5813 | finish_ciphertext_size, |
| 5814 | &ciphertext_length, tag_buffer, |
| 5815 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5818 | |
| 5819 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5820 | psa_destroy_key(key); |
| 5821 | mbedtls_free(ciphertext); |
| 5822 | mbedtls_free(finish_ciphertext); |
| 5823 | mbedtls_free(tag_buffer); |
| 5824 | psa_aead_abort(&operation); |
| 5825 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5826 | } |
| 5827 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5828 | |
| 5829 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5830 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5831 | int alg_arg, |
| 5832 | data_t *nonce, |
| 5833 | data_t *additional_data, |
| 5834 | data_t *input_data, |
| 5835 | data_t *tag, |
| 5836 | int tag_usage_arg, |
| 5837 | int expected_setup_status_arg, |
| 5838 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5839 | { |
| 5840 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5841 | psa_key_type_t key_type = key_type_arg; |
| 5842 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5843 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5844 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5845 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5846 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5847 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5848 | unsigned char *plaintext = NULL; |
| 5849 | unsigned char *finish_plaintext = NULL; |
| 5850 | size_t plaintext_size = 0; |
| 5851 | size_t plaintext_length = 0; |
| 5852 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5853 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5854 | unsigned char *tag_buffer = NULL; |
| 5855 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5857 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5859 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5860 | psa_set_key_algorithm(&attributes, alg); |
| 5861 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5863 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5864 | &key)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5866 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5868 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5869 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5870 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5871 | TEST_CALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5873 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5874 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5875 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5877 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5878 | |
| 5879 | /* If the operation is not supported, just skip and not fail in case the |
| 5880 | * encryption involves a common limitation of cryptography hardwares and |
| 5881 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5882 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5883 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5884 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5885 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5886 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5889 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5890 | } |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5892 | PSA_ASSERT(status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5894 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5896 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5897 | input_data->len); |
| 5898 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5899 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5900 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5901 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5903 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5904 | input_data->len, |
| 5905 | plaintext, plaintext_size, |
| 5906 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5908 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5909 | tag_buffer = tag->x; |
| 5910 | tag_size = tag->len; |
| 5911 | } |
| 5912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5913 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5914 | verify_plaintext_size, |
| 5915 | &plaintext_length, |
| 5916 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5918 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5919 | |
| 5920 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5921 | psa_destroy_key(key); |
| 5922 | mbedtls_free(plaintext); |
| 5923 | mbedtls_free(finish_plaintext); |
| 5924 | psa_aead_abort(&operation); |
| 5925 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5926 | } |
| 5927 | /* END_CASE */ |
| 5928 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5929 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5930 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5931 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5932 | { |
| 5933 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5934 | psa_key_type_t key_type = key_type_arg; |
| 5935 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5936 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5937 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5938 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5939 | psa_status_t expected_status = expected_status_arg; |
| 5940 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5941 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5943 | psa_set_key_usage_flags(&attributes, |
| 5944 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5945 | psa_set_key_algorithm(&attributes, alg); |
| 5946 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5948 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5949 | &key)); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5951 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5953 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5954 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5955 | psa_aead_abort(&operation); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5957 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5959 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5960 | |
| 5961 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5962 | psa_destroy_key(key); |
| 5963 | psa_aead_abort(&operation); |
| 5964 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5965 | } |
| 5966 | /* END_CASE */ |
| 5967 | |
| 5968 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5969 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5970 | int alg_arg, |
| 5971 | data_t *nonce, |
| 5972 | data_t *additional_data, |
| 5973 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5974 | { |
| 5975 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5976 | psa_key_type_t key_type = key_type_arg; |
| 5977 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5978 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5979 | unsigned char *output_data = NULL; |
| 5980 | unsigned char *final_data = NULL; |
| 5981 | size_t output_size = 0; |
| 5982 | size_t finish_output_size = 0; |
| 5983 | size_t output_length = 0; |
| 5984 | size_t key_bits = 0; |
| 5985 | size_t tag_length = 0; |
| 5986 | size_t tag_size = 0; |
| 5987 | size_t nonce_length = 0; |
| 5988 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5989 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5990 | size_t output_part_length = 0; |
| 5991 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5993 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | psa_set_key_usage_flags(&attributes, |
| 5996 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5997 | psa_set_key_algorithm(&attributes, alg); |
| 5998 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6000 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6001 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6003 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6004 | key_bits = psa_get_key_bits(&attributes); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6006 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6007 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6008 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6010 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6011 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6012 | TEST_CALLOC(output_data, output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6014 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6016 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6017 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6018 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6019 | |
| 6020 | /* Test all operations error without calling setup first. */ |
| 6021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6022 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6023 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6025 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6027 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6028 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6029 | &nonce_length), |
| 6030 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6031 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6032 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6033 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6034 | /* ------------------------------------------------------- */ |
| 6035 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6036 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6037 | input_data->len), |
| 6038 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6040 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6041 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6042 | /* ------------------------------------------------------- */ |
| 6043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6044 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6045 | additional_data->len), |
| 6046 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6047 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6048 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6049 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6050 | /* ------------------------------------------------------- */ |
| 6051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6052 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6053 | input_data->len, output_data, |
| 6054 | output_size, &output_length), |
| 6055 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6057 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6058 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6059 | /* ------------------------------------------------------- */ |
| 6060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6061 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6062 | finish_output_size, |
| 6063 | &output_part_length, |
| 6064 | tag_buffer, tag_length, |
| 6065 | &tag_size), |
| 6066 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6068 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6069 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6070 | /* ------------------------------------------------------- */ |
| 6071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6072 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6073 | finish_output_size, |
| 6074 | &output_part_length, |
| 6075 | tag_buffer, |
| 6076 | tag_length), |
| 6077 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6079 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6080 | |
| 6081 | /* Test for double setups. */ |
| 6082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6083 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6085 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 6086 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6088 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6089 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6090 | /* ------------------------------------------------------- */ |
| 6091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6092 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6094 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 6095 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6096 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6097 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6098 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6099 | /* ------------------------------------------------------- */ |
| 6100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6101 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 6104 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6106 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6107 | |
| 6108 | /* ------------------------------------------------------- */ |
| 6109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6110 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 6113 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6115 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6116 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6117 | /* Test for not setting a nonce. */ |
| 6118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6119 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6121 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6122 | additional_data->len), |
| 6123 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6125 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6126 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6127 | /* ------------------------------------------------------- */ |
| 6128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6129 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6131 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6132 | input_data->len, output_data, |
| 6133 | output_size, &output_length), |
| 6134 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6136 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6137 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6138 | /* ------------------------------------------------------- */ |
| 6139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6140 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6142 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6143 | finish_output_size, |
| 6144 | &output_part_length, |
| 6145 | tag_buffer, tag_length, |
| 6146 | &tag_size), |
| 6147 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6149 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6150 | |
| 6151 | /* ------------------------------------------------------- */ |
| 6152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6153 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6155 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6156 | finish_output_size, |
| 6157 | &output_part_length, |
| 6158 | tag_buffer, |
| 6159 | tag_length), |
| 6160 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6162 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6163 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6164 | /* Test for double setting nonce. */ |
| 6165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6166 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6168 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6170 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6171 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6173 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6174 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6175 | /* Test for double generating nonce. */ |
| 6176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6177 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6179 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6180 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6181 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6183 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6184 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6185 | &nonce_length), |
| 6186 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6187 | |
| 6188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6189 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6190 | |
| 6191 | /* Test for generate nonce then set and vice versa */ |
| 6192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6193 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6195 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6196 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6197 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6199 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6200 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6202 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6203 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6204 | /* Test for generating nonce after calling set lengths */ |
| 6205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6206 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6208 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6209 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6211 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6212 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6213 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6215 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6216 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6217 | /* 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] | 6218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6219 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6221 | if (operation.alg == PSA_ALG_CCM) { |
| 6222 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6223 | input_data->len), |
| 6224 | PSA_ERROR_INVALID_ARGUMENT); |
| 6225 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6226 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6227 | &nonce_length), |
| 6228 | PSA_ERROR_BAD_STATE); |
| 6229 | } else { |
| 6230 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6231 | input_data->len)); |
| 6232 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6233 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6234 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6235 | } |
| 6236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6237 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6238 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6239 | /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */ |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 6240 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6241 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6243 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6244 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6245 | input_data->len), |
| 6246 | PSA_ERROR_INVALID_ARGUMENT); |
| 6247 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6248 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6249 | &nonce_length), |
| 6250 | PSA_ERROR_BAD_STATE); |
| 6251 | } else { |
| 6252 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6253 | input_data->len)); |
| 6254 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6255 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6256 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6257 | } |
| 6258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6259 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 6260 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6261 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6262 | /* 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] | 6263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6264 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6266 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6267 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6268 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6270 | if (operation.alg == PSA_ALG_CCM) { |
| 6271 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6272 | input_data->len), |
| 6273 | PSA_ERROR_INVALID_ARGUMENT); |
| 6274 | } else { |
| 6275 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6276 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6277 | } |
| 6278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6279 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6280 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6281 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6282 | /* Test for setting nonce after calling set lengths */ |
| 6283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6284 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6287 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6291 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6292 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6293 | /* 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] | 6294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6295 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6297 | if (operation.alg == PSA_ALG_CCM) { |
| 6298 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6299 | input_data->len), |
| 6300 | PSA_ERROR_INVALID_ARGUMENT); |
| 6301 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6302 | PSA_ERROR_BAD_STATE); |
| 6303 | } else { |
| 6304 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6305 | input_data->len)); |
| 6306 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6307 | } |
| 6308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6309 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6310 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6311 | /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */ |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6312 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6313 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6315 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6316 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6317 | input_data->len), |
| 6318 | PSA_ERROR_INVALID_ARGUMENT); |
| 6319 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6320 | PSA_ERROR_BAD_STATE); |
| 6321 | } else { |
| 6322 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6323 | input_data->len)); |
| 6324 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6325 | } |
| 6326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6327 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6328 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6329 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6330 | /* 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] | 6331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6332 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6334 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6336 | if (operation.alg == PSA_ALG_CCM) { |
| 6337 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6338 | input_data->len), |
| 6339 | PSA_ERROR_INVALID_ARGUMENT); |
| 6340 | } else { |
| 6341 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6342 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6343 | } |
| 6344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6345 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6346 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6347 | /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */ |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6348 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6349 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6351 | if (operation.alg == PSA_ALG_GCM) { |
| 6352 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6353 | SIZE_MAX), |
| 6354 | PSA_ERROR_INVALID_ARGUMENT); |
| 6355 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6356 | PSA_ERROR_BAD_STATE); |
| 6357 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6358 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6359 | SIZE_MAX)); |
| 6360 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6361 | } |
| 6362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6363 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6364 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6365 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6366 | /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */ |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6367 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6368 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6370 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6372 | if (operation.alg == PSA_ALG_GCM) { |
| 6373 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6374 | SIZE_MAX), |
| 6375 | PSA_ERROR_INVALID_ARGUMENT); |
| 6376 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6377 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6378 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6379 | } |
| 6380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6381 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6382 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6383 | |
| 6384 | /* ------------------------------------------------------- */ |
| 6385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6386 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6388 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6390 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6391 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6392 | &nonce_length), |
| 6393 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6395 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6396 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6397 | /* Test for generating nonce in decrypt setup. */ |
| 6398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6399 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6401 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6402 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6403 | &nonce_length), |
| 6404 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6406 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6407 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6408 | /* Test for setting lengths twice. */ |
| 6409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6410 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6412 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6414 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6415 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6417 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6418 | input_data->len), |
| 6419 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6421 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6422 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6423 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6425 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6427 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6429 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6431 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6432 | additional_data->len), |
| 6433 | PSA_ERROR_BAD_STATE); |
| 6434 | } else { |
| 6435 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6436 | additional_data->len)); |
| 6437 | |
| 6438 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6439 | input_data->len), |
| 6440 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6441 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6442 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6443 | |
| 6444 | /* ------------------------------------------------------- */ |
| 6445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6446 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6448 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6450 | if (operation.alg == PSA_ALG_CCM) { |
| 6451 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6452 | input_data->len, output_data, |
| 6453 | output_size, &output_length), |
| 6454 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6456 | } else { |
| 6457 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6458 | input_data->len, output_data, |
| 6459 | output_size, &output_length)); |
| 6460 | |
| 6461 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6462 | input_data->len), |
| 6463 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6464 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6465 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6466 | |
| 6467 | /* ------------------------------------------------------- */ |
| 6468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6469 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6471 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6473 | if (operation.alg == PSA_ALG_CCM) { |
| 6474 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6475 | finish_output_size, |
| 6476 | &output_part_length, |
| 6477 | tag_buffer, tag_length, |
| 6478 | &tag_size)); |
| 6479 | } else { |
| 6480 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6481 | finish_output_size, |
| 6482 | &output_part_length, |
| 6483 | tag_buffer, tag_length, |
| 6484 | &tag_size)); |
| 6485 | |
| 6486 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6487 | input_data->len), |
| 6488 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6489 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6490 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6491 | |
| 6492 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6494 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6496 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6497 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6498 | &nonce_length)); |
| 6499 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6501 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6502 | additional_data->len), |
| 6503 | PSA_ERROR_BAD_STATE); |
| 6504 | } else { |
| 6505 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6506 | additional_data->len)); |
| 6507 | |
| 6508 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6509 | input_data->len), |
| 6510 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6511 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6512 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6513 | |
| 6514 | /* ------------------------------------------------------- */ |
| 6515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6516 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6518 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6519 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6520 | &nonce_length)); |
| 6521 | if (operation.alg == PSA_ALG_CCM) { |
| 6522 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6523 | input_data->len, output_data, |
| 6524 | output_size, &output_length), |
| 6525 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6527 | } else { |
| 6528 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6529 | input_data->len, output_data, |
| 6530 | output_size, &output_length)); |
| 6531 | |
| 6532 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6533 | input_data->len), |
| 6534 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6535 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6536 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6537 | |
| 6538 | /* ------------------------------------------------------- */ |
| 6539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6540 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6542 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6543 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6544 | &nonce_length)); |
| 6545 | if (operation.alg == PSA_ALG_CCM) { |
| 6546 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6547 | finish_output_size, |
| 6548 | &output_part_length, |
| 6549 | tag_buffer, tag_length, |
| 6550 | &tag_size)); |
| 6551 | } else { |
| 6552 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6553 | finish_output_size, |
| 6554 | &output_part_length, |
| 6555 | tag_buffer, tag_length, |
| 6556 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6558 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6559 | input_data->len), |
| 6560 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6561 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6562 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6563 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6564 | /* Test for not sending any additional data or data after setting non zero |
| 6565 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6567 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6569 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6571 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6572 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6574 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6575 | finish_output_size, |
| 6576 | &output_part_length, |
| 6577 | tag_buffer, tag_length, |
| 6578 | &tag_size), |
| 6579 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6581 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6582 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6583 | /* Test for not sending any additional data or data after setting non-zero |
| 6584 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6586 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6588 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6590 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6591 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6593 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6594 | finish_output_size, |
| 6595 | &output_part_length, |
| 6596 | tag_buffer, |
| 6597 | tag_length), |
| 6598 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6600 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6601 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6602 | /* Test for not sending any additional data after setting a non-zero length |
| 6603 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6605 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6607 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6609 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6610 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6612 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6613 | input_data->len, output_data, |
| 6614 | output_size, &output_length), |
| 6615 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6617 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6618 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6619 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6621 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6623 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6625 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6626 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6628 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6629 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6631 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6632 | finish_output_size, |
| 6633 | &output_part_length, |
| 6634 | tag_buffer, tag_length, |
| 6635 | &tag_size), |
| 6636 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6638 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6639 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6640 | /* Test for sending too much additional data after setting lengths. */ |
| 6641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6642 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6644 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6646 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6647 | |
| 6648 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6649 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6650 | additional_data->len), |
| 6651 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6653 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6654 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6655 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6657 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6659 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6661 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6662 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6664 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6665 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6667 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6668 | 1), |
| 6669 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6671 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6672 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6673 | /* Test for sending too much data after setting lengths. */ |
| 6674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6675 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6677 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6679 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6681 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6682 | input_data->len, output_data, |
| 6683 | output_size, &output_length), |
| 6684 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6686 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6687 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6688 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6690 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6692 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6694 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6695 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6697 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6698 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6700 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6701 | input_data->len, output_data, |
| 6702 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6704 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6705 | 1, output_data, |
| 6706 | output_size, &output_length), |
| 6707 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6709 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6710 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6711 | /* Test sending additional data after data. */ |
| 6712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6713 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6715 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6717 | if (operation.alg != PSA_ALG_CCM) { |
| 6718 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6719 | input_data->len, output_data, |
| 6720 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6722 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6723 | additional_data->len), |
| 6724 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6725 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6726 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6727 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6728 | /* Test calling finish on decryption. */ |
| 6729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6730 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6732 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6734 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6735 | finish_output_size, |
| 6736 | &output_part_length, |
| 6737 | tag_buffer, tag_length, |
| 6738 | &tag_size), |
| 6739 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6741 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6742 | |
| 6743 | /* Test calling verify on encryption. */ |
| 6744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6745 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6747 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6749 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6750 | finish_output_size, |
| 6751 | &output_part_length, |
| 6752 | tag_buffer, |
| 6753 | tag_length), |
| 6754 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6756 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6757 | |
| 6758 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6759 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6760 | psa_destroy_key(key); |
| 6761 | psa_aead_abort(&operation); |
| 6762 | mbedtls_free(output_data); |
| 6763 | mbedtls_free(final_data); |
| 6764 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6765 | } |
| 6766 | /* END_CASE */ |
| 6767 | |
| 6768 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6769 | void signature_size(int type_arg, |
| 6770 | int bits, |
| 6771 | int alg_arg, |
| 6772 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6773 | { |
| 6774 | psa_key_type_t type = type_arg; |
| 6775 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6776 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6778 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6779 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6780 | exit: |
| 6781 | ; |
| 6782 | } |
| 6783 | /* END_CASE */ |
| 6784 | |
| 6785 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6786 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6787 | int alg_arg, data_t *input_data, |
| 6788 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6789 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6790 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6791 | psa_key_type_t key_type = key_type_arg; |
| 6792 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6793 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6794 | unsigned char *signature = NULL; |
| 6795 | size_t signature_size; |
| 6796 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6797 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6799 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6800 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6801 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6802 | psa_set_key_algorithm(&attributes, alg); |
| 6803 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6805 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6806 | &key)); |
| 6807 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6808 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6809 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6810 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6811 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6812 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6813 | key_bits, alg); |
| 6814 | TEST_ASSERT(signature_size != 0); |
| 6815 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6816 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6817 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6818 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6819 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6820 | input_data->x, input_data->len, |
| 6821 | signature, signature_size, |
| 6822 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6823 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6824 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6825 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6826 | |
| 6827 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6828 | /* |
| 6829 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6830 | * thus reset them as required. |
| 6831 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6832 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6834 | psa_destroy_key(key); |
| 6835 | mbedtls_free(signature); |
| 6836 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6837 | } |
| 6838 | /* END_CASE */ |
| 6839 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6840 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6841 | /** |
| 6842 | * sign_hash_interruptible() test intentions: |
| 6843 | * |
| 6844 | * Note: This test can currently only handle ECDSA. |
| 6845 | * |
| 6846 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6847 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6848 | * |
| 6849 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6850 | * expected for different max_ops values. |
| 6851 | * |
| 6852 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6853 | * and that each successful stage completes some ops (this is not mandated by |
| 6854 | * the PSA specification, but is currently the case). |
| 6855 | * |
| 6856 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6857 | * complete() calls does not alter the number of ops returned. |
| 6858 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6859 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6860 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6861 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6862 | { |
| 6863 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6864 | psa_key_type_t key_type = key_type_arg; |
| 6865 | psa_algorithm_t alg = alg_arg; |
| 6866 | size_t key_bits; |
| 6867 | unsigned char *signature = NULL; |
| 6868 | size_t signature_size; |
| 6869 | size_t signature_length = 0xdeadbeef; |
| 6870 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6871 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6872 | uint32_t num_ops = 0; |
| 6873 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6874 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6875 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6876 | size_t min_completes = 0; |
| 6877 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6878 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6879 | psa_sign_hash_interruptible_operation_t operation = |
| 6880 | psa_sign_hash_interruptible_operation_init(); |
| 6881 | |
| 6882 | PSA_ASSERT(psa_crypto_init()); |
| 6883 | |
| 6884 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6885 | psa_set_key_algorithm(&attributes, alg); |
| 6886 | psa_set_key_type(&attributes, key_type); |
| 6887 | |
| 6888 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6889 | &key)); |
| 6890 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6891 | key_bits = psa_get_key_bits(&attributes); |
| 6892 | |
| 6893 | /* Allocate a buffer which has the size advertised by the |
| 6894 | * library. */ |
| 6895 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6896 | key_bits, alg); |
| 6897 | TEST_ASSERT(signature_size != 0); |
| 6898 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6899 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6900 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6901 | psa_interruptible_set_max_ops(max_ops); |
| 6902 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6903 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6904 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6905 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6906 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6907 | TEST_ASSERT(num_ops_prior == 0); |
| 6908 | |
| 6909 | /* Start performing the signature. */ |
| 6910 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6911 | input_data->x, input_data->len)); |
| 6912 | |
| 6913 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6914 | TEST_ASSERT(num_ops_prior == 0); |
| 6915 | |
| 6916 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6917 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6918 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6919 | &signature_length); |
| 6920 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6921 | num_completes++; |
| 6922 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6923 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6924 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6925 | /* We are asserting here that every complete makes progress |
| 6926 | * (completes some ops), which is true of the internal |
| 6927 | * implementation and probably any implementation, however this is |
| 6928 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6929 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6930 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6931 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6932 | |
| 6933 | /* Ensure calling get_num_ops() twice still returns the same |
| 6934 | * number of ops as previously reported. */ |
| 6935 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6936 | |
| 6937 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6938 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6939 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6940 | |
| 6941 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6942 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6943 | TEST_LE_U(min_completes, num_completes); |
| 6944 | TEST_LE_U(num_completes, max_completes); |
| 6945 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6946 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6947 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6948 | signature, signature_length); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6949 | |
| 6950 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6951 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6952 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6953 | TEST_ASSERT(num_ops == 0); |
| 6954 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6955 | exit: |
| 6956 | |
| 6957 | /* |
| 6958 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6959 | * thus reset them as required. |
| 6960 | */ |
| 6961 | psa_reset_key_attributes(&attributes); |
| 6962 | |
| 6963 | psa_destroy_key(key); |
| 6964 | mbedtls_free(signature); |
| 6965 | PSA_DONE(); |
| 6966 | } |
| 6967 | /* END_CASE */ |
| 6968 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6969 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6970 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6971 | int alg_arg, data_t *input_data, |
| 6972 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6973 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6974 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6975 | psa_key_type_t key_type = key_type_arg; |
| 6976 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6977 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6978 | psa_status_t actual_status; |
| 6979 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6980 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6981 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6982 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6983 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6984 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6986 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6988 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6989 | psa_set_key_algorithm(&attributes, alg); |
| 6990 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6992 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6993 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6995 | actual_status = psa_sign_hash(key, alg, |
| 6996 | input_data->x, input_data->len, |
| 6997 | signature, signature_size, |
| 6998 | &signature_length); |
| 6999 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 7000 | /* The value of *signature_length is unspecified on error, but |
| 7001 | * whatever it is, it should be less than signature_size, so that |
| 7002 | * if the caller tries to read *signature_length bytes without |
| 7003 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7004 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7005 | |
| 7006 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7007 | psa_reset_key_attributes(&attributes); |
| 7008 | psa_destroy_key(key); |
| 7009 | mbedtls_free(signature); |
| 7010 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7011 | } |
| 7012 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 7013 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7014 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7015 | /** |
| 7016 | * sign_hash_fail_interruptible() test intentions: |
| 7017 | * |
| 7018 | * Note: This test can currently only handle ECDSA. |
| 7019 | * |
| 7020 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 7021 | * correct error codes, and at the correct point (at start or during |
| 7022 | * complete). |
| 7023 | * |
| 7024 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 7025 | * expected for different max_ops values. |
| 7026 | * |
| 7027 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7028 | * and that each successful stage completes some ops (this is not mandated by |
| 7029 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7030 | * |
| 7031 | * 4. Check that calling complete() when start() fails and complete() |
| 7032 | * after completion results in a BAD_STATE error. |
| 7033 | * |
| 7034 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7035 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7036 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7037 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7038 | int alg_arg, data_t *input_data, |
| 7039 | int signature_size_arg, |
| 7040 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7041 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7042 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7043 | { |
| 7044 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7045 | psa_key_type_t key_type = key_type_arg; |
| 7046 | psa_algorithm_t alg = alg_arg; |
| 7047 | size_t signature_size = signature_size_arg; |
| 7048 | psa_status_t actual_status; |
| 7049 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7050 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7051 | unsigned char *signature = NULL; |
| 7052 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7053 | uint32_t num_ops = 0; |
| 7054 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7055 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7056 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7057 | size_t min_completes = 0; |
| 7058 | size_t max_completes = 0; |
| 7059 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7060 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7061 | psa_sign_hash_interruptible_operation_t operation = |
| 7062 | psa_sign_hash_interruptible_operation_init(); |
| 7063 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7064 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7065 | |
| 7066 | PSA_ASSERT(psa_crypto_init()); |
| 7067 | |
| 7068 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 7069 | psa_set_key_algorithm(&attributes, alg); |
| 7070 | psa_set_key_type(&attributes, key_type); |
| 7071 | |
| 7072 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7073 | &key)); |
| 7074 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7075 | psa_interruptible_set_max_ops(max_ops); |
| 7076 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7077 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7078 | expected_complete_status, |
| 7079 | &min_completes, |
| 7080 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7081 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7082 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 7083 | TEST_ASSERT(num_ops_prior == 0); |
| 7084 | |
| 7085 | /* Start performing the signature. */ |
| 7086 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 7087 | input_data->x, input_data->len); |
| 7088 | |
| 7089 | TEST_EQUAL(actual_status, expected_start_status); |
| 7090 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7091 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7092 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7093 | * start failed. */ |
| 7094 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7095 | signature_size, |
| 7096 | &signature_length); |
| 7097 | |
| 7098 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7099 | |
| 7100 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7101 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 7102 | input_data->x, input_data->len); |
| 7103 | |
| 7104 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7105 | } |
| 7106 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7107 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 7108 | TEST_ASSERT(num_ops_prior == 0); |
| 7109 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7110 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7111 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7112 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7113 | signature_size, |
| 7114 | &signature_length); |
| 7115 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7116 | num_completes++; |
| 7117 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7118 | if (actual_status == PSA_SUCCESS || |
| 7119 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7120 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7121 | /* We are asserting here that every complete makes progress |
| 7122 | * (completes some ops), which is true of the internal |
| 7123 | * implementation and probably any implementation, however this is |
| 7124 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7125 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7126 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7127 | num_ops_prior = num_ops; |
| 7128 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7129 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7130 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7131 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7132 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7133 | /* Check that another complete returns BAD_STATE. */ |
| 7134 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7135 | signature_size, |
| 7136 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7137 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7138 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7139 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7140 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 7141 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7142 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 7143 | TEST_ASSERT(num_ops == 0); |
| 7144 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7145 | /* The value of *signature_length is unspecified on error, but |
| 7146 | * whatever it is, it should be less than signature_size, so that |
| 7147 | * if the caller tries to read *signature_length bytes without |
| 7148 | * checking the error code then they don't overflow a buffer. */ |
| 7149 | TEST_LE_U(signature_length, signature_size); |
| 7150 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7151 | TEST_LE_U(min_completes, num_completes); |
| 7152 | TEST_LE_U(num_completes, max_completes); |
| 7153 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7154 | exit: |
| 7155 | psa_reset_key_attributes(&attributes); |
| 7156 | psa_destroy_key(key); |
| 7157 | mbedtls_free(signature); |
| 7158 | PSA_DONE(); |
| 7159 | } |
| 7160 | /* END_CASE */ |
| 7161 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 7162 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7163 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 7164 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7165 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7166 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7167 | psa_key_type_t key_type = key_type_arg; |
| 7168 | psa_algorithm_t alg = alg_arg; |
| 7169 | size_t key_bits; |
| 7170 | unsigned char *signature = NULL; |
| 7171 | size_t signature_size; |
| 7172 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7173 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7175 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7177 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 7178 | psa_set_key_algorithm(&attributes, alg); |
| 7179 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7181 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7182 | &key)); |
| 7183 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7184 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7185 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7186 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7187 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7188 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7189 | key_bits, alg); |
| 7190 | TEST_ASSERT(signature_size != 0); |
| 7191 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7192 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7193 | |
| 7194 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7195 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 7196 | input_data->x, input_data->len, |
| 7197 | signature, signature_size, |
| 7198 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7199 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7200 | TEST_LE_U(signature_length, signature_size); |
| 7201 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7202 | |
| 7203 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7204 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7205 | input_data->x, input_data->len, |
| 7206 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7208 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7209 | /* Flip a bit in the input and verify that the signature is now |
| 7210 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7211 | * because ECDSA may ignore the last few bits of the input. */ |
| 7212 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7213 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 7214 | input_data->x, input_data->len, |
| 7215 | signature, signature_length), |
| 7216 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7217 | } |
| 7218 | |
| 7219 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7220 | /* |
| 7221 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7222 | * thus reset them as required. |
| 7223 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7224 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7226 | psa_destroy_key(key); |
| 7227 | mbedtls_free(signature); |
| 7228 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7229 | } |
| 7230 | /* END_CASE */ |
| 7231 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7232 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7233 | /** |
| 7234 | * sign_verify_hash_interruptible() test intentions: |
| 7235 | * |
| 7236 | * Note: This test can currently only handle ECDSA. |
| 7237 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7238 | * 1. Test that we can sign an input hash with the given keypair and then |
| 7239 | * afterwards verify that signature. This is currently the only way to test |
| 7240 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7241 | * |
| 7242 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 7243 | * signature. |
| 7244 | * |
| 7245 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 7246 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7247 | * |
| 7248 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 7249 | * is zero and that each successful signing stage completes some ops (this is |
| 7250 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7251 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7252 | void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7253 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7254 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7255 | { |
| 7256 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7257 | psa_key_type_t key_type = key_type_arg; |
| 7258 | psa_algorithm_t alg = alg_arg; |
| 7259 | size_t key_bits; |
| 7260 | unsigned char *signature = NULL; |
| 7261 | size_t signature_size; |
| 7262 | size_t signature_length = 0xdeadbeef; |
| 7263 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7264 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7265 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7266 | uint32_t num_ops = 0; |
| 7267 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7268 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7269 | size_t min_completes = 0; |
| 7270 | size_t max_completes = 0; |
| 7271 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7272 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7273 | psa_sign_hash_interruptible_operation_init(); |
| 7274 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7275 | psa_verify_hash_interruptible_operation_init(); |
| 7276 | |
| 7277 | PSA_ASSERT(psa_crypto_init()); |
| 7278 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7279 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7280 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7281 | psa_set_key_algorithm(&attributes, alg); |
| 7282 | psa_set_key_type(&attributes, key_type); |
| 7283 | |
| 7284 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7285 | &key)); |
| 7286 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7287 | key_bits = psa_get_key_bits(&attributes); |
| 7288 | |
| 7289 | /* Allocate a buffer which has the size advertised by the |
| 7290 | * library. */ |
| 7291 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7292 | key_bits, alg); |
| 7293 | TEST_ASSERT(signature_size != 0); |
| 7294 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7295 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7296 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7297 | psa_interruptible_set_max_ops(max_ops); |
| 7298 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7299 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7300 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7301 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7302 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7303 | TEST_ASSERT(num_ops_prior == 0); |
| 7304 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7305 | /* Start performing the signature. */ |
| 7306 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7307 | input_data->x, input_data->len)); |
| 7308 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7309 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7310 | TEST_ASSERT(num_ops_prior == 0); |
| 7311 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7312 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7313 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7314 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7315 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7316 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7317 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7318 | |
| 7319 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7320 | |
| 7321 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7322 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7323 | /* We are asserting here that every complete makes progress |
| 7324 | * (completes some ops), which is true of the internal |
| 7325 | * implementation and probably any implementation, however this is |
| 7326 | * not mandated by the PSA specification. */ |
| 7327 | TEST_ASSERT(num_ops > num_ops_prior); |
| 7328 | |
| 7329 | num_ops_prior = num_ops; |
| 7330 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7331 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7332 | |
| 7333 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7334 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7335 | TEST_LE_U(min_completes, num_completes); |
| 7336 | TEST_LE_U(num_completes, max_completes); |
| 7337 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7338 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7339 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7340 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7341 | TEST_ASSERT(num_ops == 0); |
| 7342 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7343 | /* Check that the signature length looks sensible. */ |
| 7344 | TEST_LE_U(signature_length, signature_size); |
| 7345 | TEST_ASSERT(signature_length > 0); |
| 7346 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7347 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7348 | |
| 7349 | /* Start verification. */ |
| 7350 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7351 | input_data->x, input_data->len, |
| 7352 | signature, signature_length)); |
| 7353 | |
| 7354 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7355 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7356 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7357 | |
| 7358 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7359 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7360 | |
| 7361 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7362 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7363 | TEST_LE_U(min_completes, num_completes); |
| 7364 | TEST_LE_U(num_completes, max_completes); |
| 7365 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7366 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7367 | |
| 7368 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7369 | |
| 7370 | if (input_data->len != 0) { |
| 7371 | /* Flip a bit in the input and verify that the signature is now |
| 7372 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7373 | * because ECDSA may ignore the last few bits of the input. */ |
| 7374 | input_data->x[0] ^= 1; |
| 7375 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7376 | /* Start verification. */ |
| 7377 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7378 | input_data->x, input_data->len, |
| 7379 | signature, signature_length)); |
| 7380 | |
| 7381 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7382 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7383 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7384 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7385 | |
| 7386 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7387 | } |
| 7388 | |
| 7389 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7390 | |
| 7391 | exit: |
| 7392 | /* |
| 7393 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7394 | * thus reset them as required. |
| 7395 | */ |
| 7396 | psa_reset_key_attributes(&attributes); |
| 7397 | |
| 7398 | psa_destroy_key(key); |
| 7399 | mbedtls_free(signature); |
| 7400 | PSA_DONE(); |
| 7401 | } |
| 7402 | /* END_CASE */ |
| 7403 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7404 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7405 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7406 | int alg_arg, data_t *hash_data, |
| 7407 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7408 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7409 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7410 | psa_key_type_t key_type = key_type_arg; |
| 7411 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7412 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7414 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7416 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7418 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7419 | psa_set_key_algorithm(&attributes, alg); |
| 7420 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7422 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7423 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7425 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7426 | hash_data->x, hash_data->len, |
| 7427 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7428 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7429 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7430 | psa_reset_key_attributes(&attributes); |
| 7431 | psa_destroy_key(key); |
| 7432 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7433 | } |
| 7434 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7435 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7436 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7437 | /** |
| 7438 | * verify_hash_interruptible() test intentions: |
| 7439 | * |
| 7440 | * Note: This test can currently only handle ECDSA. |
| 7441 | * |
| 7442 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7443 | * only). Given this test only does verification it can accept public keys as |
| 7444 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7445 | * |
| 7446 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7447 | * expected for different max_ops values. |
| 7448 | * |
| 7449 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7450 | * and that each successful stage completes some ops (this is not mandated by |
| 7451 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7452 | * |
| 7453 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7454 | * complete() calls does not alter the number of ops returned. |
| 7455 | * |
| 7456 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7457 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7458 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7459 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7460 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7461 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7462 | { |
| 7463 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7464 | psa_key_type_t key_type = key_type_arg; |
| 7465 | psa_algorithm_t alg = alg_arg; |
| 7466 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7467 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7468 | uint32_t num_ops = 0; |
| 7469 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7470 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7471 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7472 | size_t min_completes = 0; |
| 7473 | size_t max_completes = 0; |
| 7474 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7475 | psa_verify_hash_interruptible_operation_t operation = |
| 7476 | psa_verify_hash_interruptible_operation_init(); |
| 7477 | |
| 7478 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7479 | |
| 7480 | PSA_ASSERT(psa_crypto_init()); |
| 7481 | |
| 7482 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7483 | psa_set_key_algorithm(&attributes, alg); |
| 7484 | psa_set_key_type(&attributes, key_type); |
| 7485 | |
| 7486 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7487 | &key)); |
| 7488 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7489 | psa_interruptible_set_max_ops(max_ops); |
| 7490 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7491 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7492 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7493 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7494 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7495 | |
| 7496 | TEST_ASSERT(num_ops_prior == 0); |
| 7497 | |
| 7498 | /* Start verification. */ |
| 7499 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7500 | hash_data->x, hash_data->len, |
| 7501 | signature_data->x, signature_data->len) |
| 7502 | ); |
| 7503 | |
| 7504 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7505 | |
| 7506 | TEST_ASSERT(num_ops_prior == 0); |
| 7507 | |
| 7508 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7509 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7510 | status = psa_verify_hash_complete(&operation); |
| 7511 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7512 | num_completes++; |
| 7513 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7514 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7515 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7516 | /* We are asserting here that every complete makes progress |
| 7517 | * (completes some ops), which is true of the internal |
| 7518 | * implementation and probably any implementation, however this is |
| 7519 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7520 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7521 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7522 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7523 | |
| 7524 | /* Ensure calling get_num_ops() twice still returns the same |
| 7525 | * number of ops as previously reported. */ |
| 7526 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7527 | |
| 7528 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7529 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7530 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7531 | |
| 7532 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7533 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7534 | TEST_LE_U(min_completes, num_completes); |
| 7535 | TEST_LE_U(num_completes, max_completes); |
| 7536 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7537 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7538 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7539 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7540 | TEST_ASSERT(num_ops == 0); |
| 7541 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7542 | if (hash_data->len != 0) { |
| 7543 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7544 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7545 | * ECDSA may ignore the last few bits of the input. */ |
| 7546 | hash_data->x[0] ^= 1; |
| 7547 | |
| 7548 | /* Start verification. */ |
| 7549 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7550 | hash_data->x, hash_data->len, |
| 7551 | signature_data->x, signature_data->len)); |
| 7552 | |
| 7553 | /* Continue performing the signature until complete. */ |
| 7554 | do { |
| 7555 | status = psa_verify_hash_complete(&operation); |
| 7556 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7557 | |
| 7558 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7559 | } |
| 7560 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7561 | exit: |
| 7562 | psa_reset_key_attributes(&attributes); |
| 7563 | psa_destroy_key(key); |
| 7564 | PSA_DONE(); |
| 7565 | } |
| 7566 | /* END_CASE */ |
| 7567 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7568 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7569 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7570 | int alg_arg, data_t *hash_data, |
| 7571 | data_t *signature_data, |
| 7572 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7573 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7574 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7575 | psa_key_type_t key_type = key_type_arg; |
| 7576 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7577 | psa_status_t actual_status; |
| 7578 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7579 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7581 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7583 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7584 | psa_set_key_algorithm(&attributes, alg); |
| 7585 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7587 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7588 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7590 | actual_status = psa_verify_hash(key, alg, |
| 7591 | hash_data->x, hash_data->len, |
| 7592 | signature_data->x, signature_data->len); |
| 7593 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7594 | |
| 7595 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7596 | psa_reset_key_attributes(&attributes); |
| 7597 | psa_destroy_key(key); |
| 7598 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7599 | } |
| 7600 | /* END_CASE */ |
| 7601 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7602 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7603 | /** |
| 7604 | * verify_hash_fail_interruptible() test intentions: |
| 7605 | * |
| 7606 | * Note: This test can currently only handle ECDSA. |
| 7607 | * |
| 7608 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7609 | * the correct error codes, and at the correct point (at start or during |
| 7610 | * complete). |
| 7611 | * |
| 7612 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7613 | * expected for different max_ops values. |
| 7614 | * |
| 7615 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7616 | * and that each successful stage completes some ops (this is not mandated by |
| 7617 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7618 | * |
| 7619 | * 4. Check that calling complete() when start() fails and complete() |
| 7620 | * after completion results in a BAD_STATE error. |
| 7621 | * |
| 7622 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7623 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7624 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7625 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7626 | int alg_arg, data_t *hash_data, |
| 7627 | data_t *signature_data, |
| 7628 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7629 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7630 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7631 | { |
| 7632 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7633 | psa_key_type_t key_type = key_type_arg; |
| 7634 | psa_algorithm_t alg = alg_arg; |
| 7635 | psa_status_t actual_status; |
| 7636 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7637 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7638 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7639 | uint32_t num_ops = 0; |
| 7640 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7641 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7642 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7643 | size_t min_completes = 0; |
| 7644 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7645 | psa_verify_hash_interruptible_operation_t operation = |
| 7646 | psa_verify_hash_interruptible_operation_init(); |
| 7647 | |
| 7648 | PSA_ASSERT(psa_crypto_init()); |
| 7649 | |
| 7650 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7651 | psa_set_key_algorithm(&attributes, alg); |
| 7652 | psa_set_key_type(&attributes, key_type); |
| 7653 | |
| 7654 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7655 | &key)); |
| 7656 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7657 | psa_interruptible_set_max_ops(max_ops); |
| 7658 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7659 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7660 | expected_complete_status, |
| 7661 | &min_completes, |
| 7662 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7663 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7664 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7665 | TEST_ASSERT(num_ops_prior == 0); |
| 7666 | |
| 7667 | /* Start verification. */ |
| 7668 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7669 | hash_data->x, hash_data->len, |
| 7670 | signature_data->x, |
| 7671 | signature_data->len); |
| 7672 | |
| 7673 | TEST_EQUAL(actual_status, expected_start_status); |
| 7674 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7675 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7676 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7677 | * start failed. */ |
| 7678 | actual_status = psa_verify_hash_complete(&operation); |
| 7679 | |
| 7680 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7681 | |
| 7682 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7683 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7684 | hash_data->x, hash_data->len, |
| 7685 | signature_data->x, |
| 7686 | signature_data->len); |
| 7687 | |
| 7688 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7689 | } |
| 7690 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7691 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7692 | TEST_ASSERT(num_ops_prior == 0); |
| 7693 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7694 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7695 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7696 | actual_status = psa_verify_hash_complete(&operation); |
| 7697 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7698 | num_completes++; |
| 7699 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7700 | if (actual_status == PSA_SUCCESS || |
| 7701 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7702 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7703 | /* We are asserting here that every complete makes progress |
| 7704 | * (completes some ops), which is true of the internal |
| 7705 | * implementation and probably any implementation, however this is |
| 7706 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7707 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7708 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7709 | num_ops_prior = num_ops; |
| 7710 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7711 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7712 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7713 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7714 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7715 | /* Check that another complete returns BAD_STATE. */ |
| 7716 | actual_status = psa_verify_hash_complete(&operation); |
| 7717 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7718 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7719 | TEST_LE_U(min_completes, num_completes); |
| 7720 | TEST_LE_U(num_completes, max_completes); |
| 7721 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7722 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7723 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7724 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7725 | TEST_ASSERT(num_ops == 0); |
| 7726 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7727 | exit: |
| 7728 | psa_reset_key_attributes(&attributes); |
| 7729 | psa_destroy_key(key); |
| 7730 | PSA_DONE(); |
| 7731 | } |
| 7732 | /* END_CASE */ |
| 7733 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7734 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7735 | /** |
| 7736 | * interruptible_signverify_hash_state_test() test intentions: |
| 7737 | * |
| 7738 | * Note: This test can currently only handle ECDSA. |
| 7739 | * |
| 7740 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7741 | * in incorrect orders returns BAD_STATE errors. |
| 7742 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7743 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7744 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7745 | { |
| 7746 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7747 | psa_key_type_t key_type = key_type_arg; |
| 7748 | psa_algorithm_t alg = alg_arg; |
| 7749 | size_t key_bits; |
| 7750 | unsigned char *signature = NULL; |
| 7751 | size_t signature_size; |
| 7752 | size_t signature_length = 0xdeadbeef; |
| 7753 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7754 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7755 | psa_sign_hash_interruptible_operation_init(); |
| 7756 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7757 | psa_verify_hash_interruptible_operation_init(); |
| 7758 | |
| 7759 | PSA_ASSERT(psa_crypto_init()); |
| 7760 | |
| 7761 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7762 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7763 | psa_set_key_algorithm(&attributes, alg); |
| 7764 | psa_set_key_type(&attributes, key_type); |
| 7765 | |
| 7766 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7767 | &key)); |
| 7768 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7769 | key_bits = psa_get_key_bits(&attributes); |
| 7770 | |
| 7771 | /* Allocate a buffer which has the size advertised by the |
| 7772 | * library. */ |
| 7773 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7774 | key_bits, alg); |
| 7775 | TEST_ASSERT(signature_size != 0); |
| 7776 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7777 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7778 | |
| 7779 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7780 | |
| 7781 | /* --- Attempt completes prior to starts --- */ |
| 7782 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7783 | signature_size, |
| 7784 | &signature_length), |
| 7785 | PSA_ERROR_BAD_STATE); |
| 7786 | |
| 7787 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7788 | |
| 7789 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7790 | PSA_ERROR_BAD_STATE); |
| 7791 | |
| 7792 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7793 | |
| 7794 | /* --- Aborts in all other places. --- */ |
| 7795 | psa_sign_hash_abort(&sign_operation); |
| 7796 | |
| 7797 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7798 | input_data->x, input_data->len)); |
| 7799 | |
| 7800 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7801 | |
| 7802 | psa_interruptible_set_max_ops(1); |
| 7803 | |
| 7804 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7805 | input_data->x, input_data->len)); |
| 7806 | |
| 7807 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7808 | signature_size, |
| 7809 | &signature_length), |
| 7810 | PSA_OPERATION_INCOMPLETE); |
| 7811 | |
| 7812 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7813 | |
| 7814 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7815 | |
| 7816 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7817 | input_data->x, input_data->len)); |
| 7818 | |
| 7819 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7820 | signature_size, |
| 7821 | &signature_length)); |
| 7822 | |
| 7823 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7824 | |
| 7825 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7826 | |
| 7827 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7828 | input_data->x, input_data->len, |
| 7829 | signature, signature_length)); |
| 7830 | |
| 7831 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7832 | |
| 7833 | psa_interruptible_set_max_ops(1); |
| 7834 | |
| 7835 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7836 | input_data->x, input_data->len, |
| 7837 | signature, signature_length)); |
| 7838 | |
| 7839 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7840 | PSA_OPERATION_INCOMPLETE); |
| 7841 | |
| 7842 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7843 | |
| 7844 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7845 | |
| 7846 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7847 | input_data->x, input_data->len, |
| 7848 | signature, signature_length)); |
| 7849 | |
| 7850 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7851 | |
| 7852 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7853 | |
| 7854 | /* --- Attempt double starts. --- */ |
| 7855 | |
| 7856 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7857 | input_data->x, input_data->len)); |
| 7858 | |
| 7859 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7860 | input_data->x, input_data->len), |
| 7861 | PSA_ERROR_BAD_STATE); |
| 7862 | |
| 7863 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7864 | |
| 7865 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7866 | input_data->x, input_data->len, |
| 7867 | signature, signature_length)); |
| 7868 | |
| 7869 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7870 | input_data->x, input_data->len, |
| 7871 | signature, signature_length), |
| 7872 | PSA_ERROR_BAD_STATE); |
| 7873 | |
| 7874 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7875 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7876 | exit: |
| 7877 | /* |
| 7878 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7879 | * thus reset them as required. |
| 7880 | */ |
| 7881 | psa_reset_key_attributes(&attributes); |
| 7882 | |
| 7883 | psa_destroy_key(key); |
| 7884 | mbedtls_free(signature); |
| 7885 | PSA_DONE(); |
| 7886 | } |
| 7887 | /* END_CASE */ |
| 7888 | |
| 7889 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7890 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7891 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7892 | * |
| 7893 | * Note: This test can currently only handle ECDSA. |
| 7894 | * |
| 7895 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7896 | * interfaces. |
| 7897 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7898 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7899 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7900 | { |
| 7901 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7902 | psa_key_type_t key_type = key_type_arg; |
| 7903 | psa_algorithm_t alg = alg_arg; |
| 7904 | size_t key_bits; |
| 7905 | unsigned char *signature = NULL; |
| 7906 | size_t signature_size; |
| 7907 | size_t signature_length = 0xdeadbeef; |
| 7908 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7909 | uint8_t *input_buffer = NULL; |
| 7910 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7911 | psa_sign_hash_interruptible_operation_init(); |
| 7912 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7913 | psa_verify_hash_interruptible_operation_init(); |
| 7914 | |
| 7915 | PSA_ASSERT(psa_crypto_init()); |
| 7916 | |
| 7917 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7918 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7919 | psa_set_key_algorithm(&attributes, alg); |
| 7920 | psa_set_key_type(&attributes, key_type); |
| 7921 | |
| 7922 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7923 | &key)); |
| 7924 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7925 | key_bits = psa_get_key_bits(&attributes); |
| 7926 | |
| 7927 | /* Allocate a buffer which has the size advertised by the |
| 7928 | * library. */ |
| 7929 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7930 | key_bits, alg); |
| 7931 | TEST_ASSERT(signature_size != 0); |
| 7932 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7933 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7934 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7935 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7936 | * verify passes all inputs to start. --- */ |
| 7937 | |
| 7938 | psa_interruptible_set_max_ops(1); |
| 7939 | |
| 7940 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7941 | input_data->x, input_data->len)); |
| 7942 | |
| 7943 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7944 | signature_size, |
| 7945 | &signature_length), |
| 7946 | PSA_OPERATION_INCOMPLETE); |
| 7947 | |
| 7948 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7949 | 0, |
| 7950 | &signature_length), |
| 7951 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7952 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7953 | /* And test that this invalidates the operation. */ |
| 7954 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7955 | 0, |
| 7956 | &signature_length), |
| 7957 | PSA_ERROR_BAD_STATE); |
| 7958 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7959 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7960 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7961 | /* Trash the hash buffer in between start and complete, to ensure |
| 7962 | * no reliance on external buffers. */ |
| 7963 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7964 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7965 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7966 | |
| 7967 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7968 | |
| 7969 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7970 | input_buffer, input_data->len)); |
| 7971 | |
| 7972 | memset(input_buffer, '!', input_data->len); |
| 7973 | mbedtls_free(input_buffer); |
| 7974 | input_buffer = NULL; |
| 7975 | |
| 7976 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7977 | signature_size, |
| 7978 | &signature_length)); |
| 7979 | |
| 7980 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7981 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7982 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7983 | |
| 7984 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7985 | |
| 7986 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7987 | input_buffer, input_data->len, |
| 7988 | signature, signature_length)); |
| 7989 | |
| 7990 | memset(input_buffer, '!', input_data->len); |
| 7991 | mbedtls_free(input_buffer); |
| 7992 | input_buffer = NULL; |
| 7993 | |
| 7994 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7995 | |
| 7996 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7997 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7998 | exit: |
| 7999 | /* |
| 8000 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8001 | * thus reset them as required. |
| 8002 | */ |
| 8003 | psa_reset_key_attributes(&attributes); |
| 8004 | |
| 8005 | psa_destroy_key(key); |
| 8006 | mbedtls_free(signature); |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 8007 | mbedtls_free(input_buffer); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 8008 | PSA_DONE(); |
| 8009 | } |
| 8010 | /* END_CASE */ |
| 8011 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8012 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 8013 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 8014 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 8015 | * |
| 8016 | * Note: This test can currently only handle ECDSA. |
| 8017 | * |
| 8018 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 8019 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8020 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 8021 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8022 | * |
| 8023 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 8024 | * result as calling get_num_ops() once at the end of the operation. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 8025 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 8026 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 8027 | data_t *key_data, int alg_arg, |
| 8028 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8029 | { |
| 8030 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8031 | psa_key_type_t key_type = key_type_arg; |
| 8032 | psa_algorithm_t alg = alg_arg; |
| 8033 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8034 | size_t key_bits; |
| 8035 | unsigned char *signature = NULL; |
| 8036 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8037 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8038 | uint32_t num_ops = 0; |
| 8039 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8040 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8041 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 8042 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8043 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 8044 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8045 | |
| 8046 | PSA_ASSERT(psa_crypto_init()); |
| 8047 | |
| 8048 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 8049 | PSA_KEY_USAGE_VERIFY_HASH); |
| 8050 | psa_set_key_algorithm(&attributes, alg); |
| 8051 | psa_set_key_type(&attributes, key_type); |
| 8052 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8053 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 8054 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8055 | key_bits = psa_get_key_bits(&attributes); |
| 8056 | |
| 8057 | /* Allocate a buffer which has the size advertised by the |
| 8058 | * library. */ |
| 8059 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8060 | |
| 8061 | TEST_ASSERT(signature_size != 0); |
| 8062 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8063 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8064 | |
| 8065 | /* Check that default max ops gets set if we don't set it. */ |
| 8066 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8067 | input_data->x, input_data->len)); |
| 8068 | |
| 8069 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 8070 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8071 | |
| 8072 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8073 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8074 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8075 | input_data->x, input_data->len, |
| 8076 | signature, signature_size)); |
| 8077 | |
| 8078 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 8079 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8080 | |
| 8081 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8082 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8083 | /* Check that max ops gets set properly. */ |
| 8084 | |
| 8085 | psa_interruptible_set_max_ops(0xbeef); |
| 8086 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8087 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8088 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8089 | /* --- Ensure changing the max ops mid operation works (operation should |
| 8090 | * complete successfully after setting max ops to unlimited --- */ |
| 8091 | psa_interruptible_set_max_ops(1); |
| 8092 | |
| 8093 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8094 | input_data->x, input_data->len)); |
| 8095 | |
| 8096 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 8097 | signature_size, |
| 8098 | &signature_length), |
| 8099 | PSA_OPERATION_INCOMPLETE); |
| 8100 | |
| 8101 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8102 | |
| 8103 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 8104 | signature_size, |
| 8105 | &signature_length)); |
| 8106 | |
| 8107 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8108 | |
| 8109 | psa_interruptible_set_max_ops(1); |
| 8110 | |
| 8111 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8112 | input_data->x, input_data->len, |
| 8113 | signature, signature_length)); |
| 8114 | |
| 8115 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 8116 | PSA_OPERATION_INCOMPLETE); |
| 8117 | |
| 8118 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8119 | |
| 8120 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 8121 | |
| 8122 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8123 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8124 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 8125 | * result in lost ops. ---*/ |
| 8126 | |
| 8127 | psa_interruptible_set_max_ops(1); |
| 8128 | |
| 8129 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8130 | input_data->x, input_data->len)); |
| 8131 | |
| 8132 | /* Continue performing the signature until complete. */ |
| 8133 | do { |
| 8134 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 8135 | signature_size, |
| 8136 | &signature_length); |
| 8137 | |
| 8138 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 8139 | |
| 8140 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8141 | |
| 8142 | PSA_ASSERT(status); |
| 8143 | |
| 8144 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8145 | |
| 8146 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8147 | input_data->x, input_data->len)); |
| 8148 | |
| 8149 | /* Continue performing the signature until complete. */ |
| 8150 | do { |
| 8151 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 8152 | signature_size, |
| 8153 | &signature_length); |
| 8154 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8155 | |
| 8156 | PSA_ASSERT(status); |
| 8157 | |
| 8158 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 8159 | |
| 8160 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8161 | |
| 8162 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8163 | input_data->x, input_data->len, |
| 8164 | signature, signature_length)); |
| 8165 | |
| 8166 | /* Continue performing the verification until complete. */ |
| 8167 | do { |
| 8168 | status = psa_verify_hash_complete(&verify_operation); |
| 8169 | |
| 8170 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 8171 | |
| 8172 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8173 | |
| 8174 | PSA_ASSERT(status); |
| 8175 | |
| 8176 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8177 | |
| 8178 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8179 | input_data->x, input_data->len, |
| 8180 | signature, signature_length)); |
| 8181 | |
| 8182 | /* Continue performing the verification until complete. */ |
| 8183 | do { |
| 8184 | status = psa_verify_hash_complete(&verify_operation); |
| 8185 | |
| 8186 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8187 | |
| 8188 | PSA_ASSERT(status); |
| 8189 | |
| 8190 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 8191 | |
| 8192 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8193 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8194 | exit: |
| 8195 | /* |
| 8196 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8197 | * thus reset them as required. |
| 8198 | */ |
| 8199 | psa_reset_key_attributes(&attributes); |
| 8200 | |
| 8201 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8202 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8203 | PSA_DONE(); |
| 8204 | } |
| 8205 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 8206 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8207 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8208 | void sign_message_deterministic(int key_type_arg, |
| 8209 | data_t *key_data, |
| 8210 | int alg_arg, |
| 8211 | data_t *input_data, |
| 8212 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8213 | { |
| 8214 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8215 | psa_key_type_t key_type = key_type_arg; |
| 8216 | psa_algorithm_t alg = alg_arg; |
| 8217 | size_t key_bits; |
| 8218 | unsigned char *signature = NULL; |
| 8219 | size_t signature_size; |
| 8220 | size_t signature_length = 0xdeadbeef; |
| 8221 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8223 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8225 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8226 | psa_set_key_algorithm(&attributes, alg); |
| 8227 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8229 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8230 | &key)); |
| 8231 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8232 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8234 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8235 | TEST_ASSERT(signature_size != 0); |
| 8236 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8237 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8239 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8240 | input_data->x, input_data->len, |
| 8241 | signature, signature_size, |
| 8242 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8243 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8244 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8245 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8246 | |
| 8247 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8248 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8250 | psa_destroy_key(key); |
| 8251 | mbedtls_free(signature); |
| 8252 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8253 | |
| 8254 | } |
| 8255 | /* END_CASE */ |
| 8256 | |
| 8257 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8258 | void sign_message_fail(int key_type_arg, |
| 8259 | data_t *key_data, |
| 8260 | int alg_arg, |
| 8261 | data_t *input_data, |
| 8262 | int signature_size_arg, |
| 8263 | int expected_status_arg) |
| 8264 | { |
| 8265 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8266 | psa_key_type_t key_type = key_type_arg; |
| 8267 | psa_algorithm_t alg = alg_arg; |
| 8268 | size_t signature_size = signature_size_arg; |
| 8269 | psa_status_t actual_status; |
| 8270 | psa_status_t expected_status = expected_status_arg; |
| 8271 | unsigned char *signature = NULL; |
| 8272 | size_t signature_length = 0xdeadbeef; |
| 8273 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8274 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8275 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8276 | |
| 8277 | PSA_ASSERT(psa_crypto_init()); |
| 8278 | |
| 8279 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8280 | psa_set_key_algorithm(&attributes, alg); |
| 8281 | psa_set_key_type(&attributes, key_type); |
| 8282 | |
| 8283 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8284 | &key)); |
| 8285 | |
| 8286 | actual_status = psa_sign_message(key, alg, |
| 8287 | input_data->x, input_data->len, |
| 8288 | signature, signature_size, |
| 8289 | &signature_length); |
| 8290 | TEST_EQUAL(actual_status, expected_status); |
| 8291 | /* The value of *signature_length is unspecified on error, but |
| 8292 | * whatever it is, it should be less than signature_size, so that |
| 8293 | * if the caller tries to read *signature_length bytes without |
| 8294 | * checking the error code then they don't overflow a buffer. */ |
| 8295 | TEST_LE_U(signature_length, signature_size); |
| 8296 | |
| 8297 | exit: |
| 8298 | psa_reset_key_attributes(&attributes); |
| 8299 | psa_destroy_key(key); |
| 8300 | mbedtls_free(signature); |
| 8301 | PSA_DONE(); |
| 8302 | } |
| 8303 | /* END_CASE */ |
| 8304 | |
| 8305 | /* BEGIN_CASE */ |
| 8306 | void sign_verify_message(int key_type_arg, |
| 8307 | data_t *key_data, |
| 8308 | int alg_arg, |
| 8309 | data_t *input_data) |
| 8310 | { |
| 8311 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8312 | psa_key_type_t key_type = key_type_arg; |
| 8313 | psa_algorithm_t alg = alg_arg; |
| 8314 | size_t key_bits; |
| 8315 | unsigned char *signature = NULL; |
| 8316 | size_t signature_size; |
| 8317 | size_t signature_length = 0xdeadbeef; |
| 8318 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8319 | |
| 8320 | PSA_ASSERT(psa_crypto_init()); |
| 8321 | |
| 8322 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 8323 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8324 | psa_set_key_algorithm(&attributes, alg); |
| 8325 | psa_set_key_type(&attributes, key_type); |
| 8326 | |
| 8327 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8328 | &key)); |
| 8329 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8330 | key_bits = psa_get_key_bits(&attributes); |
| 8331 | |
| 8332 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8333 | TEST_ASSERT(signature_size != 0); |
| 8334 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8335 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8336 | |
| 8337 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8338 | input_data->x, input_data->len, |
| 8339 | signature, signature_size, |
| 8340 | &signature_length)); |
| 8341 | TEST_LE_U(signature_length, signature_size); |
| 8342 | TEST_ASSERT(signature_length > 0); |
| 8343 | |
| 8344 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8345 | input_data->x, input_data->len, |
| 8346 | signature, signature_length)); |
| 8347 | |
| 8348 | if (input_data->len != 0) { |
| 8349 | /* Flip a bit in the input and verify that the signature is now |
| 8350 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8351 | * because ECDSA may ignore the last few bits of the input. */ |
| 8352 | input_data->x[0] ^= 1; |
| 8353 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8354 | input_data->x, input_data->len, |
| 8355 | signature, signature_length), |
| 8356 | PSA_ERROR_INVALID_SIGNATURE); |
| 8357 | } |
| 8358 | |
| 8359 | exit: |
| 8360 | psa_reset_key_attributes(&attributes); |
| 8361 | |
| 8362 | psa_destroy_key(key); |
| 8363 | mbedtls_free(signature); |
| 8364 | PSA_DONE(); |
| 8365 | } |
| 8366 | /* END_CASE */ |
| 8367 | |
| 8368 | /* BEGIN_CASE */ |
| 8369 | void verify_message(int key_type_arg, |
| 8370 | data_t *key_data, |
| 8371 | int alg_arg, |
| 8372 | data_t *input_data, |
| 8373 | data_t *signature_data) |
| 8374 | { |
| 8375 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8376 | psa_key_type_t key_type = key_type_arg; |
| 8377 | psa_algorithm_t alg = alg_arg; |
| 8378 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8379 | |
| 8380 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8381 | |
| 8382 | PSA_ASSERT(psa_crypto_init()); |
| 8383 | |
| 8384 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8385 | psa_set_key_algorithm(&attributes, alg); |
| 8386 | psa_set_key_type(&attributes, key_type); |
| 8387 | |
| 8388 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8389 | &key)); |
| 8390 | |
| 8391 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8392 | input_data->x, input_data->len, |
| 8393 | signature_data->x, signature_data->len)); |
| 8394 | |
| 8395 | exit: |
| 8396 | psa_reset_key_attributes(&attributes); |
| 8397 | psa_destroy_key(key); |
| 8398 | PSA_DONE(); |
| 8399 | } |
| 8400 | /* END_CASE */ |
| 8401 | |
| 8402 | /* BEGIN_CASE */ |
| 8403 | void verify_message_fail(int key_type_arg, |
| 8404 | data_t *key_data, |
| 8405 | int alg_arg, |
| 8406 | data_t *hash_data, |
| 8407 | data_t *signature_data, |
| 8408 | int expected_status_arg) |
| 8409 | { |
| 8410 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8411 | psa_key_type_t key_type = key_type_arg; |
| 8412 | psa_algorithm_t alg = alg_arg; |
| 8413 | psa_status_t actual_status; |
| 8414 | psa_status_t expected_status = expected_status_arg; |
| 8415 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8416 | |
| 8417 | PSA_ASSERT(psa_crypto_init()); |
| 8418 | |
| 8419 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8420 | psa_set_key_algorithm(&attributes, alg); |
| 8421 | psa_set_key_type(&attributes, key_type); |
| 8422 | |
| 8423 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8424 | &key)); |
| 8425 | |
| 8426 | actual_status = psa_verify_message(key, alg, |
| 8427 | hash_data->x, hash_data->len, |
| 8428 | signature_data->x, |
| 8429 | signature_data->len); |
| 8430 | TEST_EQUAL(actual_status, expected_status); |
| 8431 | |
| 8432 | exit: |
| 8433 | psa_reset_key_attributes(&attributes); |
| 8434 | psa_destroy_key(key); |
| 8435 | PSA_DONE(); |
| 8436 | } |
| 8437 | /* END_CASE */ |
| 8438 | |
| 8439 | /* BEGIN_CASE */ |
| 8440 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8441 | data_t *key_data, |
| 8442 | int alg_arg, |
| 8443 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8444 | data_t *label, |
| 8445 | int expected_output_length_arg, |
| 8446 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8447 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8448 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8449 | psa_key_type_t key_type = key_type_arg; |
| 8450 | psa_algorithm_t alg = alg_arg; |
| 8451 | size_t expected_output_length = expected_output_length_arg; |
| 8452 | size_t key_bits; |
| 8453 | unsigned char *output = NULL; |
| 8454 | size_t output_size; |
| 8455 | size_t output_length = ~0; |
| 8456 | psa_status_t actual_status; |
| 8457 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8458 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8460 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8461 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8462 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8463 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8464 | psa_set_key_algorithm(&attributes, alg); |
| 8465 | psa_set_key_type(&attributes, key_type); |
| 8466 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8467 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8468 | |
| 8469 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8470 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8471 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8473 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8474 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8475 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8476 | |
| 8477 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8478 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8479 | input_data->x, input_data->len, |
| 8480 | label->x, label->len, |
| 8481 | output, output_size, |
| 8482 | &output_length); |
| 8483 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8484 | if (actual_status == PSA_SUCCESS) { |
| 8485 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8486 | } else { |
| 8487 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8488 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8489 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8490 | /* If the label is empty, the test framework puts a non-null pointer |
| 8491 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8492 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8493 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8494 | if (output_size != 0) { |
| 8495 | memset(output, 0, output_size); |
| 8496 | } |
| 8497 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8498 | input_data->x, input_data->len, |
| 8499 | NULL, label->len, |
| 8500 | output, output_size, |
| 8501 | &output_length); |
| 8502 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8503 | if (actual_status == PSA_SUCCESS) { |
| 8504 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8505 | } else { |
| 8506 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8507 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8508 | } |
| 8509 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8510 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8511 | /* |
| 8512 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8513 | * thus reset them as required. |
| 8514 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8515 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8517 | psa_destroy_key(key); |
| 8518 | mbedtls_free(output); |
| 8519 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8520 | } |
| 8521 | /* END_CASE */ |
| 8522 | |
| 8523 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8524 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8525 | data_t *key_data, |
| 8526 | int alg_arg, |
| 8527 | data_t *input_data, |
| 8528 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8529 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8530 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8531 | psa_key_type_t key_type = key_type_arg; |
| 8532 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8533 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8534 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8535 | size_t output_size; |
| 8536 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8537 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8538 | size_t output2_size; |
| 8539 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8540 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8542 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8544 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8545 | psa_set_key_algorithm(&attributes, alg); |
| 8546 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8548 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8549 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8550 | |
| 8551 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8552 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8553 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8555 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8556 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8557 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8558 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8559 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8560 | TEST_LE_U(output2_size, |
| 8561 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8562 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8563 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8564 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8565 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8566 | * the original plaintext because of the non-optional random |
| 8567 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8568 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8569 | input_data->x, input_data->len, |
| 8570 | label->x, label->len, |
| 8571 | output, output_size, |
| 8572 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8573 | /* We don't know what ciphertext length to expect, but check that |
| 8574 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8575 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8577 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8578 | output, output_length, |
| 8579 | label->x, label->len, |
| 8580 | output2, output2_size, |
| 8581 | &output2_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8582 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8583 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8584 | |
| 8585 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8586 | /* |
| 8587 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8588 | * thus reset them as required. |
| 8589 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8590 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8592 | psa_destroy_key(key); |
| 8593 | mbedtls_free(output); |
| 8594 | mbedtls_free(output2); |
| 8595 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8596 | } |
| 8597 | /* END_CASE */ |
| 8598 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8599 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8600 | void asymmetric_decrypt(int key_type_arg, |
| 8601 | data_t *key_data, |
| 8602 | int alg_arg, |
| 8603 | data_t *input_data, |
| 8604 | data_t *label, |
| 8605 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8606 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8607 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8608 | psa_key_type_t key_type = key_type_arg; |
| 8609 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8610 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8611 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8612 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8613 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8614 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8616 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8618 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8619 | psa_set_key_algorithm(&attributes, alg); |
| 8620 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8622 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8623 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8625 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8626 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8627 | |
| 8628 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8629 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8630 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8631 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8633 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8634 | input_data->x, input_data->len, |
| 8635 | label->x, label->len, |
| 8636 | output, |
| 8637 | output_size, |
| 8638 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8639 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8640 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8641 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8642 | /* If the label is empty, the test framework puts a non-null pointer |
| 8643 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8644 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8645 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8646 | if (output_size != 0) { |
| 8647 | memset(output, 0, output_size); |
| 8648 | } |
| 8649 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8650 | input_data->x, input_data->len, |
| 8651 | NULL, label->len, |
| 8652 | output, |
| 8653 | output_size, |
| 8654 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8655 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8656 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8657 | } |
| 8658 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8659 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8660 | psa_reset_key_attributes(&attributes); |
| 8661 | psa_destroy_key(key); |
| 8662 | mbedtls_free(output); |
| 8663 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8664 | } |
| 8665 | /* END_CASE */ |
| 8666 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8667 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8668 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8669 | data_t *key_data, |
| 8670 | int alg_arg, |
| 8671 | data_t *input_data, |
| 8672 | data_t *label, |
| 8673 | int output_size_arg, |
| 8674 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8675 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8676 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8677 | psa_key_type_t key_type = key_type_arg; |
| 8678 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8679 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8680 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8681 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8682 | psa_status_t actual_status; |
| 8683 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8684 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8685 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8686 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8688 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8690 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8691 | psa_set_key_algorithm(&attributes, alg); |
| 8692 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8694 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8695 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8697 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8698 | input_data->x, input_data->len, |
| 8699 | label->x, label->len, |
| 8700 | output, output_size, |
| 8701 | &output_length); |
| 8702 | TEST_EQUAL(actual_status, expected_status); |
| 8703 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8704 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8705 | /* If the label is empty, the test framework puts a non-null pointer |
| 8706 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8707 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8708 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8709 | if (output_size != 0) { |
| 8710 | memset(output, 0, output_size); |
| 8711 | } |
| 8712 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8713 | input_data->x, input_data->len, |
| 8714 | NULL, label->len, |
| 8715 | output, output_size, |
| 8716 | &output_length); |
| 8717 | TEST_EQUAL(actual_status, expected_status); |
| 8718 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8719 | } |
| 8720 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8721 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8722 | psa_reset_key_attributes(&attributes); |
| 8723 | psa_destroy_key(key); |
| 8724 | mbedtls_free(output); |
| 8725 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8726 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8727 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8728 | |
| 8729 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8730 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8731 | { |
| 8732 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8733 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8734 | * 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] | 8735 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8736 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8737 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8738 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8739 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8741 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8742 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8743 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8744 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8745 | PSA_ERROR_BAD_STATE); |
| 8746 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8747 | PSA_ERROR_BAD_STATE); |
| 8748 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8749 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8750 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8751 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8752 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8753 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8754 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8755 | } |
| 8756 | /* END_CASE */ |
| 8757 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8758 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8759 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8760 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8761 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8762 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8763 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8765 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8767 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8768 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8769 | |
| 8770 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8771 | psa_key_derivation_abort(&operation); |
| 8772 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8773 | } |
| 8774 | /* END_CASE */ |
| 8775 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8776 | /* BEGIN_CASE */ |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 8777 | void derive_set_capacity(int alg_arg, int64_t capacity_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8778 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8779 | { |
| 8780 | psa_algorithm_t alg = alg_arg; |
| 8781 | size_t capacity = capacity_arg; |
| 8782 | psa_status_t expected_status = expected_status_arg; |
| 8783 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8785 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8787 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8789 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8790 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8791 | |
| 8792 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8793 | psa_key_derivation_abort(&operation); |
| 8794 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8795 | } |
| 8796 | /* END_CASE */ |
| 8797 | |
| 8798 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8799 | void parse_binary_string_test(data_t *input, int output) |
| 8800 | { |
| 8801 | uint64_t value; |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8802 | value = mbedtls_test_parse_binary_string(input); |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8803 | TEST_EQUAL(value, output); |
| 8804 | } |
| 8805 | /* END_CASE */ |
| 8806 | |
| 8807 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8808 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8809 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8810 | int expected_status_arg1, |
| 8811 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8812 | int expected_status_arg2, |
| 8813 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8814 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8815 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8816 | { |
| 8817 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8818 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8819 | uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8820 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8821 | expected_status_arg2, |
| 8822 | expected_status_arg3 }; |
| 8823 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8824 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8825 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8826 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8827 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8828 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8829 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8830 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8831 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8832 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8833 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8835 | PSA_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8837 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8838 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8840 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8841 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8842 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8843 | mbedtls_test_set_step(i); |
| 8844 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8845 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8846 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8847 | key_types[i] != INPUT_INTEGER) { |
| 8848 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8849 | PSA_ASSERT(psa_import_key(&attributes, |
| 8850 | inputs[i]->x, inputs[i]->len, |
| 8851 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8852 | if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8853 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8854 | // When taking a private key as secret input, use key agreement |
| 8855 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8856 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 8857 | &operation, keys[i], 0), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8858 | expected_statuses[i]); |
| 8859 | } else { |
| 8860 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8861 | keys[i]), |
| 8862 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8863 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8864 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8865 | if (key_types[i] == INPUT_INTEGER) { |
| 8866 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8867 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8868 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8869 | expected_statuses[i]); |
| 8870 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8871 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8872 | &operation, steps[i], |
| 8873 | inputs[i]->x, inputs[i]->len), |
| 8874 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8875 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8876 | } |
| 8877 | } |
| 8878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8879 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8880 | psa_reset_key_attributes(&attributes); |
| 8881 | psa_set_key_type(&attributes, output_key_type); |
| 8882 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8883 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8884 | psa_key_derivation_output_key(&attributes, &operation, |
| 8885 | &output_key); |
| 8886 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8887 | uint8_t buffer[1]; |
| 8888 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8889 | psa_key_derivation_output_bytes(&operation, |
| 8890 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8891 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8892 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8893 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8894 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8895 | psa_key_derivation_abort(&operation); |
| 8896 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8897 | psa_destroy_key(keys[i]); |
| 8898 | } |
| 8899 | psa_destroy_key(output_key); |
| 8900 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8901 | } |
| 8902 | /* END_CASE */ |
| 8903 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8904 | /* BEGIN_CASE*/ |
| 8905 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8906 | { |
| 8907 | psa_algorithm_t alg = alg_arg; |
| 8908 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8909 | |
| 8910 | PSA_ASSERT(psa_crypto_init()); |
| 8911 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8912 | |
| 8913 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8914 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8915 | cost), |
| 8916 | PSA_ERROR_NOT_SUPPORTED); |
| 8917 | |
| 8918 | exit: |
| 8919 | psa_key_derivation_abort(&operation); |
| 8920 | PSA_DONE(); |
| 8921 | } |
| 8922 | /* END_CASE*/ |
| 8923 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8924 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8925 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8926 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8927 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8928 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8929 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8930 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8931 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8932 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8933 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8934 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8935 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8936 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8937 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8938 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8939 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8940 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8942 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8944 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8945 | psa_set_key_algorithm(&attributes, alg); |
| 8946 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8948 | PSA_ASSERT(psa_import_key(&attributes, |
| 8949 | key_data, sizeof(key_data), |
| 8950 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8951 | |
| 8952 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8953 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8954 | input1, input1_length, |
| 8955 | input2, input2_length, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 8956 | capacity, 0)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8957 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8958 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8959 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8960 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8961 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8962 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8964 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8965 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8966 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8967 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8968 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8969 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8970 | psa_key_derivation_abort(&operation); |
| 8971 | psa_destroy_key(key); |
| 8972 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8973 | } |
| 8974 | /* END_CASE */ |
| 8975 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8976 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8977 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8978 | { |
| 8979 | uint8_t output_buffer[16]; |
| 8980 | size_t buffer_size = 16; |
| 8981 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8982 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8984 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8985 | output_buffer, buffer_size) |
| 8986 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8988 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8989 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8991 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8993 | TEST_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 8994 | output_buffer, buffer_size) |
| 8995 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8997 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8998 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8999 | |
| 9000 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9001 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 9002 | } |
| 9003 | /* END_CASE */ |
| 9004 | |
| 9005 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9006 | void derive_output(int alg_arg, |
| 9007 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 9008 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 9009 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 9010 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 9011 | data_t *key_agreement_peer_key, |
| 9012 | int requested_capacity_arg, |
| 9013 | data_t *expected_output1, |
| 9014 | data_t *expected_output2, |
| 9015 | int other_key_input_type, |
| 9016 | int key_input_type, |
| 9017 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9018 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9019 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9020 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 9021 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 9022 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 9023 | MBEDTLS_SVC_KEY_ID_INIT, |
| 9024 | MBEDTLS_SVC_KEY_ID_INIT, |
| 9025 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 9026 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 9027 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9028 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9029 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9030 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9031 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9032 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9033 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9034 | size_t output_buffer_size = 0; |
| 9035 | uint8_t *output_buffer = NULL; |
| 9036 | size_t expected_capacity; |
| 9037 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9038 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 9039 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 9040 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 9041 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9042 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9043 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9044 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9046 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 9047 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9048 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9049 | } |
| 9050 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9051 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9052 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9053 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9054 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9055 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9056 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9057 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9058 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9059 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 9060 | requested_capacity)); |
| 9061 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 9062 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9063 | case 0: |
| 9064 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9065 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 9066 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 9067 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 9068 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 9069 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9070 | if (statuses[i] != PSA_SUCCESS) { |
| 9071 | goto exit; |
| 9072 | } |
| 9073 | break; |
| 9074 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9075 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9076 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9077 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9078 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 9079 | &operation, steps[i], |
| 9080 | inputs[i]->x, inputs[i]->len), |
| 9081 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 9082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9083 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 9084 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9085 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9086 | break; |
| 9087 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9088 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 9089 | psa_set_key_algorithm(&attributes1, alg); |
| 9090 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9092 | PSA_ASSERT(psa_import_key(&attributes1, |
| 9093 | inputs[i]->x, inputs[i]->len, |
| 9094 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9096 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 9097 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 9098 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 9099 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9100 | } |
| 9101 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9102 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9103 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9104 | keys[i]), |
| 9105 | statuses[i]); |
| 9106 | |
| 9107 | if (statuses[i] != PSA_SUCCESS) { |
| 9108 | goto exit; |
| 9109 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9110 | break; |
| 9111 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9112 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9113 | break; |
| 9114 | } |
| 9115 | break; |
| 9116 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9117 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9118 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9119 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9120 | steps[i], |
| 9121 | inputs[i]->x, |
| 9122 | inputs[i]->len), |
| 9123 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9124 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 9125 | case 1: // input key, type DERIVE |
| 9126 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9127 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 9128 | psa_set_key_algorithm(&attributes2, alg); |
| 9129 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9130 | |
| 9131 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9132 | if (other_key_input_type == 11) { |
| 9133 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 9134 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9136 | PSA_ASSERT(psa_import_key(&attributes2, |
| 9137 | inputs[i]->x, inputs[i]->len, |
| 9138 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9140 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 9141 | steps[i], |
| 9142 | keys[i]), |
| 9143 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9144 | break; |
| 9145 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9146 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 9147 | psa_set_key_algorithm(&attributes3, alg); |
| 9148 | psa_set_key_type(&attributes3, |
| 9149 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9151 | PSA_ASSERT(psa_import_key(&attributes3, |
| 9152 | inputs[i]->x, inputs[i]->len, |
| 9153 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9155 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9156 | &operation, |
| 9157 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 9158 | keys[i], key_agreement_peer_key->x, |
| 9159 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9160 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9161 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9162 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9163 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9164 | } |
| 9165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9166 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9167 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9168 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9169 | break; |
| 9170 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9171 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 9172 | &operation, steps[i], |
| 9173 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9175 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9176 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9177 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9178 | break; |
| 9179 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 9180 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9182 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9183 | ¤t_capacity)); |
| 9184 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9185 | expected_capacity = requested_capacity; |
| 9186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9187 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9188 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 9189 | |
| 9190 | /* For output key derivation secret must be provided using |
| 9191 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9192 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9193 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9194 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9196 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 9197 | psa_set_key_algorithm(&attributes4, alg); |
| 9198 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 9199 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9201 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 9202 | &derived_key), expected_status); |
| 9203 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9204 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9205 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9206 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9207 | status = psa_key_derivation_output_bytes(&operation, |
| 9208 | output_buffer, output_sizes[i]); |
| 9209 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9210 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9211 | TEST_ASSERT(status == PSA_SUCCESS || |
| 9212 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9213 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9214 | } else if (expected_capacity == 0 || |
| 9215 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9216 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9217 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9218 | expected_capacity = 0; |
| 9219 | continue; |
| 9220 | } |
| 9221 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9222 | PSA_ASSERT(status); |
| 9223 | if (output_sizes[i] != 0) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9224 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9225 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9226 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9227 | /* Check the operation status. */ |
| 9228 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9229 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9230 | ¤t_capacity)); |
| 9231 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9232 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9233 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9234 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9235 | |
| 9236 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9237 | mbedtls_free(output_buffer); |
| 9238 | psa_key_derivation_abort(&operation); |
| 9239 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 9240 | psa_destroy_key(keys[i]); |
| 9241 | } |
| 9242 | psa_destroy_key(derived_key); |
| 9243 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9244 | } |
| 9245 | /* END_CASE */ |
| 9246 | |
| 9247 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9248 | void derive_full(int alg_arg, |
| 9249 | data_t *key_data, |
| 9250 | data_t *input1, |
| 9251 | data_t *input2, |
| 9252 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9253 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9254 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9255 | psa_algorithm_t alg = alg_arg; |
| 9256 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9257 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 9258 | unsigned char output_buffer[32]; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9259 | size_t expected_capacity = requested_capacity; |
| 9260 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9261 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9263 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9265 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9266 | psa_set_key_algorithm(&attributes, alg); |
| 9267 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9269 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9270 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9272 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 9273 | input1->x, input1->len, |
| 9274 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9275 | requested_capacity, 0)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 9276 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9277 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 9278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9279 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9280 | ¤t_capacity)); |
| 9281 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9282 | |
| 9283 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9284 | while (current_capacity > 0) { |
| 9285 | size_t read_size = sizeof(output_buffer); |
| 9286 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9287 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9288 | } |
| 9289 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9290 | output_buffer, |
| 9291 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9292 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9293 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9294 | ¤t_capacity)); |
| 9295 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9296 | } |
| 9297 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9298 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9299 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 9300 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9302 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9303 | |
| 9304 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9305 | psa_key_derivation_abort(&operation); |
| 9306 | psa_destroy_key(key); |
| 9307 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9308 | } |
| 9309 | /* END_CASE */ |
| 9310 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 9311 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9312 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 9313 | int derivation_step, |
| 9314 | int capacity, int expected_capacity_status_arg, |
| 9315 | data_t *expected_output, |
| 9316 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9317 | { |
| 9318 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 9319 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 9320 | psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step; |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9321 | uint8_t *output_buffer = NULL; |
| 9322 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 9323 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 9324 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 9325 | psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg; |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9326 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9327 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9328 | PSA_ASSERT(psa_crypto_init()); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9330 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9331 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9332 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9334 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9335 | step, input->x, input->len), |
| 9336 | expected_input_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9338 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9339 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9340 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9342 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9343 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9345 | TEST_EQUAL(status, expected_output_status); |
| 9346 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9347 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9348 | expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9349 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9350 | |
| 9351 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9352 | mbedtls_free(output_buffer); |
| 9353 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9354 | PSA_DONE(); |
| 9355 | } |
| 9356 | /* END_CASE */ |
| 9357 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9358 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9359 | void derive_key_exercise(int alg_arg, |
| 9360 | data_t *key_data, |
| 9361 | data_t *input1, |
| 9362 | data_t *input2, |
| 9363 | int derived_type_arg, |
| 9364 | int derived_bits_arg, |
| 9365 | int derived_usage_arg, |
| 9366 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9367 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9368 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9369 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9370 | psa_algorithm_t alg = alg_arg; |
| 9371 | psa_key_type_t derived_type = derived_type_arg; |
| 9372 | size_t derived_bits = derived_bits_arg; |
| 9373 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9374 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9375 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9376 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9377 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9378 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9380 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9382 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9383 | psa_set_key_algorithm(&attributes, alg); |
| 9384 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9385 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9386 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9387 | |
| 9388 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9389 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9390 | input1->x, input1->len, |
| 9391 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9392 | capacity, 0)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9393 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9394 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9396 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9397 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9398 | psa_set_key_type(&attributes, derived_type); |
| 9399 | psa_set_key_bits(&attributes, derived_bits); |
| 9400 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9401 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9402 | |
| 9403 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9404 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9405 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9406 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9407 | |
| 9408 | /* Exercise the derived key. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 9409 | if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg, 0)) { |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9410 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9411 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9412 | |
| 9413 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9414 | /* |
| 9415 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9416 | * thus reset them as required. |
| 9417 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9418 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9420 | psa_key_derivation_abort(&operation); |
| 9421 | psa_destroy_key(base_key); |
| 9422 | psa_destroy_key(derived_key); |
| 9423 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9424 | } |
| 9425 | /* END_CASE */ |
| 9426 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9427 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9428 | void derive_key_export(int alg_arg, |
| 9429 | data_t *key_data, |
| 9430 | data_t *input1, |
| 9431 | data_t *input2, |
| 9432 | int bytes1_arg, |
| 9433 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9434 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9435 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9436 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9437 | psa_algorithm_t alg = alg_arg; |
| 9438 | size_t bytes1 = bytes1_arg; |
| 9439 | size_t bytes2 = bytes2_arg; |
| 9440 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9441 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9442 | uint8_t *output_buffer = NULL; |
| 9443 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9444 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9445 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9446 | size_t length; |
| 9447 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9448 | TEST_CALLOC(output_buffer, capacity); |
| 9449 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9450 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9452 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9453 | psa_set_key_algorithm(&base_attributes, alg); |
| 9454 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9455 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9456 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9457 | |
| 9458 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9459 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9460 | input1->x, input1->len, |
| 9461 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9462 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9463 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9464 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9466 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9467 | output_buffer, |
| 9468 | capacity)); |
| 9469 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9470 | |
| 9471 | /* Derive the same output again, but this time store it in key objects. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9472 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9473 | input1->x, input1->len, |
| 9474 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9475 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9476 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9477 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9479 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9480 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9481 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9482 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9483 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9484 | &derived_key)); |
| 9485 | PSA_ASSERT(psa_export_key(derived_key, |
| 9486 | export_buffer, bytes1, |
| 9487 | &length)); |
| 9488 | TEST_EQUAL(length, bytes1); |
| 9489 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9490 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9491 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9492 | &derived_key)); |
| 9493 | PSA_ASSERT(psa_export_key(derived_key, |
| 9494 | export_buffer + bytes1, bytes2, |
| 9495 | &length)); |
| 9496 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9497 | |
| 9498 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9499 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9500 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9501 | |
| 9502 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9503 | mbedtls_free(output_buffer); |
| 9504 | mbedtls_free(export_buffer); |
| 9505 | psa_key_derivation_abort(&operation); |
| 9506 | psa_destroy_key(base_key); |
| 9507 | psa_destroy_key(derived_key); |
| 9508 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9509 | } |
| 9510 | /* END_CASE */ |
| 9511 | |
| 9512 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9513 | void derive_key_type(int alg_arg, |
| 9514 | data_t *key_data, |
| 9515 | data_t *input1, |
| 9516 | data_t *input2, |
| 9517 | int key_type_arg, int bits_arg, |
| 9518 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9519 | { |
| 9520 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9521 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9522 | const psa_algorithm_t alg = alg_arg; |
| 9523 | const psa_key_type_t key_type = key_type_arg; |
| 9524 | const size_t bits = bits_arg; |
| 9525 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9526 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9527 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9528 | uint8_t *export_buffer = NULL; |
| 9529 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9530 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9531 | size_t export_length; |
| 9532 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9533 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9534 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9536 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9537 | psa_set_key_algorithm(&base_attributes, alg); |
| 9538 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9539 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9540 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9542 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9543 | &operation, base_key, alg, |
| 9544 | input1->x, input1->len, |
| 9545 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9546 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9547 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9548 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9550 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9551 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9552 | psa_set_key_type(&derived_attributes, key_type); |
| 9553 | psa_set_key_bits(&derived_attributes, bits); |
| 9554 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9555 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9557 | PSA_ASSERT(psa_export_key(derived_key, |
| 9558 | export_buffer, export_buffer_size, |
| 9559 | &export_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9560 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9561 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9562 | |
| 9563 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9564 | mbedtls_free(export_buffer); |
| 9565 | psa_key_derivation_abort(&operation); |
| 9566 | psa_destroy_key(base_key); |
| 9567 | psa_destroy_key(derived_key); |
| 9568 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9569 | } |
| 9570 | /* END_CASE */ |
| 9571 | |
| 9572 | /* BEGIN_CASE */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9573 | void derive_key_ext(int alg_arg, |
| 9574 | data_t *key_data, |
| 9575 | data_t *input1, |
| 9576 | data_t *input2, |
| 9577 | int key_type_arg, int bits_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 9578 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9579 | data_t *params_data, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9580 | psa_status_t expected_status, |
| 9581 | data_t *expected_export) |
| 9582 | { |
| 9583 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9584 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9585 | const psa_algorithm_t alg = alg_arg; |
| 9586 | const psa_key_type_t key_type = key_type_arg; |
| 9587 | const size_t bits = bits_arg; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9588 | psa_key_production_parameters_t *params = NULL; |
| 9589 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9590 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9591 | const size_t export_buffer_size = |
| 9592 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
| 9593 | uint8_t *export_buffer = NULL; |
| 9594 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9595 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9596 | size_t export_length; |
| 9597 | |
| 9598 | TEST_CALLOC(export_buffer, export_buffer_size); |
| 9599 | PSA_ASSERT(psa_crypto_init()); |
| 9600 | |
| 9601 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9602 | psa_set_key_algorithm(&base_attributes, alg); |
| 9603 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9604 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9605 | &base_key)); |
| 9606 | |
| 9607 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
| 9608 | &operation, base_key, alg, |
| 9609 | input1->x, input1->len, |
| 9610 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9611 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9612 | goto exit; |
| 9613 | } |
| 9614 | |
| 9615 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9616 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9617 | psa_set_key_type(&derived_attributes, key_type); |
| 9618 | psa_set_key_bits(&derived_attributes, bits); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9619 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 9620 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9621 | goto exit; |
| 9622 | } |
| 9623 | |
| 9624 | TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9625 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9626 | &derived_key), |
| 9627 | expected_status); |
| 9628 | |
| 9629 | if (expected_status == PSA_SUCCESS) { |
| 9630 | PSA_ASSERT(psa_export_key(derived_key, |
| 9631 | export_buffer, export_buffer_size, |
| 9632 | &export_length)); |
| 9633 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
| 9634 | expected_export->x, expected_export->len); |
| 9635 | } |
| 9636 | |
| 9637 | exit: |
| 9638 | mbedtls_free(export_buffer); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9639 | mbedtls_free(params); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9640 | psa_key_derivation_abort(&operation); |
| 9641 | psa_destroy_key(base_key); |
| 9642 | psa_destroy_key(derived_key); |
| 9643 | PSA_DONE(); |
| 9644 | } |
| 9645 | /* END_CASE */ |
| 9646 | |
| 9647 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9648 | void derive_key(int alg_arg, |
| 9649 | data_t *key_data, data_t *input1, data_t *input2, |
| 9650 | int type_arg, int bits_arg, |
| 9651 | int expected_status_arg, |
| 9652 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9653 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9654 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9655 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9656 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9657 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9658 | size_t bits = bits_arg; |
| 9659 | psa_status_t expected_status = expected_status_arg; |
| 9660 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9661 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9662 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9664 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9666 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9667 | psa_set_key_algorithm(&base_attributes, alg); |
| 9668 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9669 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9670 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9672 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9673 | input1->x, input1->len, |
| 9674 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9675 | SIZE_MAX, 0)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9676 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9677 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9679 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9680 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9681 | psa_set_key_type(&derived_attributes, type); |
| 9682 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9683 | |
| 9684 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9685 | psa_key_derivation_output_key(&derived_attributes, |
| 9686 | &operation, |
| 9687 | &derived_key); |
| 9688 | if (is_large_output > 0) { |
| 9689 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9690 | } |
| 9691 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9692 | |
| 9693 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9694 | psa_key_derivation_abort(&operation); |
| 9695 | psa_destroy_key(base_key); |
| 9696 | psa_destroy_key(derived_key); |
| 9697 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9698 | } |
| 9699 | /* END_CASE */ |
| 9700 | |
| 9701 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9702 | void key_agreement_setup(int alg_arg, |
| 9703 | int our_key_type_arg, int our_key_alg_arg, |
| 9704 | data_t *our_key_data, data_t *peer_key_data, |
| 9705 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9706 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9707 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9708 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9709 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9710 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9711 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9712 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9713 | psa_status_t expected_status = expected_status_arg; |
| 9714 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9716 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9718 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9719 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9720 | psa_set_key_type(&attributes, our_key_type); |
| 9721 | PSA_ASSERT(psa_import_key(&attributes, |
| 9722 | our_key_data->x, our_key_data->len, |
| 9723 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9724 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9725 | /* The tests currently include inputs that should fail at either step. |
| 9726 | * Test cases that fail at the setup step should be changed to call |
| 9727 | * key_derivation_setup instead, and this function should be renamed |
| 9728 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9729 | status = psa_key_derivation_setup(&operation, alg); |
| 9730 | if (status == PSA_SUCCESS) { |
| 9731 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9732 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9733 | our_key, |
| 9734 | peer_key_data->x, peer_key_data->len), |
| 9735 | expected_status); |
| 9736 | } else { |
| 9737 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9738 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9739 | |
| 9740 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9741 | psa_key_derivation_abort(&operation); |
| 9742 | psa_destroy_key(our_key); |
| 9743 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9744 | } |
| 9745 | /* END_CASE */ |
| 9746 | |
| 9747 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9748 | void raw_key_agreement(int alg_arg, |
| 9749 | int our_key_type_arg, data_t *our_key_data, |
| 9750 | data_t *peer_key_data, |
| 9751 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9752 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9753 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9754 | psa_algorithm_t alg = alg_arg; |
| 9755 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9756 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9757 | unsigned char *output = NULL; |
| 9758 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9759 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9761 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9763 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9764 | psa_set_key_algorithm(&attributes, alg); |
| 9765 | psa_set_key_type(&attributes, our_key_type); |
| 9766 | PSA_ASSERT(psa_import_key(&attributes, |
| 9767 | our_key_data->x, our_key_data->len, |
| 9768 | &our_key)); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9770 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9771 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9772 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9773 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9774 | TEST_LE_U(expected_output->len, |
| 9775 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9776 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9777 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9778 | |
| 9779 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9780 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9781 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9782 | peer_key_data->x, peer_key_data->len, |
| 9783 | output, expected_output->len, |
| 9784 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9785 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9786 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9787 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9788 | output = NULL; |
| 9789 | output_length = ~0; |
| 9790 | |
| 9791 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9792 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9793 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9794 | peer_key_data->x, peer_key_data->len, |
| 9795 | output, expected_output->len + 1, |
| 9796 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9797 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9798 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9799 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9800 | output = NULL; |
| 9801 | output_length = ~0; |
| 9802 | |
| 9803 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9804 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9805 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9806 | peer_key_data->x, peer_key_data->len, |
| 9807 | output, expected_output->len - 1, |
| 9808 | &output_length), |
| 9809 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9810 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9811 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9812 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9813 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9814 | |
| 9815 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9816 | mbedtls_free(output); |
| 9817 | psa_destroy_key(our_key); |
| 9818 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9819 | } |
| 9820 | /* END_CASE */ |
| 9821 | |
| 9822 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9823 | void key_agreement_capacity(int alg_arg, |
| 9824 | int our_key_type_arg, data_t *our_key_data, |
| 9825 | data_t *peer_key_data, |
| 9826 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9827 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9828 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9829 | psa_algorithm_t alg = alg_arg; |
| 9830 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9831 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9832 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9833 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9834 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9836 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9838 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9839 | psa_set_key_algorithm(&attributes, alg); |
| 9840 | psa_set_key_type(&attributes, our_key_type); |
| 9841 | PSA_ASSERT(psa_import_key(&attributes, |
| 9842 | our_key_data->x, our_key_data->len, |
| 9843 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9845 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9846 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9847 | &operation, |
| 9848 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9849 | peer_key_data->x, peer_key_data->len)); |
| 9850 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9851 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9852 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9853 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9854 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9855 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9856 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9857 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9858 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9859 | &operation, &actual_capacity)); |
| 9860 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9861 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9862 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9863 | while (actual_capacity > sizeof(output)) { |
| 9864 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9865 | output, sizeof(output))); |
| 9866 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9867 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9868 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9869 | output, actual_capacity)); |
| 9870 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9871 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9872 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9873 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9874 | psa_key_derivation_abort(&operation); |
| 9875 | psa_destroy_key(our_key); |
| 9876 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9877 | } |
| 9878 | /* END_CASE */ |
| 9879 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9880 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9881 | void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg) |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9882 | { |
| 9883 | mbedtls_ecp_group_id grp_id = grp_id_arg; |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9884 | psa_ecc_family_t ecc_family = psa_family_arg; |
| 9885 | size_t bits = bits_arg; |
| 9886 | size_t bits_tmp; |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9887 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9888 | TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp)); |
| 9889 | TEST_EQUAL(bits, bits_tmp); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9890 | TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits)); |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9891 | } |
| 9892 | /* END_CASE */ |
| 9893 | |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9894 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9895 | void ecc_conversion_functions_fail() |
| 9896 | { |
| 9897 | size_t bits; |
| 9898 | |
Valerio Setti | db6e029 | 2024-01-05 10:15:45 +0100 | [diff] [blame] | 9899 | /* Invalid legacy curve identifiers. */ |
| 9900 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits)); |
| 9901 | TEST_EQUAL(0, bits); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9902 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits)); |
| 9903 | TEST_EQUAL(0, bits); |
| 9904 | |
| 9905 | /* Invalid PSA EC family. */ |
| 9906 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192)); |
| 9907 | /* Invalid bit-size for a valid EC family. */ |
| 9908 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512)); |
| 9909 | |
| 9910 | /* Twisted-Edward curves are not supported yet. */ |
| 9911 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9912 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255)); |
| 9913 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9914 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448)); |
| 9915 | } |
| 9916 | /* END_CASE */ |
| 9917 | |
| 9918 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9919 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9920 | void key_agreement_output(int alg_arg, |
| 9921 | int our_key_type_arg, data_t *our_key_data, |
| 9922 | data_t *peer_key_data, |
| 9923 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9924 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9925 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9926 | psa_algorithm_t alg = alg_arg; |
| 9927 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9928 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9929 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9930 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9931 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9932 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9933 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9935 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9937 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9938 | psa_set_key_algorithm(&attributes, alg); |
| 9939 | psa_set_key_type(&attributes, our_key_type); |
| 9940 | PSA_ASSERT(psa_import_key(&attributes, |
| 9941 | our_key_data->x, our_key_data->len, |
| 9942 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9944 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9945 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9946 | &operation, |
| 9947 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9948 | peer_key_data->x, peer_key_data->len)); |
| 9949 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9950 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9951 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9952 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9953 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9954 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9956 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9957 | actual_output, |
| 9958 | expected_output1->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9959 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9960 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9961 | if (expected_output2->len != 0) { |
| 9962 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9963 | actual_output, |
| 9964 | expected_output2->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9965 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9966 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 9967 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9968 | |
| 9969 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9970 | psa_key_derivation_abort(&operation); |
| 9971 | psa_destroy_key(our_key); |
| 9972 | PSA_DONE(); |
| 9973 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9974 | } |
| 9975 | /* END_CASE */ |
| 9976 | |
| 9977 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9978 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9979 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9980 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9981 | unsigned char *output = NULL; |
| 9982 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9983 | size_t i; |
| 9984 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9986 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 9987 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9988 | TEST_CALLOC(output, bytes); |
| 9989 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9990 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9991 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 9992 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 9993 | /* Run several times, to ensure that every output byte will be |
| 9994 | * nonzero at least once with overwhelming probability |
| 9995 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9996 | for (run = 0; run < 10; run++) { |
| 9997 | if (bytes != 0) { |
| 9998 | memset(output, 0, bytes); |
| 9999 | } |
| 10000 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10002 | for (i = 0; i < bytes; i++) { |
| 10003 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10004 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10005 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10006 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10007 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10008 | |
| 10009 | /* Check that every byte was changed to nonzero at least once. This |
| 10010 | * validates that psa_generate_random is overwriting every byte of |
| 10011 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10012 | for (i = 0; i < bytes; i++) { |
| 10013 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10014 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10015 | |
| 10016 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10017 | PSA_DONE(); |
| 10018 | mbedtls_free(output); |
| 10019 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10020 | } |
| 10021 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10022 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 10023 | #if defined MBEDTLS_THREADING_PTHREAD |
| 10024 | |
| 10025 | /* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD */ |
| 10026 | void concurrently_generate_keys(int type_arg, |
| 10027 | int bits_arg, |
| 10028 | int usage_arg, |
| 10029 | int alg_arg, |
| 10030 | int expected_status_arg, |
| 10031 | int is_large_key_arg, |
| 10032 | int arg_thread_count, |
| 10033 | int reps_arg) |
| 10034 | { |
| 10035 | size_t thread_count = (size_t) arg_thread_count; |
| 10036 | mbedtls_test_thread_t *threads = NULL; |
| 10037 | generate_key_context gkc; |
| 10038 | gkc.type = type_arg; |
| 10039 | gkc.usage = usage_arg; |
| 10040 | gkc.bits = bits_arg; |
| 10041 | gkc.alg = alg_arg; |
| 10042 | gkc.expected_status = expected_status_arg; |
| 10043 | gkc.is_large_key = is_large_key_arg; |
| 10044 | gkc.reps = reps_arg; |
| 10045 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10046 | |
| 10047 | PSA_ASSERT(psa_crypto_init()); |
| 10048 | |
| 10049 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 10050 | psa_set_key_algorithm(&attributes, alg_arg); |
| 10051 | psa_set_key_type(&attributes, type_arg); |
| 10052 | psa_set_key_bits(&attributes, bits_arg); |
| 10053 | gkc.attributes = &attributes; |
| 10054 | |
| 10055 | TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count); |
| 10056 | |
| 10057 | /* Split threads to generate key then destroy key. */ |
| 10058 | for (size_t i = 0; i < thread_count; i++) { |
| 10059 | TEST_EQUAL( |
| 10060 | mbedtls_test_thread_create(&threads[i], thread_generate_key, |
| 10061 | (void *) &gkc), 0); |
| 10062 | } |
| 10063 | |
| 10064 | /* Join threads. */ |
| 10065 | for (size_t i = 0; i < thread_count; i++) { |
| 10066 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 10067 | } |
| 10068 | |
| 10069 | exit: |
| 10070 | mbedtls_free(threads); |
| 10071 | PSA_DONE(); |
| 10072 | } |
| 10073 | /* END_CASE */ |
| 10074 | #endif |
| 10075 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10076 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10077 | void generate_key(int type_arg, |
| 10078 | int bits_arg, |
| 10079 | int usage_arg, |
| 10080 | int alg_arg, |
| 10081 | int expected_status_arg, |
| 10082 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10083 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10084 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10085 | psa_key_type_t type = type_arg; |
| 10086 | psa_key_usage_t usage = usage_arg; |
| 10087 | size_t bits = bits_arg; |
| 10088 | psa_algorithm_t alg = alg_arg; |
| 10089 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10090 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 10091 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10093 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10095 | psa_set_key_usage_flags(&attributes, usage); |
| 10096 | psa_set_key_algorithm(&attributes, alg); |
| 10097 | psa_set_key_type(&attributes, type); |
| 10098 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10099 | |
| 10100 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10101 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 10102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10103 | if (is_large_key > 0) { |
| 10104 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 10105 | } |
| 10106 | TEST_EQUAL(status, expected_status); |
| 10107 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10108 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10109 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10110 | |
| 10111 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10112 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10113 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10114 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10115 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 10116 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10117 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 10118 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10119 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10120 | |
| 10121 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10122 | /* |
| 10123 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10124 | * thus reset them as required. |
| 10125 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10126 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10128 | psa_destroy_key(key); |
| 10129 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10130 | } |
| 10131 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 10132 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10133 | /* BEGIN_CASE */ |
| 10134 | void generate_key_ext(int type_arg, |
| 10135 | int bits_arg, |
| 10136 | int usage_arg, |
| 10137 | int alg_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 10138 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10139 | data_t *params_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10140 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10141 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10142 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10143 | psa_key_type_t type = type_arg; |
| 10144 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10145 | size_t bits = bits_arg; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10146 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10147 | psa_status_t expected_status = expected_status_arg; |
| 10148 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10149 | psa_key_production_parameters_t *params = NULL; |
| 10150 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10151 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10153 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10155 | psa_set_key_usage_flags(&attributes, usage); |
| 10156 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10157 | psa_set_key_type(&attributes, type); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10158 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10159 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10160 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 10161 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10162 | goto exit; |
| 10163 | } |
| 10164 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10165 | /* Generate a key */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10166 | psa_status_t status = psa_generate_key_ext(&attributes, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10167 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10168 | &key); |
| 10169 | |
| 10170 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10171 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10172 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10173 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10174 | |
| 10175 | /* Test the key information */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10176 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10177 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10178 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 10179 | |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 10180 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 10181 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10182 | TEST_ASSERT(rsa_test_e(key, bits, params_data)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10183 | } |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 10184 | #endif |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10185 | |
| 10186 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10187 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10188 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10189 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10190 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10191 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10192 | /* |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10193 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10194 | * thus reset them as required. |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10195 | */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10196 | psa_reset_key_attributes(&got_attributes); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10197 | mbedtls_free(params); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10198 | psa_destroy_key(key); |
| 10199 | PSA_DONE(); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10200 | } |
| 10201 | /* END_CASE */ |
| 10202 | |
| 10203 | /* BEGIN_CASE */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10204 | void key_production_parameters_init() |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10205 | { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10206 | psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT; |
| 10207 | psa_key_production_parameters_t zero; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10208 | memset(&zero, 0, sizeof(zero)); |
| 10209 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10210 | TEST_EQUAL(init.flags, 0); |
| 10211 | TEST_EQUAL(zero.flags, 0); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10212 | } |
| 10213 | /* END_CASE */ |
| 10214 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10215 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10216 | void persistent_key_load_key_from_storage(data_t *data, |
| 10217 | int type_arg, int bits_arg, |
| 10218 | int usage_flags_arg, int alg_arg, |
| 10219 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10220 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10221 | 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] | 10222 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10223 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10224 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10225 | psa_key_type_t type = type_arg; |
| 10226 | size_t bits = bits_arg; |
| 10227 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 10228 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 10229 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10230 | unsigned char *first_export = NULL; |
| 10231 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10232 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 10233 | size_t first_exported_length = 0; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10234 | size_t second_exported_length; |
| 10235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10236 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10237 | TEST_CALLOC(first_export, export_size); |
| 10238 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10239 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10241 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10243 | psa_set_key_id(&attributes, key_id); |
| 10244 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 10245 | psa_set_key_algorithm(&attributes, alg); |
| 10246 | psa_set_key_type(&attributes, type); |
| 10247 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10249 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10250 | case IMPORT_KEY: |
| 10251 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10252 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 10253 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10254 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10255 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10256 | case GENERATE_KEY: |
| 10257 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10258 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10259 | break; |
| 10260 | |
| 10261 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 10262 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10263 | { |
| 10264 | /* Create base key */ |
| 10265 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 10266 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10267 | psa_set_key_usage_flags(&base_attributes, |
| 10268 | PSA_KEY_USAGE_DERIVE); |
| 10269 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 10270 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 10271 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 10272 | data->x, data->len, |
| 10273 | &base_key)); |
| 10274 | /* Derive a key. */ |
| 10275 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 10276 | PSA_ASSERT(psa_key_derivation_input_key( |
| 10277 | &operation, |
| 10278 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 10279 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 10280 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 10281 | NULL, 0)); |
| 10282 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 10283 | &operation, |
| 10284 | &key)); |
| 10285 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 10286 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 10287 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10288 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10289 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10290 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10291 | #endif |
| 10292 | break; |
| 10293 | |
| 10294 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 10295 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10296 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10297 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10298 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10299 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10300 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10301 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10302 | PSA_ASSERT(psa_export_key(key, |
| 10303 | first_export, export_size, |
| 10304 | &first_exported_length)); |
| 10305 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10306 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10307 | first_export, first_exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10308 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10309 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10310 | |
| 10311 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10312 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10313 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10314 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10315 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10316 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10317 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 10318 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 10319 | psa_get_key_id(&attributes), key_id)); |
| 10320 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 10321 | PSA_KEY_LIFETIME_PERSISTENT); |
| 10322 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 10323 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 10324 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 10325 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 10326 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10327 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10328 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10329 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10330 | PSA_ASSERT(psa_export_key(key, |
| 10331 | second_export, export_size, |
| 10332 | &second_exported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10333 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10334 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10335 | } |
| 10336 | |
| 10337 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10338 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg, 0)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10339 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10340 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10341 | |
| 10342 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10343 | /* |
| 10344 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10345 | * thus reset them as required. |
| 10346 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10347 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10349 | mbedtls_free(first_export); |
| 10350 | mbedtls_free(second_export); |
| 10351 | psa_key_derivation_abort(&operation); |
| 10352 | psa_destroy_key(base_key); |
| 10353 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10354 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10355 | } |
| 10356 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10357 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10358 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10359 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 10360 | int primitive_arg, int hash_arg, int role_arg, |
| 10361 | int test_input, data_t *pw_data, |
| 10362 | int inj_err_type_arg, |
| 10363 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10364 | { |
| 10365 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10366 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 10367 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10368 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 10369 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 10370 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10371 | psa_algorithm_t hash_alg = hash_arg; |
| 10372 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10373 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10374 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10375 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 10376 | psa_status_t expected_error = expected_error_arg; |
| 10377 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10378 | unsigned char *output_buffer = NULL; |
| 10379 | size_t output_len = 0; |
| 10380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10381 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10382 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10383 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10384 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10385 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10387 | if (pw_data->len > 0) { |
| 10388 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 10389 | psa_set_key_algorithm(&attributes, alg); |
| 10390 | psa_set_key_type(&attributes, key_type_pw); |
| 10391 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10392 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10393 | } |
| 10394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10395 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10396 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 10397 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10399 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 10400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10401 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 10402 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10403 | expected_error); |
| 10404 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10405 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10406 | expected_error); |
| 10407 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10408 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 10409 | expected_error); |
| 10410 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10411 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 10412 | expected_error); |
| 10413 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10414 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10415 | NULL, 0, NULL), |
| 10416 | expected_error); |
| 10417 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10418 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 10419 | expected_error); |
| 10420 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10421 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10422 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10424 | status = psa_pake_setup(&operation, &cipher_suite); |
| 10425 | if (status != PSA_SUCCESS) { |
| 10426 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10427 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10428 | } |
| 10429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10430 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 10431 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 10432 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10433 | goto exit; |
| 10434 | } |
| 10435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10436 | status = psa_pake_set_role(&operation, role); |
| 10437 | if (status != PSA_SUCCESS) { |
| 10438 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10439 | goto exit; |
| 10440 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10442 | if (pw_data->len > 0) { |
| 10443 | status = psa_pake_set_password_key(&operation, key); |
| 10444 | if (status != PSA_SUCCESS) { |
| 10445 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10446 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10447 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10448 | } |
| 10449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10450 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 10451 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10452 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10453 | goto exit; |
| 10454 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10456 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 10457 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10458 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10459 | goto exit; |
| 10460 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10462 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10463 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10464 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 10465 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10466 | goto exit; |
| 10467 | } |
| 10468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10469 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10470 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10471 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10472 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10473 | goto exit; |
| 10474 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10476 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10477 | PSA_PAKE_STEP_KEY_SHARE); |
| 10478 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10479 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10480 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10481 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10483 | if (test_input) { |
| 10484 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10485 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 10486 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10487 | goto exit; |
| 10488 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10490 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10491 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10492 | output_buffer, size_zk_proof), |
| 10493 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10494 | goto exit; |
| 10495 | } |
| 10496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10497 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10498 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10499 | output_buffer, size_zk_proof), |
| 10500 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10501 | goto exit; |
| 10502 | } |
| 10503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10504 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10505 | output_buffer, size_key_share); |
| 10506 | if (status != PSA_SUCCESS) { |
| 10507 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10508 | goto exit; |
| 10509 | } |
| 10510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10511 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10512 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10513 | output_buffer, size_zk_public + 1), |
| 10514 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10515 | goto exit; |
| 10516 | } |
| 10517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10518 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10519 | // Just trigger any kind of error. We don't care about the result here |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10520 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10521 | output_buffer, size_zk_public + 1); |
| 10522 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10523 | output_buffer, size_zk_public), |
| 10524 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10525 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10526 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10527 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10528 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10529 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10530 | NULL, 0, NULL), |
| 10531 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10532 | goto exit; |
| 10533 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10535 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10536 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10537 | output_buffer, buf_size, &output_len), |
| 10538 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10539 | goto exit; |
| 10540 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10542 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10543 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10544 | output_buffer, buf_size, &output_len), |
| 10545 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10546 | goto exit; |
| 10547 | } |
| 10548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10549 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10550 | output_buffer, buf_size, &output_len); |
| 10551 | if (status != PSA_SUCCESS) { |
| 10552 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10553 | goto exit; |
| 10554 | } |
| 10555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10556 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10558 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10559 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10560 | output_buffer, size_zk_public - 1, &output_len), |
| 10561 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10562 | goto exit; |
| 10563 | } |
| 10564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10565 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10566 | // Just trigger any kind of error. We don't care about the result here |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10567 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10568 | output_buffer, size_zk_public - 1, &output_len); |
| 10569 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10570 | output_buffer, buf_size, &output_len), |
| 10571 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10572 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10573 | } |
| 10574 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10575 | |
| 10576 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10577 | PSA_ASSERT(psa_destroy_key(key)); |
| 10578 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10579 | mbedtls_free(output_buffer); |
| 10580 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10581 | } |
| 10582 | /* END_CASE */ |
| 10583 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10584 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10585 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10586 | int client_input_first, int inject_error, |
| 10587 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10588 | { |
| 10589 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10590 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10591 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10592 | psa_algorithm_t alg = alg_arg; |
| 10593 | psa_algorithm_t hash_alg = hash_arg; |
| 10594 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10595 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10597 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10599 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10600 | psa_set_key_algorithm(&attributes, alg); |
| 10601 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10602 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10603 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10605 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10606 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10607 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10608 | |
| 10609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10610 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10611 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10613 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10614 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10616 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10617 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10619 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10620 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10622 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10623 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10624 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10626 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10627 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10628 | |
| 10629 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10630 | psa_destroy_key(key); |
| 10631 | psa_pake_abort(&server); |
| 10632 | psa_pake_abort(&client); |
| 10633 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10634 | } |
| 10635 | /* END_CASE */ |
| 10636 | |
| 10637 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10638 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10639 | int derive_alg_arg, data_t *pw_data, |
| 10640 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10641 | { |
| 10642 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10643 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10644 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10645 | psa_algorithm_t alg = alg_arg; |
| 10646 | psa_algorithm_t hash_alg = hash_arg; |
| 10647 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10648 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10649 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10650 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10651 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10652 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10653 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10654 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10656 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10658 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10659 | psa_set_key_algorithm(&attributes, alg); |
| 10660 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10661 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10662 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10664 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10665 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10666 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10667 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10668 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10669 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10670 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10672 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10673 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10674 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10675 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10676 | (const uint8_t *) "", 0)); |
| 10677 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10678 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10679 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10680 | } |
| 10681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10682 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10683 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10685 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10686 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10688 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10689 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10691 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10692 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10693 | PSA_ERROR_BAD_STATE); |
| 10694 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10695 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10696 | goto exit; |
| 10697 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10698 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10699 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10700 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10701 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10703 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10704 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10705 | PSA_ERROR_BAD_STATE); |
| 10706 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10707 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10708 | goto exit; |
| 10709 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10710 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10711 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10712 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10713 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10715 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10716 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10717 | |
| 10718 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10719 | psa_key_derivation_abort(&server_derive); |
| 10720 | psa_key_derivation_abort(&client_derive); |
| 10721 | psa_destroy_key(key); |
| 10722 | psa_pake_abort(&server); |
| 10723 | psa_pake_abort(&client); |
| 10724 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10725 | } |
| 10726 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10727 | |
| 10728 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10729 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10730 | { |
| 10731 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10732 | const size_t bits = 256; |
| 10733 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10734 | PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10735 | const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10736 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10737 | |
| 10738 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10739 | /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10740 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10741 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10742 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10743 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10744 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10745 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10746 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10747 | |
| 10748 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10749 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10750 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10751 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10752 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10753 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10754 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10755 | |
| 10756 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10757 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10758 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10759 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10760 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10761 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10762 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10763 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10764 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10765 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10766 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10767 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10768 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10769 | } |
| 10770 | /* END_CASE */ |