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 | |
Valerio Setti | efce605 | 2024-06-25 18:31:36 +0200 | [diff] [blame] | 1239 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) && defined(MBEDTLS_ASN1_PARSE_C) |
Gilles Peskine | 1d25a0a | 2024-02-12 16:40:04 +0100 | [diff] [blame] | 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 | |
Michael Schuster | 275b698 | 2024-06-07 01:51:54 +0200 | [diff] [blame] | 1319 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1320 | typedef struct same_key_context { |
| 1321 | data_t *data; |
| 1322 | mbedtls_svc_key_id_t key; |
| 1323 | psa_key_attributes_t *attributes; |
| 1324 | int type; |
| 1325 | int bits; |
| 1326 | /* The following two parameters are used to ensure that when multiple |
| 1327 | * threads attempt to load/destroy the key, exactly one thread succeeds. */ |
| 1328 | int key_loaded; |
| 1329 | mbedtls_threading_mutex_t MBEDTLS_PRIVATE(key_loaded_mutex); |
| 1330 | } |
| 1331 | same_key_context; |
| 1332 | |
| 1333 | /* Attempt to import the key in ctx. This handles any valid error codes |
| 1334 | * and reports an error for any invalid codes. This function also insures |
| 1335 | * that once imported by some thread, all threads can use the key. */ |
Michael Schuster | b1e33fb | 2024-06-04 02:30:22 +0200 | [diff] [blame] | 1336 | static void *thread_import_key(void *ctx) |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1337 | { |
| 1338 | mbedtls_svc_key_id_t returned_key_id; |
| 1339 | same_key_context *skc = (struct same_key_context *) ctx; |
| 1340 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1341 | |
Ryan Everett | 6c48870 | 2024-03-14 17:49:44 +0000 | [diff] [blame] | 1342 | /* Import the key, exactly one thread must succeed. */ |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1343 | psa_status_t status = psa_import_key(skc->attributes, skc->data->x, |
| 1344 | skc->data->len, &returned_key_id); |
| 1345 | switch (status) { |
| 1346 | case PSA_SUCCESS: |
| 1347 | if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) { |
| 1348 | if (skc->key_loaded) { |
| 1349 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1350 | /* More than one thread has succeeded, report a failure. */ |
Ryan Everett | 3de040f | 2024-03-14 17:50:06 +0000 | [diff] [blame] | 1351 | 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] | 1352 | } |
| 1353 | skc->key_loaded = 1; |
| 1354 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1355 | } |
| 1356 | break; |
| 1357 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 1358 | /* If all of the key slots are reserved when a thread |
| 1359 | * locks the mutex to reserve a new slot, it will return |
| 1360 | * PSA_ERROR_INSUFFICIENT_MEMORY; this is correct behaviour. |
| 1361 | * There is a chance for this to occur here when the number of |
| 1362 | * threads running this function is larger than the number of |
| 1363 | * free key slots. Each thread reserves an empty key slot, |
| 1364 | * unlocks the mutex, then relocks it to finalize key creation. |
| 1365 | * It is at that point where the thread sees that the key |
| 1366 | * already exists, releases the reserved slot, |
| 1367 | * and returns PSA_ERROR_ALREADY_EXISTS. |
| 1368 | * There is no guarantee that the key is loaded upon this return |
| 1369 | * code, so we can't test the key information. Just stop this |
| 1370 | * thread from executing, note that this is not an error. */ |
| 1371 | goto exit; |
| 1372 | break; |
| 1373 | case PSA_ERROR_ALREADY_EXISTS: |
| 1374 | /* The key has been loaded by a different thread. */ |
| 1375 | break; |
| 1376 | default: |
| 1377 | PSA_ASSERT(status); |
| 1378 | } |
| 1379 | /* At this point the key must exist, test the key information. */ |
| 1380 | status = psa_get_key_attributes(skc->key, &got_attributes); |
| 1381 | if (status == PSA_ERROR_INSUFFICIENT_MEMORY) { |
| 1382 | /* This is not a test failure. The following sequence of events |
| 1383 | * causes this to occur: |
| 1384 | * 1: This thread successfuly imports a persistent key skc->key. |
| 1385 | * 2: N threads reserve an empty key slot in psa_import_key, |
| 1386 | * where N is equal to the number of free key slots. |
| 1387 | * 3: A final thread attempts to reserve an empty key slot, kicking |
| 1388 | * skc->key (which has no registered readers) out of its slot. |
| 1389 | * 4: This thread calls psa_get_key_attributes(skc->key,...): |
| 1390 | * it sees that skc->key is not in a slot, attempts to load it and |
| 1391 | * finds that there are no free slots. |
| 1392 | * This thread returns PSA_ERROR_INSUFFICIENT_MEMORY. |
| 1393 | * |
| 1394 | * The PSA spec allows this behaviour, it is an unavoidable consequence |
| 1395 | * of allowing persistent keys to be kicked out of the key store while |
| 1396 | * they are still valid. */ |
| 1397 | goto exit; |
| 1398 | } |
| 1399 | PSA_ASSERT(status); |
| 1400 | TEST_EQUAL(psa_get_key_type(&got_attributes), skc->type); |
| 1401 | TEST_EQUAL(psa_get_key_bits(&got_attributes), skc->bits); |
| 1402 | |
| 1403 | exit: |
| 1404 | /* Key attributes may have been returned by psa_get_key_attributes(), |
| 1405 | * reset them as required. */ |
| 1406 | psa_reset_key_attributes(&got_attributes); |
| 1407 | return NULL; |
| 1408 | } |
| 1409 | |
Michael Schuster | b1e33fb | 2024-06-04 02:30:22 +0200 | [diff] [blame] | 1410 | static void *thread_use_and_destroy_key(void *ctx) |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1411 | { |
| 1412 | same_key_context *skc = (struct same_key_context *) ctx; |
| 1413 | |
| 1414 | /* Do something with the key according |
| 1415 | * to its type and permitted usage. */ |
| 1416 | TEST_ASSERT(mbedtls_test_psa_exercise_key(skc->key, |
| 1417 | skc->attributes->policy.usage, |
| 1418 | skc->attributes->policy.alg, 1)); |
| 1419 | |
| 1420 | psa_status_t status = psa_destroy_key(skc->key); |
| 1421 | if (status == PSA_SUCCESS) { |
| 1422 | if (mbedtls_mutex_lock(&skc->key_loaded_mutex) == 0) { |
| 1423 | /* Ensure that we are the only thread to succeed. */ |
| 1424 | if (skc->key_loaded != 1) { |
| 1425 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
Ryan Everett | 3de040f | 2024-03-14 17:50:06 +0000 | [diff] [blame] | 1426 | TEST_FAIL("The same key has been destroyed multiple times."); |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1427 | } |
| 1428 | skc->key_loaded = 0; |
| 1429 | mbedtls_mutex_unlock(&skc->key_loaded_mutex); |
| 1430 | } |
| 1431 | } else { |
| 1432 | TEST_EQUAL(status, PSA_ERROR_INVALID_HANDLE); |
| 1433 | } |
| 1434 | |
| 1435 | exit: |
| 1436 | return NULL; |
| 1437 | } |
Michael Schuster | 275b698 | 2024-06-07 01:51:54 +0200 | [diff] [blame] | 1438 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1439 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1440 | typedef struct generate_key_context { |
| 1441 | psa_key_type_t type; |
| 1442 | psa_key_usage_t usage; |
| 1443 | size_t bits; |
| 1444 | psa_algorithm_t alg; |
| 1445 | psa_status_t expected_status; |
| 1446 | psa_key_attributes_t *attributes; |
| 1447 | int is_large_key; |
| 1448 | int reps; |
| 1449 | } |
| 1450 | generate_key_context; |
Michael Schuster | b1e33fb | 2024-06-04 02:30:22 +0200 | [diff] [blame] | 1451 | static void *thread_generate_key(void *ctx) |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1452 | { |
| 1453 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1454 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1455 | generate_key_context *gkc = (struct generate_key_context *) ctx; |
| 1456 | |
| 1457 | /* If there are race conditions, it is likely the case that they do not |
| 1458 | * arise every time the code runs. We repeat the code to increase the |
| 1459 | * chance that any race conditions will be hit. */ |
| 1460 | for (int n = 0; n < gkc->reps; n++) { |
| 1461 | /* Generate a key */ |
| 1462 | psa_status_t status = psa_generate_key(gkc->attributes, &key); |
| 1463 | |
| 1464 | if (gkc->is_large_key > 0) { |
| 1465 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1466 | } |
| 1467 | |
| 1468 | TEST_EQUAL(status, gkc->expected_status); |
| 1469 | if (gkc->expected_status != PSA_SUCCESS) { |
| 1470 | PSA_ASSERT(psa_destroy_key(key)); |
| 1471 | goto exit; |
| 1472 | } |
| 1473 | |
| 1474 | /* Test the key information */ |
| 1475 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1476 | TEST_EQUAL(psa_get_key_type(&got_attributes), gkc->type); |
| 1477 | TEST_EQUAL(psa_get_key_bits(&got_attributes), gkc->bits); |
| 1478 | |
| 1479 | /* Do something with the key according |
| 1480 | * to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1481 | if (!mbedtls_test_psa_exercise_key(key, gkc->usage, gkc->alg, 0)) { |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 1482 | psa_destroy_key(key); |
| 1483 | goto exit; |
| 1484 | } |
| 1485 | psa_reset_key_attributes(&got_attributes); |
| 1486 | |
| 1487 | PSA_ASSERT(psa_destroy_key(key)); |
| 1488 | } |
| 1489 | exit: |
| 1490 | /* |
| 1491 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1492 | * thus reset them as required. |
| 1493 | */ |
| 1494 | psa_reset_key_attributes(&got_attributes); |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | #endif /* MBEDTLS_THREADING_PTHREAD */ |
| 1498 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1499 | /* END_HEADER */ |
| 1500 | |
| 1501 | /* BEGIN_DEPENDENCIES |
| 1502 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1503 | * END_DEPENDENCIES |
| 1504 | */ |
| 1505 | |
| 1506 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 7abdf7e | 2023-03-09 11:17:43 +0100 | [diff] [blame] | 1507 | void psa_can_do_hash() |
| 1508 | { |
| 1509 | /* We can't test that this is specific to drivers until partial init has |
| 1510 | * been implemented, but we can at least test before/after full init. */ |
| 1511 | TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE)); |
| 1512 | PSA_INIT(); |
| 1513 | TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE)); |
| 1514 | PSA_DONE(); |
| 1515 | } |
| 1516 | /* END_CASE */ |
| 1517 | |
| 1518 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1519 | void static_checks() |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1520 | { |
| 1521 | size_t max_truncated_mac_size = |
| 1522 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1523 | |
| 1524 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1525 | * encoding. The shifted mask is the maximum truncated value. The |
| 1526 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1527 | TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1528 | } |
| 1529 | /* END_CASE */ |
| 1530 | |
| 1531 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1532 | void import_with_policy(int type_arg, |
| 1533 | int usage_arg, int alg_arg, |
| 1534 | int expected_status_arg) |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1535 | { |
| 1536 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1537 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1538 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1539 | psa_key_type_t type = type_arg; |
| 1540 | psa_key_usage_t usage = usage_arg; |
| 1541 | psa_algorithm_t alg = alg_arg; |
| 1542 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1543 | const uint8_t key_material[16] = { 0 }; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1544 | psa_status_t status; |
| 1545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1546 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1548 | psa_set_key_type(&attributes, type); |
| 1549 | psa_set_key_usage_flags(&attributes, usage); |
| 1550 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1552 | status = psa_import_key(&attributes, |
| 1553 | key_material, sizeof(key_material), |
| 1554 | &key); |
| 1555 | TEST_EQUAL(status, expected_status); |
| 1556 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1557 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1558 | } |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1560 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1561 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1562 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), |
| 1563 | mbedtls_test_update_key_usage_flags(usage)); |
| 1564 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 1565 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1567 | PSA_ASSERT(psa_destroy_key(key)); |
| 1568 | test_operations_on_invalid_key(key); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1569 | |
| 1570 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1571 | /* |
| 1572 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1573 | * thus reset them as required. |
| 1574 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1575 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1577 | psa_destroy_key(key); |
| 1578 | PSA_DONE(); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1579 | } |
| 1580 | /* END_CASE */ |
| 1581 | |
| 1582 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1583 | void import_with_data(data_t *data, int type_arg, |
| 1584 | int attr_bits_arg, |
| 1585 | int expected_status_arg) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1586 | { |
| 1587 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1588 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1589 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1590 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1591 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1592 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1593 | psa_status_t status; |
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_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1597 | psa_set_key_type(&attributes, type); |
| 1598 | psa_set_key_bits(&attributes, attr_bits); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1599 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1600 | status = psa_import_key(&attributes, data->x, data->len, &key); |
Manuel Pégourié-Gonnard | 0509b58 | 2023-08-08 12:47:56 +0200 | [diff] [blame] | 1601 | /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED. |
| 1602 | * |
| 1603 | * This can happen with a type supported only by a driver: |
| 1604 | * - the driver sees the invalid data (for example wrong size) and thinks |
| 1605 | * "well perhaps this is a key size I don't support" so it returns |
| 1606 | * NOT_SUPPORTED which is correct at this point; |
| 1607 | * - we fallback to built-ins, which don't support this type, so return |
| 1608 | * NOT_SUPPORTED which again is correct at this point. |
| 1609 | */ |
| 1610 | if (expected_status == PSA_ERROR_INVALID_ARGUMENT && |
| 1611 | status == PSA_ERROR_NOT_SUPPORTED) { |
| 1612 | ; // OK |
| 1613 | } else { |
| 1614 | TEST_EQUAL(status, expected_status); |
| 1615 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1616 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1617 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1618 | } |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1620 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1621 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1622 | if (attr_bits != 0) { |
| 1623 | TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes)); |
| 1624 | } |
| 1625 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1627 | PSA_ASSERT(psa_destroy_key(key)); |
| 1628 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1629 | |
| 1630 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1631 | /* |
| 1632 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1633 | * thus reset them as required. |
| 1634 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1635 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1637 | psa_destroy_key(key); |
| 1638 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1639 | } |
| 1640 | /* END_CASE */ |
| 1641 | |
Valerio Setti | 168d24a | 2024-06-20 14:40:54 +0200 | [diff] [blame] | 1642 | /* BEGIN_CASE depends_on: !MBEDTLS_PSA_STATIC_KEY_SLOTS*/ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1643 | /* Construct and attempt to import a large unstructured key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1644 | void import_large_key(int type_arg, int byte_size_arg, |
| 1645 | int expected_status_arg) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1646 | { |
| 1647 | psa_key_type_t type = type_arg; |
| 1648 | size_t byte_size = byte_size_arg; |
| 1649 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1650 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1651 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1652 | psa_status_t status; |
| 1653 | uint8_t *buffer = NULL; |
| 1654 | size_t buffer_size = byte_size + 1; |
| 1655 | size_t n; |
| 1656 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1657 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1658 | * accommodate large keys due to heap size constraints */ |
Tom Cosgrove | 412a813 | 2023-07-20 16:55:14 +0100 | [diff] [blame] | 1659 | TEST_CALLOC_OR_SKIP(buffer, buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1660 | memset(buffer, 'K', byte_size); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1662 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1663 | |
| 1664 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1665 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1666 | psa_set_key_type(&attributes, type); |
| 1667 | status = psa_import_key(&attributes, buffer, byte_size, &key); |
| 1668 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 1669 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1671 | if (status == PSA_SUCCESS) { |
| 1672 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1673 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 1674 | TEST_EQUAL(psa_get_key_bits(&attributes), |
| 1675 | PSA_BYTES_TO_BITS(byte_size)); |
| 1676 | ASSERT_NO_SLOT_NUMBER(&attributes); |
| 1677 | memset(buffer, 0, byte_size + 1); |
| 1678 | PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n)); |
| 1679 | for (n = 0; n < byte_size; n++) { |
| 1680 | TEST_EQUAL(buffer[n], 'K'); |
| 1681 | } |
| 1682 | for (n = byte_size; n < buffer_size; n++) { |
| 1683 | TEST_EQUAL(buffer[n], 0); |
| 1684 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1688 | /* |
| 1689 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1690 | * thus reset them as required. |
| 1691 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1692 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1694 | psa_destroy_key(key); |
| 1695 | PSA_DONE(); |
| 1696 | mbedtls_free(buffer); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1697 | } |
| 1698 | /* END_CASE */ |
| 1699 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1700 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 07510f5 | 2022-11-11 16:37:16 +0100 | [diff] [blame] | 1701 | /* Import an RSA key with a valid structure (but not valid numbers |
| 1702 | * inside, beyond having sensible size and parity). This is expected to |
| 1703 | * fail for large keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1704 | 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] | 1705 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1706 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1707 | size_t bits = bits_arg; |
| 1708 | psa_status_t expected_status = expected_status_arg; |
| 1709 | psa_status_t status; |
| 1710 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1711 | 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] | 1712 | size_t buffer_size = /* Slight overapproximations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1713 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1714 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1715 | unsigned char *p; |
| 1716 | int ret; |
| 1717 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1718 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1720 | PSA_ASSERT(psa_crypto_init()); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1721 | TEST_CALLOC(buffer, buffer_size); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1723 | TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p, |
| 1724 | bits, keypair)) >= 0); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1725 | length = ret; |
| 1726 | |
| 1727 | /* Try importing the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1728 | psa_set_key_type(&attributes, type); |
| 1729 | status = psa_import_key(&attributes, p, length, &key); |
| 1730 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1732 | if (status == PSA_SUCCESS) { |
| 1733 | PSA_ASSERT(psa_destroy_key(key)); |
| 1734 | } |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1735 | |
| 1736 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1737 | mbedtls_free(buffer); |
| 1738 | PSA_DONE(); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1739 | } |
| 1740 | /* END_CASE */ |
| 1741 | |
| 1742 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1743 | void import_export(data_t *data, |
| 1744 | int type_arg, |
| 1745 | int usage_arg, int alg_arg, |
| 1746 | int lifetime_arg, |
| 1747 | int expected_bits, |
| 1748 | int export_size_delta, |
| 1749 | int expected_export_status_arg, |
| 1750 | /*whether reexport must give the original input exactly*/ |
| 1751 | int canonical_input) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1752 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1753 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1754 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1755 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1756 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1757 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1758 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1759 | unsigned char *exported = NULL; |
| 1760 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1761 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1762 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1763 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1764 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1765 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1766 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1767 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1768 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1769 | if (!canonical_input) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1770 | TEST_CALLOC(reexported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | } |
| 1772 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1774 | psa_set_key_lifetime(&attributes, lifetime); |
| 1775 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 1776 | psa_set_key_algorithm(&attributes, alg); |
| 1777 | psa_set_key_type(&attributes, type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1778 | |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1779 | if (PSA_KEY_TYPE_IS_DH(type) && |
| 1780 | expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) { |
Przemek Stekiel | 654bef0 | 2022-12-15 13:28:02 +0100 | [diff] [blame] | 1781 | /* Simulate that buffer is too small, by decreasing its size by 1 byte. */ |
| 1782 | export_size -= 1; |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1783 | } |
| 1784 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1785 | /* Import the key */ |
Przemek Stekiel | 1d9c2b6 | 2022-12-01 15:01:39 +0100 | [diff] [blame] | 1786 | TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key), |
Przemek Stekiel | 2e7c33d | 2023-04-27 12:29:45 +0200 | [diff] [blame] | 1787 | PSA_SUCCESS); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1788 | |
| 1789 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1790 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 1791 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 1792 | TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits); |
| 1793 | ASSERT_NO_SLOT_NUMBER(&got_attributes); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1794 | |
| 1795 | /* Export the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1796 | status = psa_export_key(key, exported, export_size, &exported_length); |
| 1797 | TEST_EQUAL(status, expected_export_status); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1798 | |
| 1799 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1800 | * and export_size. On errors, the exported length must be 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1801 | TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH); |
| 1802 | TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0); |
| 1803 | TEST_LE_U(exported_length, export_size); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1805 | TEST_ASSERT(mem_is_char(exported + exported_length, 0, |
| 1806 | export_size - exported_length)); |
| 1807 | if (status != PSA_SUCCESS) { |
| 1808 | TEST_EQUAL(exported_length, 0); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1809 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1810 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1811 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1812 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1813 | * this validates the canonical representations. For canonical inputs, |
| 1814 | * this doesn't directly validate the implementation, but it still helps |
| 1815 | * by cross-validating the test data with the sanity check code. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1816 | if (!psa_key_lifetime_is_external(lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 1817 | if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0, 0)) { |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1818 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1819 | } |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1820 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1822 | if (canonical_input) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1823 | TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1824 | } else { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1825 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1826 | PSA_ASSERT(psa_import_key(&attributes, exported, exported_length, |
| 1827 | &key2)); |
| 1828 | PSA_ASSERT(psa_export_key(key2, |
| 1829 | reexported, |
| 1830 | export_size, |
| 1831 | &reexported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1832 | TEST_MEMORY_COMPARE(exported, exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1833 | reexported, reexported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1834 | PSA_ASSERT(psa_destroy_key(key2)); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1835 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1836 | TEST_LE_U(exported_length, |
| 1837 | PSA_EXPORT_KEY_OUTPUT_SIZE(type, |
| 1838 | psa_get_key_bits(&got_attributes))); |
Valerio Setti | 1eacae8 | 2023-07-28 16:07:03 +0200 | [diff] [blame] | 1839 | if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) { |
| 1840 | TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE); |
| 1841 | } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) { |
| 1842 | TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
| 1843 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1844 | |
| 1845 | destroy: |
| 1846 | /* Destroy the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | PSA_ASSERT(psa_destroy_key(key)); |
| 1848 | test_operations_on_invalid_key(key); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1849 | |
| 1850 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1851 | /* |
| 1852 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1853 | * thus reset them as required. |
| 1854 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1855 | psa_reset_key_attributes(&got_attributes); |
| 1856 | psa_destroy_key(key); |
| 1857 | mbedtls_free(exported); |
| 1858 | mbedtls_free(reexported); |
| 1859 | PSA_DONE(); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1860 | } |
| 1861 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1862 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1863 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1864 | void import_export_public_key(data_t *data, |
| 1865 | int type_arg, // key pair or public key |
| 1866 | int alg_arg, |
| 1867 | int lifetime_arg, |
| 1868 | int export_size_delta, |
| 1869 | int expected_export_status_arg, |
| 1870 | data_t *expected_public_key) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1871 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1872 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1873 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1874 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1875 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1876 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1877 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1878 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1879 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1880 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1881 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1885 | psa_set_key_lifetime(&attributes, lifetime); |
| 1886 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT); |
| 1887 | psa_set_key_algorithm(&attributes, alg); |
| 1888 | psa_set_key_type(&attributes, type); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1889 | |
| 1890 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1891 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1892 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1893 | /* Export the public key */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1894 | TEST_CALLOC(exported, export_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1895 | status = psa_export_public_key(key, |
| 1896 | exported, export_size, |
| 1897 | &exported_length); |
| 1898 | TEST_EQUAL(status, expected_export_status); |
| 1899 | if (status == PSA_SUCCESS) { |
| 1900 | 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] | 1901 | size_t bits; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1902 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 1903 | bits = psa_get_key_bits(&attributes); |
| 1904 | TEST_LE_U(expected_public_key->len, |
| 1905 | PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1906 | TEST_LE_U(expected_public_key->len, |
| 1907 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits)); |
| 1908 | TEST_LE_U(expected_public_key->len, |
| 1909 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1910 | TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1911 | exported, exported_length); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1912 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1913 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1914 | /* |
| 1915 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1916 | * thus reset them as required. |
| 1917 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1918 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1920 | mbedtls_free(exported); |
| 1921 | psa_destroy_key(key); |
| 1922 | PSA_DONE(); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1923 | } |
| 1924 | /* END_CASE */ |
| 1925 | |
Ryan Everett | 5061999 | 2024-03-12 16:55:14 +0000 | [diff] [blame] | 1926 | |
| 1927 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 1928 | /* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 1929 | void concurrently_use_same_persistent_key(data_t *data, |
| 1930 | int type_arg, |
| 1931 | int bits_arg, |
| 1932 | int alg_arg, |
| 1933 | int thread_count_arg) |
| 1934 | { |
| 1935 | size_t thread_count = (size_t) thread_count_arg; |
| 1936 | mbedtls_test_thread_t *threads = NULL; |
| 1937 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1); |
| 1938 | same_key_context skc; |
| 1939 | skc.data = data; |
| 1940 | skc.key = key_id; |
| 1941 | skc.type = type_arg; |
| 1942 | skc.bits = bits_arg; |
| 1943 | skc.key_loaded = 0; |
| 1944 | mbedtls_mutex_init(&skc.key_loaded_mutex); |
| 1945 | psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(skc.type, alg_arg); |
| 1946 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1947 | |
| 1948 | PSA_ASSERT(psa_crypto_init()); |
| 1949 | |
| 1950 | psa_set_key_id(&attributes, key_id); |
| 1951 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_PERSISTENT); |
| 1952 | psa_set_key_usage_flags(&attributes, usage); |
| 1953 | psa_set_key_algorithm(&attributes, alg_arg); |
| 1954 | psa_set_key_type(&attributes, type_arg); |
| 1955 | psa_set_key_bits(&attributes, bits_arg); |
| 1956 | skc.attributes = &attributes; |
| 1957 | |
| 1958 | TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count); |
| 1959 | |
| 1960 | /* Test that when multiple threads import the same key, |
| 1961 | * exactly one thread succeeds and the rest fail with valid errors. |
| 1962 | * Also test that all threads can use the key as soon as it has been |
| 1963 | * imported. */ |
| 1964 | for (size_t i = 0; i < thread_count; i++) { |
| 1965 | TEST_EQUAL( |
| 1966 | mbedtls_test_thread_create(&threads[i], thread_import_key, |
| 1967 | (void *) &skc), 0); |
| 1968 | } |
| 1969 | |
| 1970 | /* Join threads. */ |
| 1971 | for (size_t i = 0; i < thread_count; i++) { |
| 1972 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 1973 | } |
| 1974 | |
| 1975 | /* Test that when multiple threads use and destroy a key no corruption |
| 1976 | * occurs, and exactly one thread succeeds when destroying the key. */ |
| 1977 | for (size_t i = 0; i < thread_count; i++) { |
| 1978 | TEST_EQUAL( |
| 1979 | mbedtls_test_thread_create(&threads[i], thread_use_and_destroy_key, |
| 1980 | (void *) &skc), 0); |
| 1981 | } |
| 1982 | |
| 1983 | /* Join threads. */ |
| 1984 | for (size_t i = 0; i < thread_count; i++) { |
| 1985 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 1986 | } |
| 1987 | /* Ensure that one thread succeeded in destroying the key. */ |
| 1988 | TEST_ASSERT(!skc.key_loaded); |
| 1989 | exit: |
| 1990 | psa_reset_key_attributes(&attributes); |
| 1991 | mbedtls_mutex_free(&skc.key_loaded_mutex); |
| 1992 | mbedtls_free(threads); |
| 1993 | PSA_DONE(); |
| 1994 | } |
| 1995 | /* END_CASE */ |
| 1996 | #endif |
| 1997 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1998 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1999 | void import_and_exercise_key(data_t *data, |
| 2000 | int type_arg, |
| 2001 | int bits_arg, |
| 2002 | int alg_arg) |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2003 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2004 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2005 | psa_key_type_t type = type_arg; |
| 2006 | size_t bits = bits_arg; |
| 2007 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2008 | 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] | 2009 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2010 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2014 | psa_set_key_usage_flags(&attributes, usage); |
| 2015 | psa_set_key_algorithm(&attributes, alg); |
| 2016 | psa_set_key_type(&attributes, type); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2017 | |
| 2018 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2019 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key)); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2020 | |
| 2021 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2022 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2023 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 2024 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2025 | |
| 2026 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2027 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 2028 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2029 | } |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2031 | PSA_ASSERT(psa_destroy_key(key)); |
| 2032 | test_operations_on_invalid_key(key); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 2033 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2034 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2035 | /* |
| 2036 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2037 | * thus reset them as required. |
| 2038 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2039 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2041 | psa_reset_key_attributes(&attributes); |
| 2042 | psa_destroy_key(key); |
| 2043 | PSA_DONE(); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 2044 | } |
| 2045 | /* END_CASE */ |
| 2046 | |
| 2047 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2048 | void effective_key_attributes(int type_arg, int expected_type_arg, |
| 2049 | int bits_arg, int expected_bits_arg, |
| 2050 | int usage_arg, int expected_usage_arg, |
| 2051 | int alg_arg, int expected_alg_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2052 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2053 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 2054 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2055 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 2056 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2057 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2058 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2059 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2060 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2061 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2062 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2066 | psa_set_key_usage_flags(&attributes, usage); |
| 2067 | psa_set_key_algorithm(&attributes, alg); |
| 2068 | psa_set_key_type(&attributes, key_type); |
| 2069 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2071 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
| 2072 | psa_reset_key_attributes(&attributes); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2074 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2075 | TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type); |
| 2076 | TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits); |
| 2077 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
| 2078 | TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2079 | |
| 2080 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2081 | /* |
| 2082 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2083 | * thus reset them as required. |
| 2084 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2085 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2087 | psa_destroy_key(key); |
| 2088 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2089 | } |
| 2090 | /* END_CASE */ |
| 2091 | |
| 2092 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2093 | void check_key_policy(int type_arg, int bits_arg, |
| 2094 | int usage_arg, int alg_arg) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2095 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2096 | test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg, |
| 2097 | usage_arg, |
| 2098 | mbedtls_test_update_key_usage_flags(usage_arg), |
| 2099 | alg_arg, alg_arg); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 2100 | goto exit; |
| 2101 | } |
| 2102 | /* END_CASE */ |
| 2103 | |
| 2104 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2105 | void key_attributes_init() |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2106 | { |
| 2107 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2108 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2109 | * 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] | 2110 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2111 | psa_key_attributes_t func = psa_key_attributes_init(); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2112 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 2113 | psa_key_attributes_t 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 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2117 | TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE); |
| 2118 | TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE); |
| 2119 | TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2121 | TEST_EQUAL(psa_get_key_type(&func), 0); |
| 2122 | TEST_EQUAL(psa_get_key_type(&init), 0); |
| 2123 | TEST_EQUAL(psa_get_key_type(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2125 | TEST_EQUAL(psa_get_key_bits(&func), 0); |
| 2126 | TEST_EQUAL(psa_get_key_bits(&init), 0); |
| 2127 | TEST_EQUAL(psa_get_key_bits(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2129 | TEST_EQUAL(psa_get_key_usage_flags(&func), 0); |
| 2130 | TEST_EQUAL(psa_get_key_usage_flags(&init), 0); |
| 2131 | TEST_EQUAL(psa_get_key_usage_flags(&zero), 0); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2133 | TEST_EQUAL(psa_get_key_algorithm(&func), 0); |
| 2134 | TEST_EQUAL(psa_get_key_algorithm(&init), 0); |
| 2135 | TEST_EQUAL(psa_get_key_algorithm(&zero), 0); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 2136 | } |
| 2137 | /* END_CASE */ |
| 2138 | |
| 2139 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2140 | void mac_key_policy(int policy_usage_arg, |
| 2141 | int policy_alg_arg, |
| 2142 | int key_type_arg, |
| 2143 | data_t *key_data, |
| 2144 | int exercise_alg_arg, |
| 2145 | int expected_status_sign_arg, |
| 2146 | int expected_status_verify_arg) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2147 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2148 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2149 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2150 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2151 | psa_key_type_t key_type = key_type_arg; |
| 2152 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 2153 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 2154 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2155 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 2156 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 2157 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2158 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
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_ASSERT(psa_crypto_init()); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2162 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2163 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2164 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2166 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2167 | &key)); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2169 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 2170 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2172 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2173 | TEST_EQUAL(status, expected_status_sign); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2174 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2175 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2176 | uint8_t input[128] = { 0 }; |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2177 | size_t mac_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2178 | TEST_EQUAL(psa_mac_compute(key, exercise_alg, |
| 2179 | input, 128, |
| 2180 | mac, PSA_MAC_MAX_SIZE, &mac_len), |
| 2181 | expected_status_sign); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2182 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2183 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2184 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 2185 | status = psa_mac_sign_setup(&operation, key, exercise_alg); |
| 2186 | if (status == PSA_SUCCESS) { |
| 2187 | status = psa_mac_update(&operation, input, 128); |
| 2188 | if (status == PSA_SUCCESS) { |
| 2189 | TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE, |
| 2190 | &mac_len), |
| 2191 | expected_status_sign); |
| 2192 | } else { |
| 2193 | TEST_EQUAL(status, expected_status_sign); |
| 2194 | } |
| 2195 | } else { |
| 2196 | TEST_EQUAL(status, expected_status_sign); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2197 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2198 | PSA_ASSERT(psa_mac_abort(&operation)); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2199 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2200 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2201 | status = psa_mac_verify(key, exercise_alg, input, 128, |
| 2202 | mac, mac_len); |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 2203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2204 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2205 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2206 | } else { |
| 2207 | TEST_EQUAL(status, expected_status_verify); |
| 2208 | } |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2209 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2210 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2211 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2212 | if (status == PSA_SUCCESS) { |
| 2213 | status = psa_mac_update(&operation, input, 128); |
| 2214 | if (status == PSA_SUCCESS) { |
| 2215 | status = psa_mac_verify_finish(&operation, mac, mac_len); |
| 2216 | if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) { |
| 2217 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2218 | } else { |
| 2219 | TEST_EQUAL(status, expected_status_verify); |
| 2220 | } |
| 2221 | } else { |
| 2222 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2223 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2224 | } else { |
| 2225 | TEST_EQUAL(status, expected_status_verify); |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 2226 | } |
| 2227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2228 | psa_mac_abort(&operation); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2230 | memset(mac, 0, sizeof(mac)); |
| 2231 | status = psa_mac_verify_setup(&operation, key, exercise_alg); |
| 2232 | TEST_EQUAL(status, expected_status_verify); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2233 | |
| 2234 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2235 | psa_mac_abort(&operation); |
| 2236 | psa_destroy_key(key); |
| 2237 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2238 | } |
| 2239 | /* END_CASE */ |
| 2240 | |
| 2241 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2242 | void cipher_key_policy(int policy_usage_arg, |
| 2243 | int policy_alg, |
| 2244 | int key_type, |
| 2245 | data_t *key_data, |
| 2246 | int exercise_alg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2247 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2248 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2249 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2250 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2251 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2252 | size_t output_buffer_size = 0; |
| 2253 | size_t input_buffer_size = 0; |
| 2254 | size_t output_length = 0; |
| 2255 | uint8_t *output = NULL; |
| 2256 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2257 | psa_status_t status; |
| 2258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2259 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg); |
| 2260 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg, |
| 2261 | input_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2262 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2263 | TEST_CALLOC(input, input_buffer_size); |
| 2264 | TEST_CALLOC(output, output_buffer_size); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2266 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2268 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2269 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2270 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2272 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2273 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2274 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2275 | /* Check if no key usage flag implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2276 | TEST_EQUAL(policy_usage, |
| 2277 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2278 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2279 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2280 | status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size, |
| 2281 | output, output_buffer_size, |
| 2282 | &output_length); |
| 2283 | if (policy_alg == exercise_alg && |
| 2284 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2285 | PSA_ASSERT(status); |
| 2286 | } else { |
| 2287 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2288 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2289 | |
| 2290 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2291 | status = psa_cipher_encrypt_setup(&operation, key, exercise_alg); |
| 2292 | if (policy_alg == exercise_alg && |
| 2293 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2294 | PSA_ASSERT(status); |
| 2295 | } else { |
| 2296 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2297 | } |
| 2298 | psa_cipher_abort(&operation); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2299 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2300 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2301 | status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size, |
| 2302 | input, input_buffer_size, |
| 2303 | &output_length); |
| 2304 | if (policy_alg == exercise_alg && |
| 2305 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2306 | PSA_ASSERT(status); |
| 2307 | } else { |
| 2308 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2309 | } |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2310 | |
| 2311 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2312 | status = psa_cipher_decrypt_setup(&operation, key, exercise_alg); |
| 2313 | if (policy_alg == exercise_alg && |
| 2314 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2315 | PSA_ASSERT(status); |
| 2316 | } else { |
| 2317 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2318 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2319 | |
| 2320 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2321 | psa_cipher_abort(&operation); |
| 2322 | mbedtls_free(input); |
| 2323 | mbedtls_free(output); |
| 2324 | psa_destroy_key(key); |
| 2325 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2326 | } |
| 2327 | /* END_CASE */ |
| 2328 | |
| 2329 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2330 | void aead_key_policy(int policy_usage_arg, |
| 2331 | int policy_alg, |
| 2332 | int key_type, |
| 2333 | data_t *key_data, |
| 2334 | int nonce_length_arg, |
| 2335 | int tag_length_arg, |
| 2336 | int exercise_alg, |
| 2337 | int expected_status_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2338 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2339 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2340 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2341 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2342 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2343 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2344 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2345 | unsigned char nonce[16] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2346 | size_t nonce_length = nonce_length_arg; |
| 2347 | unsigned char tag[16]; |
| 2348 | size_t tag_length = tag_length_arg; |
| 2349 | size_t output_length; |
| 2350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2351 | TEST_LE_U(nonce_length, sizeof(nonce)); |
| 2352 | TEST_LE_U(tag_length, sizeof(tag)); |
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_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2356 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2357 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2358 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2360 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2361 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2362 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2363 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2364 | TEST_EQUAL(policy_usage, |
| 2365 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2366 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2367 | /* Encrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2368 | status = psa_aead_encrypt(key, exercise_alg, |
| 2369 | nonce, nonce_length, |
| 2370 | NULL, 0, |
| 2371 | NULL, 0, |
| 2372 | tag, tag_length, |
| 2373 | &output_length); |
| 2374 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2375 | TEST_EQUAL(status, expected_status); |
| 2376 | } else { |
| 2377 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2378 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2379 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2380 | /* Encrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2381 | status = psa_aead_encrypt_setup(&operation, key, exercise_alg); |
| 2382 | if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2383 | TEST_EQUAL(status, expected_status); |
| 2384 | } else { |
| 2385 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2386 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2387 | |
| 2388 | /* Decrypt check, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2389 | memset(tag, 0, sizeof(tag)); |
| 2390 | status = psa_aead_decrypt(key, exercise_alg, |
| 2391 | nonce, nonce_length, |
| 2392 | NULL, 0, |
| 2393 | tag, tag_length, |
| 2394 | NULL, 0, |
| 2395 | &output_length); |
| 2396 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2397 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2398 | } else if (expected_status == PSA_SUCCESS) { |
| 2399 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2400 | } else { |
| 2401 | TEST_EQUAL(status, expected_status); |
| 2402 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2403 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2404 | /* Decrypt check, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2405 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2406 | status = psa_aead_decrypt_setup(&operation, key, exercise_alg); |
| 2407 | if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) { |
| 2408 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2409 | } else { |
| 2410 | TEST_EQUAL(status, expected_status); |
| 2411 | } |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2412 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2413 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2414 | PSA_ASSERT(psa_aead_abort(&operation)); |
| 2415 | psa_destroy_key(key); |
| 2416 | PSA_DONE(); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2417 | } |
| 2418 | /* END_CASE */ |
| 2419 | |
| 2420 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2421 | void asymmetric_encryption_key_policy(int policy_usage_arg, |
| 2422 | int policy_alg, |
| 2423 | int key_type, |
| 2424 | data_t *key_data, |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2425 | int exercise_alg, |
| 2426 | int use_opaque_key) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2427 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2428 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2429 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2430 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2431 | psa_status_t status; |
| 2432 | size_t key_bits; |
| 2433 | size_t buffer_length; |
| 2434 | unsigned char *buffer = NULL; |
| 2435 | size_t output_length; |
| 2436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2437 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2439 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2440 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2441 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2442 | |
Valerio Setti | f202c29 | 2024-01-15 10:42:37 +0100 | [diff] [blame] | 2443 | if (use_opaque_key) { |
| 2444 | psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 2445 | PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION)); |
| 2446 | } |
| 2447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2449 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2450 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2451 | /* Check if no key usage implication is done */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | TEST_EQUAL(policy_usage, |
| 2453 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2455 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 2456 | key_bits = psa_get_key_bits(&attributes); |
| 2457 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, |
| 2458 | exercise_alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2459 | TEST_CALLOC(buffer, buffer_length); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2461 | status = psa_asymmetric_encrypt(key, exercise_alg, |
| 2462 | NULL, 0, |
| 2463 | NULL, 0, |
| 2464 | buffer, buffer_length, |
| 2465 | &output_length); |
| 2466 | if (policy_alg == exercise_alg && |
| 2467 | (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) { |
| 2468 | PSA_ASSERT(status); |
| 2469 | } else { |
| 2470 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2471 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | if (buffer_length != 0) { |
| 2474 | memset(buffer, 0, buffer_length); |
| 2475 | } |
| 2476 | status = psa_asymmetric_decrypt(key, exercise_alg, |
| 2477 | buffer, buffer_length, |
| 2478 | NULL, 0, |
| 2479 | buffer, buffer_length, |
| 2480 | &output_length); |
| 2481 | if (policy_alg == exercise_alg && |
| 2482 | (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) { |
| 2483 | TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING); |
| 2484 | } else { |
| 2485 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2486 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2487 | |
| 2488 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2489 | /* |
| 2490 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2491 | * thus reset them as required. |
| 2492 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2493 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2495 | psa_destroy_key(key); |
| 2496 | PSA_DONE(); |
| 2497 | mbedtls_free(buffer); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2498 | } |
| 2499 | /* END_CASE */ |
| 2500 | |
| 2501 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2502 | void asymmetric_signature_key_policy(int policy_usage_arg, |
| 2503 | int policy_alg, |
| 2504 | int key_type, |
| 2505 | data_t *key_data, |
| 2506 | int exercise_alg, |
| 2507 | int payload_length_arg, |
| 2508 | int expected_usage_arg) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2509 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2510 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2511 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2512 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2513 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2514 | psa_status_t status; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2515 | unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 }; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2516 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2517 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2518 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2519 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2520 | int compatible_alg = payload_length_arg > 0; |
| 2521 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2522 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 }; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2523 | size_t signature_length; |
| 2524 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2525 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2526 | in the expected usage flags. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2527 | TEST_EQUAL(expected_usage, |
| 2528 | mbedtls_test_update_key_usage_flags(policy_usage)); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2530 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2532 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2533 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2534 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2536 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2537 | &key)); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2539 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2541 | status = psa_sign_hash(key, exercise_alg, |
| 2542 | payload, payload_length, |
| 2543 | signature, sizeof(signature), |
| 2544 | &signature_length); |
| 2545 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) { |
| 2546 | PSA_ASSERT(status); |
| 2547 | } else { |
| 2548 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2549 | } |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2550 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2551 | memset(signature, 0, sizeof(signature)); |
| 2552 | status = psa_verify_hash(key, exercise_alg, |
| 2553 | payload, payload_length, |
| 2554 | signature, sizeof(signature)); |
| 2555 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) { |
| 2556 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2557 | } else { |
| 2558 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2559 | } |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2561 | if (PSA_ALG_IS_SIGN_HASH(exercise_alg) && |
| 2562 | PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) { |
| 2563 | status = psa_sign_message(key, exercise_alg, |
| 2564 | payload, payload_length, |
| 2565 | signature, sizeof(signature), |
| 2566 | &signature_length); |
| 2567 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) { |
| 2568 | PSA_ASSERT(status); |
| 2569 | } else { |
| 2570 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2571 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2573 | memset(signature, 0, sizeof(signature)); |
| 2574 | status = psa_verify_message(key, exercise_alg, |
| 2575 | payload, payload_length, |
| 2576 | signature, sizeof(signature)); |
| 2577 | if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) { |
| 2578 | TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE); |
| 2579 | } else { |
| 2580 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2581 | } |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2582 | } |
| 2583 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2584 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2585 | psa_destroy_key(key); |
| 2586 | PSA_DONE(); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2587 | } |
| 2588 | /* END_CASE */ |
| 2589 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2590 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2591 | void derive_key_policy(int policy_usage, |
| 2592 | int policy_alg, |
| 2593 | int key_type, |
| 2594 | data_t *key_data, |
| 2595 | int exercise_alg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2596 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2597 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2598 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2599 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2600 | psa_status_t status; |
| 2601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2602 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2604 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2605 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2606 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2608 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2609 | &key)); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2611 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2613 | if (PSA_ALG_IS_TLS12_PRF(exercise_alg) || |
| 2614 | PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) { |
| 2615 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 2616 | &operation, |
| 2617 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2618 | (const uint8_t *) "", 0)); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2619 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2621 | status = psa_key_derivation_input_key(&operation, |
| 2622 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 2623 | key); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2625 | if (policy_alg == exercise_alg && |
| 2626 | (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) { |
| 2627 | PSA_ASSERT(status); |
| 2628 | } else { |
| 2629 | TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED); |
| 2630 | } |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2631 | |
| 2632 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2633 | psa_key_derivation_abort(&operation); |
| 2634 | psa_destroy_key(key); |
| 2635 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2636 | } |
| 2637 | /* END_CASE */ |
| 2638 | |
| 2639 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | void agreement_key_policy(int policy_usage, |
| 2641 | int policy_alg, |
| 2642 | int key_type_arg, |
| 2643 | data_t *key_data, |
| 2644 | int exercise_alg, |
| 2645 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2646 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2647 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2648 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2649 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2650 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2651 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2652 | psa_status_t expected_status = expected_status_arg; |
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_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2656 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2657 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2658 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2660 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2661 | &key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2663 | PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg)); |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 2664 | status = mbedtls_test_psa_key_agreement_with_self(&operation, key, 0); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2666 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2667 | |
| 2668 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2669 | psa_key_derivation_abort(&operation); |
| 2670 | psa_destroy_key(key); |
| 2671 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2672 | } |
| 2673 | /* END_CASE */ |
| 2674 | |
| 2675 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2676 | void key_policy_alg2(int key_type_arg, data_t *key_data, |
| 2677 | int usage_arg, int alg_arg, int alg2_arg) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2678 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2679 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2680 | psa_key_type_t key_type = key_type_arg; |
| 2681 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2682 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2683 | psa_key_usage_t usage = usage_arg; |
| 2684 | psa_algorithm_t alg = alg_arg; |
| 2685 | psa_algorithm_t alg2 = alg2_arg; |
| 2686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2687 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2689 | psa_set_key_usage_flags(&attributes, usage); |
| 2690 | psa_set_key_algorithm(&attributes, alg); |
| 2691 | psa_set_key_enrollment_algorithm(&attributes, alg2); |
| 2692 | psa_set_key_type(&attributes, key_type); |
| 2693 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2694 | &key)); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2695 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2696 | /* Update the usage flags to obtain implicit usage flags */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2697 | usage = mbedtls_test_update_key_usage_flags(usage); |
| 2698 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 2699 | TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage); |
| 2700 | TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg); |
| 2701 | TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2702 | |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2703 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2704 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2705 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2706 | if (!mbedtls_test_psa_exercise_key(key, usage, alg2, 0)) { |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2707 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2708 | } |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2709 | |
| 2710 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2711 | /* |
| 2712 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2713 | * thus reset them as required. |
| 2714 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2715 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2717 | psa_destroy_key(key); |
| 2718 | PSA_DONE(); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2719 | } |
| 2720 | /* END_CASE */ |
| 2721 | |
| 2722 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2723 | void raw_agreement_key_policy(int policy_usage, |
| 2724 | int policy_alg, |
| 2725 | int key_type_arg, |
| 2726 | data_t *key_data, |
| 2727 | int exercise_alg, |
| 2728 | int expected_status_arg) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2729 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2730 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2731 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2732 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2733 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2734 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2735 | psa_status_t expected_status = expected_status_arg; |
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_ASSERT(psa_crypto_init()); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2739 | psa_set_key_usage_flags(&attributes, policy_usage); |
| 2740 | psa_set_key_algorithm(&attributes, policy_alg); |
| 2741 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2743 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 2744 | &key)); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2745 | |
Ryan Everett | 8163028 | 2024-03-12 16:21:12 +0000 | [diff] [blame] | 2746 | 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] | 2747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2748 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2749 | |
| 2750 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2751 | psa_key_derivation_abort(&operation); |
| 2752 | psa_destroy_key(key); |
| 2753 | PSA_DONE(); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2754 | } |
| 2755 | /* END_CASE */ |
| 2756 | |
| 2757 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2758 | void copy_success(int source_usage_arg, |
| 2759 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2760 | int source_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2761 | int type_arg, data_t *material, |
| 2762 | int copy_attributes, |
| 2763 | int target_usage_arg, |
| 2764 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4ea4ad0 | 2022-12-04 15:11:00 +0100 | [diff] [blame] | 2765 | int target_lifetime_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2766 | int expected_usage_arg, |
| 2767 | int expected_alg_arg, int expected_alg2_arg) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2768 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2769 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2770 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2771 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2772 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2773 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2774 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2775 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2776 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2777 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2778 | uint8_t *export_buffer = NULL; |
| 2779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2780 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2781 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2782 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2783 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2784 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2785 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2786 | psa_set_key_type(&source_attributes, type_arg); |
| 2787 | psa_set_key_lifetime(&source_attributes, source_lifetime); |
| 2788 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2789 | material->x, material->len, |
| 2790 | &source_key)); |
| 2791 | PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2792 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2793 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2794 | if (copy_attributes) { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2795 | target_attributes = source_attributes; |
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 | psa_set_key_lifetime(&target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2799 | if (target_usage_arg != -1) { |
| 2800 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2801 | } |
| 2802 | if (target_alg_arg != -1) { |
| 2803 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2804 | } |
| 2805 | if (target_alg2_arg != -1) { |
| 2806 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
| 2807 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2808 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2809 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2810 | /* Copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2811 | PSA_ASSERT(psa_copy_key(source_key, |
| 2812 | &target_attributes, &target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2813 | |
| 2814 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2815 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2816 | |
| 2817 | /* Test that the target slot has the expected content and policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2818 | PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes)); |
| 2819 | TEST_EQUAL(psa_get_key_type(&source_attributes), |
| 2820 | psa_get_key_type(&target_attributes)); |
| 2821 | TEST_EQUAL(psa_get_key_bits(&source_attributes), |
| 2822 | psa_get_key_bits(&target_attributes)); |
| 2823 | TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes)); |
| 2824 | TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes)); |
| 2825 | TEST_EQUAL(expected_alg2, |
| 2826 | psa_get_key_enrollment_algorithm(&target_attributes)); |
| 2827 | if (expected_usage & PSA_KEY_USAGE_EXPORT) { |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2828 | size_t length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2829 | TEST_CALLOC(export_buffer, material->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2830 | PSA_ASSERT(psa_export_key(target_key, export_buffer, |
| 2831 | material->len, &length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 2832 | TEST_MEMORY_COMPARE(material->x, material->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 2833 | export_buffer, length); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2834 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2836 | if (!psa_key_lifetime_is_external(target_lifetime)) { |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2837 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2838 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2839 | } |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 2840 | if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2, 0)) { |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2841 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2842 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2843 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2845 | PSA_ASSERT(psa_destroy_key(target_key)); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2846 | |
| 2847 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2848 | /* |
| 2849 | * Source and target key attributes may have been returned by |
| 2850 | * psa_get_key_attributes() thus reset them as required. |
| 2851 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2852 | psa_reset_key_attributes(&source_attributes); |
| 2853 | psa_reset_key_attributes(&target_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2855 | PSA_DONE(); |
| 2856 | mbedtls_free(export_buffer); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2857 | } |
| 2858 | /* END_CASE */ |
| 2859 | |
| 2860 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2861 | void copy_fail(int source_usage_arg, |
| 2862 | int source_alg_arg, int source_alg2_arg, |
| 2863 | int source_lifetime_arg, |
| 2864 | int type_arg, data_t *material, |
| 2865 | int target_type_arg, int target_bits_arg, |
| 2866 | int target_usage_arg, |
| 2867 | int target_alg_arg, int target_alg2_arg, |
| 2868 | int target_id_arg, int target_lifetime_arg, |
| 2869 | int expected_status_arg) |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2870 | { |
| 2871 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2872 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2873 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2874 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | 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] | 2876 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2877 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2878 | |
| 2879 | /* Prepare the source key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2880 | psa_set_key_usage_flags(&source_attributes, source_usage_arg); |
| 2881 | psa_set_key_algorithm(&source_attributes, source_alg_arg); |
| 2882 | psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg); |
| 2883 | psa_set_key_type(&source_attributes, type_arg); |
| 2884 | psa_set_key_lifetime(&source_attributes, source_lifetime_arg); |
| 2885 | PSA_ASSERT(psa_import_key(&source_attributes, |
| 2886 | material->x, material->len, |
| 2887 | &source_key)); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2888 | |
| 2889 | /* Prepare the target attributes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2890 | psa_set_key_id(&target_attributes, key_id); |
| 2891 | psa_set_key_lifetime(&target_attributes, target_lifetime_arg); |
| 2892 | psa_set_key_type(&target_attributes, target_type_arg); |
| 2893 | psa_set_key_bits(&target_attributes, target_bits_arg); |
| 2894 | psa_set_key_usage_flags(&target_attributes, target_usage_arg); |
| 2895 | psa_set_key_algorithm(&target_attributes, target_alg_arg); |
| 2896 | psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2897 | |
| 2898 | /* Try to copy the key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | TEST_EQUAL(psa_copy_key(source_key, |
| 2900 | &target_attributes, &target_key), |
| 2901 | expected_status_arg); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2903 | PSA_ASSERT(psa_destroy_key(source_key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2904 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2905 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2906 | psa_reset_key_attributes(&source_attributes); |
| 2907 | psa_reset_key_attributes(&target_attributes); |
| 2908 | PSA_DONE(); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2909 | } |
| 2910 | /* END_CASE */ |
| 2911 | |
| 2912 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | void hash_operation_init() |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2914 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2915 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2916 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2917 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2918 | * 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] | 2919 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2920 | psa_hash_operation_t func = psa_hash_operation_init(); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2921 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2922 | psa_hash_operation_t zero; |
| 2923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2924 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2925 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2926 | /* A freshly-initialized hash operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2927 | TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)), |
| 2928 | PSA_ERROR_BAD_STATE); |
| 2929 | TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)), |
| 2930 | PSA_ERROR_BAD_STATE); |
| 2931 | TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)), |
| 2932 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2933 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2934 | /* A default hash operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2935 | PSA_ASSERT(psa_hash_abort(&func)); |
| 2936 | PSA_ASSERT(psa_hash_abort(&init)); |
| 2937 | PSA_ASSERT(psa_hash_abort(&zero)); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2938 | } |
| 2939 | /* END_CASE */ |
| 2940 | |
| 2941 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2942 | void hash_setup(int alg_arg, |
| 2943 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2944 | { |
| 2945 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2946 | uint8_t *output = NULL; |
| 2947 | size_t output_size = 0; |
| 2948 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2949 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2950 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2951 | psa_status_t status; |
| 2952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2953 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2954 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2955 | /* Hash Setup, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2956 | output_size = PSA_HASH_LENGTH(alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 2957 | TEST_CALLOC(output, output_size); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2959 | status = psa_hash_compute(alg, NULL, 0, |
| 2960 | output, output_size, &output_length); |
| 2961 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2962 | |
| 2963 | /* Hash Setup, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2964 | status = psa_hash_setup(&operation, alg); |
| 2965 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2966 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2967 | /* Whether setup succeeded or failed, abort must succeed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2968 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2969 | |
| 2970 | /* If setup failed, reproduce the failure, so as to |
| 2971 | * test the resulting state of the operation object. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2972 | if (status != PSA_SUCCESS) { |
| 2973 | TEST_EQUAL(psa_hash_setup(&operation, alg), status); |
| 2974 | } |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2975 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2976 | /* Now the operation object should be reusable. */ |
| 2977 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG)); |
| 2979 | PSA_ASSERT(psa_hash_abort(&operation)); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2980 | #endif |
| 2981 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2982 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2983 | mbedtls_free(output); |
| 2984 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2985 | } |
| 2986 | /* END_CASE */ |
| 2987 | |
| 2988 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2989 | void hash_compute_fail(int alg_arg, data_t *input, |
| 2990 | int output_size_arg, int expected_status_arg) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2991 | { |
| 2992 | psa_algorithm_t alg = alg_arg; |
| 2993 | uint8_t *output = NULL; |
| 2994 | size_t output_size = output_size_arg; |
| 2995 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2996 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2997 | psa_status_t expected_status = expected_status_arg; |
| 2998 | psa_status_t status; |
| 2999 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3000 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3002 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3003 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3004 | /* Hash Compute, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3005 | status = psa_hash_compute(alg, input->x, input->len, |
| 3006 | output, output_size, &output_length); |
| 3007 | TEST_EQUAL(status, expected_status); |
| 3008 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3009 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3010 | /* Hash Compute, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3011 | status = psa_hash_setup(&operation, alg); |
| 3012 | if (status == PSA_SUCCESS) { |
| 3013 | status = psa_hash_update(&operation, input->x, input->len); |
| 3014 | if (status == PSA_SUCCESS) { |
| 3015 | status = psa_hash_finish(&operation, output, output_size, |
| 3016 | &output_length); |
| 3017 | if (status == PSA_SUCCESS) { |
| 3018 | TEST_LE_U(output_length, output_size); |
| 3019 | } else { |
| 3020 | TEST_EQUAL(status, expected_status); |
| 3021 | } |
| 3022 | } else { |
| 3023 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3024 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3025 | } else { |
| 3026 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 3027 | } |
| 3028 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3029 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3030 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3031 | mbedtls_free(output); |
| 3032 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3033 | } |
| 3034 | /* END_CASE */ |
| 3035 | |
| 3036 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3037 | void hash_compare_fail(int alg_arg, data_t *input, |
| 3038 | data_t *reference_hash, |
| 3039 | int expected_status_arg) |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3040 | { |
| 3041 | psa_algorithm_t alg = alg_arg; |
| 3042 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3043 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3044 | psa_status_t status; |
| 3045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3046 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3047 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3048 | /* Hash Compare, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3049 | status = psa_hash_compare(alg, input->x, input->len, |
| 3050 | reference_hash->x, reference_hash->len); |
| 3051 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3052 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3053 | /* Hash Compare, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3054 | status = psa_hash_setup(&operation, alg); |
| 3055 | if (status == PSA_SUCCESS) { |
| 3056 | status = psa_hash_update(&operation, input->x, input->len); |
| 3057 | if (status == PSA_SUCCESS) { |
| 3058 | status = psa_hash_verify(&operation, reference_hash->x, |
| 3059 | reference_hash->len); |
| 3060 | TEST_EQUAL(status, expected_status); |
| 3061 | } else { |
| 3062 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3063 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3064 | } else { |
| 3065 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 3066 | } |
| 3067 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3068 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3069 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3070 | PSA_DONE(); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 3071 | } |
| 3072 | /* END_CASE */ |
| 3073 | |
| 3074 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3075 | void hash_compute_compare(int alg_arg, data_t *input, |
| 3076 | data_t *expected_output) |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3077 | { |
| 3078 | psa_algorithm_t alg = alg_arg; |
| 3079 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 3080 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3081 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3082 | size_t i; |
| 3083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3084 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3085 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3086 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3087 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 3088 | output, PSA_HASH_LENGTH(alg), |
| 3089 | &output_length)); |
| 3090 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3091 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3092 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3093 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3094 | /* Compute with tight buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3095 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3096 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3097 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 3098 | PSA_HASH_LENGTH(alg), |
| 3099 | &output_length)); |
| 3100 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3101 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3102 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3103 | |
| 3104 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3105 | PSA_ASSERT(psa_hash_compute(alg, input->x, input->len, |
| 3106 | output, sizeof(output), |
| 3107 | &output_length)); |
| 3108 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3109 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3110 | expected_output->x, expected_output->len); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3111 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3112 | /* Compute with larger buffer, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3113 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3114 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3115 | PSA_ASSERT(psa_hash_finish(&operation, output, |
| 3116 | sizeof(output), &output_length)); |
| 3117 | TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3118 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3119 | expected_output->x, expected_output->len); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3120 | |
| 3121 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3122 | PSA_ASSERT(psa_hash_compare(alg, input->x, input->len, |
| 3123 | output, output_length)); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3124 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3125 | /* Compare with correct hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3127 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3128 | PSA_ASSERT(psa_hash_verify(&operation, output, |
| 3129 | output_length)); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3130 | |
| 3131 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3132 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3133 | output, output_length + 1), |
| 3134 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3135 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3136 | /* Compare with trailing garbage, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3137 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3138 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3139 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1), |
| 3140 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3141 | |
| 3142 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3143 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3144 | output, output_length - 1), |
| 3145 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3146 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3147 | /* Compare with truncated hash, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3148 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3149 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3150 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1), |
| 3151 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3152 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3153 | /* Compare with corrupted value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3154 | for (i = 0; i < output_length; i++) { |
| 3155 | mbedtls_test_set_step(i); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3156 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3157 | |
| 3158 | /* One-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3159 | TEST_EQUAL(psa_hash_compare(alg, input->x, input->len, |
| 3160 | output, output_length), |
| 3161 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3162 | |
| 3163 | /* Multi-Part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3164 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3165 | PSA_ASSERT(psa_hash_update(&operation, input->x, input->len)); |
| 3166 | TEST_EQUAL(psa_hash_verify(&operation, output, output_length), |
| 3167 | PSA_ERROR_INVALID_SIGNATURE); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 3168 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3169 | output[i] ^= 1; |
| 3170 | } |
| 3171 | |
| 3172 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3173 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3174 | PSA_DONE(); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 3175 | } |
| 3176 | /* END_CASE */ |
| 3177 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3178 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3179 | void hash_bad_order() |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3180 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3181 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3182 | unsigned char input[] = ""; |
| 3183 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3184 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3185 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3186 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3187 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 3188 | }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3189 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3190 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3191 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3193 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3194 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3195 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3196 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3197 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3198 | TEST_EQUAL(psa_hash_setup(&operation, alg), |
| 3199 | PSA_ERROR_BAD_STATE); |
| 3200 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3201 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3202 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3203 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3204 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3205 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3206 | PSA_ERROR_BAD_STATE); |
| 3207 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3208 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3209 | /* Check that update calls abort on error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3210 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 3211 | operation.id = UINT_MAX; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3212 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3213 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3214 | PSA_ERROR_BAD_STATE); |
| 3215 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3216 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3217 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3218 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3219 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3220 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3221 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3222 | hash, sizeof(hash), &hash_len)); |
| 3223 | TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)), |
| 3224 | PSA_ERROR_BAD_STATE); |
| 3225 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3226 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3227 | /* Call verify without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3228 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3229 | valid_hash, sizeof(valid_hash)), |
| 3230 | PSA_ERROR_BAD_STATE); |
| 3231 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3232 | |
| 3233 | /* Call verify after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3234 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3235 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3236 | hash, sizeof(hash), &hash_len)); |
| 3237 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3238 | valid_hash, sizeof(valid_hash)), |
| 3239 | PSA_ERROR_BAD_STATE); |
| 3240 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3241 | |
| 3242 | /* Call verify twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3243 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3244 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3245 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3246 | valid_hash, sizeof(valid_hash))); |
| 3247 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3248 | TEST_EQUAL(psa_hash_verify(&operation, |
| 3249 | valid_hash, sizeof(valid_hash)), |
| 3250 | PSA_ERROR_BAD_STATE); |
| 3251 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3252 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3253 | |
| 3254 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3255 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3256 | hash, sizeof(hash), &hash_len), |
| 3257 | PSA_ERROR_BAD_STATE); |
| 3258 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3259 | |
| 3260 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3261 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3262 | PSA_ASSERT(psa_hash_finish(&operation, |
| 3263 | hash, sizeof(hash), &hash_len)); |
| 3264 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3265 | hash, sizeof(hash), &hash_len), |
| 3266 | PSA_ERROR_BAD_STATE); |
| 3267 | PSA_ASSERT(psa_hash_abort(&operation)); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 3268 | |
| 3269 | /* Call finish after calling verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3270 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3271 | PSA_ASSERT(psa_hash_verify(&operation, |
| 3272 | valid_hash, sizeof(valid_hash))); |
| 3273 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3274 | hash, sizeof(hash), &hash_len), |
| 3275 | PSA_ERROR_BAD_STATE); |
| 3276 | PSA_ASSERT(psa_hash_abort(&operation)); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3277 | |
| 3278 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3279 | PSA_DONE(); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 3280 | } |
| 3281 | /* END_CASE */ |
| 3282 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3283 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3284 | void hash_verify_bad_args() |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3285 | { |
| 3286 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3287 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 3288 | * appended to it */ |
| 3289 | unsigned char hash[] = { |
| 3290 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3291 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb |
| 3293 | }; |
| 3294 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3295 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3297 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3298 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3299 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3300 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3301 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3302 | TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1), |
| 3303 | PSA_ERROR_INVALID_SIGNATURE); |
| 3304 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3305 | PSA_ASSERT(psa_hash_abort(&operation)); |
| 3306 | ASSERT_OPERATION_IS_INACTIVE(operation); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3307 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3308 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3309 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3310 | TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size), |
| 3311 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3312 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3313 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3314 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3315 | TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)), |
| 3316 | PSA_ERROR_INVALID_SIGNATURE); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3317 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3318 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3319 | PSA_DONE(); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3320 | } |
| 3321 | /* END_CASE */ |
| 3322 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3323 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3324 | void hash_finish_bad_args() |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3325 | { |
| 3326 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3327 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3328 | size_t expected_size = PSA_HASH_LENGTH(alg); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3329 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3330 | size_t hash_len; |
| 3331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3332 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3333 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3334 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3335 | PSA_ASSERT(psa_hash_setup(&operation, alg)); |
| 3336 | TEST_EQUAL(psa_hash_finish(&operation, |
| 3337 | hash, expected_size - 1, &hash_len), |
| 3338 | PSA_ERROR_BUFFER_TOO_SMALL); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3339 | |
| 3340 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3341 | PSA_DONE(); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3342 | } |
| 3343 | /* END_CASE */ |
| 3344 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3345 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3346 | void hash_clone_source_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3347 | { |
| 3348 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3349 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3350 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3351 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3352 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3353 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3354 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3355 | size_t hash_len; |
| 3356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3357 | PSA_ASSERT(psa_crypto_init()); |
| 3358 | PSA_ASSERT(psa_hash_setup(&op_source, alg)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3360 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3361 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3362 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3363 | hash, sizeof(hash), &hash_len)); |
| 3364 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3365 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3367 | TEST_EQUAL(psa_hash_clone(&op_source, &op_setup), |
| 3368 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3370 | PSA_ASSERT(psa_hash_clone(&op_source, &op_init)); |
| 3371 | PSA_ASSERT(psa_hash_finish(&op_init, |
| 3372 | hash, sizeof(hash), &hash_len)); |
| 3373 | PSA_ASSERT(psa_hash_clone(&op_source, &op_finished)); |
| 3374 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3375 | hash, sizeof(hash), &hash_len)); |
| 3376 | PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted)); |
| 3377 | PSA_ASSERT(psa_hash_finish(&op_aborted, |
| 3378 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3379 | |
| 3380 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3381 | psa_hash_abort(&op_source); |
| 3382 | psa_hash_abort(&op_init); |
| 3383 | psa_hash_abort(&op_setup); |
| 3384 | psa_hash_abort(&op_finished); |
| 3385 | psa_hash_abort(&op_aborted); |
| 3386 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3387 | } |
| 3388 | /* END_CASE */ |
| 3389 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3390 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3391 | void hash_clone_target_state() |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3392 | { |
| 3393 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3394 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3395 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3396 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3397 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3398 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3399 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3400 | size_t hash_len; |
| 3401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3402 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3404 | PSA_ASSERT(psa_hash_setup(&op_setup, alg)); |
| 3405 | PSA_ASSERT(psa_hash_setup(&op_finished, alg)); |
| 3406 | PSA_ASSERT(psa_hash_finish(&op_finished, |
| 3407 | hash, sizeof(hash), &hash_len)); |
| 3408 | PSA_ASSERT(psa_hash_setup(&op_aborted, alg)); |
| 3409 | PSA_ASSERT(psa_hash_abort(&op_aborted)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3411 | PSA_ASSERT(psa_hash_clone(&op_setup, &op_target)); |
| 3412 | PSA_ASSERT(psa_hash_finish(&op_target, |
| 3413 | hash, sizeof(hash), &hash_len)); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3415 | TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE); |
| 3416 | TEST_EQUAL(psa_hash_clone(&op_finished, &op_target), |
| 3417 | PSA_ERROR_BAD_STATE); |
| 3418 | TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target), |
| 3419 | PSA_ERROR_BAD_STATE); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3420 | |
| 3421 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3422 | psa_hash_abort(&op_target); |
| 3423 | psa_hash_abort(&op_init); |
| 3424 | psa_hash_abort(&op_setup); |
| 3425 | psa_hash_abort(&op_finished); |
| 3426 | psa_hash_abort(&op_aborted); |
| 3427 | PSA_DONE(); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3428 | } |
| 3429 | /* END_CASE */ |
| 3430 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3431 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3432 | void mac_operation_init() |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3433 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3434 | const uint8_t input[1] = { 0 }; |
| 3435 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3436 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3437 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3438 | * 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] | 3439 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3440 | psa_mac_operation_t func = psa_mac_operation_init(); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3441 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3442 | psa_mac_operation_t zero; |
| 3443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3444 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3445 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3446 | /* A freshly-initialized MAC operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3447 | TEST_EQUAL(psa_mac_update(&func, |
| 3448 | input, sizeof(input)), |
| 3449 | PSA_ERROR_BAD_STATE); |
| 3450 | TEST_EQUAL(psa_mac_update(&init, |
| 3451 | input, sizeof(input)), |
| 3452 | PSA_ERROR_BAD_STATE); |
| 3453 | TEST_EQUAL(psa_mac_update(&zero, |
| 3454 | input, sizeof(input)), |
| 3455 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3456 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3457 | /* A default MAC operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3458 | PSA_ASSERT(psa_mac_abort(&func)); |
| 3459 | PSA_ASSERT(psa_mac_abort(&init)); |
| 3460 | PSA_ASSERT(psa_mac_abort(&zero)); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3461 | } |
| 3462 | /* END_CASE */ |
| 3463 | |
| 3464 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3465 | void mac_setup(int key_type_arg, |
| 3466 | data_t *key, |
| 3467 | int alg_arg, |
| 3468 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3469 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3470 | psa_key_type_t key_type = key_type_arg; |
| 3471 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3472 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3473 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3474 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3475 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3476 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3477 | #endif |
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 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3481 | if (!exercise_mac_setup(key_type, key->x, key->len, alg, |
| 3482 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3483 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3484 | } |
| 3485 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3486 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3487 | /* The operation object should be reusable. */ |
| 3488 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3489 | if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3490 | smoke_test_key_data, |
| 3491 | sizeof(smoke_test_key_data), |
| 3492 | KNOWN_SUPPORTED_MAC_ALG, |
| 3493 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3494 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3495 | } |
| 3496 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3497 | #endif |
| 3498 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3499 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3500 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3501 | } |
| 3502 | /* END_CASE */ |
| 3503 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3504 | /* 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] | 3505 | void mac_bad_order() |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3506 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3507 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3508 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3509 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3510 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3511 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3512 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3513 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa |
| 3514 | }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3515 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3516 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3517 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3518 | size_t sign_mac_length = 0; |
| 3519 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3520 | const uint8_t verify_mac[] = { |
| 3521 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3522 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3523 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 |
| 3524 | }; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3526 | PSA_ASSERT(psa_crypto_init()); |
| 3527 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 3528 | psa_set_key_algorithm(&attributes, alg); |
| 3529 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3531 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3532 | &key)); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3533 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3534 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3535 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3536 | PSA_ERROR_BAD_STATE); |
| 3537 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3538 | |
| 3539 | /* Call sign finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3540 | TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac), |
| 3541 | &sign_mac_length), |
| 3542 | PSA_ERROR_BAD_STATE); |
| 3543 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3544 | |
| 3545 | /* Call verify finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3546 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3547 | verify_mac, sizeof(verify_mac)), |
| 3548 | PSA_ERROR_BAD_STATE); |
| 3549 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3550 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3551 | /* Call setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3552 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3553 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3554 | TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg), |
| 3555 | PSA_ERROR_BAD_STATE); |
| 3556 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3557 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3558 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3559 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3560 | /* Call update after sign finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3561 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3562 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3563 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3564 | sign_mac, sizeof(sign_mac), |
| 3565 | &sign_mac_length)); |
| 3566 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3567 | PSA_ERROR_BAD_STATE); |
| 3568 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3569 | |
| 3570 | /* Call update after verify finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3571 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3572 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3573 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3574 | verify_mac, sizeof(verify_mac))); |
| 3575 | TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)), |
| 3576 | PSA_ERROR_BAD_STATE); |
| 3577 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3578 | |
| 3579 | /* Call sign finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3580 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3581 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3582 | PSA_ASSERT(psa_mac_sign_finish(&operation, |
| 3583 | sign_mac, sizeof(sign_mac), |
| 3584 | &sign_mac_length)); |
| 3585 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3586 | sign_mac, sizeof(sign_mac), |
| 3587 | &sign_mac_length), |
| 3588 | PSA_ERROR_BAD_STATE); |
| 3589 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3590 | |
| 3591 | /* Call verify finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3592 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3593 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3594 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3595 | verify_mac, sizeof(verify_mac))); |
| 3596 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3597 | verify_mac, sizeof(verify_mac)), |
| 3598 | PSA_ERROR_BAD_STATE); |
| 3599 | PSA_ASSERT(psa_mac_abort(&operation)); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3600 | |
| 3601 | /* Setup sign but try verify. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3602 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3603 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3604 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3605 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3606 | verify_mac, sizeof(verify_mac)), |
| 3607 | PSA_ERROR_BAD_STATE); |
| 3608 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3609 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3610 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3611 | |
| 3612 | /* Setup verify but try sign. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3613 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3614 | PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input))); |
| 3615 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3616 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3617 | sign_mac, sizeof(sign_mac), |
| 3618 | &sign_mac_length), |
| 3619 | PSA_ERROR_BAD_STATE); |
| 3620 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3621 | PSA_ASSERT(psa_mac_abort(&operation)); |
| 3622 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3624 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3625 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3626 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3627 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3628 | } |
| 3629 | /* END_CASE */ |
| 3630 | |
| 3631 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3632 | void mac_sign_verify_multi(int key_type_arg, |
| 3633 | data_t *key_data, |
| 3634 | int alg_arg, |
| 3635 | data_t *input, |
| 3636 | int is_verify, |
| 3637 | data_t *expected_mac) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3638 | { |
| 3639 | size_t data_part_len = 0; |
| 3640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3641 | 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] | 3642 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3643 | mbedtls_test_set_step(2000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3645 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3646 | alg_arg, |
| 3647 | input, data_part_len, |
| 3648 | expected_mac, |
| 3649 | is_verify, 0) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3650 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3651 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3652 | |
| 3653 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3654 | mbedtls_test_set_step(3000 + data_part_len); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3656 | if (mac_multipart_internal_func(key_type_arg, key_data, |
| 3657 | alg_arg, |
| 3658 | input, data_part_len, |
| 3659 | expected_mac, |
| 3660 | is_verify, 1) == 0) { |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3661 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3662 | } |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | /* Goto is required to silence warnings about unused labels, as we |
| 3666 | * don't actually do any test assertions in this function. */ |
| 3667 | goto exit; |
| 3668 | } |
| 3669 | /* END_CASE */ |
| 3670 | |
| 3671 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3672 | void mac_sign(int key_type_arg, |
| 3673 | data_t *key_data, |
| 3674 | int alg_arg, |
| 3675 | data_t *input, |
| 3676 | data_t *expected_mac) |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3677 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3678 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3679 | psa_key_type_t key_type = key_type_arg; |
| 3680 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3681 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3682 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3683 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3684 | size_t mac_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3685 | 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] | 3686 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3687 | const size_t output_sizes_to_test[] = { |
| 3688 | 0, |
| 3689 | 1, |
| 3690 | expected_mac->len - 1, |
| 3691 | expected_mac->len, |
| 3692 | expected_mac->len + 1, |
| 3693 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3695 | TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3696 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3697 | TEST_ASSERT(expected_mac->len == mac_buffer_size); |
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_ASSERT(psa_crypto_init()); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3700 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3701 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3702 | psa_set_key_algorithm(&attributes, alg); |
| 3703 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3705 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3706 | &key)); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3708 | 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] | 3709 | const size_t output_size = output_sizes_to_test[i]; |
| 3710 | psa_status_t expected_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3711 | (output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3712 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3714 | mbedtls_test_set_step(output_size); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3715 | TEST_CALLOC(actual_mac, output_size); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3716 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3717 | /* Calculate the MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3718 | TEST_EQUAL(psa_mac_compute(key, alg, |
| 3719 | input->x, input->len, |
| 3720 | actual_mac, output_size, &mac_length), |
| 3721 | expected_status); |
| 3722 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3723 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3724 | actual_mac, mac_length); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3725 | } |
| 3726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3727 | if (output_size > 0) { |
| 3728 | memset(actual_mac, 0, output_size); |
| 3729 | } |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3730 | |
| 3731 | /* Calculate the MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3732 | PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg)); |
| 3733 | PSA_ASSERT(psa_mac_update(&operation, |
| 3734 | input->x, input->len)); |
| 3735 | TEST_EQUAL(psa_mac_sign_finish(&operation, |
| 3736 | actual_mac, output_size, |
| 3737 | &mac_length), |
| 3738 | expected_status); |
| 3739 | PSA_ASSERT(psa_mac_abort(&operation)); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3741 | if (expected_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3742 | TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 3743 | actual_mac, mac_length); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3744 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3745 | mbedtls_free(actual_mac); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3746 | actual_mac = NULL; |
| 3747 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3748 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3749 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3750 | psa_mac_abort(&operation); |
| 3751 | psa_destroy_key(key); |
| 3752 | PSA_DONE(); |
| 3753 | mbedtls_free(actual_mac); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3754 | } |
| 3755 | /* END_CASE */ |
| 3756 | |
| 3757 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3758 | void mac_verify(int key_type_arg, |
| 3759 | data_t *key_data, |
| 3760 | int alg_arg, |
| 3761 | data_t *input, |
| 3762 | data_t *expected_mac) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3763 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3764 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3765 | psa_key_type_t key_type = key_type_arg; |
| 3766 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3767 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3768 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3769 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3771 | TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3773 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3775 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 3776 | psa_set_key_algorithm(&attributes, alg); |
| 3777 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3779 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 3780 | &key)); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3781 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3782 | /* Verify correct MAC, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3783 | PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len, |
| 3784 | expected_mac->x, expected_mac->len)); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3785 | |
| 3786 | /* Verify correct MAC, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3787 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3788 | PSA_ASSERT(psa_mac_update(&operation, |
| 3789 | input->x, input->len)); |
| 3790 | PSA_ASSERT(psa_mac_verify_finish(&operation, |
| 3791 | expected_mac->x, |
| 3792 | expected_mac->len)); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3793 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3794 | /* Test a MAC that's too short, one-shot case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3795 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3796 | input->x, input->len, |
| 3797 | expected_mac->x, |
| 3798 | expected_mac->len - 1), |
| 3799 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3800 | |
| 3801 | /* Test a MAC that's too short, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3802 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3803 | PSA_ASSERT(psa_mac_update(&operation, |
| 3804 | input->x, input->len)); |
| 3805 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3806 | expected_mac->x, |
| 3807 | expected_mac->len - 1), |
| 3808 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3809 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3810 | /* Test a MAC that's too long, one-shot case. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 3811 | TEST_CALLOC(perturbed_mac, expected_mac->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3812 | memcpy(perturbed_mac, expected_mac->x, expected_mac->len); |
| 3813 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3814 | input->x, input->len, |
| 3815 | perturbed_mac, expected_mac->len + 1), |
| 3816 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3817 | |
| 3818 | /* Test a MAC that's too long, multi-part case. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3819 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3820 | PSA_ASSERT(psa_mac_update(&operation, |
| 3821 | input->x, input->len)); |
| 3822 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3823 | perturbed_mac, |
| 3824 | expected_mac->len + 1), |
| 3825 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3826 | |
| 3827 | /* Test changing one byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3828 | for (size_t i = 0; i < expected_mac->len; i++) { |
| 3829 | mbedtls_test_set_step(i); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3830 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3832 | TEST_EQUAL(psa_mac_verify(key, alg, |
| 3833 | input->x, input->len, |
| 3834 | perturbed_mac, expected_mac->len), |
| 3835 | PSA_ERROR_INVALID_SIGNATURE); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3837 | PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg)); |
| 3838 | PSA_ASSERT(psa_mac_update(&operation, |
| 3839 | input->x, input->len)); |
| 3840 | TEST_EQUAL(psa_mac_verify_finish(&operation, |
| 3841 | perturbed_mac, |
| 3842 | expected_mac->len), |
| 3843 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3844 | perturbed_mac[i] ^= 1; |
| 3845 | } |
| 3846 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3847 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3848 | psa_mac_abort(&operation); |
| 3849 | psa_destroy_key(key); |
| 3850 | PSA_DONE(); |
| 3851 | mbedtls_free(perturbed_mac); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3852 | } |
| 3853 | /* END_CASE */ |
| 3854 | |
| 3855 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3856 | void cipher_operation_init() |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3857 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3858 | const uint8_t input[1] = { 0 }; |
| 3859 | unsigned char output[1] = { 0 }; |
| 3860 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3861 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3862 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3863 | * 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] | 3864 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3865 | psa_cipher_operation_t func = psa_cipher_operation_init(); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3866 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3867 | psa_cipher_operation_t zero; |
| 3868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3869 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3870 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3871 | /* A freshly-initialized cipher operation should not be usable. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3872 | TEST_EQUAL(psa_cipher_update(&func, |
| 3873 | input, sizeof(input), |
| 3874 | output, sizeof(output), |
| 3875 | &output_length), |
| 3876 | PSA_ERROR_BAD_STATE); |
| 3877 | TEST_EQUAL(psa_cipher_update(&init, |
| 3878 | input, sizeof(input), |
| 3879 | output, sizeof(output), |
| 3880 | &output_length), |
| 3881 | PSA_ERROR_BAD_STATE); |
| 3882 | TEST_EQUAL(psa_cipher_update(&zero, |
| 3883 | input, sizeof(input), |
| 3884 | output, sizeof(output), |
| 3885 | &output_length), |
| 3886 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3887 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3888 | /* A default cipher operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3889 | PSA_ASSERT(psa_cipher_abort(&func)); |
| 3890 | PSA_ASSERT(psa_cipher_abort(&init)); |
| 3891 | PSA_ASSERT(psa_cipher_abort(&zero)); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3892 | } |
| 3893 | /* END_CASE */ |
| 3894 | |
| 3895 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3896 | void cipher_setup(int key_type_arg, |
| 3897 | data_t *key, |
| 3898 | int alg_arg, |
| 3899 | int expected_status_arg) |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3900 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3901 | psa_key_type_t key_type = key_type_arg; |
| 3902 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3903 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3904 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3905 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3906 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3907 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3908 | #endif |
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 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3912 | if (!exercise_cipher_setup(key_type, key->x, key->len, alg, |
| 3913 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3914 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3915 | } |
| 3916 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3917 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3918 | /* The operation object should be reusable. */ |
| 3919 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3920 | if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3921 | smoke_test_key_data, |
| 3922 | sizeof(smoke_test_key_data), |
| 3923 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3924 | &operation, &status)) { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3925 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3926 | } |
| 3927 | TEST_EQUAL(status, PSA_SUCCESS); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3928 | #endif |
| 3929 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3930 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3931 | psa_cipher_abort(&operation); |
| 3932 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3933 | } |
| 3934 | /* END_CASE */ |
| 3935 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3936 | /* 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] | 3937 | void cipher_bad_order() |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3938 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3939 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3940 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3941 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3942 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3943 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3944 | 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] | 3945 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3946 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3947 | 0xaa, 0xaa, 0xaa, 0xaa |
| 3948 | }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3949 | const uint8_t text[] = { |
| 3950 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3951 | 0xbb, 0xbb, 0xbb, 0xbb |
| 3952 | }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3953 | 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] | 3954 | size_t length = 0; |
| 3955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3956 | PSA_ASSERT(psa_crypto_init()); |
| 3957 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 3958 | psa_set_key_algorithm(&attributes, alg); |
| 3959 | psa_set_key_type(&attributes, key_type); |
| 3960 | PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data), |
| 3961 | &key)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3962 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3963 | /* Call encrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3964 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3965 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3966 | TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg), |
| 3967 | PSA_ERROR_BAD_STATE); |
| 3968 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3969 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3970 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3971 | |
| 3972 | /* Call decrypt setup twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3973 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 3974 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3975 | TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg), |
| 3976 | PSA_ERROR_BAD_STATE); |
| 3977 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3978 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 3979 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3980 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3981 | /* Generate an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3982 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3983 | buffer, sizeof(buffer), |
| 3984 | &length), |
| 3985 | PSA_ERROR_BAD_STATE); |
| 3986 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3987 | |
| 3988 | /* Generate an IV twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3989 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 3990 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 3991 | buffer, sizeof(buffer), |
| 3992 | &length)); |
| 3993 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 3994 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 3995 | buffer, sizeof(buffer), |
| 3996 | &length), |
| 3997 | PSA_ERROR_BAD_STATE); |
| 3998 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 3999 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4000 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4001 | |
| 4002 | /* Generate an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4003 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4004 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4005 | iv, sizeof(iv))); |
| 4006 | TEST_EQUAL(psa_cipher_generate_iv(&operation, |
| 4007 | buffer, sizeof(buffer), |
| 4008 | &length), |
| 4009 | PSA_ERROR_BAD_STATE); |
| 4010 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4011 | |
| 4012 | /* Set an IV without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4013 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4014 | iv, sizeof(iv)), |
| 4015 | PSA_ERROR_BAD_STATE); |
| 4016 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4017 | |
| 4018 | /* Set an IV after it's already set. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4019 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4020 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4021 | iv, sizeof(iv))); |
| 4022 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4023 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4024 | iv, sizeof(iv)), |
| 4025 | PSA_ERROR_BAD_STATE); |
| 4026 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4027 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4028 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4029 | |
| 4030 | /* Set an IV after it's already generated. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4031 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4032 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4033 | buffer, sizeof(buffer), |
| 4034 | &length)); |
| 4035 | TEST_EQUAL(psa_cipher_set_iv(&operation, |
| 4036 | iv, sizeof(iv)), |
| 4037 | PSA_ERROR_BAD_STATE); |
| 4038 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4039 | |
| 4040 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4041 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4042 | text, sizeof(text), |
| 4043 | buffer, sizeof(buffer), |
| 4044 | &length), |
| 4045 | PSA_ERROR_BAD_STATE); |
| 4046 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4047 | |
| 4048 | /* Call update without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4049 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4050 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4051 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4052 | text, sizeof(text), |
| 4053 | buffer, sizeof(buffer), |
| 4054 | &length), |
| 4055 | PSA_ERROR_BAD_STATE); |
| 4056 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4057 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4058 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4059 | |
| 4060 | /* Call update after finish. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4061 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4062 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4063 | iv, sizeof(iv))); |
| 4064 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4065 | buffer, sizeof(buffer), &length)); |
| 4066 | TEST_EQUAL(psa_cipher_update(&operation, |
| 4067 | text, sizeof(text), |
| 4068 | buffer, sizeof(buffer), |
| 4069 | &length), |
| 4070 | PSA_ERROR_BAD_STATE); |
| 4071 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4072 | |
| 4073 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4074 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4075 | buffer, sizeof(buffer), &length), |
| 4076 | PSA_ERROR_BAD_STATE); |
| 4077 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4078 | |
| 4079 | /* Call finish without an IV where an IV is required. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4080 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4081 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 4082 | * for cipher modes with padding. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4083 | ASSERT_OPERATION_IS_ACTIVE(operation); |
| 4084 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4085 | buffer, sizeof(buffer), &length), |
| 4086 | PSA_ERROR_BAD_STATE); |
| 4087 | ASSERT_OPERATION_IS_INACTIVE(operation); |
| 4088 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4089 | ASSERT_OPERATION_IS_INACTIVE(operation); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4090 | |
| 4091 | /* Call finish twice in a row. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4092 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4093 | PSA_ASSERT(psa_cipher_set_iv(&operation, |
| 4094 | iv, sizeof(iv))); |
| 4095 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4096 | buffer, sizeof(buffer), &length)); |
| 4097 | TEST_EQUAL(psa_cipher_finish(&operation, |
| 4098 | buffer, sizeof(buffer), &length), |
| 4099 | PSA_ERROR_BAD_STATE); |
| 4100 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4102 | PSA_ASSERT(psa_destroy_key(key)); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 4103 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 4104 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4105 | psa_cipher_abort(&operation); |
| 4106 | PSA_DONE(); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 4107 | } |
| 4108 | /* END_CASE */ |
| 4109 | |
| 4110 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4111 | void cipher_encrypt_fail(int alg_arg, |
| 4112 | int key_type_arg, |
| 4113 | data_t *key_data, |
| 4114 | data_t *input, |
| 4115 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4116 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4117 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4118 | psa_status_t status; |
| 4119 | psa_key_type_t key_type = key_type_arg; |
| 4120 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 4121 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4122 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 }; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4123 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 4124 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4125 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4126 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4127 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4128 | size_t function_output_length; |
| 4129 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4130 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4132 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4133 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4135 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4136 | psa_set_key_algorithm(&attributes, alg); |
| 4137 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4139 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4140 | input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4141 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4143 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4144 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4145 | } |
| 4146 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4147 | /* Encrypt, one-shot */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4148 | status = psa_cipher_encrypt(key, alg, input->x, input->len, output, |
| 4149 | output_buffer_size, &output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4151 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4152 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4153 | /* Encrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4154 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
| 4155 | if (status == PSA_SUCCESS) { |
| 4156 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4157 | PSA_ASSERT(psa_cipher_generate_iv(&operation, |
| 4158 | iv, iv_size, |
| 4159 | &iv_length)); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4160 | } |
| 4161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4162 | status = psa_cipher_update(&operation, input->x, input->len, |
| 4163 | output, output_buffer_size, |
| 4164 | &function_output_length); |
| 4165 | if (status == PSA_SUCCESS) { |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4166 | output_length += function_output_length; |
| 4167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4168 | status = psa_cipher_finish(&operation, output + output_length, |
| 4169 | output_buffer_size - output_length, |
| 4170 | &function_output_length); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4172 | TEST_EQUAL(status, expected_status); |
| 4173 | } else { |
| 4174 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4175 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4176 | } else { |
| 4177 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 4178 | } |
| 4179 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4180 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4181 | psa_cipher_abort(&operation); |
| 4182 | mbedtls_free(output); |
| 4183 | psa_destroy_key(key); |
| 4184 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4185 | } |
| 4186 | /* END_CASE */ |
| 4187 | |
| 4188 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4189 | void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data, |
| 4190 | data_t *input, int iv_length, |
| 4191 | int expected_result) |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4192 | { |
| 4193 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4194 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4195 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4196 | size_t output_buffer_size = 0; |
| 4197 | unsigned char *output = NULL; |
| 4198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4199 | 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] | 4200 | TEST_CALLOC(output, output_buffer_size); |
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_ASSERT(psa_crypto_init()); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4204 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4205 | psa_set_key_algorithm(&attributes, alg); |
| 4206 | psa_set_key_type(&attributes, key_type); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4208 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4209 | &key)); |
| 4210 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4211 | TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output, |
| 4212 | iv_length)); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4213 | |
| 4214 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4215 | psa_cipher_abort(&operation); |
| 4216 | mbedtls_free(output); |
| 4217 | psa_destroy_key(key); |
| 4218 | PSA_DONE(); |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 4219 | } |
| 4220 | /* END_CASE */ |
| 4221 | |
| 4222 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4223 | void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data, |
| 4224 | data_t *plaintext, data_t *ciphertext) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4225 | { |
| 4226 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4227 | psa_key_type_t key_type = key_type_arg; |
| 4228 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4229 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4230 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4231 | unsigned char *output = NULL; |
| 4232 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4233 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4234 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4236 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4237 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4238 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4239 | TEST_LE_U(ciphertext->len, |
| 4240 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len)); |
| 4241 | TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len), |
| 4242 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len)); |
| 4243 | TEST_LE_U(plaintext->len, |
| 4244 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len)); |
| 4245 | TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len), |
| 4246 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len)); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4247 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4248 | |
| 4249 | /* Set up key and output buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4250 | psa_set_key_usage_flags(&attributes, |
| 4251 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4252 | psa_set_key_algorithm(&attributes, alg); |
| 4253 | psa_set_key_type(&attributes, key_type); |
| 4254 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4255 | &key)); |
| 4256 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, |
| 4257 | plaintext->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4258 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4259 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4260 | /* set_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4261 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4262 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4263 | PSA_ERROR_BAD_STATE); |
| 4264 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4265 | TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)), |
| 4266 | PSA_ERROR_BAD_STATE); |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 4267 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4268 | /* generate_iv() is not allowed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4269 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4270 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4271 | &length), |
| 4272 | PSA_ERROR_BAD_STATE); |
| 4273 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
| 4274 | TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv), |
| 4275 | &length), |
| 4276 | PSA_ERROR_BAD_STATE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4277 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4278 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4279 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4280 | output_length = 0; |
| 4281 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4282 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4283 | plaintext->x, plaintext->len, |
| 4284 | output, output_buffer_size, |
| 4285 | &length)); |
| 4286 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4287 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4288 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4289 | mbedtls_buffer_offset(output, output_length), |
| 4290 | output_buffer_size - output_length, |
| 4291 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4292 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4293 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4294 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4295 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4296 | /* Multipart encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4297 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4298 | output_length = 0; |
| 4299 | length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4300 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4301 | ciphertext->x, ciphertext->len, |
| 4302 | output, output_buffer_size, |
| 4303 | &length)); |
| 4304 | TEST_LE_U(length, output_buffer_size); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4305 | output_length += length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4306 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4307 | mbedtls_buffer_offset(output, output_length), |
| 4308 | output_buffer_size - output_length, |
| 4309 | &length)); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4310 | output_length += length; |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4311 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4312 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4313 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4314 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4315 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4316 | PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len, |
| 4317 | output, output_buffer_size, |
| 4318 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4319 | TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4320 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4321 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4322 | /* One-shot decryption */ |
| 4323 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4324 | PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len, |
| 4325 | output, output_buffer_size, |
| 4326 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4327 | TEST_MEMORY_COMPARE(plaintext->x, plaintext->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4328 | output, output_length); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4329 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4330 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4331 | PSA_ASSERT(psa_cipher_abort(&operation)); |
| 4332 | mbedtls_free(output); |
| 4333 | psa_cipher_abort(&operation); |
| 4334 | psa_destroy_key(key); |
| 4335 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4336 | } |
| 4337 | /* END_CASE */ |
| 4338 | |
| 4339 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4340 | 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] | 4341 | { |
| 4342 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4343 | psa_algorithm_t alg = alg_arg; |
| 4344 | psa_key_type_t key_type = key_type_arg; |
| 4345 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4346 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4347 | psa_status_t status; |
| 4348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4349 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4351 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4352 | psa_set_key_algorithm(&attributes, alg); |
| 4353 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4354 | |
| 4355 | /* Usage of either of these two size macros would cause divide by zero |
| 4356 | * with incorrect key types previously. Input length should be irrelevant |
| 4357 | * here. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4358 | TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16), |
| 4359 | 0); |
| 4360 | TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4361 | |
| 4362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4363 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4364 | &key)); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4365 | |
| 4366 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4367 | * Encrypt or decrypt will end up in the same place. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4368 | status = psa_cipher_encrypt_setup(&operation, key, alg); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4370 | TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4371 | |
| 4372 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4373 | psa_cipher_abort(&operation); |
| 4374 | psa_destroy_key(key); |
| 4375 | PSA_DONE(); |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4376 | } |
| 4377 | /* END_CASE */ |
| 4378 | |
| 4379 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4380 | void cipher_encrypt_validation(int alg_arg, |
| 4381 | int key_type_arg, |
| 4382 | data_t *key_data, |
| 4383 | data_t *input) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4384 | { |
| 4385 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4386 | psa_key_type_t key_type = key_type_arg; |
| 4387 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4388 | size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4389 | unsigned char *output1 = NULL; |
| 4390 | size_t output1_buffer_size = 0; |
| 4391 | size_t output1_length = 0; |
| 4392 | unsigned char *output2 = NULL; |
| 4393 | size_t output2_buffer_size = 0; |
| 4394 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4395 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4396 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4397 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4401 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4402 | psa_set_key_algorithm(&attributes, alg); |
| 4403 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4405 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4406 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4407 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4408 | TEST_CALLOC(output1, output1_buffer_size); |
| 4409 | TEST_CALLOC(output2, output2_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4411 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4412 | &key)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4413 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4414 | /* The one-shot cipher encryption uses generated iv so validating |
| 4415 | the output is not possible. Validating with multipart encryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4416 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1, |
| 4417 | output1_buffer_size, &output1_length)); |
| 4418 | TEST_LE_U(output1_length, |
| 4419 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4420 | TEST_LE_U(output1_length, |
| 4421 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4423 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
| 4424 | PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4426 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4427 | input->x, input->len, |
| 4428 | output2, output2_buffer_size, |
| 4429 | &function_output_length)); |
| 4430 | TEST_LE_U(function_output_length, |
| 4431 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4432 | TEST_LE_U(function_output_length, |
| 4433 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4434 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4436 | PSA_ASSERT(psa_cipher_finish(&operation, |
| 4437 | output2 + output2_length, |
| 4438 | output2_buffer_size - output2_length, |
| 4439 | &function_output_length)); |
| 4440 | TEST_LE_U(function_output_length, |
| 4441 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4442 | TEST_LE_U(function_output_length, |
| 4443 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4444 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4446 | PSA_ASSERT(psa_cipher_abort(&operation)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4447 | TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4448 | output2, output2_length); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4449 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4450 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4451 | psa_cipher_abort(&operation); |
| 4452 | mbedtls_free(output1); |
| 4453 | mbedtls_free(output2); |
| 4454 | psa_destroy_key(key); |
| 4455 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4456 | } |
| 4457 | /* END_CASE */ |
| 4458 | |
| 4459 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4460 | void cipher_encrypt_multipart(int alg_arg, int key_type_arg, |
| 4461 | data_t *key_data, data_t *iv, |
| 4462 | data_t *input, |
| 4463 | int first_part_size_arg, |
| 4464 | int output1_length_arg, int output2_length_arg, |
| 4465 | data_t *expected_output, |
| 4466 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4467 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4468 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4469 | psa_key_type_t key_type = key_type_arg; |
| 4470 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4471 | psa_status_t status; |
| 4472 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4473 | size_t first_part_size = first_part_size_arg; |
| 4474 | size_t output1_length = output1_length_arg; |
| 4475 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4476 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4477 | size_t output_buffer_size = 0; |
| 4478 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4479 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4480 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4481 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4485 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 4486 | psa_set_key_algorithm(&attributes, alg); |
| 4487 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4489 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4490 | &key)); |
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 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4494 | if (iv->len > 0) { |
| 4495 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4496 | } |
| 4497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4498 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4499 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4500 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4502 | TEST_LE_U(first_part_size, input->len); |
| 4503 | PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size, |
| 4504 | output, output_buffer_size, |
| 4505 | &function_output_length)); |
| 4506 | TEST_ASSERT(function_output_length == output1_length); |
| 4507 | TEST_LE_U(function_output_length, |
| 4508 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4509 | TEST_LE_U(function_output_length, |
| 4510 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4511 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4513 | if (first_part_size < input->len) { |
| 4514 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4515 | input->x + first_part_size, |
| 4516 | input->len - first_part_size, |
| 4517 | (output_buffer_size == 0 ? NULL : |
| 4518 | output + total_output_length), |
| 4519 | output_buffer_size - total_output_length, |
| 4520 | &function_output_length)); |
| 4521 | TEST_ASSERT(function_output_length == output2_length); |
| 4522 | TEST_LE_U(function_output_length, |
| 4523 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4524 | alg, |
| 4525 | input->len - first_part_size)); |
| 4526 | TEST_LE_U(function_output_length, |
| 4527 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4528 | total_output_length += function_output_length; |
| 4529 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4531 | status = psa_cipher_finish(&operation, |
| 4532 | (output_buffer_size == 0 ? NULL : |
| 4533 | output + total_output_length), |
| 4534 | output_buffer_size - total_output_length, |
| 4535 | &function_output_length); |
| 4536 | TEST_LE_U(function_output_length, |
| 4537 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4538 | TEST_LE_U(function_output_length, |
| 4539 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4540 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4541 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4543 | if (expected_status == PSA_SUCCESS) { |
| 4544 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4545 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4546 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4547 | output, total_output_length); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4548 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4549 | |
| 4550 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4551 | psa_cipher_abort(&operation); |
| 4552 | mbedtls_free(output); |
| 4553 | psa_destroy_key(key); |
| 4554 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4555 | } |
| 4556 | /* END_CASE */ |
| 4557 | |
| 4558 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4559 | void cipher_decrypt_multipart(int alg_arg, int key_type_arg, |
| 4560 | data_t *key_data, data_t *iv, |
| 4561 | data_t *input, |
| 4562 | int first_part_size_arg, |
| 4563 | int output1_length_arg, int output2_length_arg, |
| 4564 | data_t *expected_output, |
| 4565 | int expected_status_arg) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4566 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4567 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4568 | psa_key_type_t key_type = key_type_arg; |
| 4569 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4570 | psa_status_t status; |
| 4571 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4572 | size_t first_part_size = first_part_size_arg; |
| 4573 | size_t output1_length = output1_length_arg; |
| 4574 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4575 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4576 | size_t output_buffer_size = 0; |
| 4577 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4578 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4579 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4580 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4584 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4585 | psa_set_key_algorithm(&attributes, alg); |
| 4586 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4588 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4589 | &key)); |
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 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg)); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4593 | if (iv->len > 0) { |
| 4594 | PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4595 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4597 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) + |
| 4598 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4599 | TEST_CALLOC(output, output_buffer_size); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4601 | TEST_LE_U(first_part_size, input->len); |
| 4602 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4603 | input->x, first_part_size, |
| 4604 | output, output_buffer_size, |
| 4605 | &function_output_length)); |
| 4606 | TEST_ASSERT(function_output_length == output1_length); |
| 4607 | TEST_LE_U(function_output_length, |
| 4608 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4609 | TEST_LE_U(function_output_length, |
| 4610 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4611 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4613 | if (first_part_size < input->len) { |
| 4614 | PSA_ASSERT(psa_cipher_update(&operation, |
| 4615 | input->x + first_part_size, |
| 4616 | input->len - first_part_size, |
| 4617 | (output_buffer_size == 0 ? NULL : |
| 4618 | output + total_output_length), |
| 4619 | output_buffer_size - total_output_length, |
| 4620 | &function_output_length)); |
| 4621 | TEST_ASSERT(function_output_length == output2_length); |
| 4622 | TEST_LE_U(function_output_length, |
| 4623 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4624 | alg, |
| 4625 | input->len - first_part_size)); |
| 4626 | TEST_LE_U(function_output_length, |
| 4627 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4628 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4629 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4631 | status = psa_cipher_finish(&operation, |
| 4632 | (output_buffer_size == 0 ? NULL : |
| 4633 | output + total_output_length), |
| 4634 | output_buffer_size - total_output_length, |
| 4635 | &function_output_length); |
| 4636 | TEST_LE_U(function_output_length, |
| 4637 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4638 | TEST_LE_U(function_output_length, |
| 4639 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4640 | total_output_length += function_output_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4641 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4643 | if (expected_status == PSA_SUCCESS) { |
| 4644 | PSA_ASSERT(psa_cipher_abort(&operation)); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4645 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4646 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4647 | output, total_output_length); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4648 | } |
| 4649 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4650 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4651 | psa_cipher_abort(&operation); |
| 4652 | mbedtls_free(output); |
| 4653 | psa_destroy_key(key); |
| 4654 | PSA_DONE(); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4655 | } |
| 4656 | /* END_CASE */ |
| 4657 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4658 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4659 | void cipher_decrypt_fail(int alg_arg, |
| 4660 | int key_type_arg, |
| 4661 | data_t *key_data, |
| 4662 | data_t *iv, |
| 4663 | data_t *input_arg, |
| 4664 | int expected_status_arg) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4665 | { |
| 4666 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4667 | psa_status_t status; |
| 4668 | psa_key_type_t key_type = key_type_arg; |
| 4669 | psa_algorithm_t alg = alg_arg; |
| 4670 | psa_status_t expected_status = expected_status_arg; |
| 4671 | unsigned char *input = NULL; |
| 4672 | size_t input_buffer_size = 0; |
| 4673 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4674 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4675 | size_t output_buffer_size = 0; |
| 4676 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4677 | size_t function_output_length; |
| 4678 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4679 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4681 | if (PSA_ERROR_BAD_STATE != expected_status) { |
| 4682 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4683 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4684 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4685 | psa_set_key_algorithm(&attributes, alg); |
| 4686 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4688 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4689 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4690 | } |
| 4691 | |
| 4692 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4693 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4694 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4695 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4696 | memcpy(input, iv->x, iv->len); |
| 4697 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4698 | } |
| 4699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4700 | 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] | 4701 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4702 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4703 | /* Decrypt, one-short */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4704 | status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4705 | output_buffer_size, &output_length); |
| 4706 | TEST_EQUAL(status, expected_status); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4707 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4708 | /* Decrypt, multi-part */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4709 | status = psa_cipher_decrypt_setup(&operation, key, alg); |
| 4710 | if (status == PSA_SUCCESS) { |
| 4711 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 4712 | input_arg->len) + |
| 4713 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4714 | TEST_CALLOC(output_multi, output_buffer_size); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4716 | if (iv->len > 0) { |
| 4717 | status = psa_cipher_set_iv(&operation, iv->x, iv->len); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4719 | if (status != PSA_SUCCESS) { |
| 4720 | TEST_EQUAL(status, expected_status); |
| 4721 | } |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4722 | } |
| 4723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4724 | if (status == PSA_SUCCESS) { |
| 4725 | status = psa_cipher_update(&operation, |
| 4726 | input_arg->x, input_arg->len, |
| 4727 | output_multi, output_buffer_size, |
| 4728 | &function_output_length); |
| 4729 | if (status == PSA_SUCCESS) { |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4730 | output_length = function_output_length; |
| 4731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4732 | status = psa_cipher_finish(&operation, |
| 4733 | output_multi + output_length, |
| 4734 | output_buffer_size - output_length, |
| 4735 | &function_output_length); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4737 | TEST_EQUAL(status, expected_status); |
| 4738 | } else { |
| 4739 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4740 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4741 | } else { |
| 4742 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4743 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4744 | } else { |
| 4745 | TEST_EQUAL(status, expected_status); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4746 | } |
| 4747 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4748 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4749 | psa_cipher_abort(&operation); |
| 4750 | mbedtls_free(input); |
| 4751 | mbedtls_free(output); |
| 4752 | mbedtls_free(output_multi); |
| 4753 | psa_destroy_key(key); |
| 4754 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4755 | } |
| 4756 | /* END_CASE */ |
| 4757 | |
| 4758 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | void cipher_decrypt(int alg_arg, |
| 4760 | int key_type_arg, |
| 4761 | data_t *key_data, |
| 4762 | data_t *iv, |
| 4763 | data_t *input_arg, |
| 4764 | data_t *expected_output) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4765 | { |
| 4766 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4767 | psa_key_type_t key_type = key_type_arg; |
| 4768 | psa_algorithm_t alg = alg_arg; |
| 4769 | unsigned char *input = NULL; |
| 4770 | size_t input_buffer_size = 0; |
| 4771 | unsigned char *output = NULL; |
| 4772 | size_t output_buffer_size = 0; |
| 4773 | size_t output_length = 0; |
| 4774 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4776 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4778 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 4779 | psa_set_key_algorithm(&attributes, alg); |
| 4780 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4781 | |
| 4782 | /* Allocate input buffer and copy the iv and the plaintext */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4783 | input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len); |
| 4784 | if (input_buffer_size > 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4785 | TEST_CALLOC(input, input_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4786 | memcpy(input, iv->x, iv->len); |
| 4787 | memcpy(input + iv->len, input_arg->x, input_arg->len); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4788 | } |
| 4789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4790 | 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] | 4791 | TEST_CALLOC(output, output_buffer_size); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4793 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4794 | &key)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4796 | PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output, |
| 4797 | output_buffer_size, &output_length)); |
| 4798 | TEST_LE_U(output_length, |
| 4799 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size)); |
| 4800 | TEST_LE_U(output_length, |
| 4801 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size)); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4802 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4803 | TEST_MEMORY_COMPARE(expected_output->x, expected_output->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 4804 | output, output_length); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4805 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4806 | mbedtls_free(input); |
| 4807 | mbedtls_free(output); |
| 4808 | psa_destroy_key(key); |
| 4809 | PSA_DONE(); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4810 | } |
| 4811 | /* END_CASE */ |
| 4812 | |
| 4813 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | void cipher_verify_output(int alg_arg, |
| 4815 | int key_type_arg, |
| 4816 | data_t *key_data, |
| 4817 | data_t *input) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4818 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4819 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4820 | psa_key_type_t key_type = key_type_arg; |
| 4821 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4822 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4823 | size_t output1_size = 0; |
| 4824 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4825 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4826 | size_t output2_size = 0; |
| 4827 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4828 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4832 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4833 | psa_set_key_algorithm(&attributes, alg); |
| 4834 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4836 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4837 | &key)); |
| 4838 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4839 | TEST_CALLOC(output1, output1_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4841 | PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, |
| 4842 | output1, output1_size, |
| 4843 | &output1_length)); |
| 4844 | TEST_LE_U(output1_length, |
| 4845 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len)); |
| 4846 | TEST_LE_U(output1_length, |
| 4847 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4848 | |
| 4849 | output2_size = output1_length; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4850 | TEST_CALLOC(output2, output2_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4851 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4852 | PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length, |
| 4853 | output2, output2_size, |
| 4854 | &output2_length)); |
| 4855 | TEST_LE_U(output2_length, |
| 4856 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4857 | TEST_LE_U(output2_length, |
| 4858 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4859 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 4860 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4861 | |
| 4862 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4863 | mbedtls_free(output1); |
| 4864 | mbedtls_free(output2); |
| 4865 | psa_destroy_key(key); |
| 4866 | PSA_DONE(); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4867 | } |
| 4868 | /* END_CASE */ |
| 4869 | |
| 4870 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4871 | void cipher_verify_output_multipart(int alg_arg, |
| 4872 | int key_type_arg, |
| 4873 | data_t *key_data, |
| 4874 | data_t *input, |
| 4875 | int first_part_size_arg) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4876 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4877 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4878 | psa_key_type_t key_type = key_type_arg; |
| 4879 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4880 | size_t first_part_size = first_part_size_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4881 | unsigned char iv[16] = { 0 }; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4882 | size_t iv_size = 16; |
| 4883 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4884 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4885 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4886 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4887 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4888 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4889 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4890 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4891 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4892 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4893 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4897 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 4898 | psa_set_key_algorithm(&attributes, alg); |
| 4899 | psa_set_key_type(&attributes, key_type); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4901 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 4902 | &key)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4904 | PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg)); |
| 4905 | PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg)); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4907 | if (alg != PSA_ALG_ECB_NO_PADDING) { |
| 4908 | PSA_ASSERT(psa_cipher_generate_iv(&operation1, |
| 4909 | iv, iv_size, |
| 4910 | &iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4911 | } |
| 4912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4913 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len); |
| 4914 | TEST_LE_U(output1_buffer_size, |
| 4915 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4916 | TEST_CALLOC(output1, output1_buffer_size); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4918 | TEST_LE_U(first_part_size, input->len); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4920 | PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size, |
| 4921 | output1, output1_buffer_size, |
| 4922 | &function_output_length)); |
| 4923 | TEST_LE_U(function_output_length, |
| 4924 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4925 | TEST_LE_U(function_output_length, |
| 4926 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4927 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4929 | PSA_ASSERT(psa_cipher_update(&operation1, |
| 4930 | input->x + first_part_size, |
| 4931 | input->len - first_part_size, |
David Horstmann | b8dc245 | 2024-02-06 17:03:13 +0000 | [diff] [blame] | 4932 | output1 + output1_length, |
| 4933 | output1_buffer_size - output1_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4934 | &function_output_length)); |
| 4935 | TEST_LE_U(function_output_length, |
| 4936 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4937 | alg, |
| 4938 | input->len - first_part_size)); |
| 4939 | TEST_LE_U(function_output_length, |
| 4940 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4941 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4943 | PSA_ASSERT(psa_cipher_finish(&operation1, |
| 4944 | output1 + output1_length, |
| 4945 | output1_buffer_size - output1_length, |
| 4946 | &function_output_length)); |
| 4947 | TEST_LE_U(function_output_length, |
| 4948 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4949 | TEST_LE_U(function_output_length, |
| 4950 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4951 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4953 | PSA_ASSERT(psa_cipher_abort(&operation1)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4954 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4955 | output2_buffer_size = output1_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4956 | TEST_LE_U(output2_buffer_size, |
| 4957 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length)); |
| 4958 | TEST_LE_U(output2_buffer_size, |
| 4959 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 4960 | TEST_CALLOC(output2, output2_buffer_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4962 | if (iv_length > 0) { |
| 4963 | PSA_ASSERT(psa_cipher_set_iv(&operation2, |
| 4964 | iv, iv_length)); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4965 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4967 | PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size, |
| 4968 | output2, output2_buffer_size, |
| 4969 | &function_output_length)); |
| 4970 | TEST_LE_U(function_output_length, |
| 4971 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size)); |
| 4972 | TEST_LE_U(function_output_length, |
| 4973 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4974 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4976 | PSA_ASSERT(psa_cipher_update(&operation2, |
| 4977 | output1 + first_part_size, |
| 4978 | output1_length - first_part_size, |
David Horstmann | b8dc245 | 2024-02-06 17:03:13 +0000 | [diff] [blame] | 4979 | output2 + output2_length, |
| 4980 | output2_buffer_size - output2_length, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4981 | &function_output_length)); |
| 4982 | TEST_LE_U(function_output_length, |
| 4983 | PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, |
| 4984 | alg, |
| 4985 | output1_length - first_part_size)); |
| 4986 | TEST_LE_U(function_output_length, |
| 4987 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size)); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4988 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4990 | PSA_ASSERT(psa_cipher_finish(&operation2, |
| 4991 | output2 + output2_length, |
| 4992 | output2_buffer_size - output2_length, |
| 4993 | &function_output_length)); |
| 4994 | TEST_LE_U(function_output_length, |
| 4995 | PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg)); |
| 4996 | TEST_LE_U(function_output_length, |
| 4997 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4998 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5000 | PSA_ASSERT(psa_cipher_abort(&operation2)); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5001 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5002 | TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5003 | |
| 5004 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5005 | psa_cipher_abort(&operation1); |
| 5006 | psa_cipher_abort(&operation2); |
| 5007 | mbedtls_free(output1); |
| 5008 | mbedtls_free(output2); |
| 5009 | psa_destroy_key(key); |
| 5010 | PSA_DONE(); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 5011 | } |
| 5012 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 5013 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5014 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5015 | void aead_encrypt_decrypt(int key_type_arg, data_t *key_data, |
| 5016 | int alg_arg, |
| 5017 | data_t *nonce, |
| 5018 | data_t *additional_data, |
| 5019 | data_t *input_data, |
| 5020 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5021 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5022 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5023 | psa_key_type_t key_type = key_type_arg; |
| 5024 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5025 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5026 | unsigned char *output_data = NULL; |
| 5027 | size_t output_size = 0; |
| 5028 | size_t output_length = 0; |
| 5029 | unsigned char *output_data2 = NULL; |
| 5030 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5031 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5032 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5033 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5036 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5037 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5038 | psa_set_key_algorithm(&attributes, alg); |
| 5039 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5040 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5041 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5042 | &key)); |
| 5043 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5044 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5046 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5047 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5048 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 5049 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5050 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 5051 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
| 5052 | TEST_EQUAL(output_size, |
| 5053 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5054 | TEST_LE_U(output_size, |
| 5055 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5056 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5057 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5059 | status = psa_aead_encrypt(key, alg, |
| 5060 | nonce->x, nonce->len, |
| 5061 | additional_data->x, |
| 5062 | additional_data->len, |
| 5063 | input_data->x, input_data->len, |
| 5064 | output_data, output_size, |
| 5065 | &output_length); |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 5066 | |
| 5067 | /* If the operation is not supported, just skip and not fail in case the |
| 5068 | * encryption involves a common limitation of cryptography hardwares and |
| 5069 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5070 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5071 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5072 | 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] | 5073 | } |
| 5074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5075 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5077 | if (PSA_SUCCESS == expected_result) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5078 | TEST_CALLOC(output_data2, output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5079 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 5080 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 5081 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5082 | TEST_EQUAL(input_data->len, |
| 5083 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length)); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 5084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5085 | TEST_LE_U(input_data->len, |
| 5086 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length)); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5087 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5088 | TEST_EQUAL(psa_aead_decrypt(key, alg, |
| 5089 | nonce->x, nonce->len, |
| 5090 | additional_data->x, |
| 5091 | additional_data->len, |
| 5092 | output_data, output_length, |
| 5093 | output_data2, output_length, |
| 5094 | &output_length2), |
| 5095 | expected_result); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5096 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5097 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5098 | output_data2, output_length2); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5099 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5100 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5101 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5102 | psa_destroy_key(key); |
| 5103 | mbedtls_free(output_data); |
| 5104 | mbedtls_free(output_data2); |
| 5105 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5106 | } |
| 5107 | /* END_CASE */ |
| 5108 | |
| 5109 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5110 | void aead_encrypt(int key_type_arg, data_t *key_data, |
| 5111 | int alg_arg, |
| 5112 | data_t *nonce, |
| 5113 | data_t *additional_data, |
| 5114 | data_t *input_data, |
| 5115 | data_t *expected_result) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5116 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5117 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5118 | psa_key_type_t key_type = key_type_arg; |
| 5119 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5120 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5121 | unsigned char *output_data = NULL; |
| 5122 | size_t output_size = 0; |
| 5123 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5124 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5125 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
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_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5129 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5130 | psa_set_key_algorithm(&attributes, alg); |
| 5131 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5133 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5134 | &key)); |
| 5135 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5136 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5138 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5139 | alg); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5140 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 5141 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5142 | TEST_EQUAL(output_size, |
| 5143 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5144 | TEST_LE_U(output_size, |
| 5145 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5146 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5148 | status = psa_aead_encrypt(key, alg, |
| 5149 | nonce->x, nonce->len, |
| 5150 | additional_data->x, additional_data->len, |
| 5151 | input_data->x, input_data->len, |
| 5152 | output_data, output_size, |
| 5153 | &output_length); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5154 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 5155 | /* If the operation is not supported, just skip and not fail in case the |
| 5156 | * encryption involves a common limitation of cryptography hardwares and |
| 5157 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5158 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5159 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5160 | 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] | 5161 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5163 | PSA_ASSERT(status); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5164 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5165 | output_data, output_length); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5166 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5167 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5168 | psa_destroy_key(key); |
| 5169 | mbedtls_free(output_data); |
| 5170 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5171 | } |
| 5172 | /* END_CASE */ |
| 5173 | |
| 5174 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5175 | void aead_decrypt(int key_type_arg, data_t *key_data, |
| 5176 | int alg_arg, |
| 5177 | data_t *nonce, |
| 5178 | data_t *additional_data, |
| 5179 | data_t *input_data, |
| 5180 | data_t *expected_data, |
| 5181 | int expected_result_arg) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5182 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5183 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5184 | psa_key_type_t key_type = key_type_arg; |
| 5185 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5186 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5187 | unsigned char *output_data = NULL; |
| 5188 | size_t output_size = 0; |
| 5189 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5190 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5191 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5192 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
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_ASSERT(psa_crypto_init()); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5196 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5197 | psa_set_key_algorithm(&attributes, alg); |
| 5198 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5200 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5201 | &key)); |
| 5202 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 5203 | key_bits = psa_get_key_bits(&attributes); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5205 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits, |
| 5206 | alg); |
| 5207 | if (expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 5208 | expected_result != PSA_ERROR_NOT_SUPPORTED) { |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5209 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 5210 | * should be exact. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5211 | TEST_EQUAL(output_size, |
| 5212 | PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len)); |
| 5213 | TEST_LE_U(output_size, |
| 5214 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len)); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 5215 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5216 | TEST_CALLOC(output_data, output_size); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5218 | status = psa_aead_decrypt(key, alg, |
| 5219 | nonce->x, nonce->len, |
| 5220 | additional_data->x, |
| 5221 | additional_data->len, |
| 5222 | input_data->x, input_data->len, |
| 5223 | output_data, output_size, |
| 5224 | &output_length); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5225 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 5226 | /* If the operation is not supported, just skip and not fail in case the |
| 5227 | * decryption involves a common limitation of cryptography hardwares and |
| 5228 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5229 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5230 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5231 | 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] | 5232 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 5233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5234 | TEST_EQUAL(status, expected_result); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5236 | if (expected_result == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 5237 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 5238 | output_data, output_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5239 | } |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5240 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5241 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5242 | psa_destroy_key(key); |
| 5243 | mbedtls_free(output_data); |
| 5244 | PSA_DONE(); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 5245 | } |
| 5246 | /* END_CASE */ |
| 5247 | |
| 5248 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5249 | void aead_multipart_encrypt(int key_type_arg, data_t *key_data, |
| 5250 | int alg_arg, |
| 5251 | data_t *nonce, |
| 5252 | data_t *additional_data, |
| 5253 | data_t *input_data, |
| 5254 | int do_set_lengths, |
| 5255 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5256 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5257 | size_t ad_part_len = 0; |
| 5258 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5259 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5261 | for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) { |
| 5262 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5264 | if (do_set_lengths) { |
| 5265 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5266 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5267 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5268 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5269 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5270 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5271 | |
| 5272 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5274 | alg_arg, nonce, |
| 5275 | additional_data, |
| 5276 | ad_part_len, |
| 5277 | input_data, -1, |
| 5278 | set_lengths_method, |
| 5279 | expected_output, |
| 5280 | 1, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5281 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5282 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5284 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5285 | mbedtls_test_set_step(1000 + ad_part_len); |
| 5286 | |
| 5287 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5288 | alg_arg, nonce, |
| 5289 | additional_data, |
| 5290 | ad_part_len, |
| 5291 | input_data, -1, |
| 5292 | set_lengths_method, |
| 5293 | expected_output, |
| 5294 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5295 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5296 | } |
| 5297 | } |
| 5298 | |
| 5299 | for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) { |
| 5300 | /* Split data into length(data_part_len) parts. */ |
| 5301 | mbedtls_test_set_step(2000 + data_part_len); |
| 5302 | |
| 5303 | if (do_set_lengths) { |
| 5304 | if (data_part_len & 0x01) { |
| 5305 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5306 | } else { |
| 5307 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 5308 | } |
| 5309 | } |
| 5310 | |
| 5311 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5312 | alg_arg, nonce, |
| 5313 | additional_data, -1, |
| 5314 | input_data, data_part_len, |
| 5315 | set_lengths_method, |
| 5316 | expected_output, |
| 5317 | 1, 0)) { |
| 5318 | break; |
| 5319 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5320 | |
| 5321 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5322 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5324 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5325 | alg_arg, nonce, |
| 5326 | additional_data, -1, |
| 5327 | input_data, data_part_len, |
| 5328 | set_lengths_method, |
| 5329 | expected_output, |
| 5330 | 1, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5331 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5332 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5333 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5334 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5335 | /* Goto is required to silence warnings about unused labels, as we |
| 5336 | * don't actually do any test assertions in this function. */ |
| 5337 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5338 | } |
| 5339 | /* END_CASE */ |
| 5340 | |
| 5341 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5342 | void aead_multipart_decrypt(int key_type_arg, data_t *key_data, |
| 5343 | int alg_arg, |
| 5344 | data_t *nonce, |
| 5345 | data_t *additional_data, |
| 5346 | data_t *input_data, |
| 5347 | int do_set_lengths, |
| 5348 | data_t *expected_output) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5349 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5350 | size_t ad_part_len = 0; |
| 5351 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5352 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5354 | 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] | 5355 | /* Split ad into length(ad_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | mbedtls_test_set_step(ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5358 | if (do_set_lengths) { |
| 5359 | if (ad_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5360 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5361 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5362 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5363 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5364 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5366 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5367 | alg_arg, nonce, |
| 5368 | additional_data, |
| 5369 | ad_part_len, |
| 5370 | input_data, -1, |
| 5371 | set_lengths_method, |
| 5372 | expected_output, |
| 5373 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5374 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5375 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5376 | |
| 5377 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5378 | mbedtls_test_set_step(1000 + ad_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5380 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5381 | alg_arg, nonce, |
| 5382 | additional_data, |
| 5383 | ad_part_len, |
| 5384 | input_data, -1, |
| 5385 | set_lengths_method, |
| 5386 | expected_output, |
| 5387 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5388 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5389 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5390 | } |
| 5391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5392 | 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] | 5393 | /* Split data into length(data_part_len) parts. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5394 | mbedtls_test_set_step(2000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5396 | if (do_set_lengths) { |
| 5397 | if (data_part_len & 0x01) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5398 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5399 | } else { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5400 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5402 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5405 | alg_arg, nonce, |
| 5406 | additional_data, -1, |
| 5407 | input_data, data_part_len, |
| 5408 | set_lengths_method, |
| 5409 | expected_output, |
| 5410 | 0, 0)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5411 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5412 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5413 | |
| 5414 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5415 | mbedtls_test_set_step(3000 + data_part_len); |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5417 | if (!aead_multipart_internal_func(key_type_arg, key_data, |
| 5418 | alg_arg, nonce, |
| 5419 | additional_data, -1, |
| 5420 | input_data, data_part_len, |
| 5421 | set_lengths_method, |
| 5422 | expected_output, |
| 5423 | 0, 1)) { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5424 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5425 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5426 | } |
| 5427 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5428 | /* Goto is required to silence warnings about unused labels, as we |
| 5429 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5430 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5431 | } |
| 5432 | /* END_CASE */ |
| 5433 | |
| 5434 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data, |
| 5436 | int alg_arg, |
| 5437 | int nonce_length, |
| 5438 | int expected_nonce_length_arg, |
| 5439 | data_t *additional_data, |
| 5440 | data_t *input_data, |
| 5441 | int expected_status_arg) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5442 | { |
| 5443 | |
| 5444 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5445 | psa_key_type_t key_type = key_type_arg; |
| 5446 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5447 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
David Horstmann | 52402ec | 2023-12-11 15:09:46 +0000 | [diff] [blame] | 5448 | /* Some tests try to get more than the maximum nonce length, |
| 5449 | * so allocate double. */ |
| 5450 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE * 2]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5451 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5452 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5453 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5454 | size_t actual_nonce_length = 0; |
| 5455 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5456 | unsigned char *output = NULL; |
| 5457 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5458 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5459 | size_t ciphertext_size = 0; |
| 5460 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5461 | size_t tag_length = 0; |
| 5462 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
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_ASSERT(psa_crypto_init()); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5466 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5467 | psa_set_key_algorithm(&attributes, alg); |
| 5468 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5470 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5471 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5475 | 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] | 5476 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5477 | TEST_CALLOC(output, output_size); |
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 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5481 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5482 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5483 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5485 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5486 | |
| 5487 | /* If the operation is not supported, just skip and not fail in case the |
| 5488 | * encryption involves a common limitation of cryptography hardwares and |
| 5489 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5490 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5491 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5492 | 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] | 5493 | } |
| 5494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5495 | PSA_ASSERT(status); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5497 | status = psa_aead_generate_nonce(&operation, nonce_buffer, |
| 5498 | nonce_length, |
| 5499 | &actual_nonce_length); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5501 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5503 | TEST_EQUAL(actual_nonce_length, expected_nonce_length); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5505 | if (expected_status == PSA_SUCCESS) { |
| 5506 | TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type, |
| 5507 | alg)); |
| 5508 | } |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5510 | TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5512 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5513 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5514 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5515 | input_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5517 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5518 | additional_data->len)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5520 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5521 | output, output_size, |
| 5522 | &ciphertext_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5524 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5525 | &ciphertext_length, tag_buffer, |
| 5526 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5527 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5528 | |
| 5529 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5530 | psa_destroy_key(key); |
| 5531 | mbedtls_free(output); |
| 5532 | mbedtls_free(ciphertext); |
| 5533 | psa_aead_abort(&operation); |
| 5534 | PSA_DONE(); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5535 | } |
| 5536 | /* END_CASE */ |
| 5537 | |
| 5538 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5539 | void aead_multipart_set_nonce(int key_type_arg, data_t *key_data, |
| 5540 | int alg_arg, |
| 5541 | int nonce_length_arg, |
| 5542 | int set_lengths_method_arg, |
| 5543 | data_t *additional_data, |
| 5544 | data_t *input_data, |
| 5545 | int expected_status_arg) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5546 | { |
| 5547 | |
| 5548 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5549 | psa_key_type_t key_type = key_type_arg; |
| 5550 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5551 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5552 | uint8_t *nonce_buffer = NULL; |
| 5553 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5554 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5555 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5556 | unsigned char *output = NULL; |
| 5557 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5558 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5559 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5560 | size_t ciphertext_size = 0; |
| 5561 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5562 | size_t tag_length = 0; |
| 5563 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5564 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5565 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
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_ASSERT(psa_crypto_init()); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5569 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5570 | psa_set_key_algorithm(&attributes, alg); |
| 5571 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5573 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5574 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5578 | 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] | 5579 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5580 | TEST_CALLOC(output, output_size); |
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 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5584 | TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5585 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5586 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5588 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5589 | |
| 5590 | /* If the operation is not supported, just skip and not fail in case the |
| 5591 | * encryption involves a common limitation of cryptography hardwares and |
| 5592 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5593 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5594 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5595 | 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] | 5596 | } |
| 5597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | PSA_ASSERT(status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5599 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5600 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5601 | if (nonce_length_arg == -1) { |
| 5602 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5603 | TEST_CALLOC(nonce_buffer, 4); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5604 | nonce_length = 0; |
| 5605 | } else { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5606 | /* If length is zero, then this will return NULL. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | nonce_length = (size_t) nonce_length_arg; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5608 | TEST_CALLOC(nonce_buffer, nonce_length); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5610 | if (nonce_buffer) { |
| 5611 | for (index = 0; index < nonce_length - 1; ++index) { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5612 | nonce_buffer[index] = 'a' + index; |
| 5613 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5614 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5615 | } |
| 5616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5617 | if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) { |
| 5618 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5619 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5620 | } |
| 5621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5622 | status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length); |
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 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5626 | if (expected_status == PSA_SUCCESS) { |
| 5627 | if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) { |
| 5628 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5629 | input_data->len)); |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5630 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5631 | 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] | 5632 | expected_status = PSA_ERROR_BAD_STATE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5633 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5634 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5635 | /* 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] | 5636 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 5637 | additional_data->len), |
| 5638 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5640 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5641 | output, output_size, |
| 5642 | &ciphertext_length), |
| 5643 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5645 | TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5646 | &ciphertext_length, tag_buffer, |
| 5647 | PSA_AEAD_TAG_MAX_SIZE, &tag_length), |
| 5648 | expected_status); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5649 | } |
| 5650 | |
| 5651 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5652 | psa_destroy_key(key); |
| 5653 | mbedtls_free(output); |
| 5654 | mbedtls_free(ciphertext); |
| 5655 | mbedtls_free(nonce_buffer); |
| 5656 | psa_aead_abort(&operation); |
| 5657 | PSA_DONE(); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5658 | } |
| 5659 | /* END_CASE */ |
| 5660 | |
| 5661 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5662 | 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] | 5663 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5664 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5665 | data_t *nonce, |
| 5666 | data_t *additional_data, |
| 5667 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5668 | int expected_status_arg) |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5669 | { |
| 5670 | |
| 5671 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5672 | psa_key_type_t key_type = key_type_arg; |
| 5673 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5674 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5675 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5676 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5677 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5678 | unsigned char *output = NULL; |
| 5679 | unsigned char *ciphertext = NULL; |
| 5680 | size_t output_size = output_size_arg; |
| 5681 | size_t ciphertext_size = 0; |
| 5682 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5683 | size_t tag_length = 0; |
| 5684 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5686 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5688 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5689 | psa_set_key_algorithm(&attributes, alg); |
| 5690 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5692 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5693 | &key)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5695 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5696 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5697 | TEST_CALLOC(output, output_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5699 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5700 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5701 | TEST_CALLOC(ciphertext, ciphertext_size); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5703 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5704 | |
| 5705 | /* If the operation is not supported, just skip and not fail in case the |
| 5706 | * encryption involves a common limitation of cryptography hardwares and |
| 5707 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5708 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5709 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5710 | 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] | 5711 | } |
| 5712 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | PSA_ASSERT(status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5715 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5716 | input_data->len)); |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5718 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5719 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5720 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5721 | additional_data->len)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5723 | status = psa_aead_update(&operation, input_data->x, input_data->len, |
| 5724 | output, output_size, &ciphertext_length); |
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 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5728 | if (expected_status == PSA_SUCCESS) { |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5729 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5730 | PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size, |
| 5731 | &ciphertext_length, tag_buffer, |
| 5732 | PSA_AEAD_TAG_MAX_SIZE, &tag_length)); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5733 | } |
| 5734 | |
| 5735 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5736 | psa_destroy_key(key); |
| 5737 | mbedtls_free(output); |
| 5738 | mbedtls_free(ciphertext); |
| 5739 | psa_aead_abort(&operation); |
| 5740 | PSA_DONE(); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5741 | } |
| 5742 | /* END_CASE */ |
| 5743 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5744 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5745 | void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data, |
| 5746 | int alg_arg, |
| 5747 | int finish_ciphertext_size_arg, |
| 5748 | int tag_size_arg, |
| 5749 | data_t *nonce, |
| 5750 | data_t *additional_data, |
| 5751 | data_t *input_data, |
| 5752 | int expected_status_arg) |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5753 | { |
| 5754 | |
| 5755 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5756 | psa_key_type_t key_type = key_type_arg; |
| 5757 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5758 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5759 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5760 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5761 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5762 | unsigned char *ciphertext = NULL; |
| 5763 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5764 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5765 | size_t ciphertext_size = 0; |
| 5766 | size_t ciphertext_length = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5767 | size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg; |
| 5768 | size_t tag_size = (size_t) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5769 | size_t tag_length = 0; |
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_ASSERT(psa_crypto_init()); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 5774 | psa_set_key_algorithm(&attributes, alg); |
| 5775 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5777 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5778 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5782 | 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] | 5783 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5784 | TEST_CALLOC(ciphertext, 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(finish_ciphertext, finish_ciphertext_size); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5787 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5788 | TEST_CALLOC(tag_buffer, tag_size); |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5790 | status = psa_aead_encrypt_setup(&operation, key, alg); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5791 | |
| 5792 | /* If the operation is not supported, just skip and not fail in case the |
| 5793 | * encryption involves a common limitation of cryptography hardwares and |
| 5794 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5795 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5796 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5797 | 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] | 5798 | } |
| 5799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5800 | PSA_ASSERT(status); |
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_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5804 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 5805 | input_data->len)); |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5807 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5808 | additional_data->len)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5810 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len, |
| 5811 | ciphertext, ciphertext_size, &ciphertext_length)); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5812 | |
| 5813 | /* Ensure we can still complete operation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5814 | status = psa_aead_finish(&operation, finish_ciphertext, |
| 5815 | finish_ciphertext_size, |
| 5816 | &ciphertext_length, tag_buffer, |
| 5817 | tag_size, &tag_length); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5819 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5820 | |
| 5821 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5822 | psa_destroy_key(key); |
| 5823 | mbedtls_free(ciphertext); |
| 5824 | mbedtls_free(finish_ciphertext); |
| 5825 | mbedtls_free(tag_buffer); |
| 5826 | psa_aead_abort(&operation); |
| 5827 | PSA_DONE(); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5828 | } |
| 5829 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5830 | |
| 5831 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5832 | void aead_multipart_verify(int key_type_arg, data_t *key_data, |
| 5833 | int alg_arg, |
| 5834 | data_t *nonce, |
| 5835 | data_t *additional_data, |
| 5836 | data_t *input_data, |
| 5837 | data_t *tag, |
| 5838 | int tag_usage_arg, |
| 5839 | int expected_setup_status_arg, |
| 5840 | int expected_status_arg) |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5841 | { |
| 5842 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5843 | psa_key_type_t key_type = key_type_arg; |
| 5844 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5845 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5846 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5847 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5848 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5849 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5850 | unsigned char *plaintext = NULL; |
| 5851 | unsigned char *finish_plaintext = NULL; |
| 5852 | size_t plaintext_size = 0; |
| 5853 | size_t plaintext_length = 0; |
| 5854 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5855 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5856 | unsigned char *tag_buffer = NULL; |
| 5857 | size_t tag_size = 0; |
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_ASSERT(psa_crypto_init()); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5861 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 5862 | psa_set_key_algorithm(&attributes, alg); |
| 5863 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5864 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5865 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5866 | &key)); |
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 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5870 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, |
| 5871 | input_data->len); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5872 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5873 | TEST_CALLOC(plaintext, plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5876 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 5877 | TEST_CALLOC(finish_plaintext, verify_plaintext_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5880 | |
| 5881 | /* If the operation is not supported, just skip and not fail in case the |
| 5882 | * encryption involves a common limitation of cryptography hardwares and |
| 5883 | * an alternative implementation. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5884 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
| 5885 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8); |
| 5886 | 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] | 5887 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | TEST_EQUAL(status, expected_setup_status); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5890 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5891 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5892 | } |
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(status); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5898 | status = psa_aead_set_lengths(&operation, additional_data->len, |
| 5899 | input_data->len); |
| 5900 | PSA_ASSERT(status); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5902 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 5903 | additional_data->len)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5905 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 5906 | input_data->len, |
| 5907 | plaintext, plaintext_size, |
| 5908 | &plaintext_length)); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5910 | if (tag_usage == USE_GIVEN_TAG) { |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5911 | tag_buffer = tag->x; |
| 5912 | tag_size = tag->len; |
| 5913 | } |
| 5914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5915 | status = psa_aead_verify(&operation, finish_plaintext, |
| 5916 | verify_plaintext_size, |
| 5917 | &plaintext_length, |
| 5918 | tag_buffer, tag_size); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5920 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5921 | |
| 5922 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5923 | psa_destroy_key(key); |
| 5924 | mbedtls_free(plaintext); |
| 5925 | mbedtls_free(finish_plaintext); |
| 5926 | psa_aead_abort(&operation); |
| 5927 | PSA_DONE(); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5928 | } |
| 5929 | /* END_CASE */ |
| 5930 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5931 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5932 | void aead_multipart_setup(int key_type_arg, data_t *key_data, |
| 5933 | int alg_arg, int expected_status_arg) |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5934 | { |
| 5935 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5936 | psa_key_type_t key_type = key_type_arg; |
| 5937 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5938 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5939 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5940 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5941 | psa_status_t expected_status = expected_status_arg; |
| 5942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5943 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5945 | psa_set_key_usage_flags(&attributes, |
| 5946 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5947 | psa_set_key_algorithm(&attributes, alg); |
| 5948 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5950 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 5951 | &key)); |
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 | status = psa_aead_encrypt_setup(&operation, key, alg); |
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 | TEST_EQUAL(status, expected_status); |
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 | psa_aead_abort(&operation); |
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 | status = psa_aead_decrypt_setup(&operation, key, alg); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5961 | TEST_EQUAL(status, expected_status); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5962 | |
| 5963 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5964 | psa_destroy_key(key); |
| 5965 | psa_aead_abort(&operation); |
| 5966 | PSA_DONE(); |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5967 | } |
| 5968 | /* END_CASE */ |
| 5969 | |
| 5970 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5971 | void aead_multipart_state_test(int key_type_arg, data_t *key_data, |
| 5972 | int alg_arg, |
| 5973 | data_t *nonce, |
| 5974 | data_t *additional_data, |
| 5975 | data_t *input_data) |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5976 | { |
| 5977 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5978 | psa_key_type_t key_type = key_type_arg; |
| 5979 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5980 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5981 | unsigned char *output_data = NULL; |
| 5982 | unsigned char *final_data = NULL; |
| 5983 | size_t output_size = 0; |
| 5984 | size_t finish_output_size = 0; |
| 5985 | size_t output_length = 0; |
| 5986 | size_t key_bits = 0; |
| 5987 | size_t tag_length = 0; |
| 5988 | size_t tag_size = 0; |
| 5989 | size_t nonce_length = 0; |
| 5990 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5991 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5992 | size_t output_part_length = 0; |
| 5993 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5995 | PSA_ASSERT(psa_crypto_init()); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5997 | psa_set_key_usage_flags(&attributes, |
| 5998 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 5999 | psa_set_key_algorithm(&attributes, alg); |
| 6000 | psa_set_key_type(&attributes, key_type); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6002 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6003 | &key)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6005 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6006 | key_bits = psa_get_key_bits(&attributes); |
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 | tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg); |
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 | TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6012 | 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] | 6013 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6014 | TEST_CALLOC(output_data, output_size); |
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 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6018 | TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6019 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6020 | TEST_CALLOC(final_data, finish_output_size); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6021 | |
| 6022 | /* Test all operations error without calling setup first. */ |
| 6023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6024 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6025 | PSA_ERROR_BAD_STATE); |
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 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6029 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6030 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6031 | &nonce_length), |
| 6032 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6033 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6034 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6035 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6036 | /* ------------------------------------------------------- */ |
| 6037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6038 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6039 | input_data->len), |
| 6040 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6042 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6043 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6044 | /* ------------------------------------------------------- */ |
| 6045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6046 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6047 | additional_data->len), |
| 6048 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6050 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6051 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6052 | /* ------------------------------------------------------- */ |
| 6053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6054 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6055 | input_data->len, output_data, |
| 6056 | output_size, &output_length), |
| 6057 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6058 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6059 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6060 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6061 | /* ------------------------------------------------------- */ |
| 6062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6063 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6064 | finish_output_size, |
| 6065 | &output_part_length, |
| 6066 | tag_buffer, tag_length, |
| 6067 | &tag_size), |
| 6068 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6070 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6071 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6072 | /* ------------------------------------------------------- */ |
| 6073 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6074 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6075 | finish_output_size, |
| 6076 | &output_part_length, |
| 6077 | tag_buffer, |
| 6078 | tag_length), |
| 6079 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6081 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6082 | |
| 6083 | /* Test for double setups. */ |
| 6084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6085 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6087 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 6088 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6090 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6091 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 6092 | /* ------------------------------------------------------- */ |
| 6093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6094 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6096 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 6097 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6099 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6100 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6101 | /* ------------------------------------------------------- */ |
| 6102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6103 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6105 | TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg), |
| 6106 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6108 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6109 | |
| 6110 | /* ------------------------------------------------------- */ |
| 6111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6112 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6114 | TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg), |
| 6115 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6117 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6118 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6119 | /* Test for not setting a nonce. */ |
| 6120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6121 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6123 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6124 | additional_data->len), |
| 6125 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6127 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6128 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6129 | /* ------------------------------------------------------- */ |
| 6130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6131 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6133 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6134 | input_data->len, output_data, |
| 6135 | output_size, &output_length), |
| 6136 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6138 | psa_aead_abort(&operation); |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 6139 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6140 | /* ------------------------------------------------------- */ |
| 6141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6142 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6144 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6145 | finish_output_size, |
| 6146 | &output_part_length, |
| 6147 | tag_buffer, tag_length, |
| 6148 | &tag_size), |
| 6149 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6151 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6152 | |
| 6153 | /* ------------------------------------------------------- */ |
| 6154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6155 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6157 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6158 | finish_output_size, |
| 6159 | &output_part_length, |
| 6160 | tag_buffer, |
| 6161 | tag_length), |
| 6162 | PSA_ERROR_BAD_STATE); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6164 | psa_aead_abort(&operation); |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 6165 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6166 | /* Test for double setting nonce. */ |
| 6167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6168 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6172 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6173 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6175 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6176 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6177 | /* Test for double generating nonce. */ |
| 6178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6179 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6181 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6182 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6183 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6185 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6186 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6187 | &nonce_length), |
| 6188 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6189 | |
| 6190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6191 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6192 | |
| 6193 | /* Test for generate nonce then set and vice versa */ |
| 6194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6195 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6197 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6198 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6199 | &nonce_length)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6201 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6202 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6204 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6205 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6206 | /* Test for generating nonce after calling set lengths */ |
| 6207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6208 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6210 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6211 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6213 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6214 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6215 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6217 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6218 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6219 | /* 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] | 6220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6221 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6223 | if (operation.alg == PSA_ALG_CCM) { |
| 6224 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6225 | input_data->len), |
| 6226 | PSA_ERROR_INVALID_ARGUMENT); |
| 6227 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6228 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6229 | &nonce_length), |
| 6230 | PSA_ERROR_BAD_STATE); |
| 6231 | } else { |
| 6232 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6233 | input_data->len)); |
| 6234 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6235 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6236 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6237 | } |
| 6238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6239 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6240 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6241 | /* 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] | 6242 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6243 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6245 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6246 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6247 | input_data->len), |
| 6248 | PSA_ERROR_INVALID_ARGUMENT); |
| 6249 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6250 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6251 | &nonce_length), |
| 6252 | PSA_ERROR_BAD_STATE); |
| 6253 | } else { |
| 6254 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6255 | input_data->len)); |
| 6256 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6257 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6258 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6259 | } |
| 6260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6261 | psa_aead_abort(&operation); |
Dave Rodgman | d26d744 | 2023-02-11 17:14:54 +0000 | [diff] [blame] | 6262 | #endif |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6263 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6264 | /* 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] | 6265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6266 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6268 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6269 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6270 | &nonce_length)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6272 | if (operation.alg == PSA_ALG_CCM) { |
| 6273 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6274 | input_data->len), |
| 6275 | PSA_ERROR_INVALID_ARGUMENT); |
| 6276 | } else { |
| 6277 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6278 | input_data->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6279 | } |
| 6280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6281 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6282 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6283 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6284 | /* Test for setting nonce after calling set lengths */ |
| 6285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6286 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6288 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6289 | input_data->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_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6293 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6294 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6295 | /* 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] | 6296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6297 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6299 | if (operation.alg == PSA_ALG_CCM) { |
| 6300 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6301 | input_data->len), |
| 6302 | PSA_ERROR_INVALID_ARGUMENT); |
| 6303 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6304 | PSA_ERROR_BAD_STATE); |
| 6305 | } else { |
| 6306 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6307 | input_data->len)); |
| 6308 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6309 | } |
| 6310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6311 | psa_aead_abort(&operation); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6312 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6313 | /* 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] | 6314 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6315 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6317 | if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) { |
| 6318 | TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6319 | input_data->len), |
| 6320 | PSA_ERROR_INVALID_ARGUMENT); |
| 6321 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6322 | PSA_ERROR_BAD_STATE); |
| 6323 | } else { |
| 6324 | PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX, |
| 6325 | input_data->len)); |
| 6326 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6327 | } |
| 6328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6329 | psa_aead_abort(&operation); |
Dave Rodgman | a476363 | 2023-02-11 18:36:23 +0000 | [diff] [blame] | 6330 | #endif |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6331 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6332 | /* 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] | 6333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6334 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6338 | if (operation.alg == PSA_ALG_CCM) { |
| 6339 | TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6340 | input_data->len), |
| 6341 | PSA_ERROR_INVALID_ARGUMENT); |
| 6342 | } else { |
| 6343 | PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX, |
| 6344 | input_data->len)); |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6345 | } |
| 6346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6347 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6348 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6349 | /* 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] | 6350 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6351 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6353 | if (operation.alg == PSA_ALG_GCM) { |
| 6354 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6355 | SIZE_MAX), |
| 6356 | PSA_ERROR_INVALID_ARGUMENT); |
| 6357 | TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len), |
| 6358 | PSA_ERROR_BAD_STATE); |
| 6359 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6360 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6361 | SIZE_MAX)); |
| 6362 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6363 | } |
| 6364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6365 | psa_aead_abort(&operation); |
Dave Rodgman | 91e8321 | 2023-02-11 20:07:43 +0000 | [diff] [blame] | 6366 | #endif |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6367 | |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 6368 | /* 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] | 6369 | #if SIZE_MAX > UINT32_MAX |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6370 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6374 | if (operation.alg == PSA_ALG_GCM) { |
| 6375 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6376 | SIZE_MAX), |
| 6377 | PSA_ERROR_INVALID_ARGUMENT); |
| 6378 | } else if (operation.alg != PSA_ALG_CCM) { |
| 6379 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6380 | SIZE_MAX)); |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6381 | } |
| 6382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6383 | psa_aead_abort(&operation); |
Dave Rodgman | 641288b | 2023-02-11 22:02:04 +0000 | [diff] [blame] | 6384 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6385 | |
| 6386 | /* ------------------------------------------------------- */ |
| 6387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6388 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6392 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6393 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6394 | &nonce_length), |
| 6395 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6397 | psa_aead_abort(&operation); |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6398 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6399 | /* Test for generating nonce in decrypt setup. */ |
| 6400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6401 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6403 | TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6404 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6405 | &nonce_length), |
| 6406 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6408 | psa_aead_abort(&operation); |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6409 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6410 | /* Test for setting lengths twice. */ |
| 6411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6412 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6416 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6417 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6419 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6420 | input_data->len), |
| 6421 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6423 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6424 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6425 | /* Test for setting lengths after setting nonce + already starting data. */ |
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_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6431 | if (operation.alg == PSA_ALG_CCM) { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6433 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6434 | additional_data->len), |
| 6435 | PSA_ERROR_BAD_STATE); |
| 6436 | } else { |
| 6437 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6438 | additional_data->len)); |
| 6439 | |
| 6440 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6441 | input_data->len), |
| 6442 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6443 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6444 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6445 | |
| 6446 | /* ------------------------------------------------------- */ |
| 6447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6448 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6452 | if (operation.alg == PSA_ALG_CCM) { |
| 6453 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6454 | input_data->len, output_data, |
| 6455 | output_size, &output_length), |
| 6456 | PSA_ERROR_BAD_STATE); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6458 | } else { |
| 6459 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6460 | input_data->len, output_data, |
| 6461 | output_size, &output_length)); |
| 6462 | |
| 6463 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6464 | input_data->len), |
| 6465 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6466 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6467 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6468 | |
| 6469 | /* ------------------------------------------------------- */ |
| 6470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6471 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6475 | if (operation.alg == PSA_ALG_CCM) { |
| 6476 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6477 | finish_output_size, |
| 6478 | &output_part_length, |
| 6479 | tag_buffer, tag_length, |
| 6480 | &tag_size)); |
| 6481 | } else { |
| 6482 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6483 | finish_output_size, |
| 6484 | &output_part_length, |
| 6485 | tag_buffer, tag_length, |
| 6486 | &tag_size)); |
| 6487 | |
| 6488 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6489 | input_data->len), |
| 6490 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6491 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6492 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6493 | |
| 6494 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6496 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6498 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6499 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6500 | &nonce_length)); |
| 6501 | if (operation.alg == PSA_ALG_CCM) { |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6503 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6504 | additional_data->len), |
| 6505 | PSA_ERROR_BAD_STATE); |
| 6506 | } else { |
| 6507 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6508 | additional_data->len)); |
| 6509 | |
| 6510 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6511 | input_data->len), |
| 6512 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6513 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6514 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6515 | |
| 6516 | /* ------------------------------------------------------- */ |
| 6517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6518 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6520 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6521 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6522 | &nonce_length)); |
| 6523 | if (operation.alg == PSA_ALG_CCM) { |
| 6524 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6525 | input_data->len, output_data, |
| 6526 | output_size, &output_length), |
| 6527 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6529 | } else { |
| 6530 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6531 | input_data->len, output_data, |
| 6532 | output_size, &output_length)); |
| 6533 | |
| 6534 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6535 | input_data->len), |
| 6536 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6537 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6538 | psa_aead_abort(&operation); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6539 | |
| 6540 | /* ------------------------------------------------------- */ |
| 6541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6542 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6544 | PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer, |
| 6545 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6546 | &nonce_length)); |
| 6547 | if (operation.alg == PSA_ALG_CCM) { |
| 6548 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6549 | finish_output_size, |
| 6550 | &output_part_length, |
| 6551 | tag_buffer, tag_length, |
| 6552 | &tag_size)); |
| 6553 | } else { |
| 6554 | PSA_ASSERT(psa_aead_finish(&operation, final_data, |
| 6555 | finish_output_size, |
| 6556 | &output_part_length, |
| 6557 | tag_buffer, tag_length, |
| 6558 | &tag_size)); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6560 | TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len, |
| 6561 | input_data->len), |
| 6562 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6563 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6564 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6565 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6566 | /* Test for not sending any additional data or data after setting non zero |
| 6567 | * lengths for them. (encrypt) */ |
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_encrypt_setup(&operation, key, alg)); |
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_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6573 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6574 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6576 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6577 | finish_output_size, |
| 6578 | &output_part_length, |
| 6579 | tag_buffer, tag_length, |
| 6580 | &tag_size), |
| 6581 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6583 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6584 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6585 | /* Test for not sending any additional data or data after setting non-zero |
| 6586 | * lengths for them. (decrypt) */ |
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_decrypt_setup(&operation, key, alg)); |
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_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6592 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6593 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6595 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6596 | finish_output_size, |
| 6597 | &output_part_length, |
| 6598 | tag_buffer, |
| 6599 | tag_length), |
| 6600 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6602 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6603 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6604 | /* Test for not sending any additional data after setting a non-zero length |
| 6605 | * for it. */ |
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_encrypt_setup(&operation, key, alg)); |
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_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6611 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6612 | input_data->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6614 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6615 | input_data->len, output_data, |
| 6616 | output_size, &output_length), |
| 6617 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6619 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6620 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6621 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6623 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6627 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6628 | input_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6630 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6631 | additional_data->len)); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6633 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6634 | finish_output_size, |
| 6635 | &output_part_length, |
| 6636 | tag_buffer, tag_length, |
| 6637 | &tag_size), |
| 6638 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6640 | psa_aead_abort(&operation); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6641 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6642 | /* Test for sending too much additional data after setting lengths. */ |
| 6643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6644 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6648 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6649 | |
| 6650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6651 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6652 | additional_data->len), |
| 6653 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6655 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6656 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6657 | /* ------------------------------------------------------- */ |
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_encrypt_setup(&operation, key, alg)); |
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_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6663 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6664 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6666 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6667 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6669 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6670 | 1), |
| 6671 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6673 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6674 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6675 | /* Test for sending too much data after setting lengths. */ |
| 6676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6677 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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_nonce(&operation, nonce->x, nonce->len)); |
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 | PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0)); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6683 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6684 | input_data->len, output_data, |
| 6685 | output_size, &output_length), |
| 6686 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6688 | psa_aead_abort(&operation); |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6689 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6690 | /* ------------------------------------------------------- */ |
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_encrypt_setup(&operation, key, alg)); |
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_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6696 | PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len, |
| 6697 | input_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6699 | PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x, |
| 6700 | additional_data->len)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6702 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6703 | input_data->len, output_data, |
| 6704 | output_size, &output_length)); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6706 | TEST_EQUAL(psa_aead_update(&operation, input_data->x, |
| 6707 | 1, output_data, |
| 6708 | output_size, &output_length), |
| 6709 | PSA_ERROR_INVALID_ARGUMENT); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6711 | psa_aead_abort(&operation); |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6712 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6713 | /* Test sending additional data after data. */ |
| 6714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6715 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6719 | if (operation.alg != PSA_ALG_CCM) { |
| 6720 | PSA_ASSERT(psa_aead_update(&operation, input_data->x, |
| 6721 | input_data->len, output_data, |
| 6722 | output_size, &output_length)); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6724 | TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x, |
| 6725 | additional_data->len), |
| 6726 | PSA_ERROR_BAD_STATE); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6727 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6728 | psa_aead_abort(&operation); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6729 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6730 | /* Test calling finish on decryption. */ |
| 6731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6732 | PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6736 | TEST_EQUAL(psa_aead_finish(&operation, final_data, |
| 6737 | finish_output_size, |
| 6738 | &output_part_length, |
| 6739 | tag_buffer, tag_length, |
| 6740 | &tag_size), |
| 6741 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6743 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6744 | |
| 6745 | /* Test calling verify on encryption. */ |
| 6746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6747 | PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg)); |
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 | PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len)); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6751 | TEST_EQUAL(psa_aead_verify(&operation, final_data, |
| 6752 | finish_output_size, |
| 6753 | &output_part_length, |
| 6754 | tag_buffer, |
| 6755 | tag_length), |
| 6756 | PSA_ERROR_BAD_STATE); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6758 | psa_aead_abort(&operation); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6759 | |
| 6760 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6761 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6762 | psa_destroy_key(key); |
| 6763 | psa_aead_abort(&operation); |
| 6764 | mbedtls_free(output_data); |
| 6765 | mbedtls_free(final_data); |
| 6766 | PSA_DONE(); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6767 | } |
| 6768 | /* END_CASE */ |
| 6769 | |
| 6770 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6771 | void signature_size(int type_arg, |
| 6772 | int bits, |
| 6773 | int alg_arg, |
| 6774 | int expected_size_arg) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6775 | { |
| 6776 | psa_key_type_t type = type_arg; |
| 6777 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6778 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6780 | TEST_EQUAL(actual_size, (size_t) expected_size_arg); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6781 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6782 | exit: |
| 6783 | ; |
| 6784 | } |
| 6785 | /* END_CASE */ |
| 6786 | |
| 6787 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6788 | void sign_hash_deterministic(int key_type_arg, data_t *key_data, |
| 6789 | int alg_arg, data_t *input_data, |
| 6790 | data_t *output_data) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6791 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6792 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6793 | psa_key_type_t key_type = key_type_arg; |
| 6794 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6795 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6796 | unsigned char *signature = NULL; |
| 6797 | size_t signature_size; |
| 6798 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6799 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6803 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6804 | psa_set_key_algorithm(&attributes, alg); |
| 6805 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6807 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6808 | &key)); |
| 6809 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6810 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6811 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6812 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6813 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6814 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6815 | key_bits, alg); |
| 6816 | TEST_ASSERT(signature_size != 0); |
| 6817 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6818 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6819 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6820 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6821 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 6822 | input_data->x, input_data->len, |
| 6823 | signature, signature_size, |
| 6824 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6825 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6826 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6827 | signature, signature_length); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6828 | |
| 6829 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6830 | /* |
| 6831 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6832 | * thus reset them as required. |
| 6833 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6834 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6836 | psa_destroy_key(key); |
| 6837 | mbedtls_free(signature); |
| 6838 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6839 | } |
| 6840 | /* END_CASE */ |
| 6841 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6842 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6843 | /** |
| 6844 | * sign_hash_interruptible() test intentions: |
| 6845 | * |
| 6846 | * Note: This test can currently only handle ECDSA. |
| 6847 | * |
| 6848 | * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 6849 | * and private keys / keypairs only). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 6850 | * |
| 6851 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 6852 | * expected for different max_ops values. |
| 6853 | * |
| 6854 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 6855 | * and that each successful stage completes some ops (this is not mandated by |
| 6856 | * the PSA specification, but is currently the case). |
| 6857 | * |
| 6858 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 6859 | * complete() calls does not alter the number of ops returned. |
| 6860 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6861 | void sign_hash_interruptible(int key_type_arg, data_t *key_data, |
| 6862 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6863 | data_t *output_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6864 | { |
| 6865 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6866 | psa_key_type_t key_type = key_type_arg; |
| 6867 | psa_algorithm_t alg = alg_arg; |
| 6868 | size_t key_bits; |
| 6869 | unsigned char *signature = NULL; |
| 6870 | size_t signature_size; |
| 6871 | size_t signature_length = 0xdeadbeef; |
| 6872 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6873 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6874 | uint32_t num_ops = 0; |
| 6875 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6876 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6877 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6878 | size_t min_completes = 0; |
| 6879 | size_t max_completes = 0; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 6880 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6881 | psa_sign_hash_interruptible_operation_t operation = |
| 6882 | psa_sign_hash_interruptible_operation_init(); |
| 6883 | |
| 6884 | PSA_ASSERT(psa_crypto_init()); |
| 6885 | |
| 6886 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6887 | psa_set_key_algorithm(&attributes, alg); |
| 6888 | psa_set_key_type(&attributes, key_type); |
| 6889 | |
| 6890 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6891 | &key)); |
| 6892 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 6893 | key_bits = psa_get_key_bits(&attributes); |
| 6894 | |
| 6895 | /* Allocate a buffer which has the size advertised by the |
| 6896 | * library. */ |
| 6897 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 6898 | key_bits, alg); |
| 6899 | TEST_ASSERT(signature_size != 0); |
| 6900 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6901 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6902 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6903 | psa_interruptible_set_max_ops(max_ops); |
| 6904 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 6905 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 6906 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 6907 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6908 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6909 | TEST_ASSERT(num_ops_prior == 0); |
| 6910 | |
| 6911 | /* Start performing the signature. */ |
| 6912 | PSA_ASSERT(psa_sign_hash_start(&operation, key, alg, |
| 6913 | input_data->x, input_data->len)); |
| 6914 | |
| 6915 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 6916 | TEST_ASSERT(num_ops_prior == 0); |
| 6917 | |
| 6918 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6919 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6920 | status = psa_sign_hash_complete(&operation, signature, signature_size, |
| 6921 | &signature_length); |
| 6922 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6923 | num_completes++; |
| 6924 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6925 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 6926 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 6927 | /* We are asserting here that every complete makes progress |
| 6928 | * (completes some ops), which is true of the internal |
| 6929 | * implementation and probably any implementation, however this is |
| 6930 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6931 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6932 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6933 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 6934 | |
| 6935 | /* Ensure calling get_num_ops() twice still returns the same |
| 6936 | * number of ops as previously reported. */ |
| 6937 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6938 | |
| 6939 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6940 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 6941 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6942 | |
| 6943 | TEST_ASSERT(status == PSA_SUCCESS); |
| 6944 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 6945 | TEST_LE_U(min_completes, num_completes); |
| 6946 | TEST_LE_U(num_completes, max_completes); |
| 6947 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6948 | /* Verify that the signature is what is expected. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 6949 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 6950 | signature, signature_length); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6951 | |
| 6952 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 6953 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 6954 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 6955 | TEST_ASSERT(num_ops == 0); |
| 6956 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 6957 | exit: |
| 6958 | |
| 6959 | /* |
| 6960 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6961 | * thus reset them as required. |
| 6962 | */ |
| 6963 | psa_reset_key_attributes(&attributes); |
| 6964 | |
| 6965 | psa_destroy_key(key); |
| 6966 | mbedtls_free(signature); |
| 6967 | PSA_DONE(); |
| 6968 | } |
| 6969 | /* END_CASE */ |
| 6970 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6971 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6972 | void sign_hash_fail(int key_type_arg, data_t *key_data, |
| 6973 | int alg_arg, data_t *input_data, |
| 6974 | int signature_size_arg, int expected_status_arg) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6975 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6976 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6977 | psa_key_type_t key_type = key_type_arg; |
| 6978 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6979 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6980 | psa_status_t actual_status; |
| 6981 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6982 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6983 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6984 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6985 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 6986 | TEST_CALLOC(signature, signature_size); |
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_ASSERT(psa_crypto_init()); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6990 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 6991 | psa_set_key_algorithm(&attributes, alg); |
| 6992 | psa_set_key_type(&attributes, key_type); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6994 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 6995 | &key)); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6997 | actual_status = psa_sign_hash(key, alg, |
| 6998 | input_data->x, input_data->len, |
| 6999 | signature, signature_size, |
| 7000 | &signature_length); |
| 7001 | TEST_EQUAL(actual_status, expected_status); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 7002 | /* The value of *signature_length is unspecified on error, but |
| 7003 | * whatever it is, it should be less than signature_size, so that |
| 7004 | * if the caller tries to read *signature_length bytes without |
| 7005 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7006 | TEST_LE_U(signature_length, signature_size); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7007 | |
| 7008 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7009 | psa_reset_key_attributes(&attributes); |
| 7010 | psa_destroy_key(key); |
| 7011 | mbedtls_free(signature); |
| 7012 | PSA_DONE(); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 7013 | } |
| 7014 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 7015 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7016 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7017 | /** |
| 7018 | * sign_hash_fail_interruptible() test intentions: |
| 7019 | * |
| 7020 | * Note: This test can currently only handle ECDSA. |
| 7021 | * |
| 7022 | * 1. Test that various failure cases for interruptible sign hash fail with the |
| 7023 | * correct error codes, and at the correct point (at start or during |
| 7024 | * complete). |
| 7025 | * |
| 7026 | * 2. Test the number of calls to psa_sign_hash_complete() required are as |
| 7027 | * expected for different max_ops values. |
| 7028 | * |
| 7029 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7030 | * and that each successful stage completes some ops (this is not mandated by |
| 7031 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7032 | * |
| 7033 | * 4. Check that calling complete() when start() fails and complete() |
| 7034 | * after completion results in a BAD_STATE error. |
| 7035 | * |
| 7036 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7037 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7038 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7039 | void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7040 | int alg_arg, data_t *input_data, |
| 7041 | int signature_size_arg, |
| 7042 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7043 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7044 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7045 | { |
| 7046 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7047 | psa_key_type_t key_type = key_type_arg; |
| 7048 | psa_algorithm_t alg = alg_arg; |
| 7049 | size_t signature_size = signature_size_arg; |
| 7050 | psa_status_t actual_status; |
| 7051 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7052 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7053 | unsigned char *signature = NULL; |
| 7054 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7055 | uint32_t num_ops = 0; |
| 7056 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7057 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7058 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7059 | size_t min_completes = 0; |
| 7060 | size_t max_completes = 0; |
| 7061 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7062 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7063 | psa_sign_hash_interruptible_operation_t operation = |
| 7064 | psa_sign_hash_interruptible_operation_init(); |
| 7065 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7066 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7067 | |
| 7068 | PSA_ASSERT(psa_crypto_init()); |
| 7069 | |
| 7070 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 7071 | psa_set_key_algorithm(&attributes, alg); |
| 7072 | psa_set_key_type(&attributes, key_type); |
| 7073 | |
| 7074 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7075 | &key)); |
| 7076 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7077 | psa_interruptible_set_max_ops(max_ops); |
| 7078 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7079 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7080 | expected_complete_status, |
| 7081 | &min_completes, |
| 7082 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7083 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7084 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 7085 | TEST_ASSERT(num_ops_prior == 0); |
| 7086 | |
| 7087 | /* Start performing the signature. */ |
| 7088 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 7089 | input_data->x, input_data->len); |
| 7090 | |
| 7091 | TEST_EQUAL(actual_status, expected_start_status); |
| 7092 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7093 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7094 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7095 | * start failed. */ |
| 7096 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7097 | signature_size, |
| 7098 | &signature_length); |
| 7099 | |
| 7100 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7101 | |
| 7102 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7103 | actual_status = psa_sign_hash_start(&operation, key, alg, |
| 7104 | input_data->x, input_data->len); |
| 7105 | |
| 7106 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7107 | } |
| 7108 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7109 | num_ops_prior = psa_sign_hash_get_num_ops(&operation); |
| 7110 | TEST_ASSERT(num_ops_prior == 0); |
| 7111 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7112 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7113 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7114 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7115 | signature_size, |
| 7116 | &signature_length); |
| 7117 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7118 | num_completes++; |
| 7119 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7120 | if (actual_status == PSA_SUCCESS || |
| 7121 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7122 | num_ops = psa_sign_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7123 | /* We are asserting here that every complete makes progress |
| 7124 | * (completes some ops), which is true of the internal |
| 7125 | * implementation and probably any implementation, however this is |
| 7126 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7127 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7128 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7129 | num_ops_prior = num_ops; |
| 7130 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7131 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7132 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7133 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7134 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7135 | /* Check that another complete returns BAD_STATE. */ |
| 7136 | actual_status = psa_sign_hash_complete(&operation, signature, |
| 7137 | signature_size, |
| 7138 | &signature_length); |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7139 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7140 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7141 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7142 | PSA_ASSERT(psa_sign_hash_abort(&operation)); |
| 7143 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7144 | num_ops = psa_sign_hash_get_num_ops(&operation); |
| 7145 | TEST_ASSERT(num_ops == 0); |
| 7146 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7147 | /* The value of *signature_length is unspecified on error, but |
| 7148 | * whatever it is, it should be less than signature_size, so that |
| 7149 | * if the caller tries to read *signature_length bytes without |
| 7150 | * checking the error code then they don't overflow a buffer. */ |
| 7151 | TEST_LE_U(signature_length, signature_size); |
| 7152 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7153 | TEST_LE_U(min_completes, num_completes); |
| 7154 | TEST_LE_U(num_completes, max_completes); |
| 7155 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7156 | exit: |
| 7157 | psa_reset_key_attributes(&attributes); |
| 7158 | psa_destroy_key(key); |
| 7159 | mbedtls_free(signature); |
| 7160 | PSA_DONE(); |
| 7161 | } |
| 7162 | /* END_CASE */ |
| 7163 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 7164 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7165 | void sign_verify_hash(int key_type_arg, data_t *key_data, |
| 7166 | int alg_arg, data_t *input_data) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7167 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7168 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7169 | psa_key_type_t key_type = key_type_arg; |
| 7170 | psa_algorithm_t alg = alg_arg; |
| 7171 | size_t key_bits; |
| 7172 | unsigned char *signature = NULL; |
| 7173 | size_t signature_size; |
| 7174 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7175 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7179 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH); |
| 7180 | psa_set_key_algorithm(&attributes, alg); |
| 7181 | psa_set_key_type(&attributes, key_type); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7183 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7184 | &key)); |
| 7185 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7186 | key_bits = psa_get_key_bits(&attributes); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7187 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7188 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7189 | * library. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7190 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7191 | key_bits, alg); |
| 7192 | TEST_ASSERT(signature_size != 0); |
| 7193 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7194 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7195 | |
| 7196 | /* Perform the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7197 | PSA_ASSERT(psa_sign_hash(key, alg, |
| 7198 | input_data->x, input_data->len, |
| 7199 | signature, signature_size, |
| 7200 | &signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7201 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7202 | TEST_LE_U(signature_length, signature_size); |
| 7203 | TEST_ASSERT(signature_length > 0); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7204 | |
| 7205 | /* Use the library to verify that the signature is correct. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7206 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7207 | input_data->x, input_data->len, |
| 7208 | signature, signature_length)); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7210 | if (input_data->len != 0) { |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7211 | /* Flip a bit in the input and verify that the signature is now |
| 7212 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7213 | * because ECDSA may ignore the last few bits of the input. */ |
| 7214 | input_data->x[0] ^= 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7215 | TEST_EQUAL(psa_verify_hash(key, alg, |
| 7216 | input_data->x, input_data->len, |
| 7217 | signature, signature_length), |
| 7218 | PSA_ERROR_INVALID_SIGNATURE); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7219 | } |
| 7220 | |
| 7221 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7222 | /* |
| 7223 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7224 | * thus reset them as required. |
| 7225 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7226 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7228 | psa_destroy_key(key); |
| 7229 | mbedtls_free(signature); |
| 7230 | PSA_DONE(); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7231 | } |
| 7232 | /* END_CASE */ |
| 7233 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7234 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7235 | /** |
| 7236 | * sign_verify_hash_interruptible() test intentions: |
| 7237 | * |
| 7238 | * Note: This test can currently only handle ECDSA. |
| 7239 | * |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7240 | * 1. Test that we can sign an input hash with the given keypair and then |
| 7241 | * afterwards verify that signature. This is currently the only way to test |
| 7242 | * non deterministic ECDSA, but this test can also handle deterministic. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7243 | * |
| 7244 | * 2. Test that after corrupting the hash, the verification detects an invalid |
| 7245 | * signature. |
| 7246 | * |
| 7247 | * 3. Test the number of calls to psa_sign_hash_complete() required are as |
| 7248 | * expected for different max_ops values. |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7249 | * |
| 7250 | * 4. Test that the number of ops done prior to starting signing and after abort |
| 7251 | * is zero and that each successful signing stage completes some ops (this is |
| 7252 | * not mandated by the PSA specification, but is currently the case). |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7253 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7254 | 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] | 7255 | int alg_arg, data_t *input_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7256 | int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7257 | { |
| 7258 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7259 | psa_key_type_t key_type = key_type_arg; |
| 7260 | psa_algorithm_t alg = alg_arg; |
| 7261 | size_t key_bits; |
| 7262 | unsigned char *signature = NULL; |
| 7263 | size_t signature_size; |
| 7264 | size_t signature_length = 0xdeadbeef; |
| 7265 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7266 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7267 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7268 | uint32_t num_ops = 0; |
| 7269 | uint32_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7270 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7271 | size_t min_completes = 0; |
| 7272 | size_t max_completes = 0; |
| 7273 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7274 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7275 | psa_sign_hash_interruptible_operation_init(); |
| 7276 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7277 | psa_verify_hash_interruptible_operation_init(); |
| 7278 | |
| 7279 | PSA_ASSERT(psa_crypto_init()); |
| 7280 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7281 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7282 | PSA_KEY_USAGE_VERIFY_HASH); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7283 | psa_set_key_algorithm(&attributes, alg); |
| 7284 | psa_set_key_type(&attributes, key_type); |
| 7285 | |
| 7286 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7287 | &key)); |
| 7288 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7289 | key_bits = psa_get_key_bits(&attributes); |
| 7290 | |
| 7291 | /* Allocate a buffer which has the size advertised by the |
| 7292 | * library. */ |
| 7293 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7294 | key_bits, alg); |
| 7295 | TEST_ASSERT(signature_size != 0); |
| 7296 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7297 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7298 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7299 | psa_interruptible_set_max_ops(max_ops); |
| 7300 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7301 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7302 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7303 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7304 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7305 | TEST_ASSERT(num_ops_prior == 0); |
| 7306 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7307 | /* Start performing the signature. */ |
| 7308 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7309 | input_data->x, input_data->len)); |
| 7310 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7311 | num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation); |
| 7312 | TEST_ASSERT(num_ops_prior == 0); |
| 7313 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7314 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7315 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7316 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7317 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 7318 | signature_size, |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7319 | &signature_length); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7320 | |
| 7321 | num_completes++; |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7322 | |
| 7323 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7324 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7325 | /* We are asserting here that every complete makes progress |
| 7326 | * (completes some ops), which is true of the internal |
| 7327 | * implementation and probably any implementation, however this is |
| 7328 | * not mandated by the PSA specification. */ |
| 7329 | TEST_ASSERT(num_ops > num_ops_prior); |
| 7330 | |
| 7331 | num_ops_prior = num_ops; |
| 7332 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7333 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7334 | |
| 7335 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7336 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7337 | TEST_LE_U(min_completes, num_completes); |
| 7338 | TEST_LE_U(num_completes, max_completes); |
| 7339 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7340 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7341 | |
Paul Elliott | 7c17308 | 2023-02-26 18:44:45 +0000 | [diff] [blame] | 7342 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 7343 | TEST_ASSERT(num_ops == 0); |
| 7344 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7345 | /* Check that the signature length looks sensible. */ |
| 7346 | TEST_LE_U(signature_length, signature_size); |
| 7347 | TEST_ASSERT(signature_length > 0); |
| 7348 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7349 | num_completes = 0; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7350 | |
| 7351 | /* Start verification. */ |
| 7352 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7353 | input_data->x, input_data->len, |
| 7354 | signature, signature_length)); |
| 7355 | |
| 7356 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7357 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7358 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7359 | |
| 7360 | num_completes++; |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7361 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7362 | |
| 7363 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7364 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7365 | TEST_LE_U(min_completes, num_completes); |
| 7366 | TEST_LE_U(num_completes, max_completes); |
| 7367 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7368 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7369 | |
| 7370 | verify_operation = psa_verify_hash_interruptible_operation_init(); |
| 7371 | |
| 7372 | if (input_data->len != 0) { |
| 7373 | /* Flip a bit in the input and verify that the signature is now |
| 7374 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 7375 | * because ECDSA may ignore the last few bits of the input. */ |
| 7376 | input_data->x[0] ^= 1; |
| 7377 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7378 | /* Start verification. */ |
| 7379 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7380 | input_data->x, input_data->len, |
| 7381 | signature, signature_length)); |
| 7382 | |
| 7383 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7384 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7385 | status = psa_verify_hash_complete(&verify_operation); |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7386 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7387 | |
| 7388 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7389 | } |
| 7390 | |
| 7391 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7392 | |
| 7393 | exit: |
| 7394 | /* |
| 7395 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7396 | * thus reset them as required. |
| 7397 | */ |
| 7398 | psa_reset_key_attributes(&attributes); |
| 7399 | |
| 7400 | psa_destroy_key(key); |
| 7401 | mbedtls_free(signature); |
| 7402 | PSA_DONE(); |
| 7403 | } |
| 7404 | /* END_CASE */ |
| 7405 | |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 7406 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7407 | void verify_hash(int key_type_arg, data_t *key_data, |
| 7408 | int alg_arg, data_t *hash_data, |
| 7409 | data_t *signature_data) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7410 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7411 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7412 | psa_key_type_t key_type = key_type_arg; |
| 7413 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7414 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7416 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 7417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7418 | PSA_ASSERT(psa_crypto_init()); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7420 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7421 | psa_set_key_algorithm(&attributes, alg); |
| 7422 | psa_set_key_type(&attributes, key_type); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7424 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7425 | &key)); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7427 | PSA_ASSERT(psa_verify_hash(key, alg, |
| 7428 | hash_data->x, hash_data->len, |
| 7429 | signature_data->x, signature_data->len)); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 7430 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7431 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7432 | psa_reset_key_attributes(&attributes); |
| 7433 | psa_destroy_key(key); |
| 7434 | PSA_DONE(); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 7435 | } |
| 7436 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7437 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7438 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7439 | /** |
| 7440 | * verify_hash_interruptible() test intentions: |
| 7441 | * |
| 7442 | * Note: This test can currently only handle ECDSA. |
| 7443 | * |
| 7444 | * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA |
Paul Elliott | 8c09205 | 2023-03-06 17:49:14 +0000 | [diff] [blame] | 7445 | * only). Given this test only does verification it can accept public keys as |
| 7446 | * well as private keys / keypairs. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7447 | * |
| 7448 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7449 | * expected for different max_ops values. |
| 7450 | * |
| 7451 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7452 | * and that each successful stage completes some ops (this is not mandated by |
| 7453 | * the PSA specification, but is currently the case). |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7454 | * |
| 7455 | * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between |
| 7456 | * complete() calls does not alter the number of ops returned. |
| 7457 | * |
| 7458 | * 5. Test that after corrupting the hash, the verification detects an invalid |
| 7459 | * signature. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7460 | */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7461 | void verify_hash_interruptible(int key_type_arg, data_t *key_data, |
| 7462 | int alg_arg, data_t *hash_data, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7463 | data_t *signature_data, int max_ops_arg) |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7464 | { |
| 7465 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7466 | psa_key_type_t key_type = key_type_arg; |
| 7467 | psa_algorithm_t alg = alg_arg; |
| 7468 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7469 | psa_status_t status = PSA_OPERATION_INCOMPLETE; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7470 | uint32_t num_ops = 0; |
| 7471 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7472 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7473 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7474 | size_t min_completes = 0; |
| 7475 | size_t max_completes = 0; |
| 7476 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7477 | psa_verify_hash_interruptible_operation_t operation = |
| 7478 | psa_verify_hash_interruptible_operation_init(); |
| 7479 | |
| 7480 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 7481 | |
| 7482 | PSA_ASSERT(psa_crypto_init()); |
| 7483 | |
| 7484 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7485 | psa_set_key_algorithm(&attributes, alg); |
| 7486 | psa_set_key_type(&attributes, key_type); |
| 7487 | |
| 7488 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7489 | &key)); |
| 7490 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7491 | psa_interruptible_set_max_ops(max_ops); |
| 7492 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7493 | interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS, |
| 7494 | &min_completes, &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7495 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7496 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7497 | |
| 7498 | TEST_ASSERT(num_ops_prior == 0); |
| 7499 | |
| 7500 | /* Start verification. */ |
| 7501 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7502 | hash_data->x, hash_data->len, |
| 7503 | signature_data->x, signature_data->len) |
| 7504 | ); |
| 7505 | |
| 7506 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7507 | |
| 7508 | TEST_ASSERT(num_ops_prior == 0); |
| 7509 | |
| 7510 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7511 | do { |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7512 | status = psa_verify_hash_complete(&operation); |
| 7513 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7514 | num_completes++; |
| 7515 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7516 | if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) { |
| 7517 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7518 | /* We are asserting here that every complete makes progress |
| 7519 | * (completes some ops), which is true of the internal |
| 7520 | * implementation and probably any implementation, however this is |
| 7521 | * not mandated by the PSA specification. */ |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7522 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7523 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7524 | num_ops_prior = num_ops; |
Paul Elliott | f8e5b56 | 2023-02-19 18:43:45 +0000 | [diff] [blame] | 7525 | |
| 7526 | /* Ensure calling get_num_ops() twice still returns the same |
| 7527 | * number of ops as previously reported. */ |
| 7528 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7529 | |
| 7530 | TEST_EQUAL(num_ops, num_ops_prior); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7531 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7532 | } while (status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7533 | |
| 7534 | TEST_ASSERT(status == PSA_SUCCESS); |
| 7535 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7536 | TEST_LE_U(min_completes, num_completes); |
| 7537 | TEST_LE_U(num_completes, max_completes); |
| 7538 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7539 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7540 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7541 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7542 | TEST_ASSERT(num_ops == 0); |
| 7543 | |
Paul Elliott | 8359c14 | 2023-02-24 18:40:10 +0000 | [diff] [blame] | 7544 | if (hash_data->len != 0) { |
| 7545 | /* Flip a bit in the hash and verify that the signature is now detected |
| 7546 | * as invalid. Flip a bit at the beginning, not at the end, because |
| 7547 | * ECDSA may ignore the last few bits of the input. */ |
| 7548 | hash_data->x[0] ^= 1; |
| 7549 | |
| 7550 | /* Start verification. */ |
| 7551 | PSA_ASSERT(psa_verify_hash_start(&operation, key, alg, |
| 7552 | hash_data->x, hash_data->len, |
| 7553 | signature_data->x, signature_data->len)); |
| 7554 | |
| 7555 | /* Continue performing the signature until complete. */ |
| 7556 | do { |
| 7557 | status = psa_verify_hash_complete(&operation); |
| 7558 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 7559 | |
| 7560 | TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE); |
| 7561 | } |
| 7562 | |
Paul Elliott | 712d512 | 2022-12-07 14:03:10 +0000 | [diff] [blame] | 7563 | exit: |
| 7564 | psa_reset_key_attributes(&attributes); |
| 7565 | psa_destroy_key(key); |
| 7566 | PSA_DONE(); |
| 7567 | } |
| 7568 | /* END_CASE */ |
| 7569 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7570 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7571 | void verify_hash_fail(int key_type_arg, data_t *key_data, |
| 7572 | int alg_arg, data_t *hash_data, |
| 7573 | data_t *signature_data, |
| 7574 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7575 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7576 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7577 | psa_key_type_t key_type = key_type_arg; |
| 7578 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7579 | psa_status_t actual_status; |
| 7580 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7581 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7585 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7586 | psa_set_key_algorithm(&attributes, alg); |
| 7587 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7589 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7590 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7592 | actual_status = psa_verify_hash(key, alg, |
| 7593 | hash_data->x, hash_data->len, |
| 7594 | signature_data->x, signature_data->len); |
| 7595 | TEST_EQUAL(actual_status, expected_status); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7596 | |
| 7597 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 7598 | psa_reset_key_attributes(&attributes); |
| 7599 | psa_destroy_key(key); |
| 7600 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7601 | } |
| 7602 | /* END_CASE */ |
| 7603 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7604 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7605 | /** |
| 7606 | * verify_hash_fail_interruptible() test intentions: |
| 7607 | * |
| 7608 | * Note: This test can currently only handle ECDSA. |
| 7609 | * |
| 7610 | * 1. Test that various failure cases for interruptible verify hash fail with |
| 7611 | * the correct error codes, and at the correct point (at start or during |
| 7612 | * complete). |
| 7613 | * |
| 7614 | * 2. Test the number of calls to psa_verify_hash_complete() required are as |
| 7615 | * expected for different max_ops values. |
| 7616 | * |
| 7617 | * 3. Test that the number of ops done prior to start and after abort is zero |
| 7618 | * and that each successful stage completes some ops (this is not mandated by |
| 7619 | * the PSA specification, but is currently the case). |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7620 | * |
| 7621 | * 4. Check that calling complete() when start() fails and complete() |
| 7622 | * after completion results in a BAD_STATE error. |
| 7623 | * |
| 7624 | * 5. Check that calling start() again after start fails results in a BAD_STATE |
| 7625 | * error. |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7626 | */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7627 | void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data, |
| 7628 | int alg_arg, data_t *hash_data, |
| 7629 | data_t *signature_data, |
| 7630 | int expected_start_status_arg, |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7631 | int expected_complete_status_arg, |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7632 | int max_ops_arg) |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7633 | { |
| 7634 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7635 | psa_key_type_t key_type = key_type_arg; |
| 7636 | psa_algorithm_t alg = alg_arg; |
| 7637 | psa_status_t actual_status; |
| 7638 | psa_status_t expected_start_status = expected_start_status_arg; |
| 7639 | psa_status_t expected_complete_status = expected_complete_status_arg; |
| 7640 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | ab7c5c8 | 2023-02-03 15:49:42 +0000 | [diff] [blame] | 7641 | uint32_t num_ops = 0; |
| 7642 | uint32_t max_ops = max_ops_arg; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7643 | size_t num_ops_prior = 0; |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7644 | size_t num_completes = 0; |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7645 | size_t min_completes = 0; |
| 7646 | size_t max_completes = 0; |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7647 | psa_verify_hash_interruptible_operation_t operation = |
| 7648 | psa_verify_hash_interruptible_operation_init(); |
| 7649 | |
| 7650 | PSA_ASSERT(psa_crypto_init()); |
| 7651 | |
| 7652 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 7653 | psa_set_key_algorithm(&attributes, alg); |
| 7654 | psa_set_key_type(&attributes, key_type); |
| 7655 | |
| 7656 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7657 | &key)); |
| 7658 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7659 | psa_interruptible_set_max_ops(max_ops); |
| 7660 | |
Paul Elliott | 6f60037 | 2023-02-06 18:41:05 +0000 | [diff] [blame] | 7661 | interruptible_signverify_get_minmax_completes(max_ops, |
| 7662 | expected_complete_status, |
| 7663 | &min_completes, |
| 7664 | &max_completes); |
Paul Elliott | 97ac7d9 | 2023-01-23 18:09:06 +0000 | [diff] [blame] | 7665 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7666 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7667 | TEST_ASSERT(num_ops_prior == 0); |
| 7668 | |
| 7669 | /* Start verification. */ |
| 7670 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7671 | hash_data->x, hash_data->len, |
| 7672 | signature_data->x, |
| 7673 | signature_data->len); |
| 7674 | |
| 7675 | TEST_EQUAL(actual_status, expected_start_status); |
| 7676 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7677 | if (expected_start_status != PSA_SUCCESS) { |
Paul Elliott | de7c31e | 2023-03-01 14:37:48 +0000 | [diff] [blame] | 7678 | /* Emulate poor application code, and call complete anyway, even though |
Paul Elliott | 587e780 | 2023-02-27 09:53:08 +0000 | [diff] [blame] | 7679 | * start failed. */ |
| 7680 | actual_status = psa_verify_hash_complete(&operation); |
| 7681 | |
| 7682 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7683 | |
| 7684 | /* Test that calling start again after failure also causes BAD_STATE. */ |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7685 | actual_status = psa_verify_hash_start(&operation, key, alg, |
| 7686 | hash_data->x, hash_data->len, |
| 7687 | signature_data->x, |
| 7688 | signature_data->len); |
| 7689 | |
| 7690 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
| 7691 | } |
| 7692 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7693 | num_ops_prior = psa_verify_hash_get_num_ops(&operation); |
| 7694 | TEST_ASSERT(num_ops_prior == 0); |
| 7695 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7696 | /* Continue performing the signature until complete. */ |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7697 | do { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7698 | actual_status = psa_verify_hash_complete(&operation); |
| 7699 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7700 | num_completes++; |
| 7701 | |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7702 | if (actual_status == PSA_SUCCESS || |
| 7703 | actual_status == PSA_OPERATION_INCOMPLETE) { |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7704 | num_ops = psa_verify_hash_get_num_ops(&operation); |
Paul Elliott | 96b89b2 | 2023-02-15 23:10:37 +0000 | [diff] [blame] | 7705 | /* We are asserting here that every complete makes progress |
| 7706 | * (completes some ops), which is true of the internal |
| 7707 | * implementation and probably any implementation, however this is |
| 7708 | * not mandated by the PSA specification. */ |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7709 | TEST_ASSERT(num_ops > num_ops_prior); |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7710 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7711 | num_ops_prior = num_ops; |
| 7712 | } |
Paul Elliott | edfc883 | 2023-01-20 17:13:10 +0000 | [diff] [blame] | 7713 | } while (actual_status == PSA_OPERATION_INCOMPLETE); |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7714 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7715 | TEST_EQUAL(actual_status, expected_complete_status); |
| 7716 | |
Paul Elliott | efebad0 | 2023-02-15 16:56:45 +0000 | [diff] [blame] | 7717 | /* Check that another complete returns BAD_STATE. */ |
| 7718 | actual_status = psa_verify_hash_complete(&operation); |
| 7719 | TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE); |
Paul Elliott | 334d726 | 2023-01-20 17:29:41 +0000 | [diff] [blame] | 7720 | |
Paul Elliott | 0c68335 | 2022-12-16 19:16:56 +0000 | [diff] [blame] | 7721 | TEST_LE_U(min_completes, num_completes); |
| 7722 | TEST_LE_U(num_completes, max_completes); |
| 7723 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7724 | PSA_ASSERT(psa_verify_hash_abort(&operation)); |
| 7725 | |
Paul Elliott | 59ad945 | 2022-12-18 15:09:02 +0000 | [diff] [blame] | 7726 | num_ops = psa_verify_hash_get_num_ops(&operation); |
| 7727 | TEST_ASSERT(num_ops == 0); |
| 7728 | |
Paul Elliott | 9100797 | 2022-12-16 12:21:24 +0000 | [diff] [blame] | 7729 | exit: |
| 7730 | psa_reset_key_attributes(&attributes); |
| 7731 | psa_destroy_key(key); |
| 7732 | PSA_DONE(); |
| 7733 | } |
| 7734 | /* END_CASE */ |
| 7735 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7736 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7737 | /** |
| 7738 | * interruptible_signverify_hash_state_test() test intentions: |
| 7739 | * |
| 7740 | * Note: This test can currently only handle ECDSA. |
| 7741 | * |
| 7742 | * 1. Test that calling the various interruptible sign and verify hash functions |
| 7743 | * in incorrect orders returns BAD_STATE errors. |
| 7744 | */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7745 | void interruptible_signverify_hash_state_test(int key_type_arg, |
| 7746 | data_t *key_data, int alg_arg, data_t *input_data) |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7747 | { |
| 7748 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7749 | psa_key_type_t key_type = key_type_arg; |
| 7750 | psa_algorithm_t alg = alg_arg; |
| 7751 | size_t key_bits; |
| 7752 | unsigned char *signature = NULL; |
| 7753 | size_t signature_size; |
| 7754 | size_t signature_length = 0xdeadbeef; |
| 7755 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7756 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7757 | psa_sign_hash_interruptible_operation_init(); |
| 7758 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7759 | psa_verify_hash_interruptible_operation_init(); |
| 7760 | |
| 7761 | PSA_ASSERT(psa_crypto_init()); |
| 7762 | |
| 7763 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7764 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7765 | psa_set_key_algorithm(&attributes, alg); |
| 7766 | psa_set_key_type(&attributes, key_type); |
| 7767 | |
| 7768 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7769 | &key)); |
| 7770 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7771 | key_bits = psa_get_key_bits(&attributes); |
| 7772 | |
| 7773 | /* Allocate a buffer which has the size advertised by the |
| 7774 | * library. */ |
| 7775 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7776 | key_bits, alg); |
| 7777 | TEST_ASSERT(signature_size != 0); |
| 7778 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7779 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7780 | |
| 7781 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7782 | |
| 7783 | /* --- Attempt completes prior to starts --- */ |
| 7784 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7785 | signature_size, |
| 7786 | &signature_length), |
| 7787 | PSA_ERROR_BAD_STATE); |
| 7788 | |
| 7789 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7790 | |
| 7791 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7792 | PSA_ERROR_BAD_STATE); |
| 7793 | |
| 7794 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7795 | |
| 7796 | /* --- Aborts in all other places. --- */ |
| 7797 | psa_sign_hash_abort(&sign_operation); |
| 7798 | |
| 7799 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7800 | input_data->x, input_data->len)); |
| 7801 | |
| 7802 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7803 | |
| 7804 | psa_interruptible_set_max_ops(1); |
| 7805 | |
| 7806 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7807 | input_data->x, input_data->len)); |
| 7808 | |
| 7809 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7810 | signature_size, |
| 7811 | &signature_length), |
| 7812 | PSA_OPERATION_INCOMPLETE); |
| 7813 | |
| 7814 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7815 | |
| 7816 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7817 | |
| 7818 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7819 | input_data->x, input_data->len)); |
| 7820 | |
| 7821 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7822 | signature_size, |
| 7823 | &signature_length)); |
| 7824 | |
| 7825 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7826 | |
| 7827 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7828 | |
| 7829 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7830 | input_data->x, input_data->len, |
| 7831 | signature, signature_length)); |
| 7832 | |
| 7833 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7834 | |
| 7835 | psa_interruptible_set_max_ops(1); |
| 7836 | |
| 7837 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7838 | input_data->x, input_data->len, |
| 7839 | signature, signature_length)); |
| 7840 | |
| 7841 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 7842 | PSA_OPERATION_INCOMPLETE); |
| 7843 | |
| 7844 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7845 | |
| 7846 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7847 | |
| 7848 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7849 | input_data->x, input_data->len, |
| 7850 | signature, signature_length)); |
| 7851 | |
| 7852 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7853 | |
| 7854 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7855 | |
| 7856 | /* --- Attempt double starts. --- */ |
| 7857 | |
| 7858 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7859 | input_data->x, input_data->len)); |
| 7860 | |
| 7861 | TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg, |
| 7862 | input_data->x, input_data->len), |
| 7863 | PSA_ERROR_BAD_STATE); |
| 7864 | |
| 7865 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7866 | |
| 7867 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7868 | input_data->x, input_data->len, |
| 7869 | signature, signature_length)); |
| 7870 | |
| 7871 | TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg, |
| 7872 | input_data->x, input_data->len, |
| 7873 | signature, signature_length), |
| 7874 | PSA_ERROR_BAD_STATE); |
| 7875 | |
| 7876 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7877 | |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7878 | exit: |
| 7879 | /* |
| 7880 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7881 | * thus reset them as required. |
| 7882 | */ |
| 7883 | psa_reset_key_attributes(&attributes); |
| 7884 | |
| 7885 | psa_destroy_key(key); |
| 7886 | mbedtls_free(signature); |
| 7887 | PSA_DONE(); |
| 7888 | } |
| 7889 | /* END_CASE */ |
| 7890 | |
| 7891 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7892 | /** |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7893 | * interruptible_signverify_hash_edgecase_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 7894 | * |
| 7895 | * Note: This test can currently only handle ECDSA. |
| 7896 | * |
| 7897 | * 1. Test various edge cases in the interruptible sign and verify hash |
| 7898 | * interfaces. |
| 7899 | */ |
Paul Elliott | c203350 | 2023-02-26 17:09:14 +0000 | [diff] [blame] | 7900 | void interruptible_signverify_hash_edgecase_tests(int key_type_arg, |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7901 | data_t *key_data, int alg_arg, data_t *input_data) |
| 7902 | { |
| 7903 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7904 | psa_key_type_t key_type = key_type_arg; |
| 7905 | psa_algorithm_t alg = alg_arg; |
| 7906 | size_t key_bits; |
| 7907 | unsigned char *signature = NULL; |
| 7908 | size_t signature_size; |
| 7909 | size_t signature_length = 0xdeadbeef; |
| 7910 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7911 | uint8_t *input_buffer = NULL; |
| 7912 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 7913 | psa_sign_hash_interruptible_operation_init(); |
| 7914 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 7915 | psa_verify_hash_interruptible_operation_init(); |
| 7916 | |
| 7917 | PSA_ASSERT(psa_crypto_init()); |
| 7918 | |
| 7919 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 7920 | PSA_KEY_USAGE_VERIFY_HASH); |
| 7921 | psa_set_key_algorithm(&attributes, alg); |
| 7922 | psa_set_key_type(&attributes, key_type); |
| 7923 | |
| 7924 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 7925 | &key)); |
| 7926 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 7927 | key_bits = psa_get_key_bits(&attributes); |
| 7928 | |
| 7929 | /* Allocate a buffer which has the size advertised by the |
| 7930 | * library. */ |
| 7931 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, |
| 7932 | key_bits, alg); |
| 7933 | TEST_ASSERT(signature_size != 0); |
| 7934 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 7935 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 7936 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7937 | /* --- Change function inputs mid run, to cause an error (sign only, |
| 7938 | * verify passes all inputs to start. --- */ |
| 7939 | |
| 7940 | psa_interruptible_set_max_ops(1); |
| 7941 | |
| 7942 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7943 | input_data->x, input_data->len)); |
| 7944 | |
| 7945 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7946 | signature_size, |
| 7947 | &signature_length), |
| 7948 | PSA_OPERATION_INCOMPLETE); |
| 7949 | |
| 7950 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7951 | 0, |
| 7952 | &signature_length), |
| 7953 | PSA_ERROR_BUFFER_TOO_SMALL); |
| 7954 | |
Paul Elliott | c977441 | 2023-02-06 15:14:07 +0000 | [diff] [blame] | 7955 | /* And test that this invalidates the operation. */ |
| 7956 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 7957 | 0, |
| 7958 | &signature_length), |
| 7959 | PSA_ERROR_BAD_STATE); |
| 7960 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 7961 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7962 | |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7963 | /* Trash the hash buffer in between start and complete, to ensure |
| 7964 | * no reliance on external buffers. */ |
| 7965 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 7966 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7967 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7968 | |
| 7969 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7970 | |
| 7971 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 7972 | input_buffer, input_data->len)); |
| 7973 | |
| 7974 | memset(input_buffer, '!', input_data->len); |
| 7975 | mbedtls_free(input_buffer); |
| 7976 | input_buffer = NULL; |
| 7977 | |
| 7978 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 7979 | signature_size, |
| 7980 | &signature_length)); |
| 7981 | |
| 7982 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 7983 | |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 7984 | TEST_CALLOC(input_buffer, input_data->len); |
Paul Elliott | f9c91a7 | 2023-02-05 18:06:38 +0000 | [diff] [blame] | 7985 | |
| 7986 | memcpy(input_buffer, input_data->x, input_data->len); |
| 7987 | |
| 7988 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 7989 | input_buffer, input_data->len, |
| 7990 | signature, signature_length)); |
| 7991 | |
| 7992 | memset(input_buffer, '!', input_data->len); |
| 7993 | mbedtls_free(input_buffer); |
| 7994 | input_buffer = NULL; |
| 7995 | |
| 7996 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 7997 | |
| 7998 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 7999 | |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 8000 | exit: |
| 8001 | /* |
| 8002 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8003 | * thus reset them as required. |
| 8004 | */ |
| 8005 | psa_reset_key_attributes(&attributes); |
| 8006 | |
| 8007 | psa_destroy_key(key); |
| 8008 | mbedtls_free(signature); |
Paul Elliott | 6c68df4 | 2023-10-23 15:33:37 +0100 | [diff] [blame] | 8009 | mbedtls_free(input_buffer); |
Paul Elliott | 20a3606 | 2022-12-18 13:21:25 +0000 | [diff] [blame] | 8010 | PSA_DONE(); |
| 8011 | } |
| 8012 | /* END_CASE */ |
| 8013 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8014 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 8015 | /** |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 8016 | * interruptible_signverify_hash_ops_tests() test intentions: |
Paul Elliott | c7f6882 | 2023-02-24 17:37:04 +0000 | [diff] [blame] | 8017 | * |
| 8018 | * Note: This test can currently only handle ECDSA. |
| 8019 | * |
| 8020 | * 1. Test that setting max ops is reflected in both interruptible sign and |
| 8021 | * verify hash |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8022 | * 2. Test that changing the value of max_ops to unlimited during an operation |
| 8023 | * causes that operation to complete in the next call. |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8024 | * |
| 8025 | * 3. Test that calling get_num_ops() between complete calls gives the same |
| 8026 | * 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] | 8027 | */ |
Paul Elliott | 5770224 | 2023-02-26 20:36:10 +0000 | [diff] [blame] | 8028 | void interruptible_signverify_hash_ops_tests(int key_type_arg, |
| 8029 | data_t *key_data, int alg_arg, |
| 8030 | data_t *input_data) |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8031 | { |
| 8032 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8033 | psa_key_type_t key_type = key_type_arg; |
| 8034 | psa_algorithm_t alg = alg_arg; |
| 8035 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8036 | size_t key_bits; |
| 8037 | unsigned char *signature = NULL; |
| 8038 | size_t signature_size; |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8039 | size_t signature_length = 0xdeadbeef; |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8040 | uint32_t num_ops = 0; |
| 8041 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 8042 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8043 | psa_sign_hash_interruptible_operation_t sign_operation = |
| 8044 | psa_sign_hash_interruptible_operation_init(); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8045 | psa_verify_hash_interruptible_operation_t verify_operation = |
| 8046 | psa_verify_hash_interruptible_operation_init(); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8047 | |
| 8048 | PSA_ASSERT(psa_crypto_init()); |
| 8049 | |
| 8050 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | |
| 8051 | PSA_KEY_USAGE_VERIFY_HASH); |
| 8052 | psa_set_key_algorithm(&attributes, alg); |
| 8053 | psa_set_key_type(&attributes, key_type); |
| 8054 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8055 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key)); |
| 8056 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8057 | key_bits = psa_get_key_bits(&attributes); |
| 8058 | |
| 8059 | /* Allocate a buffer which has the size advertised by the |
| 8060 | * library. */ |
| 8061 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8062 | |
| 8063 | TEST_ASSERT(signature_size != 0); |
| 8064 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8065 | TEST_CALLOC(signature, signature_size); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8066 | |
| 8067 | /* Check that default max ops gets set if we don't set it. */ |
| 8068 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8069 | input_data->x, input_data->len)); |
| 8070 | |
| 8071 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 8072 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8073 | |
| 8074 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8075 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8076 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8077 | input_data->x, input_data->len, |
| 8078 | signature, signature_size)); |
| 8079 | |
| 8080 | TEST_EQUAL(psa_interruptible_get_max_ops(), |
| 8081 | PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8082 | |
| 8083 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8084 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8085 | /* Check that max ops gets set properly. */ |
| 8086 | |
| 8087 | psa_interruptible_set_max_ops(0xbeef); |
| 8088 | |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8089 | TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8090 | |
Paul Elliott | 9e8819f | 2023-02-26 19:01:35 +0000 | [diff] [blame] | 8091 | /* --- Ensure changing the max ops mid operation works (operation should |
| 8092 | * complete successfully after setting max ops to unlimited --- */ |
| 8093 | psa_interruptible_set_max_ops(1); |
| 8094 | |
| 8095 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8096 | input_data->x, input_data->len)); |
| 8097 | |
| 8098 | TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature, |
| 8099 | signature_size, |
| 8100 | &signature_length), |
| 8101 | PSA_OPERATION_INCOMPLETE); |
| 8102 | |
| 8103 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8104 | |
| 8105 | PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature, |
| 8106 | signature_size, |
| 8107 | &signature_length)); |
| 8108 | |
| 8109 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8110 | |
| 8111 | psa_interruptible_set_max_ops(1); |
| 8112 | |
| 8113 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8114 | input_data->x, input_data->len, |
| 8115 | signature, signature_length)); |
| 8116 | |
| 8117 | TEST_EQUAL(psa_verify_hash_complete(&verify_operation), |
| 8118 | PSA_OPERATION_INCOMPLETE); |
| 8119 | |
| 8120 | psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED); |
| 8121 | |
| 8122 | PSA_ASSERT(psa_verify_hash_complete(&verify_operation)); |
| 8123 | |
| 8124 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8125 | |
Paul Elliott | c1e0400 | 2023-02-26 20:27:23 +0000 | [diff] [blame] | 8126 | /* --- Test that not calling get_num_ops inbetween complete calls does not |
| 8127 | * result in lost ops. ---*/ |
| 8128 | |
| 8129 | psa_interruptible_set_max_ops(1); |
| 8130 | |
| 8131 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8132 | input_data->x, input_data->len)); |
| 8133 | |
| 8134 | /* Continue performing the signature until complete. */ |
| 8135 | do { |
| 8136 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 8137 | signature_size, |
| 8138 | &signature_length); |
| 8139 | |
| 8140 | num_ops = psa_sign_hash_get_num_ops(&sign_operation); |
| 8141 | |
| 8142 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8143 | |
| 8144 | PSA_ASSERT(status); |
| 8145 | |
| 8146 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8147 | |
| 8148 | PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg, |
| 8149 | input_data->x, input_data->len)); |
| 8150 | |
| 8151 | /* Continue performing the signature until complete. */ |
| 8152 | do { |
| 8153 | status = psa_sign_hash_complete(&sign_operation, signature, |
| 8154 | signature_size, |
| 8155 | &signature_length); |
| 8156 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8157 | |
| 8158 | PSA_ASSERT(status); |
| 8159 | |
| 8160 | TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation)); |
| 8161 | |
| 8162 | PSA_ASSERT(psa_sign_hash_abort(&sign_operation)); |
| 8163 | |
| 8164 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8165 | input_data->x, input_data->len, |
| 8166 | signature, signature_length)); |
| 8167 | |
| 8168 | /* Continue performing the verification until complete. */ |
| 8169 | do { |
| 8170 | status = psa_verify_hash_complete(&verify_operation); |
| 8171 | |
| 8172 | num_ops = psa_verify_hash_get_num_ops(&verify_operation); |
| 8173 | |
| 8174 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8175 | |
| 8176 | PSA_ASSERT(status); |
| 8177 | |
| 8178 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8179 | |
| 8180 | PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg, |
| 8181 | input_data->x, input_data->len, |
| 8182 | signature, signature_length)); |
| 8183 | |
| 8184 | /* Continue performing the verification until complete. */ |
| 8185 | do { |
| 8186 | status = psa_verify_hash_complete(&verify_operation); |
| 8187 | |
| 8188 | } while (status == PSA_OPERATION_INCOMPLETE); |
| 8189 | |
| 8190 | PSA_ASSERT(status); |
| 8191 | |
| 8192 | TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation)); |
| 8193 | |
| 8194 | PSA_ASSERT(psa_verify_hash_abort(&verify_operation)); |
| 8195 | |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8196 | exit: |
| 8197 | /* |
| 8198 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8199 | * thus reset them as required. |
| 8200 | */ |
| 8201 | psa_reset_key_attributes(&attributes); |
| 8202 | |
| 8203 | psa_destroy_key(key); |
Paul Elliott | f1743e2 | 2023-02-15 18:44:16 +0000 | [diff] [blame] | 8204 | mbedtls_free(signature); |
Paul Elliott | a4cb909 | 2023-02-07 18:01:55 +0000 | [diff] [blame] | 8205 | PSA_DONE(); |
| 8206 | } |
| 8207 | /* END_CASE */ |
Paul Elliott | 76d671a | 2023-02-07 17:45:18 +0000 | [diff] [blame] | 8208 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8209 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8210 | void sign_message_deterministic(int key_type_arg, |
| 8211 | data_t *key_data, |
| 8212 | int alg_arg, |
| 8213 | data_t *input_data, |
| 8214 | data_t *output_data) |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8215 | { |
| 8216 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8217 | psa_key_type_t key_type = key_type_arg; |
| 8218 | psa_algorithm_t alg = alg_arg; |
| 8219 | size_t key_bits; |
| 8220 | unsigned char *signature = NULL; |
| 8221 | size_t signature_size; |
| 8222 | size_t signature_length = 0xdeadbeef; |
| 8223 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8225 | PSA_ASSERT(psa_crypto_init()); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8227 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8228 | psa_set_key_algorithm(&attributes, alg); |
| 8229 | psa_set_key_type(&attributes, key_type); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8231 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8232 | &key)); |
| 8233 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8234 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8236 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8237 | TEST_ASSERT(signature_size != 0); |
| 8238 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8239 | TEST_CALLOC(signature, signature_size); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8241 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8242 | input_data->x, input_data->len, |
| 8243 | signature, signature_size, |
| 8244 | &signature_length)); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8245 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8246 | TEST_MEMORY_COMPARE(output_data->x, output_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8247 | signature, signature_length); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8248 | |
| 8249 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8250 | psa_reset_key_attributes(&attributes); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8252 | psa_destroy_key(key); |
| 8253 | mbedtls_free(signature); |
| 8254 | PSA_DONE(); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8255 | |
| 8256 | } |
| 8257 | /* END_CASE */ |
| 8258 | |
| 8259 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8260 | void sign_message_fail(int key_type_arg, |
| 8261 | data_t *key_data, |
| 8262 | int alg_arg, |
| 8263 | data_t *input_data, |
| 8264 | int signature_size_arg, |
| 8265 | int expected_status_arg) |
| 8266 | { |
| 8267 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8268 | psa_key_type_t key_type = key_type_arg; |
| 8269 | psa_algorithm_t alg = alg_arg; |
| 8270 | size_t signature_size = signature_size_arg; |
| 8271 | psa_status_t actual_status; |
| 8272 | psa_status_t expected_status = expected_status_arg; |
| 8273 | unsigned char *signature = NULL; |
| 8274 | size_t signature_length = 0xdeadbeef; |
| 8275 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8276 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8277 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8278 | |
| 8279 | PSA_ASSERT(psa_crypto_init()); |
| 8280 | |
| 8281 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 8282 | psa_set_key_algorithm(&attributes, alg); |
| 8283 | psa_set_key_type(&attributes, key_type); |
| 8284 | |
| 8285 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8286 | &key)); |
| 8287 | |
| 8288 | actual_status = psa_sign_message(key, alg, |
| 8289 | input_data->x, input_data->len, |
| 8290 | signature, signature_size, |
| 8291 | &signature_length); |
| 8292 | TEST_EQUAL(actual_status, expected_status); |
| 8293 | /* The value of *signature_length is unspecified on error, but |
| 8294 | * whatever it is, it should be less than signature_size, so that |
| 8295 | * if the caller tries to read *signature_length bytes without |
| 8296 | * checking the error code then they don't overflow a buffer. */ |
| 8297 | TEST_LE_U(signature_length, signature_size); |
| 8298 | |
| 8299 | exit: |
| 8300 | psa_reset_key_attributes(&attributes); |
| 8301 | psa_destroy_key(key); |
| 8302 | mbedtls_free(signature); |
| 8303 | PSA_DONE(); |
| 8304 | } |
| 8305 | /* END_CASE */ |
| 8306 | |
| 8307 | /* BEGIN_CASE */ |
| 8308 | void sign_verify_message(int key_type_arg, |
| 8309 | data_t *key_data, |
| 8310 | int alg_arg, |
| 8311 | data_t *input_data) |
| 8312 | { |
| 8313 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8314 | psa_key_type_t key_type = key_type_arg; |
| 8315 | psa_algorithm_t alg = alg_arg; |
| 8316 | size_t key_bits; |
| 8317 | unsigned char *signature = NULL; |
| 8318 | size_t signature_size; |
| 8319 | size_t signature_length = 0xdeadbeef; |
| 8320 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8321 | |
| 8322 | PSA_ASSERT(psa_crypto_init()); |
| 8323 | |
| 8324 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 8325 | PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8326 | psa_set_key_algorithm(&attributes, alg); |
| 8327 | psa_set_key_type(&attributes, key_type); |
| 8328 | |
| 8329 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8330 | &key)); |
| 8331 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8332 | key_bits = psa_get_key_bits(&attributes); |
| 8333 | |
| 8334 | signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8335 | TEST_ASSERT(signature_size != 0); |
| 8336 | TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8337 | TEST_CALLOC(signature, signature_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8338 | |
| 8339 | PSA_ASSERT(psa_sign_message(key, alg, |
| 8340 | input_data->x, input_data->len, |
| 8341 | signature, signature_size, |
| 8342 | &signature_length)); |
| 8343 | TEST_LE_U(signature_length, signature_size); |
| 8344 | TEST_ASSERT(signature_length > 0); |
| 8345 | |
| 8346 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8347 | input_data->x, input_data->len, |
| 8348 | signature, signature_length)); |
| 8349 | |
| 8350 | if (input_data->len != 0) { |
| 8351 | /* Flip a bit in the input and verify that the signature is now |
| 8352 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 8353 | * because ECDSA may ignore the last few bits of the input. */ |
| 8354 | input_data->x[0] ^= 1; |
| 8355 | TEST_EQUAL(psa_verify_message(key, alg, |
| 8356 | input_data->x, input_data->len, |
| 8357 | signature, signature_length), |
| 8358 | PSA_ERROR_INVALID_SIGNATURE); |
| 8359 | } |
| 8360 | |
| 8361 | exit: |
| 8362 | psa_reset_key_attributes(&attributes); |
| 8363 | |
| 8364 | psa_destroy_key(key); |
| 8365 | mbedtls_free(signature); |
| 8366 | PSA_DONE(); |
| 8367 | } |
| 8368 | /* END_CASE */ |
| 8369 | |
| 8370 | /* BEGIN_CASE */ |
| 8371 | void verify_message(int key_type_arg, |
| 8372 | data_t *key_data, |
| 8373 | int alg_arg, |
| 8374 | data_t *input_data, |
| 8375 | data_t *signature_data) |
| 8376 | { |
| 8377 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8378 | psa_key_type_t key_type = key_type_arg; |
| 8379 | psa_algorithm_t alg = alg_arg; |
| 8380 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8381 | |
| 8382 | TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE); |
| 8383 | |
| 8384 | PSA_ASSERT(psa_crypto_init()); |
| 8385 | |
| 8386 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8387 | psa_set_key_algorithm(&attributes, alg); |
| 8388 | psa_set_key_type(&attributes, key_type); |
| 8389 | |
| 8390 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8391 | &key)); |
| 8392 | |
| 8393 | PSA_ASSERT(psa_verify_message(key, alg, |
| 8394 | input_data->x, input_data->len, |
| 8395 | signature_data->x, signature_data->len)); |
| 8396 | |
| 8397 | exit: |
| 8398 | psa_reset_key_attributes(&attributes); |
| 8399 | psa_destroy_key(key); |
| 8400 | PSA_DONE(); |
| 8401 | } |
| 8402 | /* END_CASE */ |
| 8403 | |
| 8404 | /* BEGIN_CASE */ |
| 8405 | void verify_message_fail(int key_type_arg, |
| 8406 | data_t *key_data, |
| 8407 | int alg_arg, |
| 8408 | data_t *hash_data, |
| 8409 | data_t *signature_data, |
| 8410 | int expected_status_arg) |
| 8411 | { |
| 8412 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8413 | psa_key_type_t key_type = key_type_arg; |
| 8414 | psa_algorithm_t alg = alg_arg; |
| 8415 | psa_status_t actual_status; |
| 8416 | psa_status_t expected_status = expected_status_arg; |
| 8417 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8418 | |
| 8419 | PSA_ASSERT(psa_crypto_init()); |
| 8420 | |
| 8421 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 8422 | psa_set_key_algorithm(&attributes, alg); |
| 8423 | psa_set_key_type(&attributes, key_type); |
| 8424 | |
| 8425 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8426 | &key)); |
| 8427 | |
| 8428 | actual_status = psa_verify_message(key, alg, |
| 8429 | hash_data->x, hash_data->len, |
| 8430 | signature_data->x, |
| 8431 | signature_data->len); |
| 8432 | TEST_EQUAL(actual_status, expected_status); |
| 8433 | |
| 8434 | exit: |
| 8435 | psa_reset_key_attributes(&attributes); |
| 8436 | psa_destroy_key(key); |
| 8437 | PSA_DONE(); |
| 8438 | } |
| 8439 | /* END_CASE */ |
| 8440 | |
| 8441 | /* BEGIN_CASE */ |
| 8442 | void asymmetric_encrypt(int key_type_arg, |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 8443 | data_t *key_data, |
| 8444 | int alg_arg, |
| 8445 | data_t *input_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8446 | data_t *label, |
| 8447 | int expected_output_length_arg, |
| 8448 | int expected_status_arg) |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8449 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8450 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8451 | psa_key_type_t key_type = key_type_arg; |
| 8452 | psa_algorithm_t alg = alg_arg; |
| 8453 | size_t expected_output_length = expected_output_length_arg; |
| 8454 | size_t key_bits; |
| 8455 | unsigned char *output = NULL; |
| 8456 | size_t output_size; |
| 8457 | size_t output_length = ~0; |
| 8458 | psa_status_t actual_status; |
| 8459 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8460 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8462 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 8463 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8464 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8465 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 8466 | psa_set_key_algorithm(&attributes, alg); |
| 8467 | psa_set_key_type(&attributes, key_type); |
| 8468 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8469 | &key)); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8470 | |
| 8471 | /* Determine the maximum output length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8472 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8473 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8475 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8476 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8477 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8478 | |
| 8479 | /* Encrypt the input */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8480 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8481 | input_data->x, input_data->len, |
| 8482 | label->x, label->len, |
| 8483 | output, output_size, |
| 8484 | &output_length); |
| 8485 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8486 | if (actual_status == PSA_SUCCESS) { |
| 8487 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8488 | } else { |
| 8489 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8490 | } |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8491 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8492 | /* If the label is empty, the test framework puts a non-null pointer |
| 8493 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8494 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8495 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8496 | if (output_size != 0) { |
| 8497 | memset(output, 0, output_size); |
| 8498 | } |
| 8499 | actual_status = psa_asymmetric_encrypt(key, alg, |
| 8500 | input_data->x, input_data->len, |
| 8501 | NULL, label->len, |
| 8502 | output, output_size, |
| 8503 | &output_length); |
| 8504 | TEST_EQUAL(actual_status, expected_status); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8505 | if (actual_status == PSA_SUCCESS) { |
| 8506 | TEST_EQUAL(output_length, expected_output_length); |
Stephan Koch | 5819d2c | 2023-02-22 13:39:21 +0100 | [diff] [blame] | 8507 | } else { |
| 8508 | TEST_LE_U(output_length, output_size); |
oberon-sk | 10c0f77 | 2023-02-13 13:42:02 +0100 | [diff] [blame] | 8509 | } |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8510 | } |
| 8511 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8512 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8513 | /* |
| 8514 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8515 | * thus reset them as required. |
| 8516 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8517 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8519 | psa_destroy_key(key); |
| 8520 | mbedtls_free(output); |
| 8521 | PSA_DONE(); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 8522 | } |
| 8523 | /* END_CASE */ |
| 8524 | |
| 8525 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8526 | void asymmetric_encrypt_decrypt(int key_type_arg, |
| 8527 | data_t *key_data, |
| 8528 | int alg_arg, |
| 8529 | data_t *input_data, |
| 8530 | data_t *label) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8531 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8532 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8533 | psa_key_type_t key_type = key_type_arg; |
| 8534 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8535 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8536 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8537 | size_t output_size; |
| 8538 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8539 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8540 | size_t output2_size; |
| 8541 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8542 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8546 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 8547 | psa_set_key_algorithm(&attributes, alg); |
| 8548 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8550 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8551 | &key)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8552 | |
| 8553 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8554 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8555 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8557 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8558 | TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8559 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8560 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8561 | output2_size = input_data->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8562 | TEST_LE_U(output2_size, |
| 8563 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)); |
| 8564 | TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8565 | TEST_CALLOC(output2, output2_size); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8566 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 8567 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 8568 | * the original plaintext because of the non-optional random |
| 8569 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8570 | PSA_ASSERT(psa_asymmetric_encrypt(key, alg, |
| 8571 | input_data->x, input_data->len, |
| 8572 | label->x, label->len, |
| 8573 | output, output_size, |
| 8574 | &output_length)); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8575 | /* We don't know what ciphertext length to expect, but check that |
| 8576 | * it looks sensible. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8577 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 8578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8579 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8580 | output, output_length, |
| 8581 | label->x, label->len, |
| 8582 | output2, output2_size, |
| 8583 | &output2_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8584 | TEST_MEMORY_COMPARE(input_data->x, input_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8585 | output2, output2_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8586 | |
| 8587 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8588 | /* |
| 8589 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8590 | * thus reset them as required. |
| 8591 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8592 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8594 | psa_destroy_key(key); |
| 8595 | mbedtls_free(output); |
| 8596 | mbedtls_free(output2); |
| 8597 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8598 | } |
| 8599 | /* END_CASE */ |
| 8600 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8601 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8602 | void asymmetric_decrypt(int key_type_arg, |
| 8603 | data_t *key_data, |
| 8604 | int alg_arg, |
| 8605 | data_t *input_data, |
| 8606 | data_t *label, |
| 8607 | data_t *expected_data) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8608 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8609 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8610 | psa_key_type_t key_type = key_type_arg; |
| 8611 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8612 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8613 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 8614 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8615 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8616 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_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_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8620 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8621 | psa_set_key_algorithm(&attributes, alg); |
| 8622 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8624 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8625 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8627 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 8628 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8629 | |
| 8630 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8631 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg); |
| 8632 | TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8633 | TEST_CALLOC(output, output_size); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8635 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8636 | input_data->x, input_data->len, |
| 8637 | label->x, label->len, |
| 8638 | output, |
| 8639 | output_size, |
| 8640 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8641 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8642 | output, output_length); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8643 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8644 | /* If the label is empty, the test framework puts a non-null pointer |
| 8645 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8646 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8647 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8648 | if (output_size != 0) { |
| 8649 | memset(output, 0, output_size); |
| 8650 | } |
| 8651 | PSA_ASSERT(psa_asymmetric_decrypt(key, alg, |
| 8652 | input_data->x, input_data->len, |
| 8653 | NULL, label->len, |
| 8654 | output, |
| 8655 | output_size, |
| 8656 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 8657 | TEST_MEMORY_COMPARE(expected_data->x, expected_data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 8658 | output, output_length); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8659 | } |
| 8660 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8661 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8662 | psa_reset_key_attributes(&attributes); |
| 8663 | psa_destroy_key(key); |
| 8664 | mbedtls_free(output); |
| 8665 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8666 | } |
| 8667 | /* END_CASE */ |
| 8668 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8669 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8670 | void asymmetric_decrypt_fail(int key_type_arg, |
| 8671 | data_t *key_data, |
| 8672 | int alg_arg, |
| 8673 | data_t *input_data, |
| 8674 | data_t *label, |
| 8675 | int output_size_arg, |
| 8676 | int expected_status_arg) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8677 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8678 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8679 | psa_key_type_t key_type = key_type_arg; |
| 8680 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8681 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 8682 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 8683 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8684 | psa_status_t actual_status; |
| 8685 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8686 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8687 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 8688 | TEST_CALLOC(output, output_size); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8690 | PSA_ASSERT(psa_crypto_init()); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8691 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8692 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 8693 | psa_set_key_algorithm(&attributes, alg); |
| 8694 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 8695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8696 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 8697 | &key)); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8699 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8700 | input_data->x, input_data->len, |
| 8701 | label->x, label->len, |
| 8702 | output, output_size, |
| 8703 | &output_length); |
| 8704 | TEST_EQUAL(actual_status, expected_status); |
| 8705 | TEST_LE_U(output_length, output_size); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8706 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8707 | /* If the label is empty, the test framework puts a non-null pointer |
| 8708 | * in label->x. Test that a null pointer works as well. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8709 | if (label->len == 0) { |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8710 | output_length = ~0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8711 | if (output_size != 0) { |
| 8712 | memset(output, 0, output_size); |
| 8713 | } |
| 8714 | actual_status = psa_asymmetric_decrypt(key, alg, |
| 8715 | input_data->x, input_data->len, |
| 8716 | NULL, label->len, |
| 8717 | output, output_size, |
| 8718 | &output_length); |
| 8719 | TEST_EQUAL(actual_status, expected_status); |
| 8720 | TEST_LE_U(output_length, output_size); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 8721 | } |
| 8722 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8723 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8724 | psa_reset_key_attributes(&attributes); |
| 8725 | psa_destroy_key(key); |
| 8726 | mbedtls_free(output); |
| 8727 | PSA_DONE(); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 8728 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 8729 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8730 | |
| 8731 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8732 | void key_derivation_init() |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8733 | { |
| 8734 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 8735 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 8736 | * 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] | 8737 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8738 | size_t capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8739 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init(); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8740 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8741 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8743 | memset(&zero, 0, sizeof(zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8744 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8745 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8746 | TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity), |
| 8747 | PSA_ERROR_BAD_STATE); |
| 8748 | TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity), |
| 8749 | PSA_ERROR_BAD_STATE); |
| 8750 | TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity), |
| 8751 | PSA_ERROR_BAD_STATE); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 8752 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8753 | /* A default operation should be abortable without error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8754 | PSA_ASSERT(psa_key_derivation_abort(&func)); |
| 8755 | PSA_ASSERT(psa_key_derivation_abort(&init)); |
| 8756 | PSA_ASSERT(psa_key_derivation_abort(&zero)); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 8757 | } |
| 8758 | /* END_CASE */ |
| 8759 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 8760 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8761 | void derive_setup(int alg_arg, int expected_status_arg) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8762 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8763 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8764 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8765 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_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 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8769 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8770 | expected_status); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8771 | |
| 8772 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8773 | psa_key_derivation_abort(&operation); |
| 8774 | PSA_DONE(); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 8775 | } |
| 8776 | /* END_CASE */ |
| 8777 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8778 | /* BEGIN_CASE */ |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 8779 | void derive_set_capacity(int alg_arg, int64_t capacity_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8780 | int expected_status_arg) |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8781 | { |
| 8782 | psa_algorithm_t alg = alg_arg; |
| 8783 | size_t capacity = capacity_arg; |
| 8784 | psa_status_t expected_status = expected_status_arg; |
| 8785 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8787 | PSA_ASSERT(psa_crypto_init()); |
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 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8791 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 8792 | expected_status); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8793 | |
| 8794 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8795 | psa_key_derivation_abort(&operation); |
| 8796 | PSA_DONE(); |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 8797 | } |
| 8798 | /* END_CASE */ |
| 8799 | |
| 8800 | /* BEGIN_CASE */ |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8801 | void parse_binary_string_test(data_t *input, int output) |
| 8802 | { |
| 8803 | uint64_t value; |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8804 | value = mbedtls_test_parse_binary_string(input); |
Kusumit Ghoderao | d60dfc0 | 2023-05-01 17:39:27 +0530 | [diff] [blame] | 8805 | TEST_EQUAL(value, output); |
| 8806 | } |
| 8807 | /* END_CASE */ |
| 8808 | |
| 8809 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8810 | void derive_input(int alg_arg, |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8811 | int step_arg1, int key_type_arg1, data_t *input1, |
| 8812 | int expected_status_arg1, |
| 8813 | int step_arg2, int key_type_arg2, data_t *input2, |
| 8814 | int expected_status_arg2, |
| 8815 | int step_arg3, int key_type_arg3, data_t *input3, |
| 8816 | int expected_status_arg3, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8817 | int output_key_type_arg, int expected_output_status_arg) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8818 | { |
| 8819 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8820 | psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 }; |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8821 | 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] | 8822 | psa_status_t expected_statuses[] = { expected_status_arg1, |
| 8823 | expected_status_arg2, |
| 8824 | expected_status_arg3 }; |
| 8825 | data_t *inputs[] = { input1, input2, input3 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8826 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 8827 | MBEDTLS_SVC_KEY_ID_INIT, |
| 8828 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8829 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8830 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8831 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8832 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8833 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8834 | psa_status_t expected_output_status = expected_output_status_arg; |
| 8835 | psa_status_t actual_output_status; |
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_ASSERT(psa_crypto_init()); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8839 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8840 | psa_set_key_algorithm(&attributes, alg); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8841 | |
Waleed Elmelegy | cba05ec | 2025-03-03 12:48:40 +0000 | [diff] [blame] | 8842 | if (alg != PSA_ALG_NONE) { |
Waleed Elmelegy | b6ed6f7 | 2025-03-03 12:42:55 +0000 | [diff] [blame] | 8843 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8844 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8846 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 8847 | mbedtls_test_set_step(i); |
| 8848 | if (steps[i] == 0) { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 8849 | /* Skip this step */ |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8850 | } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE && |
| 8851 | key_types[i] != INPUT_INTEGER) { |
| 8852 | psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i])); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8853 | PSA_ASSERT(psa_import_key(&attributes, |
| 8854 | inputs[i]->x, inputs[i]->len, |
| 8855 | &keys[i])); |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8856 | 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] | 8857 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) { |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8858 | // When taking a private key as secret input, use key agreement |
| 8859 | // to add the shared secret to the derivation |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8860 | TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self( |
Ryan Everett | 73e4ea3 | 2024-03-12 16:29:55 +0000 | [diff] [blame] | 8861 | &operation, keys[i], 0), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8862 | expected_statuses[i]); |
| 8863 | } else { |
| 8864 | TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i], |
| 8865 | keys[i]), |
| 8866 | expected_statuses[i]); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 8867 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8868 | } else { |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8869 | if (key_types[i] == INPUT_INTEGER) { |
| 8870 | TEST_EQUAL(psa_key_derivation_input_integer( |
| 8871 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 8872 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | 12e0b4b | 2023-04-27 16:58:23 +0530 | [diff] [blame] | 8873 | expected_statuses[i]); |
| 8874 | } else { |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8875 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 8876 | &operation, steps[i], |
| 8877 | inputs[i]->x, inputs[i]->len), |
| 8878 | expected_statuses[i]); |
Kusumit Ghoderao | 02326d5 | 2023-04-06 17:47:59 +0530 | [diff] [blame] | 8879 | } |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8880 | } |
| 8881 | } |
| 8882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8883 | if (output_key_type != PSA_KEY_TYPE_NONE) { |
| 8884 | psa_reset_key_attributes(&attributes); |
| 8885 | psa_set_key_type(&attributes, output_key_type); |
| 8886 | psa_set_key_bits(&attributes, 8); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8887 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8888 | psa_key_derivation_output_key(&attributes, &operation, |
| 8889 | &output_key); |
| 8890 | } else { |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8891 | uint8_t buffer[1]; |
| 8892 | actual_output_status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8893 | psa_key_derivation_output_bytes(&operation, |
| 8894 | buffer, sizeof(buffer)); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8895 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8896 | TEST_EQUAL(actual_output_status, expected_output_status); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 8897 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8898 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8899 | psa_key_derivation_abort(&operation); |
| 8900 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 8901 | psa_destroy_key(keys[i]); |
| 8902 | } |
| 8903 | psa_destroy_key(output_key); |
| 8904 | PSA_DONE(); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 8905 | } |
| 8906 | /* END_CASE */ |
| 8907 | |
Kusumit Ghoderao | 42b02b9 | 2023-06-06 16:48:46 +0530 | [diff] [blame] | 8908 | /* BEGIN_CASE*/ |
| 8909 | void derive_input_invalid_cost(int alg_arg, int64_t cost) |
| 8910 | { |
| 8911 | psa_algorithm_t alg = alg_arg; |
| 8912 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8913 | |
| 8914 | PSA_ASSERT(psa_crypto_init()); |
| 8915 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 8916 | |
| 8917 | TEST_EQUAL(psa_key_derivation_input_integer(&operation, |
| 8918 | PSA_KEY_DERIVATION_INPUT_COST, |
| 8919 | cost), |
| 8920 | PSA_ERROR_NOT_SUPPORTED); |
| 8921 | |
| 8922 | exit: |
| 8923 | psa_key_derivation_abort(&operation); |
| 8924 | PSA_DONE(); |
| 8925 | } |
| 8926 | /* END_CASE*/ |
| 8927 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8928 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8929 | void derive_over_capacity(int alg_arg) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8930 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8931 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8932 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 8933 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8934 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8935 | unsigned char input1[] = "Input 1"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8936 | size_t input1_length = sizeof(input1); |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8937 | unsigned char input2[] = "Input 2"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8938 | size_t input2_length = sizeof(input2); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8939 | uint8_t buffer[42]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8940 | size_t capacity = sizeof(buffer); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 8941 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 8942 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8943 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 8944 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 8945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8946 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 8949 | psa_set_key_algorithm(&attributes, alg); |
| 8950 | psa_set_key_type(&attributes, key_type); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8952 | PSA_ASSERT(psa_import_key(&attributes, |
| 8953 | key_data, sizeof(key_data), |
| 8954 | &key)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8955 | |
| 8956 | /* valid key derivation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8957 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 8958 | input1, input1_length, |
| 8959 | input2, input2_length, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 8960 | capacity, 0)) { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 8961 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8962 | } |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8963 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8964 | /* state of operation shouldn't allow additional generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8965 | TEST_EQUAL(psa_key_derivation_setup(&operation, alg), |
| 8966 | PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8968 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity)); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8970 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity), |
| 8971 | PSA_ERROR_INSUFFICIENT_DATA); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8972 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8973 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8974 | psa_key_derivation_abort(&operation); |
| 8975 | psa_destroy_key(key); |
| 8976 | PSA_DONE(); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8977 | } |
| 8978 | /* END_CASE */ |
| 8979 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8980 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8981 | void derive_actions_without_setup() |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8982 | { |
| 8983 | uint8_t output_buffer[16]; |
| 8984 | size_t buffer_size = 16; |
| 8985 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8986 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
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_output_bytes(&operation, |
| 8989 | output_buffer, buffer_size) |
| 8990 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8992 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 8993 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 8994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 8995 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
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_output_bytes(&operation, |
| 8998 | output_buffer, buffer_size) |
| 8999 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9000 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9001 | TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity) |
| 9002 | == PSA_ERROR_BAD_STATE); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 9003 | |
| 9004 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9005 | psa_key_derivation_abort(&operation); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 9006 | } |
| 9007 | /* END_CASE */ |
| 9008 | |
| 9009 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9010 | void derive_output(int alg_arg, |
| 9011 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 9012 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 9013 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 9014 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 9015 | data_t *key_agreement_peer_key, |
| 9016 | int requested_capacity_arg, |
| 9017 | data_t *expected_output1, |
| 9018 | data_t *expected_output2, |
| 9019 | int other_key_input_type, |
| 9020 | int key_input_type, |
| 9021 | int derive_type) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9022 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9023 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9024 | psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg }; |
| 9025 | data_t *inputs[] = { input1, input2, input3, input4 }; |
| 9026 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 9027 | MBEDTLS_SVC_KEY_ID_INIT, |
| 9028 | MBEDTLS_SVC_KEY_ID_INIT, |
| 9029 | MBEDTLS_SVC_KEY_ID_INIT }; |
| 9030 | psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2, |
| 9031 | expected_status_arg3, expected_status_arg4 }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9032 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9033 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9034 | uint8_t *expected_outputs[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9035 | { expected_output1->x, expected_output2->x }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9036 | size_t output_sizes[2] = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9037 | { expected_output1->len, expected_output2->len }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9038 | size_t output_buffer_size = 0; |
| 9039 | uint8_t *output_buffer = NULL; |
| 9040 | size_t expected_capacity; |
| 9041 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9042 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 9043 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 9044 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 9045 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9046 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9047 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9048 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9050 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
| 9051 | if (output_sizes[i] > output_buffer_size) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9052 | output_buffer_size = output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9053 | } |
| 9054 | if (output_sizes[i] == 0) { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9055 | expected_outputs[i] = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9056 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9057 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9058 | TEST_CALLOC(output_buffer, output_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9059 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9060 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9061 | /* Extraction phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9062 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9063 | PSA_ASSERT(psa_key_derivation_set_capacity(&operation, |
| 9064 | requested_capacity)); |
| 9065 | for (i = 0; i < ARRAY_LENGTH(steps); i++) { |
| 9066 | switch (steps[i]) { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9067 | case 0: |
| 9068 | break; |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9069 | case PSA_KEY_DERIVATION_INPUT_COST: |
| 9070 | TEST_EQUAL(psa_key_derivation_input_integer( |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 9071 | &operation, steps[i], |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 9072 | mbedtls_test_parse_binary_string(inputs[i])), |
Kusumit Ghoderao | f28e0f5 | 2023-06-06 15:03:22 +0530 | [diff] [blame] | 9073 | statuses[i]); |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9074 | if (statuses[i] != PSA_SUCCESS) { |
| 9075 | goto exit; |
| 9076 | } |
| 9077 | break; |
| 9078 | case PSA_KEY_DERIVATION_INPUT_PASSWORD: |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9079 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9080 | switch (key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9081 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9082 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 9083 | &operation, steps[i], |
| 9084 | inputs[i]->x, inputs[i]->len), |
| 9085 | statuses[i]); |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 9086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9087 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 9088 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9089 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9090 | break; |
| 9091 | case 1: // input key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9092 | psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE); |
| 9093 | psa_set_key_algorithm(&attributes1, alg); |
| 9094 | psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE); |
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 | PSA_ASSERT(psa_import_key(&attributes1, |
| 9097 | inputs[i]->x, inputs[i]->len, |
| 9098 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9100 | if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) { |
| 9101 | PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1)); |
| 9102 | TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)), |
| 9103 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9104 | } |
| 9105 | |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9106 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9107 | steps[i], |
Kusumit Ghoderao | 81797fc | 2023-06-05 15:05:09 +0530 | [diff] [blame] | 9108 | keys[i]), |
| 9109 | statuses[i]); |
| 9110 | |
| 9111 | if (statuses[i] != PSA_SUCCESS) { |
| 9112 | goto exit; |
| 9113 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9114 | break; |
| 9115 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9116 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9117 | break; |
| 9118 | } |
| 9119 | break; |
| 9120 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9121 | switch (other_key_input_type) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9122 | case 0: // input bytes |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9123 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9124 | steps[i], |
| 9125 | inputs[i]->x, |
| 9126 | inputs[i]->len), |
| 9127 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9128 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 9129 | case 1: // input key, type DERIVE |
| 9130 | case 11: // input key, type RAW |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9131 | psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE); |
| 9132 | psa_set_key_algorithm(&attributes2, alg); |
| 9133 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9134 | |
| 9135 | // other secret of type RAW_DATA passed with input_key |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9136 | if (other_key_input_type == 11) { |
| 9137 | psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA); |
| 9138 | } |
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 | PSA_ASSERT(psa_import_key(&attributes2, |
| 9141 | inputs[i]->x, inputs[i]->len, |
| 9142 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9144 | TEST_EQUAL(psa_key_derivation_input_key(&operation, |
| 9145 | steps[i], |
| 9146 | keys[i]), |
| 9147 | statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9148 | break; |
| 9149 | case 2: // key agreement |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9150 | psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE); |
| 9151 | psa_set_key_algorithm(&attributes3, alg); |
| 9152 | psa_set_key_type(&attributes3, |
| 9153 | PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
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 | PSA_ASSERT(psa_import_key(&attributes3, |
| 9156 | inputs[i]->x, inputs[i]->len, |
| 9157 | &keys[i])); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9159 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9160 | &operation, |
| 9161 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 9162 | keys[i], key_agreement_peer_key->x, |
| 9163 | key_agreement_peer_key->len), statuses[i]); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9164 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9165 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 9166 | TEST_FAIL("default case not supported"); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9167 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9168 | } |
| 9169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9170 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9171 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9172 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9173 | break; |
| 9174 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9175 | TEST_EQUAL(psa_key_derivation_input_bytes( |
| 9176 | &operation, steps[i], |
| 9177 | inputs[i]->x, inputs[i]->len), statuses[i]); |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9179 | if (statuses[i] != PSA_SUCCESS) { |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 9180 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9181 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9182 | break; |
| 9183 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 9184 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 9185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9186 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9187 | ¤t_capacity)); |
| 9188 | TEST_EQUAL(current_capacity, requested_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9189 | expected_capacity = requested_capacity; |
| 9190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9191 | if (derive_type == 1) { // output key |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9192 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 9193 | |
| 9194 | /* For output key derivation secret must be provided using |
| 9195 | input key, otherwise operation is not permitted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9196 | if (key_input_type == 1) { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 9197 | expected_status = PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9198 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9200 | psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT); |
| 9201 | psa_set_key_algorithm(&attributes4, alg); |
| 9202 | psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE); |
| 9203 | psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity)); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9205 | TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation, |
| 9206 | &derived_key), expected_status); |
| 9207 | } else { // output bytes |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9208 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9209 | for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9210 | /* Read some bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9211 | status = psa_key_derivation_output_bytes(&operation, |
| 9212 | output_buffer, output_sizes[i]); |
| 9213 | if (expected_capacity == 0 && output_sizes[i] == 0) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9214 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9215 | TEST_ASSERT(status == PSA_SUCCESS || |
| 9216 | status == PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9217 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9218 | } else if (expected_capacity == 0 || |
| 9219 | output_sizes[i] > expected_capacity) { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9220 | /* Capacity exceeded. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9221 | TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9222 | expected_capacity = 0; |
| 9223 | continue; |
| 9224 | } |
| 9225 | /* Success. Check the read data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9226 | PSA_ASSERT(status); |
| 9227 | if (output_sizes[i] != 0) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9228 | TEST_MEMORY_COMPARE(output_buffer, output_sizes[i], |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9229 | expected_outputs[i], output_sizes[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9230 | } |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 9231 | /* Check the operation status. */ |
| 9232 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9233 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9234 | ¤t_capacity)); |
| 9235 | TEST_EQUAL(expected_capacity, current_capacity); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9236 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9237 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9238 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9239 | |
| 9240 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9241 | mbedtls_free(output_buffer); |
| 9242 | psa_key_derivation_abort(&operation); |
| 9243 | for (i = 0; i < ARRAY_LENGTH(keys); i++) { |
| 9244 | psa_destroy_key(keys[i]); |
| 9245 | } |
| 9246 | psa_destroy_key(derived_key); |
| 9247 | PSA_DONE(); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 9248 | } |
| 9249 | /* END_CASE */ |
| 9250 | |
| 9251 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9252 | void derive_full(int alg_arg, |
| 9253 | data_t *key_data, |
| 9254 | data_t *input1, |
| 9255 | data_t *input2, |
| 9256 | int requested_capacity_arg) |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9257 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9258 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9259 | psa_algorithm_t alg = alg_arg; |
| 9260 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9261 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Kusumit Ghoderao | 9ffd397 | 2023-12-01 16:40:13 +0530 | [diff] [blame] | 9262 | unsigned char output_buffer[32]; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9263 | size_t expected_capacity = requested_capacity; |
| 9264 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9265 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9267 | PSA_ASSERT(psa_crypto_init()); |
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_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9270 | psa_set_key_algorithm(&attributes, alg); |
| 9271 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9273 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9274 | &key)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9276 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg, |
| 9277 | input1->x, input1->len, |
| 9278 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9279 | requested_capacity, 0)) { |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 9280 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9281 | } |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 9282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9283 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9284 | ¤t_capacity)); |
| 9285 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9286 | |
| 9287 | /* Expansion phase. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9288 | while (current_capacity > 0) { |
| 9289 | size_t read_size = sizeof(output_buffer); |
| 9290 | if (read_size > current_capacity) { |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9291 | read_size = current_capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9292 | } |
| 9293 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9294 | output_buffer, |
| 9295 | read_size)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9296 | expected_capacity -= read_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9297 | PSA_ASSERT(psa_key_derivation_get_capacity(&operation, |
| 9298 | ¤t_capacity)); |
| 9299 | TEST_EQUAL(current_capacity, expected_capacity); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9300 | } |
| 9301 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9302 | /* Check that the operation refuses to go over capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9303 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1), |
| 9304 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9306 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9307 | |
| 9308 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9309 | psa_key_derivation_abort(&operation); |
| 9310 | psa_destroy_key(key); |
| 9311 | PSA_DONE(); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 9312 | } |
| 9313 | /* END_CASE */ |
| 9314 | |
Stephan Koch | 78109f5 | 2023-04-12 14:19:36 +0200 | [diff] [blame] | 9315 | /* 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] | 9316 | void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg, |
| 9317 | int derivation_step, |
| 9318 | int capacity, int expected_capacity_status_arg, |
| 9319 | data_t *expected_output, |
| 9320 | int expected_output_status_arg) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9321 | { |
| 9322 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 9323 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 9324 | 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] | 9325 | uint8_t *output_buffer = NULL; |
| 9326 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 9327 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 9328 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 9329 | 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] | 9330 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9331 | TEST_CALLOC(output_buffer, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9332 | PSA_ASSERT(psa_crypto_init()); |
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 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9335 | TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity), |
| 9336 | expected_capacity_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 | TEST_EQUAL(psa_key_derivation_input_bytes(&operation, |
| 9339 | step, input->x, input->len), |
| 9340 | expected_input_status); |
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 | if (((psa_status_t) expected_input_status) != PSA_SUCCESS) { |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9343 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9344 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9346 | status = psa_key_derivation_output_bytes(&operation, output_buffer, |
| 9347 | expected_output->len); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9349 | TEST_EQUAL(status, expected_output_status); |
| 9350 | if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9351 | TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9352 | expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9353 | } |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9354 | |
| 9355 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9356 | mbedtls_free(output_buffer); |
| 9357 | psa_key_derivation_abort(&operation); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 9358 | PSA_DONE(); |
| 9359 | } |
| 9360 | /* END_CASE */ |
| 9361 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9362 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9363 | void derive_key_exercise(int alg_arg, |
| 9364 | data_t *key_data, |
| 9365 | data_t *input1, |
| 9366 | data_t *input2, |
| 9367 | int derived_type_arg, |
| 9368 | int derived_bits_arg, |
| 9369 | int derived_usage_arg, |
| 9370 | int derived_alg_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9371 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9372 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9373 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9374 | psa_algorithm_t alg = alg_arg; |
| 9375 | psa_key_type_t derived_type = derived_type_arg; |
| 9376 | size_t derived_bits = derived_bits_arg; |
| 9377 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 9378 | psa_algorithm_t derived_alg = derived_alg_arg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9379 | size_t capacity = PSA_BITS_TO_BYTES(derived_bits); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9380 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9381 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 9382 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9384 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9386 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9387 | psa_set_key_algorithm(&attributes, alg); |
| 9388 | psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 9389 | PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, |
| 9390 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9391 | |
| 9392 | /* Derive a key. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9393 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9394 | input1->x, input1->len, |
| 9395 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9396 | capacity, 0)) { |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9397 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9398 | } |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 9399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9400 | psa_set_key_usage_flags(&attributes, derived_usage); |
| 9401 | psa_set_key_algorithm(&attributes, derived_alg); |
| 9402 | psa_set_key_type(&attributes, derived_type); |
| 9403 | psa_set_key_bits(&attributes, derived_bits); |
| 9404 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation, |
| 9405 | &derived_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9406 | |
| 9407 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9408 | PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes)); |
| 9409 | TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type); |
| 9410 | TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9411 | |
| 9412 | /* Exercise the derived key. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 9413 | 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] | 9414 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9415 | } |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9416 | |
| 9417 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9418 | /* |
| 9419 | * Key attributes may have been returned by psa_get_key_attributes() |
| 9420 | * thus reset them as required. |
| 9421 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9422 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 9423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9424 | psa_key_derivation_abort(&operation); |
| 9425 | psa_destroy_key(base_key); |
| 9426 | psa_destroy_key(derived_key); |
| 9427 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9428 | } |
| 9429 | /* END_CASE */ |
| 9430 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9431 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9432 | void derive_key_export(int alg_arg, |
| 9433 | data_t *key_data, |
| 9434 | data_t *input1, |
| 9435 | data_t *input2, |
| 9436 | int bytes1_arg, |
| 9437 | int bytes2_arg) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9438 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9439 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9440 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9441 | psa_algorithm_t alg = alg_arg; |
| 9442 | size_t bytes1 = bytes1_arg; |
| 9443 | size_t bytes2 = bytes2_arg; |
| 9444 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9445 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 9446 | uint8_t *output_buffer = NULL; |
| 9447 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9448 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9449 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9450 | size_t length; |
| 9451 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9452 | TEST_CALLOC(output_buffer, capacity); |
| 9453 | TEST_CALLOC(export_buffer, capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9454 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9456 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9457 | psa_set_key_algorithm(&base_attributes, alg); |
| 9458 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9459 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9460 | &base_key)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9461 | |
| 9462 | /* Derive some material and output it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9463 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9464 | input1->x, input1->len, |
| 9465 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9466 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9467 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9468 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9470 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9471 | output_buffer, |
| 9472 | capacity)); |
| 9473 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9474 | |
| 9475 | /* 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] | 9476 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9477 | input1->x, input1->len, |
| 9478 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9479 | capacity, 0)) { |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9480 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9481 | } |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 9482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9483 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9484 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9485 | psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA); |
| 9486 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1)); |
| 9487 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9488 | &derived_key)); |
| 9489 | PSA_ASSERT(psa_export_key(derived_key, |
| 9490 | export_buffer, bytes1, |
| 9491 | &length)); |
| 9492 | TEST_EQUAL(length, bytes1); |
| 9493 | PSA_ASSERT(psa_destroy_key(derived_key)); |
| 9494 | psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2)); |
| 9495 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9496 | &derived_key)); |
| 9497 | PSA_ASSERT(psa_export_key(derived_key, |
| 9498 | export_buffer + bytes1, bytes2, |
| 9499 | &length)); |
| 9500 | TEST_EQUAL(length, bytes2); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9501 | |
| 9502 | /* Compare the outputs from the two runs. */ |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9503 | TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9504 | export_buffer, capacity); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9505 | |
| 9506 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9507 | mbedtls_free(output_buffer); |
| 9508 | mbedtls_free(export_buffer); |
| 9509 | psa_key_derivation_abort(&operation); |
| 9510 | psa_destroy_key(base_key); |
| 9511 | psa_destroy_key(derived_key); |
| 9512 | PSA_DONE(); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 9513 | } |
| 9514 | /* END_CASE */ |
| 9515 | |
| 9516 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9517 | void derive_key_type(int alg_arg, |
| 9518 | data_t *key_data, |
| 9519 | data_t *input1, |
| 9520 | data_t *input2, |
| 9521 | int key_type_arg, int bits_arg, |
| 9522 | data_t *expected_export) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9523 | { |
| 9524 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9525 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9526 | const psa_algorithm_t alg = alg_arg; |
| 9527 | const psa_key_type_t key_type = key_type_arg; |
| 9528 | const size_t bits = bits_arg; |
| 9529 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9530 | const size_t export_buffer_size = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9531 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9532 | uint8_t *export_buffer = NULL; |
| 9533 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9534 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9535 | size_t export_length; |
| 9536 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9537 | TEST_CALLOC(export_buffer, export_buffer_size); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9538 | PSA_ASSERT(psa_crypto_init()); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9540 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9541 | psa_set_key_algorithm(&base_attributes, alg); |
| 9542 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9543 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9544 | &base_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9546 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9547 | &operation, base_key, alg, |
| 9548 | input1->x, input1->len, |
| 9549 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9550 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9551 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9552 | } |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9554 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9555 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9556 | psa_set_key_type(&derived_attributes, key_type); |
| 9557 | psa_set_key_bits(&derived_attributes, bits); |
| 9558 | PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation, |
| 9559 | &derived_key)); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9561 | PSA_ASSERT(psa_export_key(derived_key, |
| 9562 | export_buffer, export_buffer_size, |
| 9563 | &export_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9564 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9565 | expected_export->x, expected_export->len); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9566 | |
| 9567 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9568 | mbedtls_free(export_buffer); |
| 9569 | psa_key_derivation_abort(&operation); |
| 9570 | psa_destroy_key(base_key); |
| 9571 | psa_destroy_key(derived_key); |
| 9572 | PSA_DONE(); |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 9573 | } |
| 9574 | /* END_CASE */ |
| 9575 | |
| 9576 | /* BEGIN_CASE */ |
Gilles Peskine | f36d785 | 2024-06-06 21:11:44 +0200 | [diff] [blame] | 9577 | void derive_key_custom(int alg_arg, |
| 9578 | data_t *key_data, |
| 9579 | data_t *input1, |
| 9580 | data_t *input2, |
| 9581 | int key_type_arg, int bits_arg, |
| 9582 | int flags_arg, |
| 9583 | data_t *custom_data, |
| 9584 | psa_status_t expected_status, |
| 9585 | data_t *expected_export) |
| 9586 | { |
| 9587 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9588 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9589 | const psa_algorithm_t alg = alg_arg; |
| 9590 | const psa_key_type_t key_type = key_type_arg; |
| 9591 | const size_t bits = bits_arg; |
| 9592 | psa_custom_key_parameters_t custom = PSA_CUSTOM_KEY_PARAMETERS_INIT; |
| 9593 | custom.flags = flags_arg; |
| 9594 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9595 | const size_t export_buffer_size = |
| 9596 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
| 9597 | uint8_t *export_buffer = NULL; |
| 9598 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9599 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9600 | size_t export_length; |
| 9601 | |
| 9602 | TEST_CALLOC(export_buffer, export_buffer_size); |
| 9603 | PSA_ASSERT(psa_crypto_init()); |
| 9604 | |
| 9605 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9606 | psa_set_key_algorithm(&base_attributes, alg); |
| 9607 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9608 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9609 | &base_key)); |
| 9610 | |
| 9611 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
| 9612 | &operation, base_key, alg, |
| 9613 | input1->x, input1->len, |
| 9614 | input2->x, input2->len, |
| 9615 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
| 9616 | goto exit; |
| 9617 | } |
| 9618 | |
| 9619 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9620 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9621 | psa_set_key_type(&derived_attributes, key_type); |
| 9622 | psa_set_key_bits(&derived_attributes, bits); |
| 9623 | |
| 9624 | TEST_EQUAL(psa_key_derivation_output_key_custom( |
| 9625 | &derived_attributes, &operation, |
| 9626 | &custom, custom_data->x, custom_data->len, |
| 9627 | &derived_key), |
| 9628 | expected_status); |
| 9629 | |
| 9630 | if (expected_status == PSA_SUCCESS) { |
| 9631 | PSA_ASSERT(psa_export_key(derived_key, |
| 9632 | export_buffer, export_buffer_size, |
| 9633 | &export_length)); |
| 9634 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
| 9635 | expected_export->x, expected_export->len); |
| 9636 | } |
| 9637 | |
| 9638 | exit: |
| 9639 | mbedtls_free(export_buffer); |
| 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 | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9648 | void derive_key_ext(int alg_arg, |
| 9649 | data_t *key_data, |
| 9650 | data_t *input1, |
| 9651 | data_t *input2, |
| 9652 | int key_type_arg, int bits_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 9653 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9654 | data_t *params_data, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9655 | psa_status_t expected_status, |
| 9656 | data_t *expected_export) |
| 9657 | { |
| 9658 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9659 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9660 | const psa_algorithm_t alg = alg_arg; |
| 9661 | const psa_key_type_t key_type = key_type_arg; |
| 9662 | const size_t bits = bits_arg; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9663 | psa_key_production_parameters_t *params = NULL; |
| 9664 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9665 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9666 | const size_t export_buffer_size = |
| 9667 | PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits); |
| 9668 | uint8_t *export_buffer = NULL; |
| 9669 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9670 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9671 | size_t export_length; |
| 9672 | |
| 9673 | TEST_CALLOC(export_buffer, export_buffer_size); |
| 9674 | PSA_ASSERT(psa_crypto_init()); |
| 9675 | |
| 9676 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9677 | psa_set_key_algorithm(&base_attributes, alg); |
| 9678 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9679 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9680 | &base_key)); |
| 9681 | |
| 9682 | if (mbedtls_test_psa_setup_key_derivation_wrap( |
| 9683 | &operation, base_key, alg, |
| 9684 | input1->x, input1->len, |
| 9685 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9686 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY, 0) == 0) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9687 | goto exit; |
| 9688 | } |
| 9689 | |
| 9690 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9691 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9692 | psa_set_key_type(&derived_attributes, key_type); |
| 9693 | psa_set_key_bits(&derived_attributes, bits); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9694 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 9695 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9696 | goto exit; |
| 9697 | } |
| 9698 | |
| 9699 | TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9700 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9701 | &derived_key), |
| 9702 | expected_status); |
| 9703 | |
| 9704 | if (expected_status == PSA_SUCCESS) { |
| 9705 | PSA_ASSERT(psa_export_key(derived_key, |
| 9706 | export_buffer, export_buffer_size, |
| 9707 | &export_length)); |
| 9708 | TEST_MEMORY_COMPARE(export_buffer, export_length, |
| 9709 | expected_export->x, expected_export->len); |
| 9710 | } |
| 9711 | |
| 9712 | exit: |
| 9713 | mbedtls_free(export_buffer); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 9714 | mbedtls_free(params); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 9715 | psa_key_derivation_abort(&operation); |
| 9716 | psa_destroy_key(base_key); |
| 9717 | psa_destroy_key(derived_key); |
| 9718 | PSA_DONE(); |
| 9719 | } |
| 9720 | /* END_CASE */ |
| 9721 | |
| 9722 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9723 | void derive_key(int alg_arg, |
| 9724 | data_t *key_data, data_t *input1, data_t *input2, |
| 9725 | int type_arg, int bits_arg, |
| 9726 | int expected_status_arg, |
| 9727 | int is_large_output) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9728 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9729 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9730 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9731 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 9732 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9733 | size_t bits = bits_arg; |
| 9734 | psa_status_t expected_status = expected_status_arg; |
| 9735 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9736 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9737 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9739 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9741 | psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE); |
| 9742 | psa_set_key_algorithm(&base_attributes, alg); |
| 9743 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 9744 | PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len, |
| 9745 | &base_key)); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9747 | if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg, |
| 9748 | input1->x, input1->len, |
| 9749 | input2->x, input2->len, |
Ryan Everett | c1cc668 | 2024-03-12 16:17:43 +0000 | [diff] [blame] | 9750 | SIZE_MAX, 0)) { |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9751 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9752 | } |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9754 | psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT); |
| 9755 | psa_set_key_algorithm(&derived_attributes, 0); |
| 9756 | psa_set_key_type(&derived_attributes, type); |
| 9757 | psa_set_key_bits(&derived_attributes, bits); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 9758 | |
| 9759 | psa_status_t status = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9760 | psa_key_derivation_output_key(&derived_attributes, |
| 9761 | &operation, |
| 9762 | &derived_key); |
| 9763 | if (is_large_output > 0) { |
| 9764 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 9765 | } |
| 9766 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9767 | |
| 9768 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9769 | psa_key_derivation_abort(&operation); |
| 9770 | psa_destroy_key(base_key); |
| 9771 | psa_destroy_key(derived_key); |
| 9772 | PSA_DONE(); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 9773 | } |
| 9774 | /* END_CASE */ |
| 9775 | |
| 9776 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9777 | void key_agreement_setup(int alg_arg, |
| 9778 | int our_key_type_arg, int our_key_alg_arg, |
| 9779 | data_t *our_key_data, data_t *peer_key_data, |
| 9780 | int expected_status_arg) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9781 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9782 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9783 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 9784 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9785 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9786 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9787 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9788 | psa_status_t expected_status = expected_status_arg; |
| 9789 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9791 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9793 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9794 | psa_set_key_algorithm(&attributes, our_key_alg); |
| 9795 | psa_set_key_type(&attributes, our_key_type); |
| 9796 | PSA_ASSERT(psa_import_key(&attributes, |
| 9797 | our_key_data->x, our_key_data->len, |
| 9798 | &our_key)); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9799 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9800 | /* The tests currently include inputs that should fail at either step. |
| 9801 | * Test cases that fail at the setup step should be changed to call |
| 9802 | * key_derivation_setup instead, and this function should be renamed |
| 9803 | * to key_agreement_fail. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9804 | status = psa_key_derivation_setup(&operation, alg); |
| 9805 | if (status == PSA_SUCCESS) { |
| 9806 | TEST_EQUAL(psa_key_derivation_key_agreement( |
| 9807 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 9808 | our_key, |
| 9809 | peer_key_data->x, peer_key_data->len), |
| 9810 | expected_status); |
| 9811 | } else { |
| 9812 | TEST_ASSERT(status == expected_status); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 9813 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 9814 | |
| 9815 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9816 | psa_key_derivation_abort(&operation); |
| 9817 | psa_destroy_key(our_key); |
| 9818 | PSA_DONE(); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +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 raw_key_agreement(int alg_arg, |
| 9824 | int our_key_type_arg, data_t *our_key_data, |
| 9825 | data_t *peer_key_data, |
| 9826 | data_t *expected_output) |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +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 | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9829 | psa_algorithm_t alg = alg_arg; |
| 9830 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9831 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9832 | unsigned char *output = NULL; |
| 9833 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9834 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9836 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +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 | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9845 | PSA_ASSERT(psa_get_key_attributes(our_key, &attributes)); |
| 9846 | key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 9847 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9848 | /* Validate size macros */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9849 | TEST_LE_U(expected_output->len, |
| 9850 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits)); |
| 9851 | TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits), |
| 9852 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9853 | |
| 9854 | /* Good case with exact output size */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9855 | TEST_CALLOC(output, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9856 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9857 | peer_key_data->x, peer_key_data->len, |
| 9858 | output, expected_output->len, |
| 9859 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9860 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9861 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9862 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9863 | output = NULL; |
| 9864 | output_length = ~0; |
| 9865 | |
| 9866 | /* Larger buffer */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9867 | TEST_CALLOC(output, expected_output->len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9868 | PSA_ASSERT(psa_raw_key_agreement(alg, our_key, |
| 9869 | peer_key_data->x, peer_key_data->len, |
| 9870 | output, expected_output->len + 1, |
| 9871 | &output_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 9872 | TEST_MEMORY_COMPARE(output, output_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 9873 | expected_output->x, expected_output->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9874 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9875 | output = NULL; |
| 9876 | output_length = ~0; |
| 9877 | |
| 9878 | /* Buffer too small */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 9879 | TEST_CALLOC(output, expected_output->len - 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9880 | TEST_EQUAL(psa_raw_key_agreement(alg, our_key, |
| 9881 | peer_key_data->x, peer_key_data->len, |
| 9882 | output, expected_output->len - 1, |
| 9883 | &output_length), |
| 9884 | PSA_ERROR_BUFFER_TOO_SMALL); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9885 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9886 | TEST_LE_U(output_length, expected_output->len - 1); |
| 9887 | mbedtls_free(output); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 9888 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9889 | |
| 9890 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9891 | mbedtls_free(output); |
| 9892 | psa_destroy_key(our_key); |
| 9893 | PSA_DONE(); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 9894 | } |
| 9895 | /* END_CASE */ |
| 9896 | |
| 9897 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9898 | void key_agreement_capacity(int alg_arg, |
| 9899 | int our_key_type_arg, data_t *our_key_data, |
| 9900 | data_t *peer_key_data, |
| 9901 | int expected_capacity_arg) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9902 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 9903 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9904 | psa_algorithm_t alg = alg_arg; |
| 9905 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 9906 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 9907 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9908 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9909 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9911 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9913 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 9914 | psa_set_key_algorithm(&attributes, alg); |
| 9915 | psa_set_key_type(&attributes, our_key_type); |
| 9916 | PSA_ASSERT(psa_import_key(&attributes, |
| 9917 | our_key_data->x, our_key_data->len, |
| 9918 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9920 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 9921 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 9922 | &operation, |
| 9923 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 9924 | peer_key_data->x, peer_key_data->len)); |
| 9925 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9926 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9927 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 9928 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 9929 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 9930 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9931 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 9932 | /* Test the advertised capacity. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9933 | PSA_ASSERT(psa_key_derivation_get_capacity( |
| 9934 | &operation, &actual_capacity)); |
| 9935 | TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9936 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9937 | /* Test the actual capacity by reading the output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9938 | while (actual_capacity > sizeof(output)) { |
| 9939 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9940 | output, sizeof(output))); |
| 9941 | actual_capacity -= sizeof(output); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9942 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9943 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 9944 | output, actual_capacity)); |
| 9945 | TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1), |
| 9946 | PSA_ERROR_INSUFFICIENT_DATA); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 9947 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9948 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9949 | psa_key_derivation_abort(&operation); |
| 9950 | psa_destroy_key(our_key); |
| 9951 | PSA_DONE(); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9952 | } |
| 9953 | /* END_CASE */ |
| 9954 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9955 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9956 | 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] | 9957 | { |
| 9958 | mbedtls_ecp_group_id grp_id = grp_id_arg; |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9959 | psa_ecc_family_t ecc_family = psa_family_arg; |
| 9960 | size_t bits = bits_arg; |
| 9961 | size_t bits_tmp; |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9962 | |
Valerio Setti | ad81967 | 2023-12-29 12:14:41 +0100 | [diff] [blame] | 9963 | TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp)); |
| 9964 | TEST_EQUAL(bits, bits_tmp); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9965 | TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits)); |
Valerio Setti | bf999cb | 2023-12-28 17:48:13 +0100 | [diff] [blame] | 9966 | } |
| 9967 | /* END_CASE */ |
| 9968 | |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9969 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 9970 | void ecc_conversion_functions_fail() |
| 9971 | { |
| 9972 | size_t bits; |
| 9973 | |
Valerio Setti | db6e029 | 2024-01-05 10:15:45 +0100 | [diff] [blame] | 9974 | /* Invalid legacy curve identifiers. */ |
| 9975 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits)); |
| 9976 | TEST_EQUAL(0, bits); |
Valerio Setti | ac73952 | 2024-01-04 10:22:01 +0100 | [diff] [blame] | 9977 | TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits)); |
| 9978 | TEST_EQUAL(0, bits); |
| 9979 | |
| 9980 | /* Invalid PSA EC family. */ |
| 9981 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192)); |
| 9982 | /* Invalid bit-size for a valid EC family. */ |
| 9983 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512)); |
| 9984 | |
| 9985 | /* Twisted-Edward curves are not supported yet. */ |
| 9986 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9987 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255)); |
| 9988 | TEST_EQUAL(MBEDTLS_ECP_DP_NONE, |
| 9989 | mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448)); |
| 9990 | } |
| 9991 | /* END_CASE */ |
| 9992 | |
| 9993 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9994 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 9995 | void key_agreement_output(int alg_arg, |
| 9996 | int our_key_type_arg, data_t *our_key_data, |
| 9997 | data_t *peer_key_data, |
| 9998 | data_t *expected_output1, data_t *expected_output2) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 9999 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10000 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10001 | psa_algorithm_t alg = alg_arg; |
| 10002 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 10003 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10004 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 10005 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10006 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10007 | TEST_CALLOC(actual_output, MAX(expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10008 | expected_output2->len)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10010 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10012 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10013 | psa_set_key_algorithm(&attributes, alg); |
| 10014 | psa_set_key_type(&attributes, our_key_type); |
| 10015 | PSA_ASSERT(psa_import_key(&attributes, |
| 10016 | our_key_data->x, our_key_data->len, |
| 10017 | &our_key)); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10019 | PSA_ASSERT(psa_key_derivation_setup(&operation, alg)); |
| 10020 | PSA_ASSERT(psa_key_derivation_key_agreement( |
| 10021 | &operation, |
| 10022 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 10023 | peer_key_data->x, peer_key_data->len)); |
| 10024 | if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) { |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 10025 | /* The test data is for info="" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10026 | PSA_ASSERT(psa_key_derivation_input_bytes(&operation, |
| 10027 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 10028 | NULL, 0)); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 10029 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10031 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 10032 | actual_output, |
| 10033 | expected_output1->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10034 | TEST_MEMORY_COMPARE(actual_output, expected_output1->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10035 | expected_output1->x, expected_output1->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10036 | if (expected_output2->len != 0) { |
| 10037 | PSA_ASSERT(psa_key_derivation_output_bytes(&operation, |
| 10038 | actual_output, |
| 10039 | expected_output2->len)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10040 | TEST_MEMORY_COMPARE(actual_output, expected_output2->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10041 | expected_output2->x, expected_output2->len); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 10042 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10043 | |
| 10044 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10045 | psa_key_derivation_abort(&operation); |
| 10046 | psa_destroy_key(our_key); |
| 10047 | PSA_DONE(); |
| 10048 | mbedtls_free(actual_output); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 10049 | } |
| 10050 | /* END_CASE */ |
| 10051 | |
| 10052 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10053 | void generate_random(int bytes_arg) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10054 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10055 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 10056 | unsigned char *output = NULL; |
| 10057 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10058 | size_t i; |
| 10059 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10061 | TEST_ASSERT(bytes_arg >= 0); |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 10062 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10063 | TEST_CALLOC(output, bytes); |
| 10064 | TEST_CALLOC(changed, bytes); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10066 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10067 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10068 | /* Run several times, to ensure that every output byte will be |
| 10069 | * nonzero at least once with overwhelming probability |
| 10070 | * (2^(-8*number_of_runs)). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10071 | for (run = 0; run < 10; run++) { |
| 10072 | if (bytes != 0) { |
| 10073 | memset(output, 0, bytes); |
| 10074 | } |
| 10075 | PSA_ASSERT(psa_generate_random(output, bytes)); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10077 | for (i = 0; i < bytes; i++) { |
| 10078 | if (output[i] != 0) { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10079 | ++changed[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10080 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10081 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10082 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10083 | |
| 10084 | /* Check that every byte was changed to nonzero at least once. This |
| 10085 | * validates that psa_generate_random is overwriting every byte of |
| 10086 | * the output buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10087 | for (i = 0; i < bytes; i++) { |
| 10088 | TEST_ASSERT(changed[i] != 0); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 10089 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10090 | |
| 10091 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10092 | PSA_DONE(); |
| 10093 | mbedtls_free(output); |
| 10094 | mbedtls_free(changed); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 10095 | } |
| 10096 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10097 | |
Ryan | 3a1b786 | 2024-03-01 17:24:04 +0000 | [diff] [blame] | 10098 | #if defined MBEDTLS_THREADING_PTHREAD |
| 10099 | |
| 10100 | /* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD */ |
| 10101 | void concurrently_generate_keys(int type_arg, |
| 10102 | int bits_arg, |
| 10103 | int usage_arg, |
| 10104 | int alg_arg, |
| 10105 | int expected_status_arg, |
| 10106 | int is_large_key_arg, |
| 10107 | int arg_thread_count, |
| 10108 | int reps_arg) |
| 10109 | { |
| 10110 | size_t thread_count = (size_t) arg_thread_count; |
| 10111 | mbedtls_test_thread_t *threads = NULL; |
| 10112 | generate_key_context gkc; |
| 10113 | gkc.type = type_arg; |
| 10114 | gkc.usage = usage_arg; |
| 10115 | gkc.bits = bits_arg; |
| 10116 | gkc.alg = alg_arg; |
| 10117 | gkc.expected_status = expected_status_arg; |
| 10118 | gkc.is_large_key = is_large_key_arg; |
| 10119 | gkc.reps = reps_arg; |
| 10120 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10121 | |
| 10122 | PSA_ASSERT(psa_crypto_init()); |
| 10123 | |
| 10124 | psa_set_key_usage_flags(&attributes, usage_arg); |
| 10125 | psa_set_key_algorithm(&attributes, alg_arg); |
| 10126 | psa_set_key_type(&attributes, type_arg); |
| 10127 | psa_set_key_bits(&attributes, bits_arg); |
| 10128 | gkc.attributes = &attributes; |
| 10129 | |
| 10130 | TEST_CALLOC(threads, sizeof(mbedtls_test_thread_t) * thread_count); |
| 10131 | |
| 10132 | /* Split threads to generate key then destroy key. */ |
| 10133 | for (size_t i = 0; i < thread_count; i++) { |
| 10134 | TEST_EQUAL( |
| 10135 | mbedtls_test_thread_create(&threads[i], thread_generate_key, |
| 10136 | (void *) &gkc), 0); |
| 10137 | } |
| 10138 | |
| 10139 | /* Join threads. */ |
| 10140 | for (size_t i = 0; i < thread_count; i++) { |
| 10141 | TEST_EQUAL(mbedtls_test_thread_join(&threads[i]), 0); |
| 10142 | } |
| 10143 | |
| 10144 | exit: |
| 10145 | mbedtls_free(threads); |
| 10146 | PSA_DONE(); |
| 10147 | } |
| 10148 | /* END_CASE */ |
| 10149 | #endif |
| 10150 | |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10151 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10152 | void generate_key(int type_arg, |
| 10153 | int bits_arg, |
| 10154 | int usage_arg, |
| 10155 | int alg_arg, |
| 10156 | int expected_status_arg, |
| 10157 | int is_large_key) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10158 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10159 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10160 | psa_key_type_t type = type_arg; |
| 10161 | psa_key_usage_t usage = usage_arg; |
| 10162 | size_t bits = bits_arg; |
| 10163 | psa_algorithm_t alg = alg_arg; |
| 10164 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10165 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 10166 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10168 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10170 | psa_set_key_usage_flags(&attributes, usage); |
| 10171 | psa_set_key_algorithm(&attributes, alg); |
| 10172 | psa_set_key_type(&attributes, type); |
| 10173 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10174 | |
| 10175 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10176 | psa_status_t status = psa_generate_key(&attributes, &key); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 10177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10178 | if (is_large_key > 0) { |
| 10179 | TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY); |
| 10180 | } |
| 10181 | TEST_EQUAL(status, expected_status); |
| 10182 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 10183 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10184 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10185 | |
| 10186 | /* Test the key information */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10187 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10188 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10189 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10190 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 10191 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10192 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 10193 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10194 | } |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10195 | |
| 10196 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10197 | /* |
| 10198 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10199 | * thus reset them as required. |
| 10200 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10201 | psa_reset_key_attributes(&got_attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10203 | psa_destroy_key(key); |
| 10204 | PSA_DONE(); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 10205 | } |
| 10206 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 10207 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10208 | /* BEGIN_CASE */ |
Gilles Peskine | f36d785 | 2024-06-06 21:11:44 +0200 | [diff] [blame] | 10209 | void generate_key_custom(int type_arg, |
| 10210 | int bits_arg, |
| 10211 | int usage_arg, |
| 10212 | int alg_arg, |
| 10213 | int flags_arg, |
| 10214 | data_t *custom_data, |
| 10215 | int expected_status_arg) |
| 10216 | { |
| 10217 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10218 | psa_key_type_t type = type_arg; |
| 10219 | psa_key_usage_t usage = usage_arg; |
| 10220 | size_t bits = bits_arg; |
| 10221 | psa_algorithm_t alg = alg_arg; |
| 10222 | psa_status_t expected_status = expected_status_arg; |
| 10223 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10224 | psa_custom_key_parameters_t custom = PSA_CUSTOM_KEY_PARAMETERS_INIT; |
| 10225 | custom.flags = flags_arg; |
| 10226 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10227 | |
| 10228 | PSA_ASSERT(psa_crypto_init()); |
| 10229 | |
| 10230 | psa_set_key_usage_flags(&attributes, usage); |
| 10231 | psa_set_key_algorithm(&attributes, alg); |
| 10232 | psa_set_key_type(&attributes, type); |
| 10233 | psa_set_key_bits(&attributes, bits); |
| 10234 | |
| 10235 | /* Generate a key */ |
| 10236 | psa_status_t status = |
| 10237 | psa_generate_key_custom(&attributes, |
| 10238 | &custom, custom_data->x, custom_data->len, |
| 10239 | &key); |
| 10240 | |
| 10241 | TEST_EQUAL(status, expected_status); |
| 10242 | if (expected_status != PSA_SUCCESS) { |
| 10243 | goto exit; |
| 10244 | } |
| 10245 | |
| 10246 | /* Test the key information */ |
| 10247 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10248 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10249 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
| 10250 | |
Valerio Setti | efce605 | 2024-06-25 18:31:36 +0200 | [diff] [blame] | 10251 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) && defined(MBEDTLS_ASN1_PARSE_C) |
Gilles Peskine | f36d785 | 2024-06-06 21:11:44 +0200 | [diff] [blame] | 10252 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 10253 | TEST_ASSERT(rsa_test_e(key, bits, custom_data)); |
| 10254 | } |
| 10255 | #endif |
| 10256 | |
| 10257 | /* Do something with the key according to its type and permitted usage. */ |
| 10258 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
| 10259 | goto exit; |
| 10260 | } |
| 10261 | |
| 10262 | exit: |
| 10263 | /* |
| 10264 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10265 | * thus reset them as required. |
| 10266 | */ |
| 10267 | psa_reset_key_attributes(&got_attributes); |
| 10268 | psa_destroy_key(key); |
| 10269 | PSA_DONE(); |
| 10270 | } |
| 10271 | /* END_CASE */ |
| 10272 | |
| 10273 | /* BEGIN_CASE */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10274 | void generate_key_ext(int type_arg, |
| 10275 | int bits_arg, |
| 10276 | int usage_arg, |
| 10277 | int alg_arg, |
Gilles Peskine | c81393b | 2024-02-14 20:51:28 +0100 | [diff] [blame] | 10278 | int flags_arg, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10279 | data_t *params_data, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10280 | int expected_status_arg) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10281 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10282 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10283 | psa_key_type_t type = type_arg; |
| 10284 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10285 | size_t bits = bits_arg; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10286 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10287 | psa_status_t expected_status = expected_status_arg; |
| 10288 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10289 | psa_key_production_parameters_t *params = NULL; |
| 10290 | size_t params_data_length = 0; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10291 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10293 | PSA_ASSERT(psa_crypto_init()); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10295 | psa_set_key_usage_flags(&attributes, usage); |
| 10296 | psa_set_key_algorithm(&attributes, alg); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10297 | psa_set_key_type(&attributes, type); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10298 | psa_set_key_bits(&attributes, bits); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10299 | |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10300 | if (!setup_key_production_parameters(¶ms, ¶ms_data_length, |
| 10301 | flags_arg, params_data)) { |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10302 | goto exit; |
| 10303 | } |
| 10304 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10305 | /* Generate a key */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10306 | psa_status_t status = psa_generate_key_ext(&attributes, |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10307 | params, params_data_length, |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10308 | &key); |
| 10309 | |
| 10310 | TEST_EQUAL(status, expected_status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10311 | if (expected_status != PSA_SUCCESS) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10312 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10313 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10314 | |
| 10315 | /* Test the key information */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10316 | PSA_ASSERT(psa_get_key_attributes(key, &got_attributes)); |
| 10317 | TEST_EQUAL(psa_get_key_type(&got_attributes), type); |
| 10318 | TEST_EQUAL(psa_get_key_bits(&got_attributes), bits); |
Pengyu Lv | d90fbf7 | 2023-12-08 17:13:22 +0800 | [diff] [blame] | 10319 | |
Gilles Peskine | 7a18f96 | 2024-02-12 16:48:11 +0100 | [diff] [blame] | 10320 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) |
| 10321 | if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10322 | TEST_ASSERT(rsa_test_e(key, bits, params_data)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10323 | } |
Pengyu Lv | 9e976f3 | 2023-12-06 16:58:05 +0800 | [diff] [blame] | 10324 | #endif |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10325 | |
| 10326 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10327 | if (!mbedtls_test_psa_exercise_key(key, usage, alg, 0)) { |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10328 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10329 | } |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10330 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10331 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10332 | /* |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10333 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10334 | * thus reset them as required. |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10335 | */ |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10336 | psa_reset_key_attributes(&got_attributes); |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10337 | mbedtls_free(params); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10338 | psa_destroy_key(key); |
| 10339 | PSA_DONE(); |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10340 | } |
| 10341 | /* END_CASE */ |
| 10342 | |
| 10343 | /* BEGIN_CASE */ |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10344 | void key_production_parameters_init() |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10345 | { |
Gilles Peskine | 092ce51 | 2024-02-20 12:31:24 +0100 | [diff] [blame] | 10346 | psa_key_production_parameters_t init = PSA_KEY_PRODUCTION_PARAMETERS_INIT; |
| 10347 | psa_key_production_parameters_t zero; |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10348 | memset(&zero, 0, sizeof(zero)); |
| 10349 | |
Gilles Peskine | f0765fa | 2024-02-12 16:46:16 +0100 | [diff] [blame] | 10350 | TEST_EQUAL(init.flags, 0); |
| 10351 | TEST_EQUAL(zero.flags, 0); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 10352 | } |
| 10353 | /* END_CASE */ |
| 10354 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10355 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10356 | void persistent_key_load_key_from_storage(data_t *data, |
| 10357 | int type_arg, int bits_arg, |
| 10358 | int usage_flags_arg, int alg_arg, |
| 10359 | int generation_method) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10360 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10361 | 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] | 10362 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 10363 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10364 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10365 | psa_key_type_t type = type_arg; |
| 10366 | size_t bits = bits_arg; |
| 10367 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 10368 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 10369 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10370 | unsigned char *first_export = NULL; |
| 10371 | unsigned char *second_export = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10372 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits); |
Sergey | bef1f63 | 2023-03-06 15:25:06 -0700 | [diff] [blame] | 10373 | size_t first_exported_length = 0; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10374 | size_t second_exported_length; |
| 10375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10376 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10377 | TEST_CALLOC(first_export, export_size); |
| 10378 | TEST_CALLOC(second_export, export_size); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10379 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10381 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10383 | psa_set_key_id(&attributes, key_id); |
| 10384 | psa_set_key_usage_flags(&attributes, usage_flags); |
| 10385 | psa_set_key_algorithm(&attributes, alg); |
| 10386 | psa_set_key_type(&attributes, type); |
| 10387 | psa_set_key_bits(&attributes, bits); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10389 | switch (generation_method) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10390 | case IMPORT_KEY: |
| 10391 | /* Import the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10392 | PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, |
| 10393 | &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10394 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10395 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10396 | case GENERATE_KEY: |
| 10397 | /* Generate a key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10398 | PSA_ASSERT(psa_generate_key(&attributes, &key)); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10399 | break; |
| 10400 | |
| 10401 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 10402 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10403 | { |
| 10404 | /* Create base key */ |
| 10405 | psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 10406 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10407 | psa_set_key_usage_flags(&base_attributes, |
| 10408 | PSA_KEY_USAGE_DERIVE); |
| 10409 | psa_set_key_algorithm(&base_attributes, derive_alg); |
| 10410 | psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE); |
| 10411 | PSA_ASSERT(psa_import_key(&base_attributes, |
| 10412 | data->x, data->len, |
| 10413 | &base_key)); |
| 10414 | /* Derive a key. */ |
| 10415 | PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg)); |
| 10416 | PSA_ASSERT(psa_key_derivation_input_key( |
| 10417 | &operation, |
| 10418 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key)); |
| 10419 | PSA_ASSERT(psa_key_derivation_input_bytes( |
| 10420 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
| 10421 | NULL, 0)); |
| 10422 | PSA_ASSERT(psa_key_derivation_output_key(&attributes, |
| 10423 | &operation, |
| 10424 | &key)); |
| 10425 | PSA_ASSERT(psa_key_derivation_abort(&operation)); |
| 10426 | PSA_ASSERT(psa_destroy_key(base_key)); |
| 10427 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10428 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10429 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10430 | TEST_ASSUME(!"KDF not supported in this configuration"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10431 | #endif |
| 10432 | break; |
| 10433 | |
| 10434 | default: |
Agathiyan Bragadeesh | dc28a5a | 2023-07-18 11:45:28 +0100 | [diff] [blame] | 10435 | TEST_FAIL("generation_method not implemented in test"); |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 10436 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10437 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10438 | psa_reset_key_attributes(&attributes); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10439 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10440 | /* Export the key if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10441 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10442 | PSA_ASSERT(psa_export_key(key, |
| 10443 | first_export, export_size, |
| 10444 | &first_exported_length)); |
| 10445 | if (generation_method == IMPORT_KEY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10446 | TEST_MEMORY_COMPARE(data->x, data->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10447 | first_export, first_exported_length); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10448 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10449 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10450 | |
| 10451 | /* Shutdown and restart */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10452 | PSA_ASSERT(psa_purge_key(key)); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10453 | PSA_DONE(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10454 | PSA_ASSERT(psa_crypto_init()); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10455 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10456 | /* Check key slot still contains key data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10457 | PSA_ASSERT(psa_get_key_attributes(key, &attributes)); |
| 10458 | TEST_ASSERT(mbedtls_svc_key_id_equal( |
| 10459 | psa_get_key_id(&attributes), key_id)); |
| 10460 | TEST_EQUAL(psa_get_key_lifetime(&attributes), |
| 10461 | PSA_KEY_LIFETIME_PERSISTENT); |
| 10462 | TEST_EQUAL(psa_get_key_type(&attributes), type); |
| 10463 | TEST_EQUAL(psa_get_key_bits(&attributes), bits); |
| 10464 | TEST_EQUAL(psa_get_key_usage_flags(&attributes), |
| 10465 | mbedtls_test_update_key_usage_flags(usage_flags)); |
| 10466 | TEST_EQUAL(psa_get_key_algorithm(&attributes), alg); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10467 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 10468 | /* Export the key again if permitted by the key policy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10469 | if (usage_flags & PSA_KEY_USAGE_EXPORT) { |
| 10470 | PSA_ASSERT(psa_export_key(key, |
| 10471 | second_export, export_size, |
| 10472 | &second_exported_length)); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 10473 | TEST_MEMORY_COMPARE(first_export, first_exported_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 10474 | second_export, second_exported_length); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10475 | } |
| 10476 | |
| 10477 | /* Do something with the key according to its type and permitted usage. */ |
Ryan Everett | 0a271fd | 2024-03-12 16:34:02 +0000 | [diff] [blame] | 10478 | if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg, 0)) { |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 10479 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10480 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10481 | |
| 10482 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10483 | /* |
| 10484 | * Key attributes may have been returned by psa_get_key_attributes() |
| 10485 | * thus reset them as required. |
| 10486 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10487 | psa_reset_key_attributes(&attributes); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 10488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10489 | mbedtls_free(first_export); |
| 10490 | mbedtls_free(second_export); |
| 10491 | psa_key_derivation_abort(&operation); |
| 10492 | psa_destroy_key(base_key); |
| 10493 | psa_destroy_key(key); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 10494 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 10495 | } |
| 10496 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10497 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10498 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10499 | void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 10500 | int primitive_arg, int hash_arg, int role_arg, |
| 10501 | int test_input, data_t *pw_data, |
| 10502 | int inj_err_type_arg, |
| 10503 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10504 | { |
| 10505 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10506 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 10507 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10508 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 10509 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 10510 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10511 | psa_algorithm_t hash_alg = hash_arg; |
| 10512 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10513 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10514 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10515 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 10516 | psa_status_t expected_error = expected_error_arg; |
| 10517 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10518 | unsigned char *output_buffer = NULL; |
| 10519 | size_t output_len = 0; |
| 10520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10521 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10522 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10523 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10524 | PSA_PAKE_STEP_KEY_SHARE); |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 10525 | TEST_CALLOC(output_buffer, buf_size); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10527 | if (pw_data->len > 0) { |
| 10528 | psa_set_key_usage_flags(&attributes, key_usage_pw); |
| 10529 | psa_set_key_algorithm(&attributes, alg); |
| 10530 | psa_set_key_type(&attributes, key_type_pw); |
| 10531 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10532 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10533 | } |
| 10534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10535 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10536 | psa_pake_cs_set_primitive(&cipher_suite, primitive); |
| 10537 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10539 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 10540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10541 | if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) { |
| 10542 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10543 | expected_error); |
| 10544 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10545 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10546 | expected_error); |
| 10547 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10548 | TEST_EQUAL(psa_pake_set_password_key(&operation, key), |
| 10549 | expected_error); |
| 10550 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10551 | TEST_EQUAL(psa_pake_set_role(&operation, role), |
| 10552 | expected_error); |
| 10553 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10554 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10555 | NULL, 0, NULL), |
| 10556 | expected_error); |
| 10557 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10558 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 10559 | expected_error); |
| 10560 | PSA_ASSERT(psa_pake_abort(&operation)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10561 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10562 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10564 | status = psa_pake_setup(&operation, &cipher_suite); |
| 10565 | if (status != PSA_SUCCESS) { |
| 10566 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10567 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10568 | } |
| 10569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10570 | if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) { |
| 10571 | TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite), |
| 10572 | expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10573 | goto exit; |
| 10574 | } |
| 10575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10576 | status = psa_pake_set_role(&operation, role); |
| 10577 | if (status != PSA_SUCCESS) { |
| 10578 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10579 | goto exit; |
| 10580 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10582 | if (pw_data->len > 0) { |
| 10583 | status = psa_pake_set_password_key(&operation, key); |
| 10584 | if (status != PSA_SUCCESS) { |
| 10585 | TEST_EQUAL(status, expected_error); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10586 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10587 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10588 | } |
| 10589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10590 | if (inj_err_type == INJECT_ERR_INVALID_USER) { |
| 10591 | TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0), |
| 10592 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10593 | goto exit; |
| 10594 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10596 | if (inj_err_type == INJECT_ERR_INVALID_PEER) { |
| 10597 | TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0), |
| 10598 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10599 | goto exit; |
| 10600 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10602 | if (inj_err_type == INJECT_ERR_SET_USER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10603 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10604 | TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4), |
| 10605 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10606 | goto exit; |
| 10607 | } |
| 10608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10609 | if (inj_err_type == INJECT_ERR_SET_PEER) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10610 | const uint8_t unsupported_id[] = "abcd"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10611 | TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4), |
| 10612 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10613 | goto exit; |
| 10614 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 10615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10616 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10617 | PSA_PAKE_STEP_KEY_SHARE); |
| 10618 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10619 | PSA_PAKE_STEP_ZK_PUBLIC); |
| 10620 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive, |
| 10621 | PSA_PAKE_STEP_ZK_PROOF); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 10622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10623 | if (test_input) { |
| 10624 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10625 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0), |
| 10626 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10627 | goto exit; |
| 10628 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10630 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10631 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10632 | output_buffer, size_zk_proof), |
| 10633 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10634 | goto exit; |
| 10635 | } |
| 10636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10637 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10638 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10639 | output_buffer, size_zk_proof), |
| 10640 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10641 | goto exit; |
| 10642 | } |
| 10643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10644 | status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10645 | output_buffer, size_key_share); |
| 10646 | if (status != PSA_SUCCESS) { |
| 10647 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10648 | goto exit; |
| 10649 | } |
| 10650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10651 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10652 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10653 | output_buffer, size_zk_public + 1), |
| 10654 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10655 | goto exit; |
| 10656 | } |
| 10657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10658 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10659 | // 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] | 10660 | psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10661 | output_buffer, size_zk_public + 1); |
| 10662 | TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10663 | output_buffer, size_zk_public), |
| 10664 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10665 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10666 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10667 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10668 | if (inj_err_type == INJECT_EMPTY_IO_BUFFER) { |
| 10669 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10670 | NULL, 0, NULL), |
| 10671 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10672 | goto exit; |
| 10673 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10675 | if (inj_err_type == INJECT_UNKNOWN_STEP) { |
| 10676 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 10677 | output_buffer, buf_size, &output_len), |
| 10678 | PSA_ERROR_INVALID_ARGUMENT); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10679 | goto exit; |
| 10680 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10682 | if (inj_err_type == INJECT_INVALID_FIRST_STEP) { |
| 10683 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF, |
| 10684 | output_buffer, buf_size, &output_len), |
| 10685 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10686 | goto exit; |
| 10687 | } |
| 10688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10689 | status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE, |
| 10690 | output_buffer, buf_size, &output_len); |
| 10691 | if (status != PSA_SUCCESS) { |
| 10692 | TEST_EQUAL(status, expected_error); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10693 | goto exit; |
| 10694 | } |
| 10695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10696 | TEST_ASSERT(output_len > 0); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10698 | if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) { |
| 10699 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10700 | output_buffer, size_zk_public - 1, &output_len), |
| 10701 | PSA_ERROR_BUFFER_TOO_SMALL); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10702 | goto exit; |
| 10703 | } |
| 10704 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10705 | if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10706 | // 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] | 10707 | psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10708 | output_buffer, size_zk_public - 1, &output_len); |
| 10709 | TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 10710 | output_buffer, buf_size, &output_len), |
| 10711 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10712 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 10713 | } |
| 10714 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10715 | |
| 10716 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10717 | PSA_ASSERT(psa_destroy_key(key)); |
| 10718 | PSA_ASSERT(psa_pake_abort(&operation)); |
| 10719 | mbedtls_free(output_buffer); |
| 10720 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10721 | } |
| 10722 | /* END_CASE */ |
| 10723 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 10724 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10725 | void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg, |
| 10726 | int client_input_first, int inject_error, |
| 10727 | data_t *pw_data) |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10728 | { |
| 10729 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10730 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10731 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10732 | psa_algorithm_t alg = alg_arg; |
| 10733 | psa_algorithm_t hash_alg = hash_arg; |
| 10734 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10735 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10737 | PSA_INIT(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10739 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10740 | psa_set_key_algorithm(&attributes, alg); |
| 10741 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10742 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10743 | &key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10745 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10746 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10747 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10748 | |
| 10749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10750 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10751 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10752 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10753 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10754 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10756 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10757 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10759 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10760 | client_input_first, 1, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10761 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10762 | if (inject_error == 1 || inject_error == 2) { |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10763 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10764 | } |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10765 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10766 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10767 | client_input_first, 2, inject_error); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10768 | |
| 10769 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10770 | psa_destroy_key(key); |
| 10771 | psa_pake_abort(&server); |
| 10772 | psa_pake_abort(&client); |
| 10773 | PSA_DONE(); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 10774 | } |
| 10775 | /* END_CASE */ |
| 10776 | |
| 10777 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10778 | void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg, |
| 10779 | int derive_alg_arg, data_t *pw_data, |
| 10780 | int client_input_first, int inj_err_type_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10781 | { |
| 10782 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 10783 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 10784 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 10785 | psa_algorithm_t alg = alg_arg; |
| 10786 | psa_algorithm_t hash_alg = hash_arg; |
| 10787 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 10788 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 10789 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 10790 | psa_key_derivation_operation_t server_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10791 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10792 | psa_key_derivation_operation_t client_derive = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10793 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10794 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10796 | PSA_INIT(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10798 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 10799 | psa_set_key_algorithm(&attributes, alg); |
| 10800 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
| 10801 | PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len, |
| 10802 | &key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10804 | psa_pake_cs_set_algorithm(&cipher_suite, alg); |
| 10805 | psa_pake_cs_set_primitive(&cipher_suite, primitive_arg); |
| 10806 | psa_pake_cs_set_hash(&cipher_suite, hash_alg); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10807 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10808 | /* Get shared key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10809 | PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg)); |
| 10810 | PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10812 | if (PSA_ALG_IS_TLS12_PRF(derive_alg) || |
| 10813 | PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) { |
| 10814 | PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive, |
| 10815 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10816 | (const uint8_t *) "", 0)); |
| 10817 | PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive, |
| 10818 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 10819 | (const uint8_t *) "", 0)); |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10820 | } |
| 10821 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10822 | PSA_ASSERT(psa_pake_setup(&server, &cipher_suite)); |
| 10823 | PSA_ASSERT(psa_pake_setup(&client, &cipher_suite)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10825 | PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER)); |
| 10826 | PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10828 | PSA_ASSERT(psa_pake_set_password_key(&server, key)); |
| 10829 | PSA_ASSERT(psa_pake_set_password_key(&client, key)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10831 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) { |
| 10832 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10833 | PSA_ERROR_BAD_STATE); |
| 10834 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10835 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10836 | goto exit; |
| 10837 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10838 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10839 | /* First round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10840 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10841 | client_input_first, 1, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10843 | if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) { |
| 10844 | TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive), |
| 10845 | PSA_ERROR_BAD_STATE); |
| 10846 | TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive), |
| 10847 | PSA_ERROR_BAD_STATE); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame] | 10848 | goto exit; |
| 10849 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 10850 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 10851 | /* Second round */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10852 | ecjpake_do_round(alg, primitive_arg, &server, &client, |
| 10853 | client_input_first, 2, 0); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10854 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10855 | PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive)); |
| 10856 | PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive)); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10857 | |
| 10858 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10859 | psa_key_derivation_abort(&server_derive); |
| 10860 | psa_key_derivation_abort(&client_derive); |
| 10861 | psa_destroy_key(key); |
| 10862 | psa_pake_abort(&server); |
| 10863 | psa_pake_abort(&client); |
| 10864 | PSA_DONE(); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 10865 | } |
| 10866 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10867 | |
| 10868 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10869 | void ecjpake_size_macros() |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10870 | { |
| 10871 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 10872 | const size_t bits = 256; |
| 10873 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10874 | 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] | 10875 | 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] | 10876 | PSA_ECC_FAMILY_SECP_R1); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10877 | |
| 10878 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 10879 | /* 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] | 10880 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10881 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
| 10882 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10883 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10884 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10885 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10886 | PSA_BITS_TO_BYTES(bits)); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10887 | |
| 10888 | /* Input sizes are the same as output sizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10889 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10890 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE)); |
| 10891 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10892 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC)); |
| 10893 | TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10894 | 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] | 10895 | |
| 10896 | /* These inequalities will always hold even when other PAKEs are added */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 10897 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10898 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10899 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10900 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10901 | TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10902 | PSA_PAKE_OUTPUT_MAX_SIZE); |
| 10903 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 10904 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10905 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 10906 | PSA_PAKE_INPUT_MAX_SIZE); |
| 10907 | TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 10908 | PSA_PAKE_INPUT_MAX_SIZE); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 10909 | } |
| 10910 | /* END_CASE */ |